Skip to content

Commit

Permalink
apply the fix for more objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri authored and gbrail committed Nov 30, 2022
1 parent ff24cf2 commit a6aa8f8
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/ArrowFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ArrowFunction(
this.targetFunction = targetFunction;
this.boundThis = boundThis;

ScriptRuntime.setFunctionProtoAndParent(this, scope);
ScriptRuntime.setFunctionProtoAndParent(this, cx, scope, false);

Function thrower = ScriptRuntime.typeErrorThrower(cx);
NativeObject throwing = new NativeObject();
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/BoundFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public BoundFunction(
length = 0;
}

ScriptRuntime.setFunctionProtoAndParent(this, scope);
ScriptRuntime.setFunctionProtoAndParent(this, cx, scope, false);

Function thrower = ScriptRuntime.typeErrorThrower(cx);
NativeObject throwing = new NativeObject();
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/FunctionObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public FunctionObject(String name, Member methodOrConstructor, Scriptable scope)
}
}

ScriptRuntime.setFunctionProtoAndParent(this, scope);
ScriptRuntime.setFunctionProtoAndParent(this, Context.getCurrentContext(), scope, false);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class JavaMembers {
this.staticMembers = new HashMap<String, Object>();
this.cl = cl;
boolean includePrivate = cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS);
reflect(scope, includeProtected, includePrivate);
reflect(cx, scope, includeProtected, includePrivate);
} finally {
Context.exit();
}
Expand Down Expand Up @@ -412,7 +412,8 @@ public int hashCode() {
}
}

private void reflect(Scriptable scope, boolean includeProtected, boolean includePrivate) {
private void reflect(
Context cx, Scriptable scope, boolean includeProtected, boolean includePrivate) {
// We reflect methods first, because we want overloaded field/method
// names to be allocated to the NativeJavaMethod before the field
// gets in the way.
Expand Down Expand Up @@ -465,7 +466,7 @@ private void reflect(Scriptable scope, boolean includeProtected, boolean include
}
NativeJavaMethod fun = new NativeJavaMethod(methodBoxes);
if (scope != null) {
ScriptRuntime.setFunctionProtoAndParent(fun, scope);
ScriptRuntime.setFunctionProtoAndParent(fun, cx, scope, false);
}
ht.put(entry.getKey(), fun);
}
Expand Down
5 changes: 1 addition & 4 deletions src/org/mozilla/javascript/NativeFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public final void initScriptFunction(Context cx, Scriptable scope) {

public final void initScriptFunction(
Context cx, Scriptable scope, boolean es6GeneratorFunction) {
ScriptRuntime.setFunctionProtoAndParent(this, scope, es6GeneratorFunction);
if (cx.getLanguageVersion() >= Context.VERSION_ES6) {
setStandardPropertyAttributes(READONLY | DONTENUM);
}
ScriptRuntime.setFunctionProtoAndParent(this, cx, scope, es6GeneratorFunction);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public int getLength() {
return 0;
}
};
ScriptRuntime.setFunctionProtoAndParent(thrower, cx.topCallScope);
ScriptRuntime.setFunctionProtoAndParent(thrower, cx, cx.topCallScope, false);
thrower.preventExtensions();
cx.typeErrorThrower = thrower;
}
Expand Down Expand Up @@ -4263,6 +4263,15 @@ public static void setFunctionProtoAndParent(
}
}

public static void setFunctionProtoAndParent(
BaseFunction fn, Context cx, Scriptable scope, boolean es6GeneratorFunction) {
setFunctionProtoAndParent(fn, scope, es6GeneratorFunction);

if (cx.getLanguageVersion() >= Context.VERSION_ES6) {
fn.setStandardPropertyAttributes(ScriptableObject.READONLY | ScriptableObject.DONTENUM);
}
}

public static void setObjectProtoAndParent(ScriptableObject object, Scriptable scope) {
// Compared with function it always sets the scope to top scope
scope = ScriptableObject.getTopLevelScope(scope);
Expand Down
41 changes: 40 additions & 1 deletion testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;

public class NativeFunctionTest {
private Context cx;
private ScriptableObject scope;

@Before
public void setUp() {
public void setUp() throws Exception {
cx = Context.enter();
cx.setLanguageVersion(Context.VERSION_ES6);
scope = cx.initStandardObjects();
ScriptableObject.defineClass(scope, HelperObject.class);
}

@After
Expand Down Expand Up @@ -85,4 +88,40 @@ public void testFunctionName() {
null);
assertEquals("configurable: true enumerable: false writable: false", result);
}

@Test
public void testFunctionNameJavaObject() {
Object result =
cx.evaluateString(
scope,
"var f=new HelperObject().foo;\n"
+ "var desc=Object.getOwnPropertyDescriptor(f, 'name');\n"
+ "var res = 'configurable: ' + desc.configurable;\n"
+ "res += ' enumerable: ' + desc.enumerable;\n"
+ "res += ' writable: ' + desc.writable;",
"test",
1,
null);
assertEquals("configurable: true enumerable: false writable: false", result);
}

public static class HelperObject extends ScriptableObject {

public HelperObject() {}

@Override
public String getClassName() {
return "HelperObject";
}

@JSConstructor
public void jsConstructorMethod() {
put("initialized", this, Boolean.TRUE);
}

@JSFunction("foo")
public Object foo() {
return "foo()";
}
}
}
9 changes: 3 additions & 6 deletions testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ built-ins/eval 3/9 (33.33%)
name.js
private-identifiers-not-empty.js {unsupported: [class-fields-private]}

built-ins/Function 188/505 (37.23%)
built-ins/Function 187/505 (37.03%)
internals/Call 2/2 (100.0%)
internals/Construct 6/6 (100.0%)
length/S15.3.5.1_A1_T3.js strict
Expand Down Expand Up @@ -528,7 +528,6 @@ built-ins/Function 188/505 (37.23%)
prototype/bind/instance-construct-newtarget-self-new.js {unsupported: [new.target]}
prototype/bind/instance-construct-newtarget-self-reflect.js {unsupported: [Reflect, new.target]}
prototype/bind/instance-length-exceeds-int32.js
prototype/bind/instance-length-prop-desc.js
prototype/bind/instance-length-tointeger.js
prototype/bind/instance-name.js
prototype/bind/instance-name-chained.js
Expand Down Expand Up @@ -954,7 +953,7 @@ built-ins/parseInt 3/60 (5.0%)
S15.1.2.2_A2_T10_U180E.js {unsupported: [u180e]}
S15.1.2.2_A9.2.js

built-ins/Promise 406/599 (67.78%)
built-ins/Promise 405/599 (67.61%)
allSettled/capability-resolve-throws-reject.js {unsupported: [async]}
allSettled/ctx-ctor.js {unsupported: [class]}
allSettled/does-not-invoke-array-setters.js {unsupported: [async]}
Expand Down Expand Up @@ -1278,7 +1277,6 @@ built-ins/Promise 406/599 (67.78%)
race/reject-ignored-deferred.js {unsupported: [async]}
race/reject-ignored-immed.js {unsupported: [async]}
race/reject-immed.js {unsupported: [async]}
race/resolve-element-function-name.js
race/resolve-ignores-late-rejection.js {unsupported: [async]}
race/resolve-ignores-late-rejection-deferred.js {unsupported: [async]}
race/resolve-non-callable.js {unsupported: [async]}
Expand Down Expand Up @@ -3096,7 +3094,7 @@ language/expressions/addition 9/48 (18.75%)
get-symbol-to-prim-err.js
order-of-evaluation.js

language/expressions/arrow-function 254/333 (76.28%)
language/expressions/arrow-function 253/333 (75.98%)
dstr/ary-init-iter-close.js
dstr/ary-init-iter-get-err.js
dstr/ary-init-iter-get-err-array-prototype.js
Expand Down Expand Up @@ -3335,7 +3333,6 @@ language/expressions/arrow-function 254/333 (76.28%)
lexical-super-property.js
lexical-super-property-from-within-constructor.js
lexical-supercall-from-immediately-invoked-arrow.js
name.js
object-destructuring-param-strict-body.js {unsupported: [rest-parameters]}
param-dflt-yield-expr.js {unsupported: [default-parameters]}
param-dflt-yield-id-non-strict.js {unsupported: [default-parameters]}
Expand Down

0 comments on commit a6aa8f8

Please sign in to comment.