From c924ecff1a1cb0432da7767cecd418968c49da80 Mon Sep 17 00:00:00 2001 From: Joash Mathew Date: Thu, 13 Jul 2023 15:05:12 -0400 Subject: [PATCH 1/8] feat: add a test for sending a JS object to Python and getting it back works --- tests/js/js2py/object.simple | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/js/js2py/object.simple diff --git a/tests/js/js2py/object.simple b/tests/js/js2py/object.simple new file mode 100644 index 00000000..b4f3d6be --- /dev/null +++ b/tests/js/js2py/object.simple @@ -0,0 +1,20 @@ +/** + * @file js2py/object.simple + * Simple test which shows that sending objects to Python and getting them back into JS + * works as expected. + * @author Joash Mathew, + * @date July 2023 + */ +'use strict'; + +const objJs = { a: 1, b: 2, c: 3 }; +const throughPython = python.eval('(lambda x: x)'); +const objPy = throughPython(objJs); + +if (objJs !== objPy) +{ + console.error(`Expected ${objJs} but got ${objPy}`); + throw new Error('Test failed'); +} + +console.log('Test passed'); \ No newline at end of file From 67fa17ca5477f684c80ca95da5725aef8d6e504d Mon Sep 17 00:00:00 2001 From: Joash Mathew Date: Mon, 17 Jul 2023 08:43:15 -0400 Subject: [PATCH 2/8] feat: Add test sending object from Python to JS, and back to Python --- tests/js/py2js/object.simple | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/js/py2js/object.simple diff --git a/tests/js/py2js/object.simple b/tests/js/py2js/object.simple new file mode 100644 index 00000000..d76c1771 --- /dev/null +++ b/tests/js/py2js/object.simple @@ -0,0 +1,20 @@ +/** + * @file py2js/object.simple + * Simple test which shows that sending objects to JS and getting them back into Python + * works as expected. + * @author Joash Mathew, + * @date July 2023 + */ +'use strict'; + +const obj = python.eval('"{a: 1, b: 2, c: 3}"'); +const throughJS = (x) => x; +const jsObj = throughJS(obj); + +if (jsObj !== obj) +{ + console.error('expected ', obj, ' but got ', jsObj); + throw new Error('Test failed'); +} + +console.log('Test passed'); \ No newline at end of file From 59ef953582f0ca32b52ed9629932b40b06f66ead Mon Sep 17 00:00:00 2001 From: Joash Mathew Date: Mon, 17 Jul 2023 08:45:42 -0400 Subject: [PATCH 3/8] feat: Add a test to check if a dictionary converted to JS can have Object methods performed on it --- tests/js/py2js/object-methods.simple | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/js/py2js/object-methods.simple diff --git a/tests/js/py2js/object-methods.simple b/tests/js/py2js/object-methods.simple new file mode 100644 index 00000000..a3fd08d4 --- /dev/null +++ b/tests/js/py2js/object-methods.simple @@ -0,0 +1,34 @@ +/** + * @file py2js/object.simple + * Simple test which shows that sending objects from Python to JS still retains + * the ability to perform basic Object methods like Object.keys(), Object.values() and + * Object.entries() on them. + * @author Joash Mathew, + * @date July 2023 + */ +'use strict'; + +const obj = python.eval('{ "a": 1, "b": 2, "c": 3 }'); +const throughJS = (x) => x; +const jsObj = throughJS(obj); +const standardJSObj = { a: 1, b: 2, c: 3 }; + +if (Object.keys(jsObj) !== Object.keys(standardJSObj)) +{ + console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.'); + throw new Error('Test failed'); +} + +if (Object.values(jsObj) !== Object.values(standardJSObj)) +{ + console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.'); + throw new Error('Test failed'); +} + +if (Object.entries(jsObj) !== Object.entries(standardJSObj)) +{ + console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.'); + throw new Error('Test failed'); +} + +console.log('Test passed'); \ No newline at end of file From 65ed2eb3047ca0a6149657e7bf010eec7a38d374 Mon Sep 17 00:00:00 2001 From: Joash Mathew Date: Mon, 17 Jul 2023 08:46:33 -0400 Subject: [PATCH 4/8] feat: Add a test to verify that JS => Python are sharing memory and can be modified Co-authored-by: Will Pringle --- tests/js/js2py/object-mutation.simple | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/js/js2py/object-mutation.simple diff --git a/tests/js/js2py/object-mutation.simple b/tests/js/js2py/object-mutation.simple new file mode 100644 index 00000000..2442f9ad --- /dev/null +++ b/tests/js/js2py/object-mutation.simple @@ -0,0 +1,37 @@ +/** + * @file js2py/object-mutation.simple + * Simple test which shows that converting objects from JS => Python uses + * shared memory, and changes made in either language will affect the object + * in the other. + * @author Joash Mathew, + * @date July 2023 + */ +'use strict'; + +const obj = { a: 1 }; +const pcode = ` +def change_and_return(obj): + obj["a"] = 5; + return obj; +`; + +python.exec(pcode); + +const fun = python.eval("change_and_return"); +const obj2 = fun(obj); + +if (obj.a !== 5 && obj2["a"] !== 5) +{ + console.error('Object isn\'t sharing memory.'); + throw new Error('Test failed'); +} + +obj.a = 1000; + +if (obj.a !== 1000 && obj2["a"] !== 1000) +{ + console.error('Object isn\'t sharing memory.'); + throw new Error('Test failed'); +} + +console.log('Test passed'); \ No newline at end of file From e0db892196ca8696d9f1b6f962073ee5dd0cbd76 Mon Sep 17 00:00:00 2001 From: Joash Mathew Date: Mon, 17 Jul 2023 08:55:22 -0400 Subject: [PATCH 5/8] chore: mark object-methods test as failing --- .../{object-methods.simple => object-methods.simple.failing} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/js/py2js/{object-methods.simple => object-methods.simple.failing} (100%) diff --git a/tests/js/py2js/object-methods.simple b/tests/js/py2js/object-methods.simple.failing similarity index 100% rename from tests/js/py2js/object-methods.simple rename to tests/js/py2js/object-methods.simple.failing From eb9646a73932be8685d5efbfb0a8735d5e0e1a80 Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Thu, 3 Aug 2023 00:00:51 +0000 Subject: [PATCH 6/8] test(PyProxyHandler): fix tests/js/py2js/object.simple --- tests/js/py2js/object.simple | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/js/py2js/object.simple b/tests/js/py2js/object.simple index d76c1771..2bebadad 100644 --- a/tests/js/py2js/object.simple +++ b/tests/js/py2js/object.simple @@ -7,7 +7,7 @@ */ 'use strict'; -const obj = python.eval('"{a: 1, b: 2, c: 3}"'); +const obj = python.eval('{"a": 1, "b": 2, "c": 3}'); const throughJS = (x) => x; const jsObj = throughJS(obj); From 43ad37467912bf3442ab91716994758454733294 Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Thu, 3 Aug 2023 00:01:46 +0000 Subject: [PATCH 7/8] test(PyProxyHandler): fix tests/js/py2js/object-methods.simple `==` only compares if the two JS objects have the same memory address. --- ...{object-methods.simple.failing => object-methods.simple} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename tests/js/py2js/{object-methods.simple.failing => object-methods.simple} (78%) diff --git a/tests/js/py2js/object-methods.simple.failing b/tests/js/py2js/object-methods.simple similarity index 78% rename from tests/js/py2js/object-methods.simple.failing rename to tests/js/py2js/object-methods.simple index a3fd08d4..8219d227 100644 --- a/tests/js/py2js/object-methods.simple.failing +++ b/tests/js/py2js/object-methods.simple @@ -13,19 +13,19 @@ const throughJS = (x) => x; const jsObj = throughJS(obj); const standardJSObj = { a: 1, b: 2, c: 3 }; -if (Object.keys(jsObj) !== Object.keys(standardJSObj)) +if (JSON.stringify(Object.keys(jsObj)) !== JSON.stringify(Object.keys(standardJSObj))) { console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.'); throw new Error('Test failed'); } -if (Object.values(jsObj) !== Object.values(standardJSObj)) +if (JSON.stringify(Object.values(jsObj)) !== JSON.stringify(Object.values(standardJSObj))) { console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.'); throw new Error('Test failed'); } -if (Object.entries(jsObj) !== Object.entries(standardJSObj)) +if (JSON.stringify(Object.entries(jsObj)) !== JSON.stringify(Object.entries(standardJSObj))) { console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.'); throw new Error('Test failed'); From 74094809aca027610a2bd5b4a1a217c0d548778b Mon Sep 17 00:00:00 2001 From: Tom Wenzheng Tang Date: Thu, 10 Aug 2023 17:33:46 -0400 Subject: [PATCH 8/8] test(PyProxyHandler): fix tests/js/js2py/object-mutation.simple --- tests/js/js2py/object-mutation.simple | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/js/js2py/object-mutation.simple b/tests/js/js2py/object-mutation.simple index 2442f9ad..2952efdb 100644 --- a/tests/js/js2py/object-mutation.simple +++ b/tests/js/js2py/object-mutation.simple @@ -20,7 +20,7 @@ python.exec(pcode); const fun = python.eval("change_and_return"); const obj2 = fun(obj); -if (obj.a !== 5 && obj2["a"] !== 5) +if (obj.a !== 5 || obj2["a"] !== 5) { console.error('Object isn\'t sharing memory.'); throw new Error('Test failed'); @@ -28,7 +28,7 @@ if (obj.a !== 5 && obj2["a"] !== 5) obj.a = 1000; -if (obj.a !== 1000 && obj2["a"] !== 1000) +if (obj.a !== 1000 || obj2["a"] !== 1000) { console.error('Object isn\'t sharing memory.'); throw new Error('Test failed');