Standard Built-in Objects (ES5)
Relevant source files
The following files were used as context for generating this wiki page:
- Jurassic/Library/Array/ArrayInstance.cs
- Jurassic/Library/String/StringInstance.cs
- Jurassic/Library/JSON/JSONObject.cs
- Jurassic/Library/RegExp/RegExpInstance.cs
- Jurassic/Library/GlobalObject.cs
- Jurassic/Library/GlobalObject.g.cs
- Jurassic/Library/RegExp/RegExpInstance.g.cs
- Jurassic/Library/Map/MapInstance.g.cs
- Jurassic/Library/Reflect/Reflect.g.cs
- README.md
The Jurassic engine provides a comprehensive implementation of the ECMAScript 5 standard, including its rich set of standard built-in objects. These objects are fundamental to the JavaScript language, providing core functionalities for data manipulation, regular expressions, JSON processing, and more. In Jurassic, these are implemented as C# classes, typically inheriting from ObjectInstance, to be integrated into .NET programs. The engine aims for high standards compliance and performance by compiling JavaScript to .NET CIL.
This document outlines the architecture and implementation of several key built-in objects within the Jurassic library, focusing on the Global object, RegExp, and others. While Jurassic's primary target is ES5, it also includes partial support for ES6+ features, some of which are covered here.
Sources: README.md, Jurassic/Library/GlobalObject.cs
Global Object
The Global Object in Jurassic serves as the container for all global properties and functions. It is the root of the scope chain and is implemented by the GlobalObject class.
Sources: Jurassic/Library/GlobalObject.cs:11-14
Initialization
The GlobalObject is initialized with a set of standard properties and functions. The GetGlobalProperties method is responsible for gathering these, which include:
- Constant Properties:
Infinity,NaN, andundefined. These are configured as sealed (read-only) properties to conform to the ES5 specification. - Declarative Functions: Core global functions like
eval(),isFinite(), and URI handling functions.
Sources: Jurassic/Library/GlobalObject.cs:25-34
The diagram below shows the initialization flow for global properties.
Global Functions
The GlobalObject class implements standard global functions. These are defined in GlobalObject.g.cs as ClrStubFunction instances and implemented in GlobalObject.cs.
| Function | Description |
|---|---|
decodeURI(uri) | Decodes a Uniform Resource Identifier (URI). |
decodeURIComponent(uriComponent) | Decodes a URI component. |
encodeURI(uri) | Encodes a URI. |
encodeURIComponent(uriComponent) | Encodes a URI component. |
escape(string) | Deprecated. Encodes a string. |
unescape(string) | Deprecated. Decodes a string encoded with escape(). |
eval(code) | Evaluates JavaScript code represented as a string. |
isFinite(number) | Determines whether a value is a finite number. |
isNaN(number) | Determines whether a value is NaN (Not-a-Number). |
Sources: Jurassic/Library/GlobalObject.g.cs:13-26, Jurassic/Library/GlobalObject.cs
URI Encoding and Decoding
The URI functions (encodeURI, decodeURI, etc.) use pre-calculated character sets for performance. For example, encodeURI uses encodeURIUnescapedSet to determine which characters should not be escaped. The decoding process handles multi-byte UTF-8 sequences to correctly reconstruct original characters.
The logic for decoding a URI component is as follows:
RegExp Objects
Regular expression objects are instances of the RegExpInstance class. They provide powerful pattern-matching capabilities. The auto-generated file RegExpInstance.g.cs defines the public-facing properties and methods.
*Sources: Jurassic/Library/RegExp/RegExpInstance.cs, Jurassic/Library/RegExp/RegExpInstance.g.cs *
Class Structure
The RegExpInstance class exposes properties for the pattern and flags, and methods for matching.
This diagram illustrates the main properties and methods of the RegExpInstance class.
Sources: Jurassic/Library/RegExp/RegExpInstance.g.cs:13-30
Properties
The core properties of a RegExpInstance are accessible as getters.
| Property | Description | Source File |
|---|---|---|
source | The text of the regular expression pattern. | RegExpInstance.g.cs:35-40 |
flags | A string containing the flags of the regular expression. | RegExpInstance.g.cs:42-47 |
global | Whether the 'g' flag is set for the regular expression. | RegExpInstance.g.cs:49-54 |
multiline | Whether the 'm' flag is set for the regular expression. | RegExpInstance.g.cs:56-61 |
ignoreCase | Whether the 'i' flag is set for the regular expression. | RegExpInstance.g.cs:63-68 |
Methods
Key methods for pattern matching are:
test(string): Returns a boolean indicating whether a pattern exists in a string.exec(string): Executes a search for a match in a string. It returns an array of information ornullon a mismatch.[Symbol.match](https://github.com/zhgabor/jurassic/blob/HEAD/string): A well-known symbol that specifies the matching of a regular expression against a string.[Symbol.replace](https://github.com/zhgabor/jurassic/blob/HEAD/string%2C%20replacement): A well-known symbol that specifies the replacement of matched substrings of a string.
Sources: Jurassic/Library/RegExp/RegExpInstance.g.cs:77-123
ES6+ Built-in Objects
Jurassic includes partial support for ES6 features, including new built-in objects like Map and Reflect.
Sources: README.md:28-29
Map
The Map object holds key-value pairs and remembers the original insertion order of the keys. It is implemented in MapInstance. The auto-generated stubs in MapInstance.g.cs define its API.
This diagram shows the properties and methods available on a MapInstance.
Sources: Jurassic/Library/Map/MapInstance.g.cs:13-27
| Method/Property | Description |
|---|---|
size (getter) | Returns the number of key/value pairs in the Map object. |
clear() | Removes all key/value pairs from the Map object. |
delete(key) | Removes any value associated with the key and returns true if an element in the Map object existed and has been removed, or false otherwise. |
get(key) | Returns the value associated with the key, or undefined if the key can't be found. |
has(key) | Returns a boolean asserting whether a value has been associated with the key. |
set(key, value) | Sets the value for the key in the Map object. Returns the Map object. |
entries() | Returns a new Iterator object that contains an array of [key, value] for each element. |
keys() | Returns a new Iterator object that contains the keys for each element. |
values() | Returns a new Iterator object that contains the values for each element. |
forEach(callback, thisArg) | Executes a provided function once per key/value pair. |
Sources: Jurassic/Library/Map/MapInstance.g.cs:13-115
Reflect
The Reflect object is a built-in object that provides methods for interceptable JavaScript operations. It is not a function object, so it's not constructible. The methods are all static and are defined in Reflect.g.cs.
| Method | Description |
|---|---|
Reflect.apply(target, thisArgument, argumentsList) | Calls a target function with arguments as specified. |
Reflect.construct(target, argumentsList, newTarget) | Acts like the new operator as a function. |
Reflect.defineProperty(target, propertyKey, attributes) | Similar to Object.defineProperty(). Returns a Boolean. |
Reflect.deleteProperty(target, propertyKey) | The delete operator as a function. |
Reflect.get(target, propertyKey, receiver) | A function that returns the value of a property. |
Reflect.getOwnPropertyDescriptor(target, propertyKey) | Similar to Object.getOwnPropertyDescriptor(). |
Reflect.getPrototypeOf(target) | Same as Object.getPrototypeOf(). |
Reflect.has(target, propertyKey) | The in operator as a function. |
Reflect.isExtensible(target) | Same as Object.isExtensible(). |
Reflect.ownKeys(target) | Returns an array of the target object's own property keys. |
Reflect.preventExtensions(target) | Same as Object.preventExtensions(). |
Reflect.set(target, propertyKey, value, receiver) | A function that assigns values to properties. |
Reflect.setPrototypeOf(target, prototype) | A function that sets the prototype of an object. |
Sources: Jurassic/Library/Reflect/Reflect.g.cs:13-27
Other Standard Objects
The Jurassic library implements many other standard built-in objects to provide a complete ES5 environment. These include, but are not limited to:
ArrayInstance: The implementation for JavaScript arrays. Sources: Jurassic/Library/Array/ArrayInstance.csStringInstance: The wrapper object for string primitives. Sources: Jurassic/Library/String/StringInstance.csJSONObject: ProvidesJSON.parse()andJSON.stringify()functionality. Sources: Jurassic/Library/JSON/JSONObject.cs
These classes, along with others in the Jurassic.Library namespace, form the foundation of the JavaScript runtime, enabling the execution of complex scripts that rely on standard library features.