From e195119a61d1b6750da6243f4cfca30ea25e7bdd Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Fri, 20 Sep 2024 19:08:24 +0200 Subject: [PATCH] Reflect - first hack for constructor detection (see #1376) --- .../org/mozilla/javascript/NativeReflect.java | 17 ++++- .../tests/es6/NativeReflectTest.java | 5 +- tests/testsrc/test262.properties | 72 ++----------------- 3 files changed, 22 insertions(+), 72 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeReflect.java b/rhino/src/main/java/org/mozilla/javascript/NativeReflect.java index 7ae6a13e7b..99ce5c44ca 100644 --- a/rhino/src/main/java/org/mozilla/javascript/NativeReflect.java +++ b/rhino/src/main/java/org/mozilla/javascript/NativeReflect.java @@ -142,7 +142,7 @@ private static Scriptable construct( Integer.toString(args.length)); } - if (!(args[0] instanceof Constructable)) { + if (!isConstructor(args[0])) { throw ScriptRuntime.typeErrorById("msg.not.ctor", ScriptRuntime.typeof(args[0])); } @@ -151,7 +151,7 @@ private static Scriptable construct( return ctor.construct(cx, scope, ScriptRuntime.emptyArgs); } - if (args.length > 2 && !(args[2] instanceof Constructable)) { + if (args.length > 2 && !isConstructor(args[2])) { throw ScriptRuntime.typeErrorById("msg.not.ctor", ScriptRuntime.typeof(args[2])); } @@ -198,6 +198,19 @@ private static Scriptable construct( return newScriptable; } + private static boolean isConstructor(final Object argument) { + // Hack for the moment because all Functions are Constructable + // see #1376 for more + if (argument instanceof LambdaConstructor) { + return true; + } + if (argument instanceof LambdaFunction) { + return false; + } + + return argument instanceof Constructable; + } + private static Object defineProperty( Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { if (args.length < 3) { diff --git a/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeReflectTest.java b/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeReflectTest.java index 619cd4186b..97c6e73dba 100644 --- a/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeReflectTest.java +++ b/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeReflectTest.java @@ -157,12 +157,11 @@ public void constructNoConstructorObject() { public void constructNoConstructorFunction() { String js = "try {\n" - + " Reflect.construct(function() {}, [], Date.now);\n" + + " Reflect.construct(function() {}, [], Math.abs);\n" + "} catch(e) {\n" + " '' + e;\n" + "}"; - // testString("TypeError: \"object\" is not a constructor.", js); - // found no way to check a function for constructor + testString("TypeError: \"function\" is not a constructor.", js); } @Test diff --git a/tests/testsrc/test262.properties b/tests/testsrc/test262.properties index 109d018fff..10de018648 100644 --- a/tests/testsrc/test262.properties +++ b/tests/testsrc/test262.properties @@ -943,10 +943,9 @@ built-ins/GeneratorPrototype 38/60 (63.33%) built-ins/Infinity 0/6 (0.0%) -built-ins/JSON 29/144 (20.14%) +built-ins/JSON 27/144 (18.75%) parse/builtin.js parse/duplicate-proto.js - parse/not-a-constructor.js parse/revived-proxy.js parse/reviver-array-define-prop-err.js parse/reviver-array-get-prop-from-prototype.js @@ -960,7 +959,6 @@ built-ins/JSON 29/144 (20.14%) parse/S15.12.2_A1.js parse/text-negative-zero.js stringify/builtin.js - stringify/not-a-constructor.js stringify/replacer-array-abrupt.js stringify/replacer-array-proxy.js stringify/replacer-array-wrong-type.js @@ -990,45 +988,10 @@ built-ins/Map 12/171 (7.02%) built-ins/MapIteratorPrototype 0/11 (0.0%) -built-ins/Math 51/327 (15.6%) - abs/not-a-constructor.js - acosh/not-a-constructor.js - acos/not-a-constructor.js - asinh/not-a-constructor.js - asin/not-a-constructor.js - atan2/not-a-constructor.js - atanh/not-a-constructor.js - atan/not-a-constructor.js - cbrt/not-a-constructor.js - ceil/not-a-constructor.js - clz32/not-a-constructor.js - cosh/not-a-constructor.js - cos/not-a-constructor.js - expm1/not-a-constructor.js - exp/not-a-constructor.js +built-ins/Math 16/327 (4.89%) f16round 5/5 (100.0%) - floor/not-a-constructor.js - fround/not-a-constructor.js - hypot/not-a-constructor.js - imul/not-a-constructor.js - log10/not-a-constructor.js - log1p/not-a-constructor.js log2/log2-basicTests.js calculation is not exact - log2/not-a-constructor.js - log/not-a-constructor.js - max/not-a-constructor.js - min/not-a-constructor.js - pow/not-a-constructor.js - random/not-a-constructor.js - round/not-a-constructor.js - sign/not-a-constructor.js - sinh/not-a-constructor.js - sin/not-a-constructor.js - sqrt/not-a-constructor.js sumPrecise 10/10 (100.0%) - tanh/not-a-constructor.js - tan/not-a-constructor.js - trunc/not-a-constructor.js built-ins/NaN 0/6 (0.0%) @@ -1292,7 +1255,7 @@ built-ins/Object 212/3408 (6.22%) proto-from-ctor-realm.js subclass-object-arg.js {unsupported: [class]} -built-ins/Promise 403/631 (63.87%) +built-ins/Promise 392/631 (62.12%) allSettled/capability-resolve-throws-reject.js {unsupported: [async]} allSettled/ctx-ctor.js {unsupported: [class]} allSettled/does-not-invoke-array-setters.js {unsupported: [async]} @@ -1329,7 +1292,6 @@ built-ins/Promise 403/631 (63.87%) allSettled/iter-returns-true-reject.js {unsupported: [async]} allSettled/iter-returns-undefined-reject.js {unsupported: [async]} allSettled/iter-step-err-reject.js {unsupported: [async]} - allSettled/not-a-constructor.js allSettled/reject-deferred.js {unsupported: [async]} allSettled/reject-element-function-property-order.js allSettled/reject-ignored-deferred.js {unsupported: [async]} @@ -1388,7 +1350,6 @@ built-ins/Promise 403/631 (63.87%) all/iter-returns-true-reject.js {unsupported: [async]} all/iter-returns-undefined-reject.js {unsupported: [async]} all/iter-step-err-reject.js {unsupported: [async]} - all/not-a-constructor.js all/reject-deferred.js {unsupported: [async]} all/reject-ignored-deferred.js {unsupported: [async]} all/reject-ignored-immed.js {unsupported: [async]} @@ -1464,7 +1425,6 @@ built-ins/Promise 403/631 (63.87%) any/iter-returns-undefined-reject.js {unsupported: [async]} any/iter-step-err-no-close.js {unsupported: [async]} any/iter-step-err-reject.js {unsupported: [async]} - any/not-a-constructor.js any/reject-all-mixed.js {unsupported: [async]} any/reject-deferred.js {unsupported: [async]} any/reject-element-function-property-order.js @@ -1482,11 +1442,8 @@ built-ins/Promise 403/631 (63.87%) any/resolved-sequence-extra-ticks.js {unsupported: [async]} any/resolved-sequence-mixed.js {unsupported: [async]} any/resolved-sequence-with-rejections.js {unsupported: [async]} - prototype/catch/not-a-constructor.js prototype/catch/S25.4.5.1_A3.1_T1.js {unsupported: [async]} prototype/catch/S25.4.5.1_A3.1_T2.js {unsupported: [async]} - prototype/finally/invokes-then-with-function.js - prototype/finally/not-a-constructor.js prototype/finally/rejected-observable-then-calls.js {unsupported: [async]} prototype/finally/rejected-observable-then-calls-argument.js {unsupported: [class, async]} prototype/finally/rejected-observable-then-calls-PromiseResolve.js {unsupported: [async]} @@ -1506,7 +1463,6 @@ built-ins/Promise 403/631 (63.87%) prototype/then/ctor-access-count.js {unsupported: [async]} prototype/then/ctor-custom.js {unsupported: [class]} prototype/then/deferred-is-resolved-value.js {unsupported: [class, async]} - prototype/then/not-a-constructor.js prototype/then/prfm-fulfilled.js {unsupported: [async]} prototype/then/prfm-pending-fulfulled.js {unsupported: [async]} prototype/then/prfm-pending-rejected.js {unsupported: [async]} @@ -1596,7 +1552,6 @@ built-ins/Promise 403/631 (63.87%) race/iter-returns-true-reject.js {unsupported: [async]} race/iter-returns-undefined-reject.js {unsupported: [async]} race/iter-step-err-reject.js {unsupported: [async]} - race/not-a-constructor.js race/reject-deferred.js {unsupported: [async]} race/reject-ignored-deferred.js {unsupported: [async]} race/reject-ignored-immed.js {unsupported: [async]} @@ -1631,12 +1586,10 @@ built-ins/Promise 403/631 (63.87%) race/S25.4.4.3_A7.3_T2.js {unsupported: [async]} reject/capability-invocation.js reject/ctx-ctor.js {unsupported: [class]} - reject/not-a-constructor.js reject/S25.4.4.4_A2.1_T1.js {unsupported: [async]} resolve/arg-non-thenable.js {unsupported: [async]} resolve/arg-poisoned-then.js {unsupported: [async]} resolve/ctx-ctor.js {unsupported: [class]} - resolve/not-a-constructor.js resolve/resolve-from-promise-capability.js resolve/resolve-non-obj.js {unsupported: [async]} resolve/resolve-non-thenable.js {unsupported: [async]} @@ -1667,7 +1620,6 @@ built-ins/Promise 403/631 (63.87%) create-resolving-functions-resolve.js {unsupported: [async]} exception-after-resolve-in-executor.js {unsupported: [async]} exception-after-resolve-in-thenable-job.js {unsupported: [async]} - executor-function-not-a-constructor.js executor-function-property-order.js get-prototype-abrupt.js property-order.js @@ -1697,7 +1649,7 @@ built-ins/Promise 403/631 (63.87%) resolve-thenable-deferred.js {unsupported: [async]} resolve-thenable-immed.js {unsupported: [async]} -built-ins/Proxy 82/311 (26.37%) +built-ins/Proxy 81/311 (26.05%) construct/arguments-realm.js construct/call-parameters.js construct/call-parameters-new-target.js @@ -1747,7 +1699,6 @@ built-ins/Proxy 82/311 (26.37%) ownKeys/trap-is-undefined-target-is-proxy.js preventExtensions/trap-is-undefined-target-is-proxy.js {unsupported: [module]} revocable/builtin.js - revocable/not-a-constructor.js revocable/revocation-function-not-a-constructor.js revocable/revocation-function-property-order.js revocable/tco-fn-realm.js {unsupported: [tail-call-optimization]} @@ -1781,29 +1732,16 @@ built-ins/Proxy 82/311 (26.37%) get-fn-realm-recursive.js property-order.js -built-ins/Reflect 26/153 (16.99%) - apply/not-a-constructor.js +built-ins/Reflect 13/153 (8.5%) construct/newtarget-is-not-constructor-throws.js - construct/not-a-constructor.js - defineProperty/not-a-constructor.js defineProperty/return-abrupt-from-property-key.js - deleteProperty/not-a-constructor.js deleteProperty/return-abrupt-from-result.js deleteProperty/return-boolean.js strict - getOwnPropertyDescriptor/not-a-constructor.js - getPrototypeOf/not-a-constructor.js - get/not-a-constructor.js get/return-value-from-receiver.js - has/not-a-constructor.js - isExtensible/not-a-constructor.js - ownKeys/not-a-constructor.js ownKeys/order-after-define-property.js ownKeys/return-on-corresponding-order-large-index.js - preventExtensions/not-a-constructor.js - setPrototypeOf/not-a-constructor.js set/call-prototype-property-set.js set/different-property-descriptors.js - set/not-a-constructor.js set/receiver-is-not-object.js set/return-abrupt-from-result.js set/return-false-if-receiver-is-not-writable.js