CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-mozilla--rhino

JavaScript engine implementation that enables JavaScript execution within Java applications with full ECMAScript support and Java-JavaScript interoperability.

Pending
Overview
Eval results
Files

javascript-objects.mddocs/

JavaScript Objects

Complete JavaScript object system implementing all ECMAScript built-in objects including ES6+ features like Promises, Symbols, Maps, Sets, and modern iteration protocols.

Capabilities

Scriptable Interface

Core interface that all JavaScript objects must implement, providing the foundation for property access, prototype chains, and object behavior.

/**
 * Core interface for all JavaScript objects
 * Defines property access, prototype chain, and introspection
 */
public interface Scriptable {
    /**
     * Gets a named property from this object
     * @param name Property name
     * @param start Object where lookup started (for prototype chain)
     * @return Property value or NOT_FOUND
     */
    Object get(String name, Scriptable start);
    
    /**
     * Gets an indexed property from this object
     * @param index Property index
     * @param start Object where lookup started
     * @return Property value or NOT_FOUND
     */
    Object get(int index, Scriptable start);
    
    /**
     * Sets a named property on this object
     * @param name Property name
     * @param start Object where lookup started
     * @param value Property value to set
     */
    void put(String name, Scriptable start, Object value);
    
    /**
     * Sets an indexed property on this object
     * @param index Property index
     * @param start Object where lookup started
     * @param value Property value to set
     */
    void put(int index, Scriptable start, Object value);
    
    /**
     * Checks if named property exists on this object
     * @param name Property name
     * @param start Object where lookup started
     * @return true if property exists
     */
    boolean has(String name, Scriptable start);
    
    /**
     * Checks if indexed property exists on this object
     * @param index Property index
     * @param start Object where lookup started
     * @return true if property exists
     */
    boolean has(int index, Scriptable start);
    
    /**
     * Deletes a named property from this object
     * @param name Property name to delete
     */
    void delete(String name);
    
    /**
     * Deletes an indexed property from this object
     * @param index Property index to delete
     */
    void delete(int index);
    
    /**
     * Gets the prototype object for this object
     * @return Prototype object or null
     */
    Scriptable getPrototype();
    
    /**
     * Sets the prototype object for this object
     * @param prototype New prototype object
     */
    void setPrototype(Scriptable prototype);
    
    /**
     * Gets the parent scope for variable resolution
     * @return Parent scope object or null
     */
    Scriptable getParentScope();
    
    /**
     * Sets the parent scope for variable resolution
     * @param parent New parent scope
     */
    void setParentScope(Scriptable parent);
    
    /**
     * Gets the [[Class]] internal property (e.g., "Object", "Array")
     * @return Class name string
     */
    String getClassName();
    
    /**
     * Gets array of enumerable property IDs
     * @return Array of property names and indices
     */
    Object[] getIds();
    
    /**
     * Converts object to primitive value with type hint
     * @param hint Type hint (String.class, Number.class, etc.)
     * @return Primitive value
     */
    Object getDefaultValue(Class<?> hint);
    
    /**
     * Implements instanceof operator behavior
     * @param instance Object to test
     * @return true if this object is in instance's prototype chain
     */
    boolean hasInstance(Scriptable instance);
}

/**
 * Sentinel value returned when property doesn't exist
 */
public static final Object NOT_FOUND;

Usage Examples:

// Basic property access
Scriptable obj = cx.newObject(scope);
obj.put("name", obj, "example");
obj.put("value", obj, 42);

Object name = obj.get("name", obj);  // "example"
boolean hasValue = obj.has("value", obj);  // true

// Array-like access
obj.put(0, obj, "first");
obj.put(1, obj, "second");
Object first = obj.get(0, obj);  // "first"

// Prototype chain
Scriptable proto = cx.newObject(scope);
proto.put("inherited", proto, "value");
obj.setPrototype(proto);
Object inherited = obj.get("inherited", obj);  // "value"

ScriptableObject

Base implementation of Scriptable with additional functionality for property attributes, sealing, and utility methods.

/**
 * Base implementation of Scriptable with enhanced functionality
 * Provides property attributes, sealing, and utility methods
 */
public abstract class ScriptableObject implements Scriptable {
    /**
     * Gets property from any Scriptable object
     * @param obj Target object
     * @param name Property name
     * @return Property value or NOT_FOUND
     */
    public static Object getProperty(Scriptable obj, String name);
    
    /**
     * Gets indexed property from any Scriptable object
     * @param obj Target object
     * @param index Property index
     * @return Property value or NOT_FOUND
     */
    public static Object getProperty(Scriptable obj, int index);
    
    /**
     * Sets property on any Scriptable object
     * @param obj Target object
     * @param name Property name
     * @param value Property value
     */
    public static void putProperty(Scriptable obj, String name, Object value);
    
    /**
     * Sets indexed property on any Scriptable object
     * @param obj Target object
     * @param index Property index
     * @param value Property value
     */
    public static void putProperty(Scriptable obj, int index, Object value);
    
    /**
     * Checks if property exists on any Scriptable object
     * @param obj Target object
     * @param name Property name
     * @return true if property exists
     */
    public static boolean hasProperty(Scriptable obj, String name);
    
    /**
     * Deletes property from any Scriptable object
     * @param obj Target object
     * @param name Property name to delete
     * @return true if deletion succeeded
     */
    public static boolean deleteProperty(Scriptable obj, String name);
    
    /**
     * Defines a property with value and attributes
     * @param propertyName Property name
     * @param value Property value
     * @param attributes Property attributes (READONLY, DONTENUM, etc.)
     */
    public void defineProperty(String propertyName, Object value, int attributes);
    
    /**
     * Defines properties from Java class methods
     * @param propertyName Property name
     * @param clazz Java class containing getter/setter methods
     * @param attributes Property attributes
     */
    public void defineProperty(String propertyName, Class<?> clazz, int attributes);
    
    /**
     * Defines multiple function properties from class methods
     * @param names Array of function names
     * @param clazz Java class containing methods
     * @param attributes Property attributes
     */
    public void defineFunctionProperties(String[] names, Class<?> clazz, int attributes);
    
    /**
     * Seals this object (prevents property addition/deletion)
     */
    public void sealObject();
    
    /**
     * Checks if this object is sealed
     * @return true if object is sealed
     */
    public boolean isSealed();
    
    /**
     * Sets attributes for a property
     * @param name Property name
     * @param attributes New attributes
     */
    public void setAttributes(String name, int attributes);
    
    /**
     * Gets attributes for a property
     * @param name Property name
     * @return Property attributes
     */
    public int getAttributes(String name);
}

// Property attribute constants
public static final int READONLY = 1;    // Property cannot be modified
public static final int DONTENUM = 2;    // Property not enumerable
public static final int DONTDELETE = 4;  // Property cannot be deleted
public static final int CONST = 13;      // READONLY | DONTENUM | DONTDELETE

Usage Examples:

// Define properties with attributes
ScriptableObject obj = (ScriptableObject) cx.initStandardObjects();
obj.defineProperty("PI", Math.PI, ScriptableObject.READONLY | ScriptableObject.DONTENUM);

// Define function properties from Java methods
obj.defineFunctionProperties(new String[]{"myFunction"}, MyClass.class, ScriptableObject.DONTENUM);

// Seal object to prevent modifications
obj.sealObject();
boolean sealed = obj.isSealed();  // true

// Utility methods work on any Scriptable
ScriptableObject.putProperty(obj, "temp", "value");
Object temp = ScriptableObject.getProperty(obj, "temp");

Core JavaScript Objects

NativeObject

JavaScript Object implementation providing the base object functionality.

/**
 * JavaScript Object implementation
 * Extends IdScriptableObject for optimized property access
 */
public class NativeObject extends IdScriptableObject {
    /**
     * Default constructor creates empty object
     */
    public NativeObject();
}

NativeArray

JavaScript Array implementation with full Array.prototype methods and Java List interface.

/**
 * JavaScript Array implementation
 * Implements both Scriptable and java.util.List interfaces
 */
public class NativeArray extends IdScriptableObject implements List<Object> {
    /**
     * Creates array with specified length
     * @param length Initial array length
     */
    public NativeArray(long length);
    
    /**
     * Creates array from Java array elements
     * @param array Initial elements
     */
    public NativeArray(Object[] array);
    
    // List interface methods
    public int size();
    public boolean isEmpty();
    public boolean contains(Object o);
    public Iterator<Object> iterator();
    public Object[] toArray();
    public boolean add(Object o);
    public boolean remove(Object o);
    public void clear();
    public Object get(int index);
    public Object set(int index, Object element);
    public void add(int index, Object element);
    public Object remove(int index);
}

Usage Examples:

// Create arrays
NativeArray arr1 = new NativeArray(5);  // length 5
Object[] elements = {"a", "b", "c"};
NativeArray arr2 = new NativeArray(elements);

// Use as Java List
List<Object> list = new NativeArray(0);
list.add("item1");
list.add("item2");
System.out.println(list.size());  // 2

// Access via Scriptable interface
arr2.put(0, arr2, "modified");
Object first = arr2.get(0, arr2);  // "modified"

NativeFunction

Base class for all JavaScript functions providing call and construct functionality.

/**
 * Base class for JavaScript functions
 * Extends BaseFunction with additional functionality
 */
public class NativeFunction extends BaseFunction {
    // Inherited from Function interface
    /**
     * Calls function with specified arguments
     * @param cx Current Context
     * @param scope Scope for execution
     * @param thisObj 'this' object for call
     * @param args Function arguments
     * @return Function result
     */
    public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args);
    
    /**
     * Constructs new object using function as constructor
     * @param cx Current Context
     * @param scope Scope for construction
     * @param args Constructor arguments
     * @return New object instance
     */
    public Scriptable construct(Context cx, Scriptable scope, Object[] args);
}

Primitive Wrapper Objects

NativeString

JavaScript String object implementation with all String.prototype methods.

/**
 * JavaScript String object implementation
 * Wraps Java String with JavaScript String prototype methods
 */
public class NativeString extends IdScriptableObject {
    /**
     * Creates String object from Java string
     * @param s Java string value
     */
    public NativeString(String s);
    
    /**
     * Gets the wrapped string value
     * @return Java String value
     */
    public String getStringValue();
}

NativeNumber

JavaScript Number object implementation with Number.prototype methods.

/**
 * JavaScript Number object implementation
 * Wraps Java Number with JavaScript Number prototype methods
 */
public class NativeNumber extends IdScriptableObject {
    /**
     * Creates Number object from double value
     * @param number Numeric value
     */
    public NativeNumber(double number);
    
    /**
     * Gets the wrapped numeric value
     * @return Numeric value as double
     */
    public double getDoubleValue();
}

NativeBoolean

JavaScript Boolean object implementation.

/**
 * JavaScript Boolean object implementation
 * Wraps Java boolean with JavaScript Boolean prototype methods
 */
public class NativeBoolean extends IdScriptableObject {
    /**
     * Creates Boolean object from boolean value
     * @param b Boolean value
     */
    public NativeBoolean(boolean b);
    
    /**
     * Gets the wrapped boolean value
     * @return Boolean value
     */
    public boolean getBooleanValue();
}

NativeDate

JavaScript Date object implementation with full Date API.

/**
 * JavaScript Date object implementation
 * Provides full Date constructor and prototype functionality
 */
public class NativeDate extends IdScriptableObject {
    /**
     * Creates Date object for current time
     */
    public NativeDate();
    
    /**
     * Creates Date object from milliseconds since epoch
     * @param time Milliseconds since January 1, 1970 UTC
     */
    public NativeDate(double time);
    
    /**
     * Gets time value as milliseconds since epoch
     * @return Time in milliseconds
     */
    public double getTime();
}

ES6+ Objects

NativeSymbol

JavaScript Symbol implementation with global symbol registry.

/**
 * JavaScript Symbol implementation
 * Provides Symbol constructor and global registry
 */
public class NativeSymbol extends IdScriptableObject {
    /**
     * Creates or retrieves global symbol for key
     * @param key Symbol key string
     * @return Global symbol for key
     */
    public static Symbol for(String key);
    
    /**
     * Gets key for global symbol
     * @param symbol Symbol to look up
     * @return Key string or null if not global symbol
     */
    public static String keyFor(Symbol symbol);
}

/**
 * Symbol primitive value
 */
public class Symbol {
    /**
     * Gets string representation of symbol
     * @return String representation
     */
    public String toString();
}

NativePromise

JavaScript Promise implementation with full ES6 Promise API.

/**
 * JavaScript Promise implementation
 * Provides full Promise constructor and prototype methods
 */
public class NativePromise extends IdScriptableObject {
    /**
     * Creates resolved Promise with value
     * @param cx Current Context
     * @param scope Current scope
     * @param value Resolution value
     * @return Resolved Promise
     */
    public static NativePromise resolve(Context cx, Scriptable scope, Object value);
    
    /**
     * Creates rejected Promise with reason
     * @param cx Current Context
     * @param scope Current scope
     * @param reason Rejection reason
     * @return Rejected Promise
     */
    public static NativePromise reject(Context cx, Scriptable scope, Object reason);
    
    /**
     * Creates Promise that resolves when all input promises resolve
     * @param cx Current Context
     * @param scope Current scope
     * @param promises Array of promises
     * @return Promise that resolves to array of results
     */
    public static NativePromise all(Context cx, Scriptable scope, Object promises);
}

NativeMap and NativeSet

ES6 Map and Set implementations with iteration support.

/**
 * JavaScript Map implementation
 * Provides Map constructor and prototype methods
 */
public class NativeMap extends IdScriptableObject {
    /**
     * Creates new empty Map
     */
    public NativeMap();
    
    /**
     * Gets value for key
     * @param key Map key
     * @return Value or undefined
     */
    public Object get(Object key);
    
    /**
     * Sets key-value pair
     * @param key Map key
     * @param value Map value
     * @return This Map for chaining
     */
    public NativeMap set(Object key, Object value);
    
    /**
     * Checks if key exists
     * @param key Key to check
     * @return true if key exists
     */
    public boolean has(Object key);
    
    /**
     * Deletes key-value pair
     * @param key Key to delete
     * @return true if key existed and was deleted
     */
    public boolean delete(Object key);
    
    /**
     * Gets number of key-value pairs
     * @return Size of map
     */
    public int size();
}

/**
 * JavaScript Set implementation
 * Provides Set constructor and prototype methods
 */
public class NativeSet extends IdScriptableObject {
    /**
     * Creates new empty Set
     */
    public NativeSet();
    
    /**
     * Adds value to set
     * @param value Value to add
     * @return This Set for chaining
     */
    public NativeSet add(Object value);
    
    /**
     * Checks if value exists in set
     * @param value Value to check
     * @return true if value exists
     */
    public boolean has(Object value);
    
    /**
     * Deletes value from set
     * @param value Value to delete
     * @return true if value existed and was deleted
     */
    public boolean delete(Object value);
    
    /**
     * Gets number of values in set
     * @return Size of set
     */
    public int size();
}

Iteration Protocol

ES6Iterator

Base class for all ES6-style iterators supporting for-of loops.

/**
 * Base class for ES6 iterators
 * Implements iterator protocol for for-of loops
 */
public abstract class ES6Iterator extends IdScriptableObject {
    /**
     * Gets next iteration result
     * @return Object with 'value' and 'done' properties
     */
    public abstract Object next();
    
    /**
     * Makes object iterable (returns itself)
     * @return This iterator
     */
    public ES6Iterator iterator();
}

/**
 * Array iterator for Array.prototype[Symbol.iterator]
 */
public class NativeArrayIterator extends ES6Iterator {
    /**
     * Creates iterator for array
     * @param array Array to iterate
     * @param type Iteration type (keys, values, entries)
     */
    public NativeArrayIterator(Scriptable array, Type type);
    
    public enum Type {
        KEYS,    // Iterate indices
        VALUES,  // Iterate values
        ENTRIES  // Iterate [index, value] pairs
    }
}

/**
 * String iterator for String.prototype[Symbol.iterator]
 */
public class NativeStringIterator extends ES6Iterator {
    /**
     * Creates iterator for string
     * @param string String to iterate (by Unicode code points)
     */
    public NativeStringIterator(String string);
}

Special Objects

NativeError

JavaScript Error object with stack trace support.

/**
 * JavaScript Error object implementation
 * Supports Error constructor and stack traces
 */
public class NativeError extends IdScriptableObject {
    /**
     * Creates Error with message
     * @param message Error message
     */
    public NativeError(String message);
    
    /**
     * Gets error message
     * @return Error message string
     */
    public String getMessage();
    
    /**
     * Gets stack trace
     * @return Stack trace string
     */
    public String getStackTrace();
}

NativeJSON

JavaScript JSON object with parse/stringify methods.

/**
 * JavaScript JSON object implementation
 * Provides JSON.parse and JSON.stringify functionality
 */
public class NativeJSON extends IdScriptableObject {
    /**
     * Parses JSON string into JavaScript value
     * @param cx Current Context
     * @param scope Current scope
     * @param text JSON string to parse
     * @param reviver Optional reviver function
     * @return Parsed JavaScript value
     */
    public static Object parse(Context cx, Scriptable scope, String text, Function reviver);
    
    /**
     * Converts JavaScript value to JSON string
     * @param cx Current Context
     * @param scope Current scope
     * @param value Value to stringify
     * @param replacer Optional replacer function/array
     * @param space Optional indentation
     * @return JSON string representation
     */
    public static String stringify(Context cx, Scriptable scope, Object value, Object replacer, Object space);
}

Usage Examples:

// Working with native objects
Scriptable obj = cx.newObject(scope);
ScriptableObject.putProperty(obj, "name", "test");

// Arrays with List interface  
NativeArray arr = new NativeArray(new Object[]{"a", "b", "c"});
arr.add("d");  // Use as Java List
Object item = arr.get(1, arr);  // Use as Scriptable

// ES6 collections
NativeMap map = new NativeMap();
map.set("key1", "value1");
map.set("key2", "value2");
System.out.println(map.size());  // 2

NativeSet set = new NativeSet();
set.add("item1");
set.add("item2");
boolean hasItem = set.has("item1");  // true

// Promises (basic example)
NativePromise resolved = NativePromise.resolve(cx, scope, "success");
NativePromise rejected = NativePromise.reject(cx, scope, "error");

// JSON operations
String json = "{\"name\":\"test\",\"value\":42}";
Object parsed = NativeJSON.parse(cx, scope, json, null);
String serialized = NativeJSON.stringify(cx, scope, parsed, null, null);

Install with Tessl CLI

npx tessl i tessl/maven-org-mozilla--rhino

docs

index.md

java-integration.md

javascript-objects.md

script-execution.md

security-debugging.md

tile.json