| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
public abstract class ES6Iterator extends IdScriptableObject { |
| 10 |
|
| 11 |
static void init(ScriptableObject scope, boolean sealed, IdScriptableObject prototype, String tag) { |
| 12 |
if (scope != null) { |
| 13 |
prototype.setParentScope(scope); |
| 14 |
prototype.setPrototype(getObjectPrototype(scope)); |
| 15 |
} |
| 16 |
prototype.activatePrototypeMap(MAX_PROTOTYPE_ID); |
| 17 |
if (sealed) { |
| 18 |
prototype.sealObject(); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
if (scope != null) { |
| 26 |
scope.associateValue(tag, prototype); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
protected boolean exhausted = false; |
| 31 |
|
| 32 |
ES6Iterator() {} |
| 33 |
|
| 34 |
ES6Iterator(Scriptable scope) { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
Scriptable top = ScriptableObject.getTopLevelScope(scope); |
| 39 |
this.setParentScope(top); |
| 40 |
IdScriptableObject prototype = (IdScriptableObject) |
| 41 |
ScriptableObject.getTopScopeValue(top, getTag()); |
| 42 |
setPrototype(prototype); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
protected void initPrototypeId(int id) |
| 47 |
{ |
| 48 |
switch (id) { |
| 49 |
case Id_next: |
| 50 |
initPrototypeMethod(getTag(), id, NEXT_METHOD, 0); |
| 51 |
return; |
| 52 |
case Id_iterator: |
| 53 |
initPrototypeMethod(getTag(), id, SymbolKey.ITERATOR, "[Symbol.iterator]", 0); |
| 54 |
return; |
| 55 |
case Id_toStringTag: |
| 56 |
initPrototypeValue(Id_toStringTag, SymbolKey.TO_STRING_TAG, getClassName(), DONTENUM | READONLY); |
| 57 |
return; |
| 58 |
default: throw new IllegalArgumentException(String.valueOf(id)); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 64 |
Scriptable thisObj, Object[] args) |
| 65 |
{ |
| 66 |
if (!f.hasTag(getTag())) { |
| 67 |
return super.execIdCall(f, cx, scope, thisObj, args); |
| 68 |
} |
| 69 |
int id = f.methodId(); |
| 70 |
|
| 71 |
if (!(thisObj instanceof ES6Iterator)) |
| 72 |
throw incompatibleCallError(f); |
| 73 |
|
| 74 |
ES6Iterator iterator = (ES6Iterator) thisObj; |
| 75 |
|
| 76 |
switch (id) { |
| 77 |
case Id_next: |
| 78 |
return iterator.next(cx, scope); |
| 79 |
case Id_iterator: |
| 80 |
return iterator; |
| 81 |
default: |
| 82 |
throw new IllegalArgumentException(String.valueOf(id)); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
protected int findPrototypeId(Symbol k) { |
| 88 |
if (SymbolKey.ITERATOR.equals(k)) { |
| 89 |
return Id_iterator; |
| 90 |
} else if (SymbolKey.TO_STRING_TAG.equals(k)) { |
| 91 |
return Id_toStringTag; |
| 92 |
} |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
protected int findPrototypeId(String s) { |
| 98 |
if ("next".equals(s)) { |
| 99 |
return Id_next; |
| 100 |
} |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
abstract protected boolean isDone(Context cx, Scriptable scope); |
| 105 |
|
| 106 |
abstract protected Object nextValue(Context cx, Scriptable scope); |
| 107 |
|
| 108 |
protected Object next(Context cx, Scriptable scope) { |
| 109 |
Object value = Undefined.instance; |
| 110 |
boolean done = isDone(cx, scope) || this.exhausted; |
| 111 |
if (!done) { |
| 112 |
value = nextValue(cx, scope); |
| 113 |
} else { |
| 114 |
this.exhausted = true; |
| 115 |
} |
| 116 |
return makeIteratorResult(cx, scope, done, value); |
| 117 |
} |
| 118 |
|
| 119 |
abstract protected String getTag(); |
| 120 |
|
| 121 |
|
| 122 |
private Scriptable makeIteratorResult(Context cx, Scriptable scope, boolean done, Object value) { |
| 123 |
Scriptable iteratorResult = cx.newObject(scope); |
| 124 |
ScriptableObject.putProperty(iteratorResult, VALUE_PROPERTY, value); |
| 125 |
ScriptableObject.putProperty(iteratorResult, DONE_PROPERTY, done); |
| 126 |
return iteratorResult; |
| 127 |
} |
| 128 |
|
| 129 |
private static final int |
| 130 |
Id_next = 1, |
| 131 |
Id_iterator = 2, |
| 132 |
Id_toStringTag = 3, |
| 133 |
MAX_PROTOTYPE_ID = Id_toStringTag; |
| 134 |
|
| 135 |
public static final String NEXT_METHOD = "next"; |
| 136 |
public static final String DONE_PROPERTY = "done"; |
| 137 |
public static final String VALUE_PROPERTY = "value"; |
| 138 |
} |