Skip to content

Commit 84bb29d

Browse files
committed
Test binding scope for eval
1 parent 10e8002 commit 84bb29d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

res/nashorn8.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var evaluate1 = function () {
2+
(function () {
3+
print(eval("this"));
4+
}).call(this);
5+
};
6+
7+
var evaluate2 = function () {
8+
var context = {};
9+
(function () {
10+
print(eval("this"));
11+
}).call(context);
12+
};
13+
14+
var evaluate3 = function (context) {
15+
(function () {
16+
print(eval("this"));
17+
}).call(context);
18+
};

src/com/winterbe/java8/Nashorn8.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.winterbe.java8;
2+
3+
import jdk.nashorn.api.scripting.NashornScriptEngine;
4+
5+
import javax.script.ScriptEngineManager;
6+
import javax.script.ScriptException;
7+
8+
/**
9+
* @author Benjamin Winterberg
10+
*/
11+
public class Nashorn8 {
12+
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
13+
NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
14+
engine.eval("load('res/nashorn8.js')");
15+
16+
engine.invokeFunction("evaluate1");
17+
engine.invokeFunction("evaluate2");
18+
engine.invokeFunction("evaluate3", "Foobar");
19+
engine.invokeFunction("evaluate3", new Person("John", "Doe"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)