【问题标题】:Python ExecJS - JavaScript Engine - work with Python classPython ExecJS - JavaScript 引擎 - 使用 Python 类
【发布时间】:2015-10-25 20:15:37
【问题描述】:

我正在尝试在 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.

                /* ----------------------------------------------------------------------- */          
        }
}

用户.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 java python node.js v8


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      相关资源
      最近更新 更多