0

I have some code where I am successfully loading and compiling a script. This is working great. Next, though, I want to be able to call a function inside the compiled script. Unfortunately, I don't see any way to make a compiled script invokable.

Compilable compEngine = (Compilable)engine;
compiledScripts.put(filename, compEngine.compile(new InputStreamReader(in)));
compiledScripts.get(filename).eval();
//All works until this point. The compiled script does not seem to be invokable.
Invocable inv = (Invocable) compiledScripts.get(filename);
inv.invokeFunction("onLoad");

Is there a way to do this? If so, how? If not, how much of a performance hit is their generally when not compiling the script?

2
  • What library are you using here? Rhino? Commented Jul 8, 2013 at 21:36
  • Rhino is the default Javascript Engine in Java, so yes. Commented Jul 9, 2013 at 3:36

1 Answer 1

1

I found the answer to my question. It is actually a very simple change.

This line:

Invocable inv = (Invocable) compiledScripts.get(filename);

Needs to change to:

Invocable inv = (Invocable) compiledScripts.get(filename).getEngine();

This returns the engine the compiled script is getting run in, thus letting us call functions from the compiled script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.