Skip to content

Commit

Permalink
Convert Map and Set to use lambdas
Browse files Browse the repository at this point in the history
Use LambdaFunction and LambdaConstructor to implement Map, Set, WeakMap, and WeakSet. 

While we're at it, fix Symbol support in WeakMap and WeakSet.
  • Loading branch information
gbrail authored Jan 31, 2025
1 parent 07492b5 commit 0d1ca69
Show file tree
Hide file tree
Showing 12 changed files with 538 additions and 748 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,19 @@ static Map<Object, List<Object>> groupBy(
Object items,
Object callback,
KEY_COERCION keyCoercion) {
return groupBy(cx, scope, f.getTag(), f.getFunctionName(), items, callback, keyCoercion);
}

static Map<Object, List<Object>> groupBy(
Context cx,
Scriptable scope,
Object classTag,
String functionName,
Object items,
Object callback,
KEY_COERCION keyCoercion) {
if (cx.getLanguageVersion() >= Context.VERSION_ES6) {
ScriptRuntimeES6.requireObjectCoercible(cx, items, f);
ScriptRuntimeES6.requireObjectCoercible(cx, items, classTag, functionName);
}
if (!(callback instanceof Callable)) {
throw ScriptRuntime.typeErrorById(
Expand Down
30 changes: 30 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/LambdaConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public void definePrototypeProperty(Symbol key, Object value, int attributes) {
proto.defineProperty(key, value, attributes);
}

public void definePrototypeProperty(Context cx, String name, ScriptableObject descriptor) {
ScriptableObject proto = getPrototypeScriptable();
proto.defineOwnProperty(cx, name, descriptor);
}

public void definePrototypeProperty(Context cx, Symbol key, ScriptableObject descriptor) {
ScriptableObject proto = getPrototypeScriptable();
proto.defineOwnProperty(cx, key, descriptor);
}

/**
* Define a property on the prototype using a function. The function will be wired to a
* JavaScript function, so the resulting property will look just like one that was defined using
Expand All @@ -203,6 +213,12 @@ public void definePrototypeProperty(
proto.defineProperty(cx, name, getter, null, attributes);
}

public void definePrototypeProperty(
Context cx, Symbol key, ScriptableObject.LambdaGetterFunction getter, int attributes) {
ScriptableObject proto = getPrototypeScriptable();
proto.defineProperty(cx, key, getter, null, attributes);
}

/**
* Define a property on the prototype using functions for getter and setter. The function will
* be wired to a JavaScript function, so the resulting property will look just like one that was
Expand All @@ -218,6 +234,20 @@ public void definePrototypeProperty(
proto.defineProperty(cx, name, getter, setter, attributes);
}

/** Define a property on the prototype that has the same value as another property. */
public void definePrototypeAlias(String name, SymbolKey alias, int attributes) {
ScriptableObject proto = getPrototypeScriptable();
Object val = proto.get(name, proto);
proto.defineProperty(alias, val, attributes);
}

/** Define a property on the prototype that has the same value as another property. */
public void definePrototypeAlias(String name, String alias, int attributes) {
ScriptableObject proto = getPrototypeScriptable();
Object val = proto.get(name, proto);
proto.defineProperty(alias, val, attributes);
}

/**
* Define a function property directly on the constructor that is implemented under the covers
* by a LambdaFunction.
Expand Down
Loading

0 comments on commit 0d1ca69

Please sign in to comment.