| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import static org.junit.Assert.assertEquals; |
| 8 |
import static org.junit.Assert.fail; |
| 9 |
|
| 10 |
import org.junit.After; |
| 11 |
import org.junit.Before; |
| 12 |
import org.junit.Test; |
| 13 |
import org.mozilla.javascript.Context; |
| 14 |
import org.mozilla.javascript.ContinuationPending; |
| 15 |
import org.mozilla.javascript.Function; |
| 16 |
import org.mozilla.javascript.Script; |
| 17 |
import org.mozilla.javascript.Scriptable; |
| 18 |
import org.mozilla.javascript.ScriptableObject; |
| 19 |
|
| 20 |
|
| 21 |
l |
| 22 |
|
| 23 |
|
| 24 |
public class Bug685403Test { |
| 25 |
private Context cx; |
| 26 |
private ScriptableObject scope; |
| 27 |
|
| 28 |
@Before |
| 29 |
public void setUp() { |
| 30 |
cx = Context.enter(); |
| 31 |
cx.setOptimizationLevel(-1); |
| 32 |
scope = cx.initStandardObjects(); |
| 33 |
} |
| 34 |
|
| 35 |
@After |
| 36 |
public void tearDown() { |
| 37 |
Context.exit(); |
| 38 |
} |
| 39 |
|
| 40 |
public static Object continuation(Context cx, Scriptable thisObj, |
| 41 |
Object[] args, Function funObj) { |
| 42 |
ContinuationPending pending = cx.captureContinuation(); |
| 43 |
throw pending; |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void test() { |
| 48 |
String source = "var state = '';"; |
| 49 |
source += "function A(){state += 'A'}"; |
| 50 |
source += "function B(){state += 'B'}"; |
| 51 |
source += "function C(){state += 'C'}"; |
| 52 |
source += "try { A(); continuation(); B() } finally { C() }"; |
| 53 |
source += "state"; |
| 54 |
|
| 55 |
String[] functions = new String[] { "continuation" }; |
| 56 |
scope.defineFunctionProperties(functions, Bug685403Test.class, |
| 57 |
ScriptableObject.DONTENUM); |
| 58 |
|
| 59 |
Object state = null; |
| 60 |
Script script = cx.compileString(source, "", 1, null); |
| 61 |
try { |
| 62 |
cx.executeScriptWithContinuations(script, scope); |
| 63 |
fail("expected ContinuationPending exception"); |
| 64 |
} catch (ContinuationPending pending) { |
| 65 |
state = cx.resumeContinuation(pending.getContinuation(), scope, ""); |
| 66 |
} |
| 67 |
assertEquals("ABC", state); |
| 68 |
} |
| 69 |
|
| 70 |
} |