Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup as follow up of pr 1284 #1285

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/org/mozilla/javascript/FunctionObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,39 @@ static Method[] getMethodList(Class<?> clazz) {
* @see org.mozilla.javascript.Scriptable#getClassName
*/
public void addAsConstructor(Scriptable scope, Scriptable prototype) {
initAsConstructor(scope, prototype);
initAsConstructor(
scope,
prototype,
ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY);
defineProperty(scope, prototype.getClassName(), this, ScriptableObject.DONTENUM);
}

/**
* Define this function as a JavaScript constructor.
*
* <p>Sets up the "prototype" and "constructor" properties. Also calls setParent and
* setPrototype with appropriate values. Then adds the function object as a property of the
* given scope, using <code>prototype.getClassName()</code> as the name of the property.
*
* @param scope the scope in which to define the constructor (typically the global object)
* @param prototype the prototype object
* @param attributes the attributes of the constructor property
* @see org.mozilla.javascript.Scriptable#setParentScope
* @see org.mozilla.javascript.Scriptable#setPrototype
* @see org.mozilla.javascript.Scriptable#getClassName
*/
public void addAsConstructor(Scriptable scope, Scriptable prototype, int attributes) {
initAsConstructor(scope, prototype, attributes);
defineProperty(scope, prototype.getClassName(), this, ScriptableObject.DONTENUM);
}

void initAsConstructor(Scriptable scope, Scriptable prototype) {
ScriptRuntime.setFunctionProtoAndParent(this, scope);
void initAsConstructor(Scriptable scope, Scriptable prototype, int attributes) {
ScriptRuntime.setFunctionProtoAndParent(this, Context.getCurrentContext(), scope);
setImmunePrototypeProperty(prototype);

prototype.setParentScope(this);

defineProperty(
prototype,
"constructor",
this,
ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY);
defineProperty(prototype, "constructor", this, attributes);
setParentScope(scope);
}

Expand Down
4 changes: 2 additions & 2 deletions src/org/mozilla/javascript/LambdaFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public LambdaFunction(Scriptable scope, String name, int length, Callable target
this.target = target;
this.name = name;
this.length = length;
ScriptRuntime.setFunctionProtoAndParent(this, scope);
ScriptRuntime.setFunctionProtoAndParent(this, Context.getCurrentContext(), scope);
setupDefaultPrototype();
}

Expand All @@ -43,7 +43,7 @@ public LambdaFunction(Scriptable scope, int length, Callable target) {
this.target = target;
this.length = length;
this.name = "";
ScriptRuntime.setFunctionProtoAndParent(this, scope);
ScriptRuntime.setFunctionProtoAndParent(this, Context.getCurrentContext(), scope);
}

@Override
Expand Down
5 changes: 0 additions & 5 deletions src/org/mozilla/javascript/NativePromise.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public static void init(Context cx, Scriptable scope, boolean sealed) {
1,
LambdaConstructor.CONSTRUCTOR_NEW,
NativePromise::constructor);
constructor.setStandardPropertyAttributes(DONTENUM | READONLY);
constructor.setPrototypePropertyAttributes(DONTENUM | READONLY | PERMANENT);

constructor.defineConstructorMethod(
Expand Down Expand Up @@ -541,7 +540,6 @@ private static class ResolvingFunctions {
scope,
promise,
(args.length > 0 ? args[0] : Undefined.instance)));
resolve.setStandardPropertyAttributes(DONTENUM | READONLY);
reject =
new LambdaFunction(
topScope,
Expand All @@ -552,7 +550,6 @@ private static class ResolvingFunctions {
scope,
promise,
(args.length > 0 ? args[0] : Undefined.instance)));
reject.setStandardPropertyAttributes(DONTENUM | READONLY);
}

private Object reject(Context cx, Scriptable scope, NativePromise promise, Object reason) {
Expand Down Expand Up @@ -664,7 +661,6 @@ private static class Capability {
2,
(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) ->
executor(args));
executorFunc.setStandardPropertyAttributes(DONTENUM | READONLY);

promise = promiseConstructor.construct(topCx, topScope, new Object[] {executorFunc});

Expand Down Expand Up @@ -777,7 +773,6 @@ Object resolve(Context topCx, Scriptable topScope) {
}
return eltResolver.resolve(cx, scope, value, this);
});
resolveFunc.setStandardPropertyAttributes(DONTENUM | READONLY);

Callable rejectFunc = capability.reject;
if (!failFast) {
Expand Down
25 changes: 19 additions & 6 deletions src/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4249,23 +4249,36 @@ public static Scriptable leaveDotQuery(Scriptable scope) {
return nw.getParentScope();
}

/**
* @deprecated Use {@link #setFunctionProtoAndParent(BaseFunction, Context, Scriptable)} instead
*/
@Deprecated
public static void setFunctionProtoAndParent(BaseFunction fn, Scriptable scope) {
setFunctionProtoAndParent(fn, scope, false);
setFunctionProtoAndParent(fn, Context.getCurrentContext(), scope, false);
}

public static void setFunctionProtoAndParent(BaseFunction fn, Context cx, Scriptable scope) {
setFunctionProtoAndParent(fn, cx, scope, false);
}

/**
* @deprecated Use {@link #setFunctionProtoAndParent(BaseFunction, Context, Scriptable,
* boolean)} instead
*/
@Deprecated
public static void setFunctionProtoAndParent(
BaseFunction fn, Scriptable scope, boolean es6GeneratorFunction) {
setFunctionProtoAndParent(fn, Context.getCurrentContext(), scope, es6GeneratorFunction);
}

public static void setFunctionProtoAndParent(
BaseFunction fn, Context cx, Scriptable scope, boolean es6GeneratorFunction) {
fn.setParentScope(scope);
if (es6GeneratorFunction) {
fn.setPrototype(ScriptableObject.getGeneratorFunctionPrototype(scope));
} else {
fn.setPrototype(ScriptableObject.getFunctionPrototype(scope));
}
}

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

if (cx != null && cx.getLanguageVersion() >= Context.VERSION_ES6) {
fn.setStandardPropertyAttributes(ScriptableObject.READONLY | ScriptableObject.DONTENUM);
Expand Down
5 changes: 4 additions & 1 deletion src/org/mozilla/javascript/ScriptableObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,10 @@ static <T extends Scriptable> BaseFunction buildClassCtor(
if (ctor.isVarArgsMethod()) {
throw Context.reportRuntimeErrorById("msg.varargs.ctor", ctorMember.getName());
}
ctor.initAsConstructor(scope, proto);
ctor.initAsConstructor(
scope,
proto,
ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY);

Method finishInit = null;
HashSet<String> staticNames = new HashSet<String>(), instanceNames = new HashSet<String>();
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/regexp/NativeRegExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static void init(Context cx, Scriptable scope, boolean sealed) {
// RegExp.prototype.constructor is the builtin RegExp constructor."
proto.defineProperty("constructor", ctor, ScriptableObject.DONTENUM);

ScriptRuntime.setFunctionProtoAndParent(ctor, scope);
ScriptRuntime.setFunctionProtoAndParent(ctor, cx, scope);

ctor.setImmunePrototypeProperty(proto);

Expand Down
3 changes: 1 addition & 2 deletions testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3542,8 +3542,7 @@ language/expressions/conditional 3/22 (13.64%)
tco-cond.js {unsupported: [tail-call-optimization]}
tco-pos.js {unsupported: [tail-call-optimization]}

language/expressions/delete 4/61 (6.56%)
11.4.1-5-a-28-s.js strict
language/expressions/delete 3/61 (4.92%)
identifier-strict.js strict
super-property.js {unsupported: [class]}
super-property-method.js {unsupported: [class]}
Expand Down