From 12e3eead77c65ce8635061bc0e13b442809399ac Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Wed, 26 Apr 2023 23:27:17 +0000 Subject: [PATCH 1/3] test(callJSFunc): write tests for BF-58 --- tests/python/test_pythonmonkey_eval.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/python/test_pythonmonkey_eval.py b/tests/python/test_pythonmonkey_eval.py index fa06e497..76ef7fa3 100644 --- a/tests/python/test_pythonmonkey_eval.py +++ b/tests/python/test_pythonmonkey_eval.py @@ -600,6 +600,15 @@ def add(a, b): int2 = random.randint(0x0000, 0xFFFF) assert caller(add, int1, int2) == int1 + int2 +def test_eval_functions_pyfunction_in_closure(): + # BF-58 https://github.com/Distributive-Network/PythonMonkey/pull/19 + def fn1(): + def fn0(n): + return n + 100 + return fn0 + assert 101.9 == fn1()(1.9) + assert 101.9 == pm.eval("(fn1) => { return fn1 }")(fn1())(1.9) + def test_eval_functions_pyfunctions_strs(): caller = pm.eval("(func, param1, param2) => { return func(param1, param2) }") def concatenate(a, b): From 5d0485df9d9df8a47677ac6baf68d02407d1bddf Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Wed, 26 Apr 2023 23:32:06 +0000 Subject: [PATCH 2/3] fix(callJSFunc): JS cannot call back Python functions defined in a closure, since the Py function is already GCed --- src/pyTypeFactory.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pyTypeFactory.cc b/src/pyTypeFactory.cc index dc6135bf..b18c333c 100644 --- a/src/pyTypeFactory.cc +++ b/src/pyTypeFactory.cc @@ -154,7 +154,9 @@ static PyObject *callJSFunc(PyObject *jsCxThisFuncTuple, PyObject *args) { JS::RootedVector jsArgsVector(cx); for (size_t i = 0; i < PyTuple_Size(args); i++) { - JS::Value jsValue = jsTypeFactory(cx, PyTuple_GetItem(args, i)); + PyObject *pyObj = PyTuple_GetItem(args, i); + Py_INCREF(pyObj); + JS::Value jsValue = jsTypeFactory(cx, pyObj); if (PyErr_Occurred()) { // Check if an exception has already been set in the flow of control return NULL; // Fail-fast } From 45a3d6ca0a06440983ebde51cd992fb3ac078329 Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Wed, 26 Apr 2023 23:43:54 +0000 Subject: [PATCH 3/3] test(callJSFunc): write tests for BF-58 --- tests/python/test_pythonmonkey_eval.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/python/test_pythonmonkey_eval.py b/tests/python/test_pythonmonkey_eval.py index 76ef7fa3..4222ac49 100644 --- a/tests/python/test_pythonmonkey_eval.py +++ b/tests/python/test_pythonmonkey_eval.py @@ -608,6 +608,8 @@ def fn0(n): return fn0 assert 101.9 == fn1()(1.9) assert 101.9 == pm.eval("(fn1) => { return fn1 }")(fn1())(1.9) + assert 101.9 == pm.eval("(fn1, x) => { return fn1()(x) }")(fn1, 1.9) + assert 101.9 == pm.eval("(fn1) => { return fn1() }")(fn1)(1.9) def test_eval_functions_pyfunctions_strs(): caller = pm.eval("(func, param1, param2) => { return func(param1, param2) }")