Created
December 10, 2013 15:10
-
-
Save purplefox/7892144 to your computer and use it in GitHub Desktop.
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
import jdk.nashorn.api.scripting.ScriptObjectMirror; | |
import javax.script.*; | |
public class LeakTest { | |
public static void main(String[] args) { | |
new LeakTest().run(); | |
} | |
private String script1 = | |
"function foo() { \n" + | |
"_javaobject.setFunction(org.vertx.java.platform.impl.LeakTest);"; | |
private void run() { | |
try { | |
ScriptEngineManager factory = new ScriptEngineManager(); | |
ScriptEngine engine = factory.getEngineByName("nashorn"); | |
// Run script1 in it's own scope | |
ScriptContext ctx = new SimpleScriptContext(); | |
Bindings bindings = engine.createBindings(); | |
bindings.put("_javaobject", this); | |
ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE); | |
engine.eval(script1, ctx); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
// This allows us to pass an JavaScript object from one scope to another | |
public void setFunction(ScriptObjectMirror function) { | |
System.out.println("Class of function is: " + function.getClass().getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment