Skip to content

Commit

Permalink
A more flexible version of the FunctionObject.addAsConstructor(Script…
Browse files Browse the repository at this point in the history
…able, Scriptable) method required at least for HtmlUnit to have better control of the attributes.

Looks like the right attributes today is only ScriptableObject.DONTENUM.
  • Loading branch information
rbri committed Dec 2, 2022
1 parent da7ddaa commit 0131b5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
32 changes: 25 additions & 7 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) {
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
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

0 comments on commit 0131b5c

Please sign in to comment.