Skip to content

Commit

Permalink
Revert "take care of Delegators when working with Sets/Maps"
Browse files Browse the repository at this point in the history
This reverts commit 2653e83.
  • Loading branch information
rbri committed Aug 10, 2021
1 parent 2653e83 commit 756dfec
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 192 deletions.
22 changes: 6 additions & 16 deletions src/org/mozilla/javascript/NativeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,23 @@ public Object execIdCall(
NativeMap nm = new NativeMap();
nm.instanceOfMap = true;
if (args.length > 0) {
loadFromIterable(cx, scope, nm, key(args));
loadFromIterable(cx, scope, nm, args[0]);
}
return nm;
}
throw ScriptRuntime.typeErrorById("msg.no.new", "Map");
case Id_set:
return realThis(thisObj, f)
.js_set(
key(args),
args.length > 0 ? args[0] : Undefined.instance,
args.length > 1 ? args[1] : Undefined.instance);
case Id_delete:
return realThis(thisObj, f).js_delete(key(args));
return realThis(thisObj, f)
.js_delete(args.length > 0 ? args[0] : Undefined.instance);
case Id_get:
return realThis(thisObj, f).js_get(key(args));
return realThis(thisObj, f).js_get(args.length > 0 ? args[0] : Undefined.instance);
case Id_has:
return realThis(thisObj, f).js_has(key(args));
return realThis(thisObj, f).js_has(args.length > 0 ? args[0] : Undefined.instance);
case Id_clear:
return realThis(thisObj, f).js_clear();
case Id_keys:
Expand Down Expand Up @@ -347,15 +348,4 @@ protected int findPrototypeId(String s) {
SymbolId_getSize = 11,
SymbolId_toStringTag = 12,
MAX_PROTOTYPE_ID = SymbolId_toStringTag;

private static Object key(Object[] args) {
if (args.length > 0) {
Object key = args[0];
if (key instanceof Delegator) {
return ((Delegator) key).getDelegee();
}
return key;
}
return Undefined.instance;
}
}
22 changes: 6 additions & 16 deletions src/org/mozilla/javascript/NativeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ public Object execIdCall(
NativeSet ns = new NativeSet();
ns.instanceOfSet = true;
if (args.length > 0) {
loadFromIterable(cx, scope, ns, key(args));
loadFromIterable(cx, scope, ns, args[0]);
}
return ns;
} else {
throw ScriptRuntime.typeErrorById("msg.no.new", "Set");
}
case Id_add:
return realThis(thisObj, f).js_add(key(args));
return realThis(thisObj, f).js_add(args.length > 0 ? args[0] : Undefined.instance);
case Id_delete:
return realThis(thisObj, f).js_delete(key(args));
return realThis(thisObj, f)
.js_delete(args.length > 0 ? args[0] : Undefined.instance);
case Id_has:
return realThis(thisObj, f).js_has(key(args));
return realThis(thisObj, f).js_has(args.length > 0 ? args[0] : Undefined.instance);
case Id_clear:
return realThis(thisObj, f).js_clear();
case Id_values:
Expand All @@ -76,7 +77,7 @@ public Object execIdCall(
.js_forEach(
cx,
scope,
key(args),
args.length > 0 ? args[0] : Undefined.instance,
args.length > 1 ? args[1] : Undefined.instance);
case SymbolId_getSize:
return realThis(thisObj, f).js_getSize();
Expand Down Expand Up @@ -310,15 +311,4 @@ protected int findPrototypeId(String s) {
SymbolId_getSize = 9,
SymbolId_toStringTag = 10,
MAX_PROTOTYPE_ID = SymbolId_toStringTag;

private static Object key(Object[] args) {
if (args.length > 0) {
Object key = args[0];
if (key instanceof Delegator) {
return ((Delegator) key).getDelegee();
}
return key;
}
return Undefined.instance;
}
}
22 changes: 6 additions & 16 deletions src/org/mozilla/javascript/NativeWeakMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ public Object execIdCall(
NativeWeakMap nm = new NativeWeakMap();
nm.instanceOfWeakMap = true;
if (args.length > 0) {
NativeMap.loadFromIterable(cx, scope, nm, key(args));
NativeMap.loadFromIterable(cx, scope, nm, args[0]);
}
return nm;
}
throw ScriptRuntime.typeErrorById("msg.no.new", "WeakMap");
case Id_delete:
return realThis(thisObj, f).js_delete(key(args));
return realThis(thisObj, f)
.js_delete(args.length > 0 ? args[0] : Undefined.instance);
case Id_get:
return realThis(thisObj, f).js_get(key(args));
return realThis(thisObj, f).js_get(args.length > 0 ? args[0] : Undefined.instance);
case Id_has:
return realThis(thisObj, f).js_has(key(args));
return realThis(thisObj, f).js_has(args.length > 0 ? args[0] : Undefined.instance);
case Id_set:
return realThis(thisObj, f)
.js_set(
key(args),
args.length > 0 ? args[0] : Undefined.instance,
args.length > 1 ? args[1] : Undefined.instance);
}
throw new IllegalArgumentException(
Expand Down Expand Up @@ -212,15 +213,4 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
stream.defaultReadObject();
map = new WeakHashMap<>();
}

private static Object key(Object[] args) {
if (args.length > 0) {
Object key = args[0];
if (key instanceof Delegator) {
return ((Delegator) key).getDelegee();
}
return key;
}
return Undefined.instance;
}
}
21 changes: 5 additions & 16 deletions src/org/mozilla/javascript/NativeWeakSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ public Object execIdCall(
return super.execIdCall(f, cx, scope, thisObj, args);
}
int id = f.methodId();

switch (id) {
case Id_constructor:
if (thisObj == null) {
NativeWeakSet ns = new NativeWeakSet();
ns.instanceOfWeakSet = true;
if (args.length > 0) {
NativeSet.loadFromIterable(cx, scope, ns, key(args));
NativeSet.loadFromIterable(cx, scope, ns, args[0]);
}
return ns;
}
throw ScriptRuntime.typeErrorById("msg.no.new", "WeakSet");
case Id_add:
return realThis(thisObj, f).js_add(key(args));
return realThis(thisObj, f).js_add(args.length > 0 ? args[0] : Undefined.instance);
case Id_delete:
return realThis(thisObj, f).js_delete(key(args));
return realThis(thisObj, f)
.js_delete(args.length > 0 ? args[0] : Undefined.instance);
case Id_has:
return realThis(thisObj, f).js_has(key(args));
return realThis(thisObj, f).js_has(args.length > 0 ? args[0] : Undefined.instance);
}
throw new IllegalArgumentException(
"WeakMap.prototype has no method: " + f.getFunctionName());
Expand Down Expand Up @@ -182,15 +182,4 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo
stream.defaultReadObject();
map = new WeakHashMap<>();
}

private static Object key(Object[] args) {
if (args.length > 0) {
Object key = args[0];
if (key instanceof Delegator) {
return ((Delegator) key).getDelegee();
}
return key;
}
return Undefined.instance;
}
}
128 changes: 0 additions & 128 deletions testsrc/org/mozilla/javascript/tests/DelegatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,134 +109,6 @@ public void setPrototypeOf() {
assertEquals(5, eval("a = {}; Object.setPrototypeOf(a, TestDelegator); a.length;"));
}

@Test
public void map() {
String script =
"var result = '';\n"
+ "var map = new Map();\n"
+ "map.set(TestDelegator, 'test');\n"

+ "result += map.has(TestDelegator);\n"
+ "result += '#';\n"

+ "result += map.get(TestDelegator);\n"
+ "result += '#';\n"

+ "result += map.delete(TestDelegator);\n"
+ "result += '#';\n"
+ "result;";
// assertEquals("true#test#true#", eval(script));

script =
"var result = '';\n"
+ "var map = new Map();\n"
+ "map.set(TestDelegator, 'test');\n"

+ "result += map.has(TestDelegee);\n"
+ "result += '#';\n"

+ "result += map.get(TestDelegee);\n"
+ "result += '#';\n"

+ "result += map.delete(TestDelegee);\n"
+ "result += '#';\n"
+ "result;";
assertEquals("true#test#true#", eval(script));
}

@Test
public void weakMap() {
String script =
"var result = '';\n"
+ "var map = new WeakMap();\n"
+ "map.set(TestDelegator, 'test');\n"

+ "result += map.has(TestDelegator);\n"
+ "result += '#';\n"

+ "result += map.get(TestDelegator);\n"
+ "result += '#';\n"

+ "result += map.delete(TestDelegator);\n"
+ "result += '#';\n"
+ "result;";
assertEquals("true#test#true#", eval(script));

script =
"var result = '';\n"
+ "var map = new WeakMap();\n"
+ "map.set(TestDelegator, 'test');\n"

+ "result += map.has(TestDelegee);\n"
+ "result += '#';\n"

+ "result += map.get(TestDelegee);\n"
+ "result += '#';\n"

+ "result += map.delete(TestDelegee);\n"
+ "result += '#';\n"
+ "result;";
assertEquals("true#test#true#", eval(script));
}

@Test
public void set() {
String script =
"var result = '';\n"
+ "var set = new Set();\n"
+ "set.add(TestDelegator);\n"

+ "result += set.has(TestDelegator);\n"
+ "result += '#';\n"

+ "result += set.delete(TestDelegator);\n"
+ "result += '#';\n"
+ "result;";
assertEquals("true#true#", eval(script));

script =
"var result = '';\n"
+ "var set = new Set();\n"
+ "set.add(TestDelegator);\n"

+ "result += set.has(TestDelegee);\n"
+ "result += '#';\n"

+ "result += set.delete(TestDelegee);\n"
+ "result += '#';\n"
+ "result;";
assertEquals("true#true#", eval(script));
}

@Test
public void weakSet() {
String script =
"var result = '';\n"
+ "var set = new WeakSet();\n"
+ "set.add(TestDelegator);\n"

+ "result += set.has(TestDelegator);\n"
+ "result += '#';\n"

+ "result += set.delete(TestDelegator);\n"
+ "result += '#';\n"
+ "result;";
assertEquals("true#true#", eval(script));

script =
"var result = '';\n"
+ "var set = new WeakSet();\n"
+ "set.add(TestDelegator);\n"

+ "result += set.has(TestDelegee);\n"
+ "result += '#';\n"

+ "result += set.delete(TestDelegee);\n"
+ "result += '#';\n"
+ "result;";
assertEquals("true#true#", eval(script));
}

public static class AnnotatedHostObject extends ScriptableObject {

Object foo = "Foo";
Expand Down

0 comments on commit 756dfec

Please sign in to comment.