简体   繁体   中英

Converting JavaScript into Python bytecode

I am trying to execute simple JavaScript code in a pure Python environment (Google AppEngine).

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).

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.

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.

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.

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. For VBScript I was dealing only with recorded macros, so code was very stable, mostly one-liners.

To my knowledge, there are no complete and robust implementations of Javascript interpreters on Python. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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