我正在尝试使用Transcrypt为XGL(https://www.xalys.com)创建Python绑定。
从Python方面,我发现您可以继承和扩展JavaScript对象,执行所需的操作,然后将它们传递回JavaScript库。
类似于在https://github.com/doconix/pyreact/blob/master/src/scripts/pyreact.py处找到的React绑定,我这样做:
class ComponentMeta(type):
def __new__ (meta, name, bases, attribs):
cls = type.__new__ (meta, name, bases, attribs)
descrip = Object.getOwnPropertyDescriptor(cls, 'name')
descrip.value = name
Object.defineProperty(cls, 'name', descrip)
return cls
class AbstractComponent(object, metaclass=ComponentMeta):
def __init__(self, props):
object.__init__(self)
class MyClass(AbstractComponent, MyJsClass.prototype):
def __init__(self, a, b):
AbstractComponent.__init__(self)
MyJsClass.apply(self, [ a, b ])
def doSomething(self):
# Calling js method doSomething
MyJsClass.prototype.doSomething.call(self)
def doSomethingElse(self, myObject: MyClass):
# Calling js method doSomethingElse
MyJsClass.prototype.doSomethingElse.call(self, myObject)
def doSomethingElse(self) -> MyClass:
# Calling js method giveMeAnotherInstance, returns a new instance of MyJsClass
return MyJsClass.prototype.giveMeAnotherInstance.call(self)
myObject = MyClass(1, 2)
mySecondObject = MyClass(2, 3)
# Call a js method
myObject.doSomething()
# Call another js method with a Python Object (but valid Javascript Object)
myObject.doSomethingElse(mySecondObject)
# Get a new instance
myThirdObject:MyClass = myObject.giveMeAnotherInstance()
type(myThirdObject) is MyClass # returns false
如何将对象myThirdObject转换为有效的MyClass对象?
在Java(带有GWT)和C#(带有Bridge.Net)中,在语言和JavaScript之间来回切换非常容易,我是否会错过Transcrypt的某些功能?
我尝试过:
def doSomethingElse(self) -> MyClass:
# Calling js method giveMeAnotherInstance, returns a new instance of MyJsClass
return dict(MyJsClass.prototype.giveMeAnotherInstance.call(self))
但这不起作用...