Python ExecJS - JavaScript 引擎 - 使用 Python class

Python ExecJS - JavaScript Engine - work with Python class

我正尝试在 Python 中使用 Java 脚本引擎。

我需要在 Java 脚本中使用 Python class,反之亦然 - 在 Python 中使用 Java 脚本代码。我该怎么做?

在 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)

使用JavaPython中的脚本函数即可。但是我不能在 JavaScript.

中使用 Python class/variable

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