by chubot · 12y ago · 0 comments
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/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.
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/Refe...
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.
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.
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_proxie...
Harmony proxies catch everything, and really overload the getter/setter mechanism.
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.__...
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/ru...
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
1https://github.com/PythonJS/PythonJS/blob/master/pythonjs/py...