Skip to content

Instantly share code, notes, and snippets.

@purplefox
Created December 10, 2013 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purplefox/7892144 to your computer and use it in GitHub Desktop.
Save purplefox/7892144 to your computer and use it in GitHub Desktop.
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