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

Use the Constructable interface instead of Function if appropriate #1635

Merged
merged 1 commit into from
Sep 23, 2024
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
5 changes: 3 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/BoundFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ex

@Override
public Scriptable construct(Context cx, Scriptable scope, Object[] extraArgs) {
if (targetFunction instanceof Function) {
return ((Function) targetFunction).construct(cx, scope, concat(boundArgs, extraArgs));
if (targetFunction instanceof Constructable) {
return ((Constructable) targetFunction)
.construct(cx, scope, concat(boundArgs, extraArgs));
}
throw ScriptRuntime.typeErrorById("msg.not.ctor");
}
Expand Down
4 changes: 2 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/Delegator.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
* closure.
* @param args the array of arguments
* @return the allocated object
* @see Function#construct(Context, Scriptable, Object[])
* @see Constructable#construct(Context, Scriptable, Object[])
*/
@Override
public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
Expand All @@ -250,6 +250,6 @@ public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
n.setDelegee(delegee);
return n;
}
return ((Function) myDelegee).construct(cx, scope, args);
return ((Constructable) myDelegee).construct(cx, scope, args);
}
}
8 changes: 4 additions & 4 deletions rhino/src/main/java/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2035,10 +2035,10 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
lhs = ScriptRuntime.wrapNumber(sDbl[stackTop]);
throw ScriptRuntime.notFunctionError(lhs);
}
Function fun = (Function) lhs;
Constructable ctor = (Constructable) lhs;

if (fun instanceof IdFunctionObject) {
IdFunctionObject ifun = (IdFunctionObject) fun;
if (ctor instanceof IdFunctionObject) {
IdFunctionObject ifun = (IdFunctionObject) ctor;
if (NativeContinuation.isContinuationConstructor(ifun)) {
frame.stack[stackTop] =
captureContinuation(
Expand All @@ -2049,7 +2049,7 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl

Object[] outArgs =
getArgsArray(stack, sDbl, stackTop + 1, indexReg);
stack[stackTop] = fun.construct(cx, frame.scope, outArgs);
stack[stackTop] = ctor.construct(cx, frame.scope, outArgs);
continue Loop;
}
case Token.TYPEOF:
Expand Down
9 changes: 4 additions & 5 deletions rhino/src/main/java/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -905,13 +905,13 @@ private static Scriptable callConstructorOrCreateArray(
Context cx, Scriptable scope, Scriptable arg, long length, boolean lengthAlways) {
Scriptable result = null;

if (arg instanceof Function) {
if (arg instanceof Constructable) {
try {
final Object[] args =
(lengthAlways || (length > 0))
? new Object[] {Long.valueOf(length)}
: ScriptRuntime.emptyArgs;
result = ((Function) arg).construct(cx, scope, args);
result = ((Constructable) arg).construct(cx, scope, args);
} catch (EcmaError ee) {
if (!"TypeError".equals(ee.getName())) {
throw ee;
Expand Down Expand Up @@ -1717,9 +1717,8 @@ private static boolean isConcatSpreadable(Context cx, Scriptable scope, Object v

if (cx.getLanguageVersion() < Context.VERSION_ES6) {
// Otherwise, for older Rhino versions, fall back to the old algorithm, which treats
// things with
// the Array constructor as arrays. However, this is contrary to ES6!
final Function ctor = ScriptRuntime.getExistingCtor(cx, scope, "Array");
// things with the Array constructor as arrays. However, this is contrary to ES6!
final Constructable ctor = ScriptRuntime.getExistingCtor(cx, scope, "Array");
if (ScriptRuntime.instanceOf(val, ctor, cx)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
// implements/extends this interface/abstract class.
Object v = topLevel.get("JavaAdapter", topLevel);
if (v != NOT_FOUND) {
Function f = (Function) v;
// Args are (interface, js object)
Object[] adapterArgs = {this, args[0]};
return f.construct(cx, topLevel, adapterArgs);
return ((Constructable) v).construct(cx, topLevel, adapterArgs);
}
} catch (Exception ex) {
// fall through to error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,14 @@ private static class Capability {
if (!(pc instanceof Constructable)) {
throw ScriptRuntime.typeErrorById("msg.constructor.expected");
}
Constructable promiseConstructor = (Constructable) pc;
LambdaFunction executorFunc =
new LambdaFunction(
topScope,
2,
(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) ->
executor(args));

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

if (!(rawResolve instanceof Callable)) {
throw ScriptRuntime.typeErrorById("msg.function.expected");
Expand Down
11 changes: 5 additions & 6 deletions rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ public static Object call(
public static Scriptable newObject(
Context cx, Scriptable scope, String constructorName, Object[] args) {
scope = ScriptableObject.getTopLevelScope(scope);
Function ctor = getExistingCtor(cx, scope, constructorName);
Constructable ctor = getExistingCtor(cx, scope, constructorName);
if (args == null) {
args = ScriptRuntime.emptyArgs;
}
Expand All @@ -1332,7 +1332,7 @@ public static Scriptable newObject(
public static Scriptable newBuiltinObject(
Context cx, Scriptable scope, TopLevel.Builtins type, Object[] args) {
scope = ScriptableObject.getTopLevelScope(scope);
Function ctor = TopLevel.getBuiltinCtor(cx, scope, type);
Constructable ctor = TopLevel.getBuiltinCtor(cx, scope, type);
if (args == null) {
args = ScriptRuntime.emptyArgs;
}
Expand All @@ -1342,7 +1342,7 @@ public static Scriptable newBuiltinObject(
static Scriptable newNativeError(
Context cx, Scriptable scope, TopLevel.NativeErrors type, Object[] args) {
scope = ScriptableObject.getTopLevelScope(scope);
Function ctor = TopLevel.getNativeErrorCtor(cx, scope, type);
Constructable ctor = TopLevel.getNativeErrorCtor(cx, scope, type);
if (args == null) {
args = ScriptRuntime.emptyArgs;
}
Expand Down Expand Up @@ -2748,11 +2748,10 @@ public static Ref callRef(Callable function, Scriptable thisObj, Object[] args,
* <p>See ECMA 11.2.2
*/
public static Scriptable newObject(Object fun, Context cx, Scriptable scope, Object[] args) {
if (!(fun instanceof Function)) {
if (!(fun instanceof Constructable)) {
throw notFunctionError(fun);
}
Function function = (Function) fun;
return function.construct(cx, scope, args);
return ((Constructable) fun).construct(cx, scope, args);
}

public static Object callSpecial(
Expand Down
Loading