-
Notifications
You must be signed in to change notification settings - Fork 78
/
TestAccessFunction.java
32 lines (26 loc) · 1.25 KB
/
TestAccessFunction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package delight.nashornsandbox;
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;
import delight.nashornsandbox.internal.NashornDetection;
import org.junit.Assert;
import org.junit.Test;
import javax.script.ScriptException;
import java.lang.reflect.InvocationTargetException;
@SuppressWarnings("all")
public class TestAccessFunction {
@Test
public void test_access_variable() throws ScriptCPUAbuseException, ScriptException, InvocationTargetException, IllegalAccessException {
final NashornSandbox sandbox = NashornSandboxes.create();
sandbox.eval("function callMe() { return 42; };");
final Object _get = sandbox.get("callMe");
Assert.assertEquals(Integer.valueOf(42), findAndCall(_get));
final Object _eval = sandbox.eval("callMe");
Assert.assertEquals(Integer.valueOf(42), findAndCall(_get));
}
private Object findAndCall(Object _get) {
if (NashornDetection.isStandaloneNashornScriptObjectMirror(_get)) {
org.openjdk.nashorn.api.scripting.ScriptObjectMirror scriptObjectMirror = (org.openjdk.nashorn.api.scripting.ScriptObjectMirror) _get;
return scriptObjectMirror.call(_get);
}
throw new IllegalStateException("Neither JDK nor standalone Nashorn has been found");
}
}