5 ms·
CPython to LLVM to Javascript - Python in the browser
- megrimlock 16y agoIt's kind of fascinating to peruse the generated js, if you are at all familiar with the CPython code: http://syntensity.com/static/python.js http://syntensity.com/static/python.js Also geeky fun to try to recognize what constants like 4294967295 correspond to -- it's all over the place! And lots of literal char* strings encoded as integer sequences: >>> s = [111,98,106,101,99,116,46,95,95,102,111,114,109,97,116,95,95,32,119,105,116,104,32,97,32,110,111,110,45,101,109,112,116,121,32,102,111,114,109,97,116,32,115,116,114,105,110,103,32,105,115,32,100,101,112,114,101,99,97,116,101,100,0] >>> ''.join(map(chr,s)) 'object.__format__ with a non-empty format string is deprecated\x00'
- Detrus 16y agoCan it run NTLK http://www.nltk.org/ http://www.nltk.org/ ?
- asclepiades 16y agoShort answer, it depends. Long answer, it can run some parts of it if you join them (e.g., separating tokens, counting probabilities, some tagging and even some more advanced stuff). You cannot however, run everything (for exemple, modules and functions depending on NumPy) and must deal with how to import/export data.
- pedrocr 16y agoIt's an interesting proof of concept. I did a quick "benchmark" by calculating the 30th Fibonacci number with a dumb recursive implementation and there was a 60-70x difference in speed. I wonder how much of the speed difference is solvable and how easy it would be to have a DOM API.
- srean 16y agoIf one is interested in doing this optimally, it should be better to compile python code to javascript directly. Given similarities between the language, it is a doable proposition. For some constructs, walking the python AST (which python so conveniently exposes) and translating the nodes to corresponding javascript should be quite direct. But a complete coverage of python will not be a trivial undertaking. Oh wait! pyjamas does exactly that http://pyjs.org/ http://pyjs.org/ This is the first time I came to know about this project. So I have no experience with it whatsoever. [Well, that applies to my experience with javascript too.] Edit: from a quick look it seems pyjamas will not give you a python REPL, I am not sure. But nevertheless this looks very interesting
- Raphael_Amiard 16y ago> If one is interested in doing this optimally, it should be better to compile python code to javascript directly. Well no, this is not possible, because python and javascript have very different semantics, notably regarding numeric types. You would have to enclose every python numeral in an object, and that in itself would already cripple the performance quite a bit. It would probably be less than the emscripten approach, but not by that much of a margin. I started doing a python to lua compiler, and did hit that wall very fast. Pyjamas python to javascript compiler is basically a toy regarding compliance to python spec. Every number is treated as a float, which means any numeric python library/function may not work correctly in pyjamas. That's not the only difference in semantics between python and javascript. Just the most performance expensive.
- wkornewald 16y agoNote that pyjs has several compilation options, one of which wraps numbers to distinguish between float and int. However, stuff like correct delegation with multiple inheritance might never work because js can only have one delegate. Currently one of the big problems in practice is that attribute access doesn't behave like getattr, which makes working with @property inconsistent. Also, __call__ doesn't work. Lots of minor details. At least they are checked in the unit tests, in case someone wants to help.
- fourk 16y agoIf you're looking for a python DOM API, pythonwebkit is currently in development here: http://www.gnu.org/software/pythonwebkit/ http://www.gnu.org/software/pythonwebkit/
- sanxiyn 16y agoPreviously on HN: http://apps.ycombinator.com/item?id=1941405 http://apps.ycombinator.com/item?id=1941405
- m_eiman 16y agoThat page causes my FF4b9/OSX to crash, FYI.
- yuvadam 16y agohttps://bugzilla.mozilla.org/show_bug.cgi?id=628170 https://bugzilla.mozilla.org/show_bug.cgi?id=628170
- nervechannel 16y agoHave you tried it with Firefox pre 4.0? Doesn't seem to work on 3.6.13. I can select the section of code but not actually edit it. It's just plain, unadorned text in a regular div. But pressing Execute does nothing anyway, apart from a page refresh.
- k0nad 16y agoThere used to be a client side python active x control for I.E.
- ecounysis 16y agoI think this is pretty awesome. Language compilation is a fascinating topic.