Skip to content

Commit 63dc6b0

Browse files
andrea.bergiagbrail
authored andcommitted
Migrated ES6Generator away from IdScriptableObject
1 parent ad0dd02 commit 63dc6b0

File tree

1 file changed

+51
-103
lines changed

1 file changed

+51
-103
lines changed

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

Lines changed: 51 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,40 @@
66

77
package org.mozilla.javascript;
88

9-
import java.util.Objects;
10-
11-
public final class ES6Generator extends IdScriptableObject {
9+
public final class ES6Generator extends ScriptableObject {
1210
private static final long serialVersionUID = 1645892441041347273L;
1311

1412
static final Object GENERATOR_TAG = "Generator";
1513

14+
private JSFunction function;
15+
private Object savedState;
16+
private String lineSource;
17+
private int lineNumber;
18+
private State state = State.SUSPENDED_START;
19+
private Object delegee;
20+
1621
static ES6Generator init(ScriptableObject scope, boolean sealed) {
1722

1823
ES6Generator prototype = new ES6Generator();
1924
if (scope != null) {
2025
prototype.setParentScope(scope);
2126
prototype.setPrototype(getObjectPrototype(scope));
2227
}
23-
prototype.activatePrototypeMap(MAX_PROTOTYPE_ID);
28+
29+
// Define prototype methods using LambdaFunction
30+
LambdaFunction next = new LambdaFunction(scope, "next", 1, ES6Generator::js_next);
31+
ScriptableObject.defineProperty(prototype, "next", next, DONTENUM);
32+
33+
LambdaFunction returnFunc = new LambdaFunction(scope, "return", 1, ES6Generator::js_return);
34+
ScriptableObject.defineProperty(prototype, "return", returnFunc, DONTENUM);
35+
36+
LambdaFunction throwFunc = new LambdaFunction(scope, "throw", 1, ES6Generator::js_throw);
37+
ScriptableObject.defineProperty(prototype, "throw", throwFunc, DONTENUM);
38+
39+
LambdaFunction iterator =
40+
new LambdaFunction(scope, "[Symbol.iterator]", 0, ES6Generator::js_iterator);
41+
prototype.defineProperty(SymbolKey.ITERATOR, iterator, DONTENUM);
42+
2443
if (sealed) {
2544
prototype.sealObject();
2645
}
@@ -57,68 +76,42 @@ public String getClassName() {
5776
return "Generator";
5877
}
5978

60-
@Override
61-
protected void initPrototypeId(int id) {
62-
if (id == SymbolId_iterator) {
63-
initPrototypeMethod(GENERATOR_TAG, id, SymbolKey.ITERATOR, "[Symbol.iterator]", 0);
64-
return;
65-
}
79+
private static ES6Generator realThis(Scriptable thisObj) {
80+
return LambdaConstructor.convertThisObject(thisObj, ES6Generator.class);
81+
}
6682

67-
String s;
68-
int arity;
69-
switch (id) {
70-
case Id_next:
71-
arity = 1;
72-
s = "next";
73-
break;
74-
case Id_return:
75-
arity = 1;
76-
s = "return";
77-
break;
78-
case Id_throw:
79-
arity = 1;
80-
s = "throw";
81-
break;
82-
default:
83-
throw new IllegalArgumentException(String.valueOf(id));
83+
private static Object js_return(
84+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
85+
ES6Generator generator = realThis(thisObj);
86+
Object value = args.length >= 1 ? args[0] : Undefined.instance;
87+
if (generator.delegee == null) {
88+
return generator.resumeAbruptLocal(cx, scope, NativeGenerator.GENERATOR_CLOSE, value);
8489
}
85-
initPrototypeMethod(GENERATOR_TAG, id, s, arity);
90+
return generator.resumeDelegeeReturn(cx, scope, value);
8691
}
8792

88-
@Override
89-
public Object execIdCall(
90-
IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
91-
if (!f.hasTag(GENERATOR_TAG)) {
92-
return super.execIdCall(f, cx, scope, thisObj, args);
93+
private static Object js_next(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
94+
ES6Generator generator = realThis(thisObj);
95+
Object value = args.length >= 1 ? args[0] : Undefined.instance;
96+
if (generator.delegee == null) {
97+
return generator.resumeLocal(cx, scope, value);
9398
}
94-
int id = f.methodId();
99+
return generator.resumeDelegee(cx, scope, value);
100+
}
95101

96-
ES6Generator generator = ensureType(thisObj, ES6Generator.class, f);
102+
private static Object js_throw(
103+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
104+
ES6Generator generator = realThis(thisObj);
97105
Object value = args.length >= 1 ? args[0] : Undefined.instance;
98-
99-
switch (id) {
100-
case Id_return:
101-
if (generator.delegee == null) {
102-
return generator.resumeAbruptLocal(
103-
cx, scope, NativeGenerator.GENERATOR_CLOSE, value);
104-
}
105-
return generator.resumeDelegeeReturn(cx, scope, value);
106-
case Id_next:
107-
if (generator.delegee == null) {
108-
return generator.resumeLocal(cx, scope, value);
109-
}
110-
return generator.resumeDelegee(cx, scope, value);
111-
case Id_throw:
112-
if (generator.delegee == null) {
113-
return generator.resumeAbruptLocal(
114-
cx, scope, NativeGenerator.GENERATOR_THROW, value);
115-
}
116-
return generator.resumeDelegeeThrow(cx, scope, value);
117-
case SymbolId_iterator:
118-
return thisObj;
119-
default:
120-
throw new IllegalArgumentException(String.valueOf(id));
106+
if (generator.delegee == null) {
107+
return generator.resumeAbruptLocal(cx, scope, NativeGenerator.GENERATOR_THROW, value);
121108
}
109+
return generator.resumeDelegeeThrow(cx, scope, value);
110+
}
111+
112+
private static Object js_iterator(
113+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
114+
return thisObj;
122115
}
123116

124117
private Scriptable resumeDelegee(Context cx, Scriptable scope, Object value) {
@@ -397,51 +390,6 @@ private Object callReturnOptionally(Context cx, Scriptable scope, Object value)
397390
return null;
398391
}
399392

400-
@Override
401-
protected int findPrototypeId(Symbol k) {
402-
if (SymbolKey.ITERATOR.equals(k)) {
403-
return SymbolId_iterator;
404-
}
405-
return 0;
406-
}
407-
408-
@Override
409-
protected int findPrototypeId(String s) {
410-
int id;
411-
L0:
412-
{
413-
id = 0;
414-
String X = null;
415-
int s_length = s.length();
416-
if (s_length == 4) {
417-
X = "next";
418-
id = Id_next;
419-
} else if (s_length == 5) {
420-
X = "throw";
421-
id = Id_throw;
422-
} else if (s_length == 6) {
423-
X = "return";
424-
id = Id_return;
425-
}
426-
if (!Objects.equals(X, s)) id = 0;
427-
break L0;
428-
}
429-
return id;
430-
}
431-
432-
private static final int Id_next = 1,
433-
Id_return = 2,
434-
Id_throw = 3,
435-
SymbolId_iterator = 4,
436-
MAX_PROTOTYPE_ID = SymbolId_iterator;
437-
438-
private JSFunction function;
439-
private Object savedState;
440-
private String lineSource;
441-
private int lineNumber;
442-
private State state = State.SUSPENDED_START;
443-
private Object delegee;
444-
445393
enum State {
446394
SUSPENDED_START,
447395
SUSPENDED_YIELD,

0 commit comments

Comments
 (0)