PyMiniRacer 添加 Python 类 到 JS 作用域

PyMiniRacer adding Python classes to JS scope

使用 PyMiniRacer,我将能够在 Javascript 中使用 Python 个对象;使用 PyV8 库:

import PyV8

class Scope(PyV8.JsClass):
    def __init__(self):
        self.obj1 = Object1()
        self.obj2 = Object2()

pythonScope = Scope()
context     = PyV8.JsContext(pythonScope)
context.enter()
context.eval(...)

并且使用此代码,javascript 可以访问 Scope 的属性:obj1obj2

对于 PyMiniRacer,查看 codeMiniRacer class 似乎不接受构造函数中的任何参数,所以我不知道如何添加a Python Scope class 进入 JS 作用域。是否有一种特定的方法来定义 class 以便能够将一组 python classes 添加到 JS 范围,或者我是否需要使用将它们注入 JS 范围我在查看源代码时错过了方法?

Ruby RubyRacer中,(我了解到RubyRacer和PyMiniRacer是不同作者的独立项目,尽管PyMiniRacer的灵感来自RubyRacer)对象在Ruby 范围可以通过调用 context["funcName"] = funcName] 嵌入,但在 Python...

>>> from py_mini_racer import py_mini_racer
>>> context = py_mini_racer.MiniRacer()
>>> def helloWorld(num):
        return num * 2

>>> context["helloWorld"] = helloWorld
Traceback (most recent call last):
    File "<pyshell#5>", line 1, in <module>
        context["helloWorld"] = helloWorld
TypeError: 'MiniRacer' object does not support item assignment
>>> 

...它会引发错误。我也试过 context.__dict__["helloWorld"] = "helloWorld",运行 context.eval(helloWorld(5)) returns ReferenceError: helloWorld is not defined 错误。它所做的只是允许我调用 context.helloWorld(5),这对从 JS 执行没有帮助。

如何将 Python 对象插入 JS 作用域,以便在 JS 代码中调用和访问 Python 对象的方法和属性?

不幸的是,PyMiniRacer 不支持将 Python 对象或函数附加到 JavaScript 上下文,因此无法从 JavaScript 代码调用 Python 代码。