Python ExecJS - JavaScript引擎-工作与Python类



我正在尝试使用Python中的JavaScript引擎。

我需要与JavaScript中的Python类工作,反之亦然-在Python中使用JavaScript代码。我该怎么做呢?

在Java中我有工作代码:

package test.test;
import javax.script.Bindings;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
public class JavaScriptInJava {
        public static void main(String[] args) throws ScriptException, NoSuchMethodException {
                ScriptEngineManager manager = new ScriptEngineManager();
                ScriptEngine engine = manager.getEngineByName("JavaScript");
                /* ----------------------------------------------------------------------- */
                // Work with Java class from JavaScript:               
                String userScript =
                                  "user1.setName("Test User");                "
                                + "print( user1.getName() );                    ";
                Bindings bindings = new SimpleBindings();
                User u = new User();
                bindings.put("user1", u);              
                engine.eval(userScript, bindings);
                // Work with Java class from JavaScript.
                /* ----------------------------------------------------------------------- */
                // Use JavaScript function in Java code:
                String math =
                                  "function addition(a, b) {                    "
                                + "             return a+b;                     "
                                + "}                                            "
                                + "                                             "
                                + "function substraction(a, b) {                "
                                + "             return a-b;                     "
                                + "}                                            "
                                + "                                             ";
                engine.eval(math);
                Invocable inv = (Invocable) engine;
                int a = 10;
                int b = 5;
                System.out.println("A=" + a + " B=" + b);
                Object aPlusB = inv.invokeFunction("addition", a, b);
                System.out.println("A+B = " + aPlusB);
                Object[] inputParams = {
                                new Integer(10),
                                b
                                };
                Object aMinusB = inv.invokeFunction("substraction", inputParams);
                System.out.println("A-B = " + aMinusB);
                int x = (Integer) aPlusB + 1;
                System.out.println("aPlusB + 1 = " + x);
                // Use JavaScript function in Java code.
                /* ----------------------------------------------------------------------- */          
        }
}

User.java

package test.test;
public class User {
        private String name;
        public User() {
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
}

如何将其重写为Python?

import execjs
execjs.runtime = 'Node'
class Clazz:
    a = 10
    b = 20
    c = "test"
    def add(self):
        return self.a + self.b
x = Clazz()
print x.add()
ctx = execjs.compile("""
    function add(x, y) {
        return x + y;
    }
""")
print ctx.call("add", 1, 2)

可以在Python中使用JavaScript函数。但是我不能在JavaScript中使用Python类/变量

根本不可能在JavaScript中使用PyExecJS中的Python类。请参考PyV8或其他Py-JS库。

最新更新