7 ms·
Can it run real programs (i.e. not benchmarks)? If so I will be impressed. Glancing over the code, it looks pretty short, and I can imagine benchmarks will ru
by chubot 12y ago
Can it run real programs (i.e. not benchmarks)?
If so I will be impressed. Glancing over the code, it looks pretty short, and I can imagine benchmarks will run, but not real programs.
https://github.com/PythonJS/PythonJS/blob/master/pythonjs/pythonjs.py https://github.com/PythonJS/PythonJS/blob/master/pythonjs/py...
It's true that most programs don't use all the dynamism of Python, but they probably depend on something that does (e.g. Django). Python is more dynamic than JavaScript, in that it has __getattr__, __setattr__, __getitem__, etc.
If not, it's not fair to compare it to PyPy... PyPy actually runs arbitrary Python programs.
- belandrew 12y ago> Python is more dynamic than JavaScript, in that it has __getattr__, __setattr__, __getitem__, etc. No, it's not. You can do equivalents to all of those in Javascript. Javascript objects are all mutable with a few exceptions. They are just maps of properties, very similar to Python's. Properties can be added or changed at runtime. For example, setattr(obj, item, value) in python is basically just obj[item] = value in js.
- craigyk 12y agoI think he meant that you can overload operators in python and not javascript... but yeah I agree with you. At the end of the day javascript is still just as, or more, dynamic than python.
- pygy_ 12y ago__getattr__ and friends allow to customize the getters and setters. JavaScript is in the process of getting similar capabilities with Harmony proxies, but the spec has yet to be finalized: http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxie...
- jonpacker 12y agoI'm slightly confused by your wording but I think the functionality you're referring to is already available via Object.defineProperty's "get" and "set" options: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- pygy_ 12y agoObject.defineProperty allows to overload get/set for a single key at a time. Harmony proxies catch everything, and really overload the getter/setter mechanism.
- bcj 12y agoMy JS knowledge isn't great. In python, I can do operator overloading such that `obj[item] = value` does something completely different than assignment. Is that possible with JavaScript?
- jonpacker 12y agoYou can set a custom getters and setters, but I wouldn't call it operator overloading. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... So... > var x = {} > Object.defineProperty(x, 'value', { get: function() { return 1 }, set: function() { console.log('set') } }) > x.value 1 > x.value = 2 set > x.value 1
- spankalee 12y agoNo, JavaScript does not have Python's __getattr__, and obj[name] is not a substitute for it. obj[name] works if you have already either assigned a value for that name, it exists in the prototype chain, or you have defined a getter via Object.defineProperty(). It's truly no different than obj.name[1] - `name` has to already exist in some form. Python's __getattr__ [2] takes the attribute name as an argument, so it can be used to implement property access for any name, even those not known ahead of time. Ruby has something similar with method_missing, and even more with define_method and instance_eval. Dart has noSuchMethod. These are possible to implement in compile-to-JS language, but tricky to make fast. Dart's static analysis looks for all names that could be called on objects with a noSuchMethod, tries to figure out the most specific class it can add them to, then adds stubs to the JS prototypes to redirect to noSuchMethod(). A compile-to-JS Ruby could implement define_method easily enough, but method_missing is would be impossible to do the way Dart does it due to the open classes: you could dynamically add method_missing and not have the stubs available. If you want full Ruby semantics you have to abstract away method calls, which will be slow. I'm guessing the same problem exists for Python, so I wonder the same thing as the grandparent post: did they implement __getattr__ fully? [1] With the small difference in the names that are allowed. [2] https://docs.python.org/3/reference/datamodel.html#object.__getattr__ https://docs.python.org/3/reference/datamodel.html#object.__...
- bretthartshorn 12y agoPythonJS implements __getattr__. See these regression tests: https://github.com/PythonJS/PythonJS/blob/master/regtests/class/__getattr__.py https://github.com/PythonJS/PythonJS/blob/master/regtests/cl... https://github.com/PythonJS/PythonJS/blob/master/regtests/class/mi__getattr__.py https://github.com/PythonJS/PythonJS/blob/master/regtests/cl... __getattr__ and __getattribute__ are implemented in the special __get__ function defined in: https://github.com/PythonJS/PythonJS/blob/master/pythonjs/runtime/pythonpythonjs.py https://github.com/PythonJS/PythonJS/blob/master/pythonjs/ru...
- euank 12y agoJavascript (6) has those in the form of Proxies [0]... Coming soon! I think that they cover the set of features you indicate anyways. However, you can already hack them in and there are multiple js projects that let you compile ES6 down to ES5, proxies included. As other commenters noted, python is not "more dynamic" than javascript. You can easily transform python code that does unusual metaprogramming stuff to javascript code that does similar metaprogramming stuff but looks significantly different; basically all languages that support runtime-eval will let you imitate all other metaprogramming features in a somewhat verbose fashion. [0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- chriswarbo 12y ago> However, you can already hack them in and there are multiple js projects that let you compile ES6 down to ES5, proxies included. Of course, but will it still be as fast? For example we can trivially turn a Python expression "a + b" into a Javascript expression "a + b" to get a great speed benefit. It may even give correct results in benchmarks, but in general it breaks the semantics and therefore any frameworks which rely on it. FYI in Python "a + b" is equivalent to "a.__add__(b)", and hence the correct Javascript would look more like "a.__add__(b)", but this requires a property lookup and a function call so a lot of the speed gains may be lost (although some benefits may be regained by JITting). Considering that the parent pointed out how small this project's codebase is, it does give legitimate concern about how many rules there are for mapping Python's semantics to Javascript's. For example, the ShedSkin project has been around for years and gets very impressive benchmark results since it compiles Python to C++; however, it tries to map Python classes directly to C++ classes, which breaks the semantics immensely.
- lclarkmichalek 12y agoIt's even more complicated than a.__add__(b), due to __radd__: https://www.inkling.com/read/learning-python-mark-lutz-4th/chapter-29/right-side-and-in-place https://www.inkling.com/read/learning-python-mark-lutz-4th/c...
- 12y ago
- bretthartshorn 12y agopythonjs.py is not the main part of the translator. PythonJS translates in two steps, most of the translation happens in the first stage, in python_to_pythonjs.py https://github.com/PythonJS/PythonJS/blob/master/pythonjs/python_to_pythonjs.py https://github.com/PythonJS/PythonJS/blob/master/pythonjs/py...