Skip to content

Commit

Permalink
streamline fallback code and improved type check
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Sep 29, 2024
1 parent 32f6bac commit b2e218c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
43 changes: 32 additions & 11 deletions rhino/src/main/java/org/mozilla/javascript/NativeProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ public boolean has(String name, Scriptable start) {
return booleanTrapResult;
}

return ScriptableObject.hasProperty(target, name);
if (start == this) {
start = target;
}
return target.has(name, start);
}

/**
Expand Down Expand Up @@ -233,7 +236,10 @@ public boolean has(int index, Scriptable start) {
return booleanTrapResult;
}

return ScriptableObject.hasProperty(target, index);
if (start == this) {
start = target;
}
return target.has(index, start);
}

/**
Expand Down Expand Up @@ -263,7 +269,11 @@ public boolean has(Symbol key, Scriptable start) {
return booleanTrapResult;
}

return ScriptableObject.hasProperty(target, key);
if (start == this) {
start = target;
}
SymbolScriptable symbolScriptableTarget = ensureSymbolScriptable(target);
return symbolScriptableTarget.has(key, start);
}

/**
Expand Down Expand Up @@ -438,7 +448,10 @@ public Object get(String name, Scriptable start) {
return trapResult;
}

return ScriptRuntime.getObjectProp(target, name, Context.getContext());
if (start == this) {
start = target;
}
return target.get(name, start);
}

/**
Expand Down Expand Up @@ -495,7 +508,10 @@ public Object get(int index, Scriptable start) {
return trapResult;
}

return ScriptRuntime.getObjectIndex(target, index, Context.getContext());
if (start == this) {
start = target;
}
return target.get(index, start);
}

/**
Expand Down Expand Up @@ -612,7 +628,10 @@ public void put(String name, Scriptable start, Object value) {
return; // true
}

ScriptableObject.putProperty(target, name, value);
if (start == this) {
start = target;
}
target.put(name, start, value);
}

/**
Expand Down Expand Up @@ -672,7 +691,10 @@ public void put(int index, Scriptable start, Object value) {
return; // true
}

ScriptableObject.putProperty(target, index, value);
if (start == this) {
start = target;
}
target.put(index, start, value);
}

/**
Expand Down Expand Up @@ -1270,11 +1292,10 @@ private static NativeProxy constructor(Context cx, Scriptable scope, Object[] ar
"2",
Integer.toString(args.length));
}
ScriptableObject trgt = ensureScriptableObject(args[0]);

ScriptableObject hndlr = ensureScriptableObject(args[1]);
ScriptableObject target = ensureScriptableObjectButNotSymbol(args[0]);
ScriptableObject handler = ensureScriptableObjectButNotSymbol(args[1]);

NativeProxy proxy = new NativeProxy(trgt, hndlr);
NativeProxy proxy = new NativeProxy(target, handler);
proxy.setPrototypeDirect(ScriptableObject.getClassPrototype(scope, PROXY_TAG));
proxy.setParentScope(scope);
return proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,13 @@ protected static ScriptableObject ensureScriptableObject(Object arg) {
throw ScriptRuntime.typeErrorById("msg.arg.not.object", ScriptRuntime.typeof(arg));
}

protected static ScriptableObject ensureScriptableObjectButNotSymbol(Object arg) {
if (arg instanceof Symbol) {
throw ScriptRuntime.typeErrorById("msg.arg.not.object", ScriptRuntime.typeof(arg));
}
return ensureScriptableObject(arg);
}

/**
* Search for names in a class, adding the resulting methods as properties.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public void ctorMissingArgs() {
"try { new Proxy(null, {}) } catch(e) { '' + e }");
}

@Test
public void ctorWrongArgs() {
Utils.assertWithAllOptimizationLevelsES6(
"TypeError: Expected argument of type object, but instead had type symbol",
"try { new Proxy({}, Symbol()) } catch(e) { '' + e }");
Utils.assertWithAllOptimizationLevelsES6(
"TypeError: Expected argument of type object, but instead had type symbol",
"try { new Proxy(Symbol(), {}) } catch(e) { '' + e }");
}

@Test
public void ctorAsFunction() {
Utils.assertWithAllOptimizationLevelsES6(
Expand Down
7 changes: 2 additions & 5 deletions tests/testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ built-ins/Promise 392/631 (62.12%)
resolve-thenable-deferred.js {unsupported: [async]}
resolve-thenable-immed.js {unsupported: [async]}

built-ins/Proxy 79/311 (25.4%)
built-ins/Proxy 76/311 (24.44%)
construct/arguments-realm.js
construct/call-parameters.js
construct/call-parameters-new-target.js
Expand All @@ -1639,7 +1639,7 @@ built-ins/Proxy 79/311 (25.4%)
deleteProperty/return-false-not-strict.js non-strict
deleteProperty/return-false-strict.js strict
deleteProperty/targetdesc-is-configurable-target-is-not-extensible.js
deleteProperty/trap-is-missing-target-is-proxy.js
deleteProperty/trap-is-missing-target-is-proxy.js strict
deleteProperty/trap-is-null-target-is-proxy.js
deleteProperty/trap-is-undefined-strict.js strict
deleteProperty/trap-is-undefined-target-is-proxy.js
Expand All @@ -1661,7 +1661,6 @@ built-ins/Proxy 79/311 (25.4%)
has/return-false-target-prop-exists-using-with.js non-strict
has/return-false-targetdesc-not-configurable-using-with.js non-strict
has/return-is-abrupt-with.js non-strict
has/return-true-target-prop-exists-using-with.js non-strict
has/trap-is-missing-target-is-proxy.js
has/trap-is-not-callable-using-with.js non-strict
ownKeys/trap-is-undefined-target-is-proxy.js
Expand Down Expand Up @@ -1693,9 +1692,7 @@ built-ins/Proxy 79/311 (25.4%)
set/trap-is-null-receiver.js
set/trap-is-null-target-is-proxy.js
set/trap-is-undefined-target-is-proxy.js
create-handler-not-object-throw-symbol.js
create-target-is-not-a-constructor.js
create-target-not-object-throw-symbol.js
get-fn-realm.js
get-fn-realm-recursive.js
property-order.js
Expand Down

0 comments on commit b2e218c

Please sign in to comment.