From 71030b0883443dad84d51ef584eaafc736241310 Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Sat, 28 Sep 2024 19:31:42 +0200 Subject: [PATCH] streamline fallback code and improved type check --- .../org/mozilla/javascript/NativeProxy.java | 43 ++++++++++++++----- .../mozilla/javascript/ScriptableObject.java | 7 +++ .../java/org/mozilla/javascript/Symbol.java | 2 +- .../javascript/tests/es6/NativeProxyTest.java | 10 +++++ tests/testsrc/test262.properties | 7 +-- 5 files changed, 52 insertions(+), 17 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeProxy.java b/rhino/src/main/java/org/mozilla/javascript/NativeProxy.java index c69e71b4d0..fbc96f0391 100644 --- a/rhino/src/main/java/org/mozilla/javascript/NativeProxy.java +++ b/rhino/src/main/java/org/mozilla/javascript/NativeProxy.java @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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; diff --git a/rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java b/rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java index f83bc42a42..5e7ef2f5ac 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java +++ b/rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java @@ -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. * diff --git a/rhino/src/main/java/org/mozilla/javascript/Symbol.java b/rhino/src/main/java/org/mozilla/javascript/Symbol.java index 181708401b..8370c58d39 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Symbol.java +++ b/rhino/src/main/java/org/mozilla/javascript/Symbol.java @@ -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 diff --git a/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeProxyTest.java b/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeProxyTest.java index da1a9a98f4..0a329fd42b 100644 --- a/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeProxyTest.java +++ b/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeProxyTest.java @@ -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( diff --git a/tests/testsrc/test262.properties b/tests/testsrc/test262.properties index 2c66cf2b1c..2e97c1f611 100644 --- a/tests/testsrc/test262.properties +++ b/tests/testsrc/test262.properties @@ -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 @@ -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 @@ -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 @@ -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