Repository: pyodide/pyodide

Issue:
Minor fix: add parameter for shallowCopyToJavascript 
Follow #1202 




Discussion:
Thanks @daoxian. I'm thinking about removing this API entirely since this makes it the same as `deepCopyToJavascript` but with a different default.
> I'm thinking about removing this API entirely since this makes it the same as `deepCopyToJavascript` but with a different default.

Will this be part of #1192, with [`Pyproxy.intoJs()` with a deep-parameter](https://github.com/iodide-project/pyodide/issues/1192#issuecomment-773536247)?


@phorward Exactly, yes.

Resolving patch:
diff --git a/conftest.py b/conftest.py
--- a/conftest.py
+++ b/conftest.py
@@ -98,8 +98,8 @@ def run(self, code):
         return self.run_js(
             f"""
             let result = pyodide.runPython({code!r});
-            if(result && result.deepCopyToJavascript){{
-                let converted_result = result.deepCopyToJavascript();
+            if(result && result.toJs){{
+                let converted_result = result.toJs();
                 result.destroy();
                 return converted_result;
             }}
@@ -111,8 +111,8 @@ def run_async(self, code):
         return self.run_js(
             f"""
             let result = await pyodide.runPythonAsync({code!r});
-            if(result && result.deepCopyToJavascript){{
-                let converted_result = result.deepCopyToJavascript();
+            if(result && result.toJs){{
+                let converted_result = result.toJs();
                 result.destroy();
                 return converted_result;
             }}


Test patch:
diff --git a/packages/numpy/test_numpy.py b/packages/numpy/test_numpy.py
--- a/packages/numpy/test_numpy.py
+++ b/packages/numpy/test_numpy.py
@@ -2,16 +2,12 @@ def test_numpy(selenium):
     selenium.load_package("numpy")
     selenium.run("import numpy")
     selenium.run("x = numpy.ones((32, 64))")
-    assert selenium.run_js(
-        "return pyodide.pyimport('x').deepCopyToJavascript().length == 32"
-    )
+    assert selenium.run_js("return pyodide.pyimport('x').toJs().length == 32")
     for i in range(32):
-        assert selenium.run_js(
-            f"return pyodide.pyimport('x').deepCopyToJavascript()[{i}].length == 64"
-        )
+        assert selenium.run_js(f"return pyodide.pyimport('x').toJs()[{i}].length == 64")
         for j in range(64):
             assert selenium.run_js(
-                f"return pyodide.pyimport('x').deepCopyToJavascript()[{i}][{j}] == 1"
+                f"return pyodide.pyimport('x').toJs()[{i}][{j}] == 1"
             )
 
 
@@ -55,7 +51,7 @@ def assert_equal():
                 for k in range(2):
                     assert (
                         selenium.run_js(
-                            f"return pyodide.pyimport('x').deepCopyToJavascript()[{i}][{j}][{k}]"
+                            f"return pyodide.pyimport('x').toJs()[{i}][{j}][{k}]"
                         )
                         == expected_result[i][j][k]
                     )
@@ -82,7 +78,7 @@ def assert_equal():
             )
             assert_equal()
             classname = selenium.run_js(
-                "return pyodide.pyimport('x').deepCopyToJavascript()[0][0].constructor.name"
+                "return pyodide.pyimport('x').toJs()[0][0].constructor.name"
             )
             if order == "C" and dtype not in ("uint64", "int64"):
                 # Here we expect a TypedArray subclass, such as Uint8Array, but
@@ -98,7 +94,7 @@ def assert_equal():
             )
             assert_equal()
             classname = selenium.run_js(
-                "return pyodide.pyimport('x').deepCopyToJavascript()[0][0].constructor.name"
+                "return pyodide.pyimport('x').toJs()[0][0].constructor.name"
             )
             if order == "C" and dtype in ("int8", "uint8"):
                 # Here we expect a TypedArray subclass, such as Uint8Array, but
@@ -112,18 +108,9 @@ def assert_equal():
     assert selenium.run("np.array([True, False])") == [True, False]
 
     selenium.run("x = np.array([['string1', 'string2'], ['string3', 'string4']])")
-    assert (
-        selenium.run_js("return pyodide.pyimport('x').deepCopyToJavascript().length")
-        == 2
-    )
-    assert (
-        selenium.run_js("return pyodide.pyimport('x').deepCopyToJavascript()[0][0]")
-        == "string1"
-    )
-    assert (
-        selenium.run_js("return pyodide.pyimport('x').deepCopyToJavascript()[1][1]")
-        == "string4"
-    )
+    assert selenium.run_js("return pyodide.pyimport('x').toJs().length") == 2
+    assert selenium.run_js("return pyodide.pyimport('x').toJs()[0][0]") == "string1"
+    assert selenium.run_js("return pyodide.pyimport('x').toJs()[1][1]") == "string4"
 
 
 def test_py2js_buffer_clear_error_flag(selenium):
@@ -194,7 +181,7 @@ def test_runpythonasync_numpy(selenium_standalone):
     )
     for i in range(5):
         assert selenium_standalone.run_js(
-            f"return pyodide.pyimport('x').deepCopyToJavascript()[{i}] == 0"
+            f"return pyodide.pyimport('x').toJs()[{i}] == 0"
         )
 
 
diff --git a/src/tests/test_jsproxy.py b/src/tests/test_jsproxy.py
--- a/src/tests/test_jsproxy.py
+++ b/src/tests/test_jsproxy.py
@@ -12,7 +12,7 @@ def test_jsproxy_dir(selenium):
             from js import a
             from js import b
             [dir(a), dir(b)]
-        `).deepCopyToJavascript();
+        `).toJs();
         """
     )
     jsproxy_items = set(
@@ -49,7 +49,7 @@ def test_jsproxy_getattr(selenium):
         return pyodide.runPython(`
             from js import a
             [ a.x, a.y, a.typeof ]
-        `).deepCopyToJavascript();
+        `).toJs();
         """
         )
         == [2, "9", "object"]
@@ -195,7 +195,7 @@ def test_jsproxy_call(selenium):
             from js import f
             [f(*range(n)) for n in range(10)]
             `
-        ).deepCopyToJavascript();
+        ).toJs();
         """
         )
         == list(range(10))
@@ -379,7 +379,7 @@ def test_mount_object(selenium):
             import b
             result += [a.s, dir(a), dir(b)]
             result
-        `).deepCopyToJavascript()
+        `).toJs()
         """
     )
     assert result[:3] == ["x1", "x2", 3]
diff --git a/src/tests/test_pyproxy.py b/src/tests/test_pyproxy.py
--- a/src/tests/test_pyproxy.py
+++ b/src/tests/test_pyproxy.py
@@ -55,8 +55,7 @@ def get_value(self, value):
             "apply",
             "destroy",
             "$$",
-            "deepCopyToJavascript",
-            "shallowCopyToJavascript",
+            "toJs",
         ]
     )
     assert selenium.run("hasattr(f, 'baz')")
diff --git a/src/tests/test_typeconversions.py b/src/tests/test_typeconversions.py
--- a/src/tests/test_typeconversions.py
+++ b/src/tests/test_typeconversions.py
@@ -22,14 +22,14 @@ def test_python2js(selenium):
     )
     assert selenium.run_js(
         """
-        let x = pyodide.runPython("[1, 2, 3]").deepCopyToJavascript();
+        let x = pyodide.runPython("[1, 2, 3]").toJs();
         return ((x instanceof window.Array) && (x.length === 3) &&
                 (x[0] == 1) && (x[1] == 2) && (x[2] == 3))
         """
     )
     assert selenium.run_js(
         """
-        let x = pyodide.runPython("{42: 64}").deepCopyToJavascript();
+        let x = pyodide.runPython("{42: 64}").toJs();
         return (typeof x === "object") && (x[42] === 64)
         """
     )
@@ -236,7 +236,7 @@ def test_recursive_list_to_js(selenium_standalone):
         x.append(x)
         """
     )
-    selenium_standalone.run_js("x = pyodide.pyimport('x').deepCopyToJavascript();")
+    selenium_standalone.run_js("x = pyodide.pyimport('x').toJs();")
 
 
 def test_recursive_dict_to_js(selenium_standalone):
@@ -246,7 +246,7 @@ def test_recursive_dict_to_js(selenium_standalone):
         x[0] = x
         """
     )
-    selenium_standalone.run_js("x = pyodide.pyimport('x').deepCopyToJavascript();")
+    selenium_standalone.run_js("x = pyodide.pyimport('x').toJs();")
 
 
 def test_list_js2py2js(selenium):