From 41027d4e51719d094371b7fe8e7770c6807bb494 Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Sat, 21 May 2022 11:37:13 +0200 Subject: [PATCH] fix missing scope definition at some places --- src/org/mozilla/javascript/ArrowFunction.java | 1 + src/org/mozilla/javascript/BaseFunction.java | 5 ++++- src/org/mozilla/javascript/BoundFunction.java | 1 + src/org/mozilla/javascript/Delegator.java | 2 +- src/org/mozilla/javascript/NativeObject.java | 2 +- .../javascript/tests/es6/NativeObjectTest.java | 11 +++++++++++ 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/org/mozilla/javascript/ArrowFunction.java b/src/org/mozilla/javascript/ArrowFunction.java index fef2797947..9d958bf00a 100644 --- a/src/org/mozilla/javascript/ArrowFunction.java +++ b/src/org/mozilla/javascript/ArrowFunction.java @@ -23,6 +23,7 @@ public ArrowFunction( Function thrower = ScriptRuntime.typeErrorThrower(cx); NativeObject throwing = new NativeObject(); + ScriptRuntime.setBuiltinProtoAndParent(throwing, scope, TopLevel.Builtins.Object); throwing.put("get", throwing, thrower); throwing.put("set", throwing, thrower); throwing.put("enumerable", throwing, Boolean.FALSE); diff --git a/src/org/mozilla/javascript/BaseFunction.java b/src/org/mozilla/javascript/BaseFunction.java index 4cda3ac833..6cfce5fbaa 100644 --- a/src/org/mozilla/javascript/BaseFunction.java +++ b/src/org/mozilla/javascript/BaseFunction.java @@ -509,7 +509,8 @@ protected synchronized Object setupDefaultPrototype() { return prototypeProperty; } NativeObject obj = new NativeObject(); - obj.defineProperty("constructor", this, DONTENUM); + obj.setParentScope(getParentScope()); + // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. @@ -519,6 +520,8 @@ protected synchronized Object setupDefaultPrototype() { // not the one we just made, it must remain grounded obj.setPrototype(proto); } + + obj.defineProperty("constructor", this, DONTENUM); return obj; } diff --git a/src/org/mozilla/javascript/BoundFunction.java b/src/org/mozilla/javascript/BoundFunction.java index d12d989acb..74210f05bb 100644 --- a/src/org/mozilla/javascript/BoundFunction.java +++ b/src/org/mozilla/javascript/BoundFunction.java @@ -39,6 +39,7 @@ public BoundFunction( Function thrower = ScriptRuntime.typeErrorThrower(cx); NativeObject throwing = new NativeObject(); + ScriptRuntime.setBuiltinProtoAndParent(throwing, scope, TopLevel.Builtins.Object); throwing.put("get", throwing, thrower); throwing.put("set", throwing, thrower); throwing.put("enumerable", throwing, Boolean.FALSE); diff --git a/src/org/mozilla/javascript/Delegator.java b/src/org/mozilla/javascript/Delegator.java index 3d5dceb660..c7091979a1 100644 --- a/src/org/mozilla/javascript/Delegator.java +++ b/src/org/mozilla/javascript/Delegator.java @@ -243,7 +243,7 @@ public Scriptable construct(Context cx, Scriptable scope, Object[] args) { Delegator n = newInstance(); Scriptable delegee; if (args.length == 0) { - delegee = new NativeObject(); + delegee = cx.newObject(scope); } else { delegee = ScriptRuntime.toObject(cx, scope, args[0]); } diff --git a/src/org/mozilla/javascript/NativeObject.java b/src/org/mozilla/javascript/NativeObject.java index ea713ab6f3..bdde0b5ad9 100644 --- a/src/org/mozilla/javascript/NativeObject.java +++ b/src/org/mozilla/javascript/NativeObject.java @@ -160,7 +160,7 @@ public Object execIdCall( return f.construct(cx, scope, args); } if (args.length == 0 || args[0] == null || Undefined.isUndefined(args[0])) { - return new NativeObject(); + return cx.newObject(scope); } return ScriptRuntime.toObject(cx, scope, args[0]); } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java index 016557328f..01a2b87aa7 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java @@ -264,6 +264,17 @@ public void testFromEntriesOnArray() { evaluateAndAssert("Object.fromEntries(Object.entries(['x','y','z']))", map); } + @Test + public void issue943() { + evaluateAndAssert( + "var foo = function e() {}\n" + + "var fooProto = foo.prototype;\n" + + + "var descProp = Object.getOwnPropertyDescriptor(fooProto, 'constructor');" + + "descProp.hasOwnProperty('value');\n", + true); + } + private void evaluateAndAssert(final String script, final Object expected) { String[] prefixes = {"", "'use strict;'\n"}; for (final String prefix : prefixes) {