GitXplorerGitXplorer
i

js2py

public
16 stars
1 forks
1 issues

Commits

List of commits on branch master.
Unverified
c54bb1e8ad584e3c0f12f8b906d76fdd7aa532ba

Add license.

iint3 committed 12 years ago
Unverified
4721b2a5724aa18e8f8921fbf6efba6b6527190f

Elaborate on limitations.

iint3 committed 12 years ago
Unverified
2da1a742aa49c78480993d6ead80b0f57f19ecec

More fixes.

iint3 committed 12 years ago
Unverified
67323e1de8171e46b21c89dcd3dfe66cb6748b0e

Re-throw the same exceptions where possible.

iint3 committed 12 years ago
Unverified
af918a3291915b5d7425c0aa8822fb14e78b45cb

Make output prettier.

iint3 committed 12 years ago
Unverified
4dc5a714ed273f9a8e0657dc048cd0dbb9a098bb

Initial commit.

iint3 committed 12 years ago

README

The README file for this repository.

js2py

A Javascript-to-Python translation assistant. It does a simple translation from Javascript to Python (as detailed below). You'll most likely need to make further changes by hand, but this automates part of the work.

I thought it would be nice to be able to import some libraries directly into Python, rather than shelling out to NodeJS. This script is the result of trying to make that a reality.

I've successfully used it to port Esprima to Python -- the result is PyEsprima.

Setup & Usage

npm install
./js2py.coffee file.js > out.py

Transformations and Shims

  • Create global declarations where necessary
  • Transform simple prototype-based classes into corresponding Python classes (but no inheritance)
  • Remove assignment statements from conditional tests
  • Convert switch statements into if-else chains
  • Convert pre-/post-increments into assignment statements
  • Array.prototype.slice and String.prototype.substr are converted to Python's slice notation
  • Function expressions are hoisted out as fully declared functions since Python's lambdas are limited
  • Shims for RegExp and JS-style dictionaries
  • Some support for typeof

Limitations

  • No support for modifying non-global nonlocals (but easy enough to add with the nonlocal keyword in Python 3)
  • a[i] in JS will return undefined if i >= a.length, but in Python it raises an IndexError.
  • No support for call / apply.
  • There many more; pull requests are welcome!