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 28, 2024
1 parent 7096411 commit f493744
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 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
2 changes: 1 addition & 1 deletion rhino/src/main/java/org/mozilla/javascript/Symbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package org.mozilla.javascript;

/**
* A Symbol is a JavaScript objecy that obeys the special properties of the Symbol prototype. This
* A Symbol is a JavaScript object that obeys the special properties of the Symbol prototype. This
* interface lets us possibly support multiple implementations of Symbol.
*
* @since 1.7.8
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
10 changes: 3 additions & 7 deletions tests/testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ built-ins/Number 23/335 (6.87%)
S9.3.1_A3_T1_U180E.js {unsupported: [u180e]}
S9.3.1_A3_T2_U180E.js {unsupported: [u180e]}

built-ins/Object 184/3408 (5.4%)
built-ins/Object 183/3408 (5.37%)
assign/assignment-to-readonly-property-of-target-must-throw-a-typeerror-exception.js
assign/not-a-constructor.js
assign/source-own-prop-error.js
Expand Down Expand Up @@ -1071,7 +1071,6 @@ built-ins/Object 184/3408 (5.4%)
defineProperties/15.2.3.7-6-a-185.js
defineProperties/15.2.3.7-6-a-282.js
defineProperties/not-a-constructor.js
defineProperties/property-description-must-be-an-object-not-symbol.js
defineProperties/proxy-no-ownkeys-returned-keys-order.js
defineProperties/typedarray-backed-by-resizable-buffer.js {unsupported: [resizable-arraybuffer]}
defineProperty/15.2.3.6-4-116.js non-strict
Expand Down Expand Up @@ -1619,7 +1618,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 +1638,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 +1660,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 +1691,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 f493744

Please sign in to comment.