From a41222d73215b7302b95e5fc5debfb119bcf8aa7 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Mon, 17 Aug 2026 17:10:16 +0200 Subject: [PATCH 1/2] Add TypedArrayBuffer.__len__ --- README.md | 8 ++++---- src/export_js_proxy.cpp | 1 + tests/tests/test_pyjs.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f22c473..ef7c615 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The old emscripten 3.1.73 based branch is available at https://github.com/emscri pyjs is modern [pybind11](https://github.com/pybind/pybind11) + emscripten [Embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html) based Python <-> JavaScript foreign function interface (FFI) for wasm/emscripten compiled Python. -The API is loosly based on the FFI of [pyodide](https://pyodide.org/en/stable/usage/type-conversions.html). +The API is loosely based on the FFI of [pyodide](https://pyodide.org/en/stable/usage/type-conversions.html). ## Full Documentation See the [documentation](https://emscripten-forge.github.io/pyjs/) for a full documentation. @@ -31,7 +31,7 @@ Access Javascript from Python: ```python import pyjs -# hello world +# hello world pyjs.js.console.log("Hello, World!") # create a JavaScript function to add two numbers @@ -44,7 +44,7 @@ js_function = pyjs.js.Function("a", "b", """ result = js_function(1, 2) ``` -Access Python from Javascript: +Access Python from JavaScript: ```JavaScript // hello world @@ -53,6 +53,6 @@ pyjs.eval("print('Hello, World!')") // eval a python expression and get the result const py_list = pyjs.eval("[i for i in range(10)]") -/// access +// access console.log(py_list.get(0)) // same as py_list[0] on the python side ``` diff --git a/src/export_js_proxy.cpp b/src/export_js_proxy.cpp index e465d6f..8ed623b 100644 --- a/src/export_js_proxy.cpp +++ b/src/export_js_proxy.cpp @@ -298,6 +298,7 @@ namespace pyjs py::class_(m, "TypedArrayBuffer", py::buffer_protocol()) + //.def("__len__", [](const TypedArrayBuffer& self) { return self.m_size; }) .def_buffer([](TypedArrayBuffer& self) -> py::buffer_info { return py::buffer_info(self.m_data, self.m_bytes_per_element, self.m_format_descriptor, diff --git a/tests/tests/test_pyjs.py b/tests/tests/test_pyjs.py index 745d6b3..f229bbf 100644 --- a/tests/tests/test_pyjs.py +++ b/tests/tests/test_pyjs.py @@ -206,6 +206,18 @@ def test_to_py(test_input, expected_type, expected_value, comperator): assert comperator(py_val, expected_value) +@pytest.mark.parametrize( + "test_input,expected_length", + [ + ("new Uint8Array([1,2,3])", 3), + ("new Uint16Array([1,2,3,4])", 4), + ("new Float64Array([])", 0), + ], +) +def test_typed_array_buffer_len(test_input, expected_length): + assert len(pyjs.to_py(ensure_js(test_input))) == expected_length + + @pytest.mark.parametrize( "test_input", [ From c0d400ced73ff4fcfa4bce6891eb839375c61003 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Mon, 17 Aug 2026 17:16:09 +0200 Subject: [PATCH 2/2] Uncomment --- src/export_js_proxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/export_js_proxy.cpp b/src/export_js_proxy.cpp index 8ed623b..76062e3 100644 --- a/src/export_js_proxy.cpp +++ b/src/export_js_proxy.cpp @@ -298,7 +298,7 @@ namespace pyjs py::class_(m, "TypedArrayBuffer", py::buffer_protocol()) - //.def("__len__", [](const TypedArrayBuffer& self) { return self.m_size; }) + .def("__len__", [](const TypedArrayBuffer& self) { return self.m_size; }) .def_buffer([](TypedArrayBuffer& self) -> py::buffer_info { return py::buffer_info(self.m_data, self.m_bytes_per_element, self.m_format_descriptor,