Python ExecJS - JavaScript Engine - 使用Python类

时间:2015-10-25 20:15:37

标签: javascript java python node.js v8

我试图在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类/变量。

1 个答案:

答案 0 :(得分:1)

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