How to call Rhino compiled JavaScript methods (class files) in a Java program?

I compiled the following JavaScript file "test.js" into "test.class":

var test = (function () { var that = {}; that.addNumbers = function (a, b) { return a+b; }; return that; }()); 

I would like to call the compiled JavaScript function "test.addNumbers (1,2)" in the simple Java program "run.java" as follows:

 public class run { public static void main(String[] args) throws Exception { Context cx = Context.enter(); try { Scriptable scope = cx.initStandardObjects(); // HOW TO CALL THE METHOD, Test.addNumbers(1,2)? Please help me! } finally { Context.exit(); } } } 

I tried many ways, but could not. I read a Rhino tutorial and looked at a lot of articles and examples, but they show how to call JavaScript methods from the command line or the source file test.js. I need to call a method from a compiled test.class file.

Many thanks for your help!

+4
source share
1 answer

Using javap , I believe that the JavaScript test type does not mean that the resulting Java type is this class. The generated Java type calls script code in its constructor; this will not expose addNumbers as a Java method.

 >javap -classpath . test public class test extends org.mozilla.javascript.NativeFunction implements org.m ozilla.javascript.Script{ public test(org.mozilla.javascript.Scriptable, org.mozilla.javascript.Contex t, int); public test(); public static void main(java.lang.String[]); public final java.lang.Object exec(org.mozilla.javascript.Context, org.mozil la.javascript.Scriptable); public final java.lang.Object call(org.mozilla.javascript.Context, org.mozil la.javascript.Scriptable, org.mozilla.javascript.Scriptable, java.lang.Object[]) ; public int getLanguageVersion(); public java.lang.String getFunctionName(); public int getParamCount(); public int getParamAndVarCount(); public java.lang.String getParamOrVarName(int); public java.lang.String getEncodedSource(); public boolean getParamOrVarConst(int); } 

Reading between the lines, I would say you need to map Java types to do what you want. From jsc doc:

-implements java-intf-name

Indicates that the java class implementation of the Java interface java-intf-name should be generated from an incoming JavaScript file source. Each global function in the source file is made by a generated class method that implements any methods in the interface with the same name.

Define this interface:

 //Adder.java public interface Adder { public int addNumbers(int a, int b); } 

Write this implementation:

 //AdderImpl.js function addNumbers(a, b) { return a+b; } 

Compile JavaScript with the -implements Adder AdderImpl.js . Call the method as follows:

 Adder adder = new AdderImpl(); int n = adder.addNumbers(1, 2); System.out.println(n); 

I would venture to suggest that this is probably necessary due to differences in language type systems.

I used Rhino 1.7R2. For brevity, I avoided using packages, etc.

+5
source

Source: https://habr.com/ru/post/1340506/


All Articles