0

I am working on a Java program where an object needs to have user-customization behavior for one function. I am implementing this using Mozilla Rhino, JavaScript and Java.

I cannot figure out how to take the already instantiated object and pass it to a pre-written script.

I have looked through many tutorials on Rhino, and none have given an example like this. Any advice or links would be greatly appreciated.

Thank you for your time.

1 Answer 1

0

This answer to another question passes an object, data, from Java to Rhino Javascript.

I have no idea whether or not it works (well I suppose it does). Here are the relevant parts:

public static class data { 
   Double value = 1.0d;
}

ScriptEngine engine = new ScriptEngineManager().getEngineByName ("rhino"); 
data data = new data();
Context.enter().getWrapFactory().setJavaPrimitiveWrap(false);

engine.eval("function test(data) { return data.get('value1') + 5;};"); 
System.out.println("Result:" + ((Invocable)engine).invokeFunction("test", data)); 

(I didn't know about that setJavaPrimitiveWrap(), here is some WrapFactory Javadoc.)

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.