PiotrDabkowski / Js2Py

JavaScript to Python Translator & JavaScript interpreter written in 100% pure Python🚀 Try it online:
http://piter.io/projects/js2py
MIT License
2.45k stars 259 forks source link

Unexpected token { #287

Open dkanze opened 2 years ago

dkanze commented 2 years ago

I am trying out js2py...converting a JS file to a PY file. I am using Python 3.10. I picked a simple function and get the following error: js2py.internals.simplex.JsException: SyntaxError: Line 1: Unexpected token {

Here is the code:

**Python:**
import js2py
js2py.translate_file('test_js2py.js', 'test_js2py.py')

**Javascript file:**
buildGreen(gameltr, pos) {
    let xpos=0
    xpos = Number(pos)  // needed to jump past left quote
    this.greensql = this.greensql.substring(0,xpos) + gameltr + this.greensql.substring(xpos + 1)
    return
}

What am I missing?

Thanks Dan

ccreater222 commented 2 years ago

function buildGreen(gameltr, pos) { .... }

dkanze commented 2 years ago

Thank you but this was just a snippet of a larger project. My code is within a class and none of my functions are prefixed with "function". Will js2py work differently if a class is parsed?

Thanks Dan

dkanze commented 2 years ago

I did try your suggestion. Here is the output. Nothing I would choose to maintain but thank you.

__all__ = ['test_js2py']

# Don't look below, you will not understand this Python code :) I don't.

from js2py.pyjs import *
# setting scope
var = Scope( JS_BUILTINS )
set_global_object(var)

# Code follows:
var.registers(['buildGreen'])
@Js
def PyJsHoisted_buildGreen_(gameltr, pos, this, arguments, var=var):
    var = Scope({'gameltr':gameltr, 'pos':pos, 'this':this, 'arguments':arguments}, var)
    var.registers(['gameltr', 'xpos', 'pos'])
    var.put('xpos', Js(0.0))
    var.put('xpos', var.get('Number')(var.get('pos')))
    var.get(u"this").put('greensql', ((var.get(u"this").get('greensql').callprop('substring', Js(0.0), var.get('xpos'))+var.get('gameltr'))+var.get(u"this").get('greensql').callprop('substring', (var.get('xpos')+Js(1.0)))))
    return var.get('undefined')
PyJsHoisted_buildGreen_.func_name = 'buildGreen'
var.put('buildGreen', PyJsHoisted_buildGreen_)
pass
pass

# Add lib to the module scope
test_js2py = var.to_python()