Skip to content

Commit 6e1adbe

Browse files
aardvark179gbrail
authored andcommitted
Convert NativeContinuation to descriptor.
1 parent 7c45b32 commit 6e1adbe

2 files changed

Lines changed: 42 additions & 68 deletions

File tree

rhino/src/main/java/org/mozilla/javascript/Interpreter.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,8 +3549,9 @@ NewState execute(Context cx, CallFrame frame, InterpreterState state, int op) {
35493549
if (fun instanceof JSFunction
35503550
&& ((JSFunction) fun).getDescriptor().getCode() instanceof InterpreterData) {
35513551
JSFunction ifun = (JSFunction) fun;
3552-
JSDescriptor desc = ifun.getDescriptor();
3553-
InterpreterData idata = (InterpreterData) desc.getCode();
3552+
3553+
JSDescriptor<JSFunction> desc = ifun.getDescriptor();
3554+
var idata = (InterpreterData<JSFunction>) desc.getCode();
35543555
if (frame.fnOrScript.getDescriptor().getSecurityDomain()
35553556
== desc.getSecurityDomain()) {
35563557
CallFrame callParentFrame = frame;
@@ -3620,8 +3621,9 @@ NewState execute(Context cx, CallFrame frame, InterpreterState state, int op) {
36203621
return BREAK_WITHOUT_EXTENSION;
36213622
}
36223623

3623-
if (fun instanceof IdFunctionObject) {
3624-
IdFunctionObject ifun = (IdFunctionObject) fun;
3624+
if (fun instanceof JSFunction) {
3625+
var ifun = (JSFunction) fun;
3626+
36253627
if (NativeContinuation.isContinuationConstructor(ifun)) {
36263628
frame.stack[state.stackTop] = captureContinuation(cx, frame.parentFrame, false);
36273629
return null;
@@ -3666,8 +3668,9 @@ NewState execute(Context cx, CallFrame frame, InterpreterState state, int op) {
36663668
if (lhs instanceof JSFunction
36673669
&& ((JSFunction) lhs).getConstructor() instanceof InterpreterData) {
36683670
JSFunction f = (JSFunction) lhs;
3669-
JSDescriptor desc = f.getDescriptor();
3670-
InterpreterData idata = (InterpreterData) desc.getConstructor();
3671+
3672+
JSDescriptor<JSFunction> desc = f.getDescriptor();
3673+
var idata = (InterpreterData<JSFunction>) desc.getConstructor();
36713674
if (frame.fnOrScript.getDescriptor().getSecurityDomain()
36723675
== desc.getSecurityDomain()) {
36733676
if (cx.getLanguageVersion() >= Context.VERSION_ES6
@@ -3700,20 +3703,21 @@ NewState execute(Context cx, CallFrame frame, InterpreterState state, int op) {
37003703
return new StateContinueResult(calleeFrame, state.indexReg);
37013704
}
37023705
}
3703-
if (!(lhs instanceof Constructable)) {
3704-
if (lhs == DOUBLE_MARK) lhs = ScriptRuntime.wrapNumber(frame.sDbl[state.stackTop]);
3705-
throw ScriptRuntime.notFunctionError(lhs);
3706-
}
3707-
Constructable ctor = (Constructable) lhs;
3706+
if (lhs instanceof JSFunction) {
3707+
var f = (JSFunction) lhs;
37083708

3709-
if (ctor instanceof IdFunctionObject) {
3710-
IdFunctionObject ifun = (IdFunctionObject) ctor;
3711-
if (NativeContinuation.isContinuationConstructor(ifun)) {
3709+
if (NativeContinuation.isContinuationConstructor(f)) {
37123710
frame.stack[state.stackTop] = captureContinuation(cx, frame.parentFrame, false);
37133711
return null;
37143712
}
37153713
}
37163714

3715+
if (!(lhs instanceof Constructable)) {
3716+
if (lhs == DOUBLE_MARK) lhs = ScriptRuntime.wrapNumber(frame.sDbl[state.stackTop]);
3717+
throw ScriptRuntime.notFunctionError(lhs);
3718+
}
3719+
Constructable ctor = (Constructable) lhs;
3720+
37173721
Object[] outArgs =
37183722
getArgsArray(frame.stack, frame.sDbl, state.stackTop + 1, state.indexReg);
37193723
frame.stack[state.stackTop] = ctor.construct(cx, frame.scope, outArgs);

rhino/src/main/java/org/mozilla/javascript/NativeContinuation.java

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,34 @@
99
import java.io.Serial;
1010
import java.util.Objects;
1111

12-
public final class NativeContinuation extends IdScriptableObject implements Function {
12+
public final class NativeContinuation extends ScriptableObject implements Function {
1313
@Serial private static final long serialVersionUID = 1794167133757605367L;
1414

15-
private static final Object FTAG = "Continuation";
15+
private static final String CLASS_NAME = "Continuation";
16+
17+
private static final ClassDescriptor DESCRIPTOR;
18+
private static final JSDescriptor<JSFunction> CTOR_DESCRIPTOR;
19+
20+
static {
21+
DESCRIPTOR =
22+
new ClassDescriptor.Builder(
23+
CLASS_NAME,
24+
0,
25+
NativeContinuation::js_constructor,
26+
NativeContinuation::js_constructor)
27+
.build();
28+
CTOR_DESCRIPTOR = DESCRIPTOR.ctorDesc();
29+
}
1630

1731
private Object implementation;
1832

1933
public static void init(Context cx, VarScope scope, boolean sealed) {
20-
NativeContinuation obj = new NativeContinuation();
21-
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
34+
DESCRIPTOR.buildConstructor(cx, scope, new NativeContinuation(), sealed);
35+
}
36+
37+
private static Object js_constructor(
38+
Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
39+
throw Context.reportRuntimeError("Direct call is not supported");
2240
}
2341

2442
public Object getImplementation() {
@@ -44,11 +62,8 @@ public Object call(Context cx, VarScope scope, Scriptable thisObj, Object[] args
4462
return Interpreter.restartContinuation(this, cx, scope, args);
4563
}
4664

47-
public static boolean isContinuationConstructor(IdFunctionObject f) {
48-
if (f.hasTag(FTAG) && f.methodId() == Id_constructor) {
49-
return true;
50-
}
51-
return false;
65+
public static boolean isContinuationConstructor(JSFunction f) {
66+
return f.getDescriptor() == CTOR_DESCRIPTOR;
5267
}
5368

5469
/**
@@ -62,49 +77,4 @@ public static boolean isContinuationConstructor(IdFunctionObject f) {
6277
public static boolean equalImplementations(NativeContinuation c1, NativeContinuation c2) {
6378
return Objects.equals(c1.implementation, c2.implementation);
6479
}
65-
66-
@Override
67-
protected void initPrototypeId(int id) {
68-
String s;
69-
int arity;
70-
switch (id) {
71-
case Id_constructor:
72-
arity = 0;
73-
s = "constructor";
74-
break;
75-
default:
76-
throw new IllegalArgumentException(String.valueOf(id));
77-
}
78-
initPrototypeMethod(FTAG, id, s, arity);
79-
}
80-
81-
@Override
82-
public Object execIdCall(
83-
IdFunctionObject f, Context cx, VarScope scope, Scriptable thisObj, Object[] args) {
84-
if (!f.hasTag(FTAG)) {
85-
return super.execIdCall(f, cx, scope, thisObj, args);
86-
}
87-
int id = f.methodId();
88-
switch (id) {
89-
case Id_constructor:
90-
throw Context.reportRuntimeError("Direct call is not supported");
91-
}
92-
throw new IllegalArgumentException(String.valueOf(id));
93-
}
94-
95-
@Override
96-
protected int findPrototypeId(String s) {
97-
int id;
98-
switch (s) {
99-
case "constructor":
100-
id = Id_constructor;
101-
break;
102-
default:
103-
id = 0;
104-
break;
105-
}
106-
return id;
107-
}
108-
109-
private static final int Id_constructor = 1, MAX_PROTOTYPE_ID = 1;
11080
}

0 commit comments

Comments
 (0)