Skip to content

Commit

Permalink
fix missing scope definition at some places
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed May 21, 2022
1 parent cfe2ac2 commit 41027d4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/org/mozilla/javascript/ArrowFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/org/mozilla/javascript/BaseFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/org/mozilla/javascript/BoundFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/Delegator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/NativeObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
11 changes: 11 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 41027d4

Please sign in to comment.