
Jaspy – A Python VM written in JavaScript - mmariani
https://github.com/koehlma/jaspy/blob/master/README.rst
======
ruffrey
People often look at things like this like 'why would you...' But it is an
interesting experiment that might make web programming more accessible. I
think of my physicist cousin who uses python for data analysis. An option like
this might lower the barrier to sharing data on the web - less JavaScript
ecosystem knowledge required. JavaScript frontendy land moves so fast it can
be overwhelming to outsiders, even if JavaScript itself is "easy."

~~~
xaduha
You need exactly zero knowledge of Javascript to share data on the web. Plain
server generated HTML + some CSS framework is more than enough for most cases.
Even if you want some more interactive stuff you don't need to know the
"Javascript ecosystem". It's still possible to link a couple pf JS files in
script tags, you know. It's even possible to link XSLT 1.0 stylesheet for some
non-interactive clientside processing.

~~~
simonster
If you want interactive stuff, there are plenty of Python plotting libraries
that can produce interactive visualizations, e.g. Bokeh, MPLD3, or Plotly.

~~~
stevens32
And IPython notebooks allow you to not only share the results but an
interactive copy of the process as well.

------
jvilk
I actually embarked on a similar Python VM called Ninia [1] awhile back, but
shelved it as other projects became more important. Ninia supports multiple
threads and preempts execution to give the browser breathing space, just like
my JVM in JavaScript, DoppioJVM [2].

One of the challenges of Python is its objects are quite complex/ad-hoc, and
can be overridden by the user. I mapped out [3] how I wanted to map Python
objects to JavaScript objects, but never had the time to implement it fully.
(That document covers Python 2.7; I'm sure there are differences for 3.) It
looks like this project has a lot of work to do if it wants to have parity
with a native Python VM! But that's part of the fun of writing these things.

Best of luck on the VM! If you need a file system to plug into the VM for
loading modules/implementing the FS API, do check out my BrowserFS library
[4], which is currently in use at the Internet Archive [5] and is also used in
Ninia/DoppioJVM. You can also cop things from Ninia, as it is MIT licensed.

Oh yeah, and if you need a Python compiler/parser, you can simply run the PyPy
interpreter's parser+compiler [6] in your intepreter once it is reasonably
complete. It is written in 100% Python!

[1] [https://github.com/plasma-umass/ninia](https://github.com/plasma-
umass/ninia)

[2] [https://github.com/plasma-umass/doppio](https://github.com/plasma-
umass/doppio)

[3] [https://github.com/plasma-umass/Ninia/wiki/Mapping-Python-
Ob...](https://github.com/plasma-umass/Ninia/wiki/Mapping-Python-Objects-to-
JavaScript-Objects)

[4] [https://github.com/jvilk/browserfs](https://github.com/jvilk/browserfs)

[5]
[http://ascii.textfiles.com/archives/4924](http://ascii.textfiles.com/archives/4924)

[6]
[https://pypy.readthedocs.org/en/latest/interpreter.html](https://pypy.readthedocs.org/en/latest/interpreter.html)

~~~
koehlma
Cool, I will have a look through your code and BrowserFS seems especially
useful to me.

------
coldtea
I often see this wish expressed (also in here by somebody):

> _JS engines in browsers should evolve to more general VMs, for which you can
> implement /port other languages._

You do understand that when this happens (and is spread enough to use in
production), the web framework and tooling fatigue will be 2-3 times worse.

It's not just that everybody and their dog will write their own framework (as
now), they'll split even more and do it for 10 languages... And expect the
same dilution of effort for libs too...

I'd actually prefer a better JS replacement, that is, a new, single, language
that draws from it and gets rid of all the caveats fixed (lack of ints,
coercion rules, small std lib, optional types, even better designed for high
speed JITing etc), than seeing Python, Clojure, Ruby, Lua, PHP and 200 others
re-implemented in web assembly...

~~~
ben_jones
Where is this "dilution" in the java world? I don't see it.

~~~
coldtea
The Java world has 1/10 the programmers Javascript has (which is basically
everyone doing web work).

The Java world also historically had a parent company giving strong guidance.

The Java world also has strong standard libs and tended to include everything
and the kitchen sink in them (including full blown servers, XML/XSLT parsers)
in the SDK (and EE SDK). Heck, it even has a full JS interpreter there.

Even so, there was huge fragmentation of effort for things like web frameworks
(Struts, WebWork, Spring, Cocoon, Turbine, Tapestry, and many many more).
Actually, if you were doing Java in 2000-2010 it was a running joke how
everybody would write their own framework.

And I would argue that all the resources diverted to numerous Java-derivative
languages (like Kotlin, Ceylon, Xtend, etc) would be better of in making Java
better, or creating a single, community driven, Java successor, if there was a
process (and access) for that.

As it is, they are all doomed to remain niche players, and ultimately fail,
while languages like C# and Swift march on.

------
Gladdyu
As a general trend with reimplementing stuff in JS, I don't quite know whether
this is exciting or horrifying...

~~~
poizan42
JavaScript may very well have the doubtful honor of being the language that
people have spent most resources to avoid writing in.

~~~
hugs
It's probably 2nd place. I imagine avoiding writing in C gets the top honor.

~~~
bklimt
Nope, every single programming language and computing interface is an attempt
to avoid writing machine code. :)

~~~
poizan42
Maybe, but the step away from assembly has been more about portability than
avoiding the language.

------
thegigaraptor
I wish people would take time to actually learn javascript. I used to see it
as a shitty toy language up until 2012. It opened me up to a completely
different style of programming.

~~~
awinter-py
I saw it as a shitty language until I worked with it full time in 2016 after
which I forgot all about clunky syntax and focused on the too-small standard
library and web standards nuttiness.

~~~
gaze
Don't forget the confusing ADHD garbage tooling!

~~~
rubber_duck
I feel like TypeScript could make JS a decent language if Microsoft starts
going more ambitious with it - they really need to do a Dart style integration
- official package manager, build system, full es5 transpiler, std lib
polyfill included.

So far they have been conservative and focused only on typing but they could
offer insane value if they started offering a full language expirience like
Dart

~~~
ludamad
It's an excellent tool. It does make JS a decent language.

A package manager is not necessary, it can work with npm.

A build system is not necessary, existing build systems support it.

I believe they will eventually do full polyfill. However, the compiler is very
high quality and that doesn't happen by accident. They have to make hard
design decisions to keep releasing features so regularly (they release
features faster than I can keep up with sometimes).

For now, you can target es6 and chain with babel.

~~~
mattmanser
It's better than javascript but it's still poor. You still run into wierd this
scoping issues, the JS it actually produces is pretty fugly, there's still no
standard libraries like decent languages have, every property accessor has to
be prefixed with 'this.', etc. etc.

And, ultimately, your code still ends up being a load of garbage boilerplate
code like you're writing oldschool Java from the early 2000s.

It's a nasty language written on top of a nastier one, lipstick on a pig.

~~~
ludamad
The extent to which all your points matter really depends, but I find myself
able to use types just for some logic, and code in a functional style far
removed from oldschool Java with lots of destructuring / structuring. I'm not
denying it's a lipstick pig, but the improving-JavaScript situation is what it
is, and it does a good job at doing it in an unopinionated way (which is why
it's succeeding)

~~~
mattmanser
It all matters a lot. There are great languages available today and typescript
isn't one of them. Why should we be forced to choose sub par languages?

I don't see it as a long term solution. It's just the next coffeescript. It's
part of the problem rather than a solution, the problem being the massive
churn in the JS ecosystem as we desperately hunt for a way of writing JS code
that isn't awful.

The actual solution is to give us other languages in the browser. We need
Python or Ruby or Go or C# or PHP or whatever. We need real choice, not
javascript 2.0.

If Firefox wanted to stay relevant today it could stop dogmatically promoting
a broken language and _that 's_ what they'd be working on right now. They're
too rigidly fixated on the past though.

~~~
gaze
How did PHP end up in that list?

------
mshenfield
I think the most salient application for client-side Python (or Ruby) are for
companies already entrenched in server side frameworks like Django and Ruby on
Rails. For all Django provides, developing in it still feels constrained to a
server heavy mid-2000's web development paradigm. While frameworks like React
and Ember are pushing the envelope on web applications by allowing you to
render HTML apps on the server and on the client, to do something similar in
Python means duplicating rendering logic between a server-side template
language and a front-end framework like Angular, React, or
Backbone/Marionette.

Some technologies tackle a part of this problem, like pybars, which allows
using Handlebars templates in both a python and javascript process. But it
would remove a mountain of mental overhead and code duplication, if feasible,
to write an entire web application in Python.

~~~
BuckRogers
I prefer the web to still be the place where people share HTML documents and
do simple data input/retrieval. Rather than shoving a square peg in a round
hole. I'm rather happy with the state of Django as a result. I suspect Django
Channels will provide all the stateful connectivity most people will need
without tossing the baby out with the bathwater.

And it's not all roses on the other side either, I've looked into the
fullstack solutions that are all JS and promise the world and best future
possible but ended up being lackluster in things like SEO.

------
koehlma
Oh wow, thanks for the great feedback to all of you!

------
thewhitetulip
This will be interesting for codeschool.com and elearning projects, now they
can have everything without requiring the backend at all to evaluate the
commands that the user sends.

------
foota
I'd be interested to see how threading support was implemented, seems
difficult to do in a browser environment.

~~~
chrisseaton
It's easy: you could just run n instructions of thread one, then n
instructions of thread two, and so on in a loop. To not freeze up the browser,
you could periodically yield to it and set a timeout to start running the next
thread again.

~~~
foota
I guess so. I think I was thinking about concurrently running multiple
threads.

~~~
chrisseaton
That is already concurrent. Do you mean in parallel? Yeah not easy to do that,
you'd have to set up web workers and I think sort of manually recreate the
shared state with your own coherence protocol!

------
mrcactu5
are we limited to certain libraries for now? I am guessing we can't do pip
install, but maybe this version of Python can interact with other javascrupt
(e.g. JQuery)?

~~~
koehlma
Most of the builtin types and functions are not fully implemented yet, but if
they are, you may use any library that is implemented in pure Python.

There is already support for threading and I am also planing to implement a
filesystem as well as multiprocessing using WebWorkers. Even sockets will be
possible with node.js.

Then yes, you can do "pip install".

There is also a module that allows you to use native JS libraries like JQuery.

------
andrewvijay
Going through the code and finding new unknown stuffs already!

------
harunurhan
But why?

