Does Python need Javascript?

Chris Angelico rosuav at gmail.com
Thu Jun 15 03:57:21 EDT 2017


On Thu, Jun 15, 2017 at 5:14 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Now that Java 8 includes a Javascript interpreter (Nashorn) as part of
> the JDK, and since Javascript is The Future™, does Python need a
> Javascript interpreter in the standard library?
>
> If Python came with a Javascript interpreter, what would you do with it?

Okay, now for a serious response.

There've been a number of attempts, over the years, to make a
sandboxed Python interpreter, where you can run untrusted code. But if
Python came with a JS interpreter, it would be possible to run
untrusted JS code, with Python functioning as a gatekeeper. You could
allow JS to require("fs"), but still govern which files it reads and
writes. You could allow JS to use the fetch() function, but still pick
and choose which servers it contacts.

It'd also make a great platform for experimenting with ECMAScript
itself. Imagine if JS-in-Python had some tweakable parameters that
technically violate backward compatibility, but which you can choose
to activate from the Python end. You could, for instance:

* Make strings truly Unicode, instead of UTF-16
* Introduce an arbitrary-precision integer type
* Eliminate the 'var' keyword (just use 'let')
* Add a dictionary type that uses non-string keys
* Etcetera

And possibly most importantly, you could have a high performance data
sharing system between the Python and JS sides. A "native code"
function could accept any JSON-safe object as a parameter, and
directly access its contents (without actually serializing and
deserializing). That would allow efficient bilingual code, where
anything written in Python can be called from JS and vice versa.
Obviously this would depend on the JS objects being internally
represented as Python objects, which may have some minor semantic
differences; my suspicion is that it'd end up being similar to the way
Jython and IronPython behave.

But really, I don't think it needs to be in the standard library. A
pip-installable third-party implementation would be just as effective.

ChrisA



More information about the Python-list mailing list