From 69862f88de935df6f713f494f8583ce140aa6fb5 Mon Sep 17 00:00:00 2001 From: Linus Schlumberger Date: Sun, 26 Nov 2023 00:17:16 +0100 Subject: [PATCH 1/2] feat: do not use wrapper for python function executions --- js2py/base.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/js2py/base.py b/js2py/base.py index f4ee721c..079f203c 100644 --- a/js2py/base.py +++ b/js2py/base.py @@ -34,7 +34,7 @@ def MakeError(name, message): return JsToPyException(ERRORS[name](Js(message))) -def to_python(val): +def to_python(val, force=False): if not isinstance(val, PyJs): return val if isinstance(val, PyJsUndefined) or isinstance(val, PyJsNull): @@ -55,6 +55,16 @@ def to_python(val): return to_list(val) elif isinstance(val, PyJsObject) and val.CONVERT_TO_PY_PRIMITIVES: return to_dict(val) + elif force: + if val.is_primitive() or val.is_callable(): + return val + # TODO: Maybe support numpy in the future + if val.Class in ('Array', 'Int8Array', 'Uint8Array', + 'Uint8ClampedArray', 'Int16Array', + 'Uint16Array', 'Int32Array', 'Uint32Array', + 'Float32Array', 'Float64Array', 'Arguments'): + return to_list(val) + return to_dict(val) else: return JsObjectWrapper(val) @@ -120,7 +130,7 @@ def HJs(val): @Js def PyWrapper(this, arguments, var=None): - args = tuple(to_python(e) for e in arguments.to_list()) + args = tuple(to_python(e, True) for e in arguments.to_list()) try: py_res = val.__call__(*args) except Exception as e: @@ -1299,7 +1309,7 @@ def put(self, prop, val, op=None, throw=False): return val def __call__(self, *args): - py_args = tuple(to_python(e) for e in args) + py_args = tuple(to_python(e, True) for e in args) try: py_res = self.obj.__call__(*py_args) except Exception as e: @@ -1312,7 +1322,7 @@ def __call__(self, *args): return py_wrap(py_res) def callprop(self, prop, *args): - py_args = tuple(to_python(e) for e in args) + py_args = tuple(to_python(e, True) for e in args) if not isinstance(prop, basestring): prop = prop.to_string().value return self.get(prop)(*py_args) From 40f73b9d0a1e2df07c5bd4438f5a2823be8ee9fc Mon Sep 17 00:00:00 2001 From: Linus Schlumberger Date: Sun, 26 Nov 2023 01:03:19 +0100 Subject: [PATCH 2/2] feat: properly output console --- js2py/base.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/js2py/base.py b/js2py/base.py index 079f203c..229953b7 100644 --- a/js2py/base.py +++ b/js2py/base.py @@ -973,6 +973,14 @@ def __repr__(self): return '{%s}' % ', '.join(res) elif self.Class == 'String': return str_repr(self.value) + elif self.Class == 'Number': + return self.to_string().value + elif self.Class == 'Boolean': + return self.to_string().value + elif self.Class == 'Null': + return 'null' + elif self.Class == 'Undefined': + return 'undefined' elif self.Class in [ 'Array', 'Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array',