-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
tests/src/test/java/org/mozilla/javascript/tests/InterpreterFunctionPeelingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.mozilla.javascript.tests; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.ContinuationPending; | ||
import org.mozilla.javascript.Script; | ||
import org.mozilla.javascript.Scriptable; | ||
|
||
// Tests that continuations work across arrow function, bound function, and apply/call invocations. | ||
public class InterpreterFunctionPeelingTest { | ||
public static final Runnable CAPTURER = | ||
() -> { | ||
try (var cx = Context.enter()) { | ||
throw cx.captureContinuation(); | ||
} | ||
}; | ||
|
||
public static void executeScript(String script) { | ||
try (var cx = Context.enter()) { | ||
cx.setOptimizationLevel(-1); | ||
Script s = cx.compileString(script, "unknown source", 0, null); | ||
Scriptable scope = cx.initStandardObjects(); | ||
scope.put("c", scope, Context.javaToJS(CAPTURER, scope)); | ||
Assert.assertThrows( | ||
ContinuationPending.class, | ||
() -> { | ||
cx.executeScriptWithContinuations(s, scope); | ||
}); | ||
} | ||
} | ||
|
||
@Test | ||
public void testBind() { | ||
executeScript("function capture(){c.run()};capture.bind(this)()"); | ||
} | ||
|
||
@Test | ||
public void testBindCall() { | ||
executeScript("function capture(){c.run()};capture.bind(this).call()"); | ||
} | ||
|
||
@Test | ||
public void testBindApply() { | ||
executeScript("function capture(){c.run()};capture.bind(this).apply()"); | ||
} | ||
|
||
@Test | ||
public void testArrow() { | ||
executeScript("capture=()=>{c.run()};capture()"); | ||
} | ||
|
||
@Test | ||
public void testArrowCall() { | ||
executeScript("capture=()=>{c.run()};capture.call()"); | ||
} | ||
|
||
@Test | ||
public void testArrowApply() { | ||
executeScript("capture=()=>{c.run()};capture.apply()"); | ||
} | ||
|
||
@Test | ||
public void testArrowBindCall() { | ||
executeScript("capture=()=>{c.run()};capture.bind(this).call()"); | ||
} | ||
|
||
@Test | ||
public void testArrowBindApply() { | ||
executeScript("capture=()=>{c.run()};capture.bind(this).apply()"); | ||
} | ||
|
||
@Test | ||
public void testArrowImmediate() { | ||
executeScript("(()=>{c.run()})()"); | ||
} | ||
} |