简体   繁体   English

将JavaScript转换为Python字节码

[英]Converting JavaScript into Python bytecode

I am trying to execute simple JavaScript code in a pure Python environment (Google AppEngine). 我正在尝试在纯Python环境(Google AppEngine)中执行简单的JavaScript代码。

I've tried PYJON, but it does not seem mature enough for real use(it does not handle eg forward referenced functions or do-while and it hangs on array usage). 我已经尝试过PYJON,但是它似乎还不够成熟,无法实际使用(它无法处理例如前向引用函数或do-while,并且会依赖于数组的用法)。

One idea would be to use pynarcissus to convert JavaScript into a syntax tree and than convert this tree jnto a Python AST which could be compiled into Python bytecode. 一种想法是使用pynarcissus将JavaScript转换为语法树,而不是将此树转换为可以编译为Python字节码的Python AST。

Has anybody done this before? 有人做过吗? Any problems with this idea? 这个想法有什么问题吗?

I did something similar using ply , for VBScript and for a new language I've been working on for a while, but instead of building the AST and then convert to a Python AST, I'd build the Python AST straight on, using its node classes and occasionally subclassing and overriding some that wouldn't fit particularly well. 我使用ply做了类似的事情,对于VBScript和我已经研究了一段时间的新语言,但是我没有构建AST然后转换为Python AST,而是直接使用它构建Python AST。节点类,偶尔子类化并覆盖一些不合适的方法。

Since you can rely on pynarcissus to build an AST for Javascript, converting the AST is probably easier, but I think it's worth checking if using its parser to build a Python AST isn't a better approach. 由于您可以依靠pynarcissus为Javascript构建AST,因此转换AST可能会更容易,但是我认为值得检查使用其解析器构建Python AST是否不是更好的方法。

There are no inherent problems with this approach, as far as I know, as long as the languages can be syntactically reduced to a common dialect. 据我所知,这种方法没有内在的问题,只要可以将这些语言在语法上简化为通用的方言即可。 It's hard work, but it can be done. 这是艰苦的工作,但可以做到。 I did this because I didn't have a parser available and writing one to just build an AST would make no sense. 我这样做是因为我没有可用的解析器,而编写一个仅用于构建AST的解析器没有任何意义。 For VBScript I was dealing only with recorded macros, so code was very stable, mostly one-liners. 对于VBScript,我只处理记录的宏,因此代码非常稳定,大多数情况是单行的。

To my knowledge, there are no complete and robust implementations of Javascript interpreters on Python. 据我所知,在Python上没有完整而健壮的Java解释器实现。 Your best option is probably to deploy an alternate version of your app with the Rhino interpreter in Java, and call this as a web service with the main version of your app. 最好的选择可能是使用Java的Rhino解释器部署应用程序的备用版本,并将其作为Web服务与应用程序的主版本一起调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM