|
6 | 6 |
|
7 | 7 | package org.mozilla.javascript; |
8 | 8 |
|
9 | | -import java.util.Objects; |
10 | | - |
11 | | -public final class ES6Generator extends IdScriptableObject { |
| 9 | +public final class ES6Generator extends ScriptableObject { |
12 | 10 | private static final long serialVersionUID = 1645892441041347273L; |
13 | 11 |
|
14 | 12 | static final Object GENERATOR_TAG = "Generator"; |
15 | 13 |
|
| 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 | + |
16 | 21 | static ES6Generator init(ScriptableObject scope, boolean sealed) { |
17 | 22 |
|
18 | 23 | ES6Generator prototype = new ES6Generator(); |
19 | 24 | if (scope != null) { |
20 | 25 | prototype.setParentScope(scope); |
21 | 26 | prototype.setPrototype(getObjectPrototype(scope)); |
22 | 27 | } |
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 | + |
24 | 43 | if (sealed) { |
25 | 44 | prototype.sealObject(); |
26 | 45 | } |
@@ -57,68 +76,42 @@ public String getClassName() { |
57 | 76 | return "Generator"; |
58 | 77 | } |
59 | 78 |
|
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 | + } |
66 | 82 |
|
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); |
84 | 89 | } |
85 | | - initPrototypeMethod(GENERATOR_TAG, id, s, arity); |
| 90 | + return generator.resumeDelegeeReturn(cx, scope, value); |
86 | 91 | } |
87 | 92 |
|
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); |
93 | 98 | } |
94 | | - int id = f.methodId(); |
| 99 | + return generator.resumeDelegee(cx, scope, value); |
| 100 | + } |
95 | 101 |
|
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); |
97 | 105 | 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); |
121 | 108 | } |
| 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; |
122 | 115 | } |
123 | 116 |
|
124 | 117 | private Scriptable resumeDelegee(Context cx, Scriptable scope, Object value) { |
@@ -397,51 +390,6 @@ private Object callReturnOptionally(Context cx, Scriptable scope, Object value) |
397 | 390 | return null; |
398 | 391 | } |
399 | 392 |
|
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 | | - |
445 | 393 | enum State { |
446 | 394 | SUSPENDED_START, |
447 | 395 | SUSPENDED_YIELD, |
|
0 commit comments