amol- / dukpy

Simple JavaScript interpreter for Python
MIT License
479 stars 43 forks source link

Dependency update #48

Closed qwenger closed 4 years ago

qwenger commented 4 years ago

Hi,

Thanks for developing and maintaining dukpy!

I stumbled into a situation that makes me think that dukpy needs to update its dependencies:

I need to babel-transpile KaTeX and noticed that this does not currently work with dukpy; I get the following error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 49: invalid continuation byte

This is caused by a regex in KaTeX (https://github.com/KaTeX/KaTeX/blob/master/src/Lexer.js#L39) containing a unicode code point. Babel 6.14 - as currently used in dukpy - wrongly transforms it into the code point literal. (An interesting point is that the error is raised "by chance", because this code point happens to represent a unicode surrogate, which Python's utf-8 disallows decoding.)

This is clearly a bug in babel 6.14, that was later fixed. Modifying dukpy to use the latest babel-standalone 6.x (currently 6.26, e.g. from https://github.com/babel/babel-standalone/releases/download/release-6.26.0/babel.min.js) fixes this issue.

A second problem arises when one tries to use dukpy with the newer babel-standalone 7.x: duktape then fails:

  File "/home/.../.local/share/virtualenvs/fsjm-django/lib/python3.7/site-packages/dukpy/evaljs.py", line 136, in evaljs
    return JSInterpreter().evaljs(code, **kwargs)
  File "/home/.../.local/share/virtualenvs/fsjm-django/lib/python3.7/site-packages/dukpy/evaljs.py", line 57, in evaljs
    res = _dukpy.eval_string(self, jscode, jsvars)
_dukpy.JSRuntimeError: RangeError: register limit (line 1)
    src/pyduktape.c:1
    duk_js_compiler.c:1376

I did not try to make dukpy use a newer duktape release.

So:

Thanks!

amol- commented 4 years ago

If you want to submit a PR for bumping the integrated babel, I'll gladly merge it. There is also an open branch with the upgrade to duktape 2.3: https://github.com/amol-/dukpy/pull/46 you might want to try if with that one babel 7 works or if it's not enough.

qwenger commented 4 years ago

1) I tried the duktape2 branch (#46) with @babel/standalone/ v7.7. It required a lot of polyfills (Set, WeakMap, String.prototype.trimRight, ...) and even then transpilation just hanged. So it seems that no, duktape (and thus dukpy, even in the new branch) is not ready for modern babel.

2) Bumping babel-standalone in the 6.x release span is really easy, only a matter of replacing https://github.com/amol-/dukpy/tree/master/dukpy/jsmodules/babel-6.14.0.min.js by https://github.com/babel/babel-standalone/releases/download/release-6.26.0/babel.min.js and updating the line https://github.com/amol-/dukpy/blob/master/dukpy/babel.py#L4 accordingly.