GitXplorerGitXplorer
i

js2py

public
16 stars
1 forks
1 issues

Commits

List of commits on branch master.
Unverified
1446c1c2cf544a93e4ff6fad0dbf12bf8079b183

More fixes.

iint3 committed 12 years ago
Unverified
4f9f4d2c0c8c00818ee169bc46ee2db1fdf45296

Preserve truthy semantics.

iint3 committed 12 years ago
Unverified
f306e3ab691d9352a335c244924b0a90e32bcd96

Convert toString.

iint3 committed 12 years ago
Unverified
c96ef2533018306592eca5e7045440a0db1ac946

Shim JSON.stringify.

iint3 committed 12 years ago
Unverified
875e4b80871feedd605694ffc54de97683bba5d9

Separate out the passes so they don't confuse each other.

iint3 committed 12 years ago
Unverified
43b2c5162e09c9a6fcad4d914ab0a86b4667a18c

Support SequenceExpression.

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!