| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public final class NativeGenerator extends IdScriptableObject { |
| 16 |
private static final long serialVersionUID = 1645892441041347273L; |
| 17 |
|
| 18 |
private static final Object GENERATOR_TAG = "Generator"; |
| 19 |
|
| 20 |
static NativeGenerator init(ScriptableObject scope, boolean sealed) { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
NativeGenerator prototype = new NativeGenerator(); |
| 26 |
if (scope != null) { |
| 27 |
prototype.setParentScope(scope); |
| 28 |
prototype.setPrototype(getObjectPrototype(scope)); |
| 29 |
} |
| 30 |
prototype.activatePrototypeMap(MAX_PROTOTYPE_ID); |
| 31 |
if (sealed) { |
| 32 |
prototype.sealObject(); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
if (scope != null) { |
| 40 |
scope.associateValue(GENERATOR_TAG, prototype); |
| 41 |
} |
| 42 |
|
| 43 |
return prototype; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private NativeGenerator() { } |
| 50 |
|
| 51 |
public NativeGenerator(Scriptable scope, NativeFunction function, |
| 52 |
Object savedState) |
| 53 |
{ |
| 54 |
this.function = function; |
| 55 |
this.savedState = savedState; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
Scriptable top = ScriptableObject.getTopLevelScope(scope); |
| 60 |
this.setParentScope(top); |
| 61 |
NativeGenerator prototype = (NativeGenerator) |
| 62 |
ScriptableObject.getTopScopeValue(top, GENERATOR_TAG); |
| 63 |
this.setPrototype(prototype); |
| 64 |
} |
| 65 |
|
| 66 |
public static final int GENERATOR_SEND = 0, |
| 67 |
GENERATOR_THROW = 1, |
| 68 |
GENERATOR_CLOSE = 2; |
| 69 |
|
| 70 |
@Override |
| 71 |
public String getClassName() { |
| 72 |
return "Generator"; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
protected void initPrototypeId(int id) { |
| 77 |
String s; |
| 78 |
int arity; |
| 79 |
switch (id) { |
| 80 |
case Id_close: arity=1; s="close"; break; |
| 81 |
case Id_next: arity=1; s="next"; break; |
| 82 |
case Id_send: arity=0; s="send"; break; |
| 83 |
case Id_throw: arity=0; s="throw"; break; |
| 84 |
case Id___iterator__: arity=1; s="__iterator__"; break; |
| 85 |
default: throw new IllegalArgumentException(String.valueOf(id)); |
| 86 |
} |
| 87 |
initPrototypeMethod(GENERATOR_TAG, id, s, arity); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 92 |
Scriptable thisObj, Object[] args) |
| 93 |
{ |
| 94 |
if (!f.hasTag(GENERATOR_TAG)) { |
| 95 |
return super.execIdCall(f, cx, scope, thisObj, args); |
| 96 |
} |
| 97 |
int id = f.methodId(); |
| 98 |
|
| 99 |
if (!(thisObj instanceof NativeGenerator)) |
| 100 |
throw incompatibleCallError(f); |
| 101 |
|
| 102 |
NativeGenerator generator = (NativeGenerator) thisObj; |
| 103 |
|
| 104 |
switch (id) { |
| 105 |
|
| 106 |
case Id_close: |
| 107 |
|
| 108 |
return generator.resume(cx, scope, GENERATOR_CLOSE, |
| 109 |
new GeneratorClosedException()); |
| 110 |
|
| 111 |
case Id_next: |
| 112 |
|
| 113 |
generator.firstTime = false; |
| 114 |
return generator.resume(cx, scope, GENERATOR_SEND, |
| 115 |
Undefined.instance); |
| 116 |
|
| 117 |
case Id_send: { |
| 118 |
Object arg = args.length > 0 ? args[0] : Undefined.instance; |
| 119 |
if (generator.firstTime && !arg.equals(Undefined.instance)) { |
| 120 |
throw ScriptRuntime.typeError0("msg.send.newborn"); |
| 121 |
} |
| 122 |
return generator.resume(cx, scope, GENERATOR_SEND, arg); |
| 123 |
} |
| 124 |
|
| 125 |
case Id_throw: |
| 126 |
return generator.resume(cx, scope, GENERATOR_THROW, |
| 127 |
args.length > 0 ? args[0] : Undefined.instance); |
| 128 |
|
| 129 |
case Id___iterator__: |
| 130 |
return thisObj; |
| 131 |
|
| 132 |
default: |
| 133 |
throw new IllegalArgumentException(String.valueOf(id)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private Object resume(Context cx, Scriptable scope, int operation, |
| 138 |
Object value) |
| 139 |
{ |
| 140 |
if (savedState == null) { |
| 141 |
if (operation == GENERATOR_CLOSE) |
| 142 |
return Undefined.instance; |
| 143 |
Object thrown; |
| 144 |
if (operation == GENERATOR_THROW) { |
| 145 |
thrown = value; |
| 146 |
} else { |
| 147 |
thrown = NativeIterator.getStopIterationObject(scope); |
| 148 |
} |
| 149 |
throw new JavaScriptException(thrown, lineSource, lineNumber); |
| 150 |
} |
| 151 |
try { |
| 152 |
synchronized (this) { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if (locked) |
| 157 |
throw ScriptRuntime.typeError0("msg.already.exec.gen"); |
| 158 |
locked = true; |
| 159 |
} |
| 160 |
return function.resumeGenerator(cx, scope, operation, savedState, |
| 161 |
value); |
| 162 |
} catch (GeneratorClosedException e) { |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
return Undefined.instance; |
| 167 |
} catch (RhinoException e) { |
| 168 |
lineNumber = e.lineNumber(); |
| 169 |
lineSource = e.lineSource(); |
| 170 |
savedState = null; |
| 171 |
throw e; |
| 172 |
} finally { |
| 173 |
synchronized (this) { |
| 174 |
locked = false; |
| 175 |
} |
| 176 |
if (operation == GENERATOR_CLOSE) |
| 177 |
savedState = null; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
@Override |
| 184 |
protected int findPrototypeId(String s) { |
| 185 |
int id; |
| 186 |
|
| 187 |
L0: { id = 0; String X = null; int c; |
| 188 |
int s_length = s.length(); |
| 189 |
if (s_length==4) { |
| 190 |
c=s.charAt(0); |
| 191 |
if (c=='n') { X="next";id=Id_next; } |
| 192 |
else if (c=='s') { X="send";id=Id_send; } |
| 193 |
} |
| 194 |
else if (s_length==5) { |
| 195 |
c=s.charAt(0); |
| 196 |
if (c=='c') { X="close";id=Id_close; } |
| 197 |
else if (c=='t') { X="throw";id=Id_throw; } |
| 198 |
} |
| 199 |
else if (s_length==12) { X="__iterator__";id=Id___iterator__; } |
| 200 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 201 |
break L0; |
| 202 |
} |
| 203 |
|
| 204 |
return id; |
| 205 |
} |
| 206 |
|
| 207 |
private static final int |
| 208 |
Id_close = 1, |
| 209 |
Id_next = 2, |
| 210 |
Id_send = 3, |
| 211 |
Id_throw = 4, |
| 212 |
Id___iterator__ = 5, |
| 213 |
MAX_PROTOTYPE_ID = 5; |
| 214 |
|
| 215 |
|
| 216 |
private NativeFunction function; |
| 217 |
private Object savedState; |
| 218 |
private String lineSource; |
| 219 |
private int lineNumber; |
| 220 |
private boolean firstTime = true; |
| 221 |
private boolean locked; |
| 222 |
|
| 223 |
public static class GeneratorClosedException extends RuntimeException { |
| 224 |
private static final long serialVersionUID = 2561315658662379681L; |
| 225 |
} |
| 226 |
} |