Skip to content

Commit 6949eb2

Browse files
authored
fix to_js converter for bytes (#110)
* literal map * fix bytes * add upper bound to pybind11
1 parent 08bfc5e commit 6949eb2

File tree

7 files changed

+12
-7
lines changed

7 files changed

+12
-7
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
include:
2424
- emsdk_ver: "3.1.73"
2525
python_version: "3.13"
26-
pybind11_version: ""
26+
pybind11_version: "<3"
2727
steps:
2828
- uses: actions/checkout@v2
2929

build_mkdocs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if [ ! -d "$WASM_ENV_PREFIX" ]; then
2121
-c https://repo.prefix.dev/emscripten-forge-dev\
2222
-c https://repo.prefix.dev/conda-forge \
2323
--yes \
24-
python=$PYTHON_VERSION "pybind11" nlohmann_json pybind11_json numpy \
24+
python=$PYTHON_VERSION "pybind11<3" nlohmann_json pybind11_json numpy \
2525
bzip2 sqlite zlib zstd libffi exceptiongroup\
2626
"xeus" "xeus-lite" xeus-python "xeus-javascript" xtl "ipython" "traitlets>=5.14.2" \
2727
openssl liblzma

include/pyjs/convert.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ namespace pyjs
5656

5757

5858
em::val py_1d_buffer_to_typed_array(py::buffer buffer, bool view);
59-
em::val bytes_to_js(char* data);
59+
em::val bytes_to_js(char* data, std::size_t length);
6060

6161
}

module/pyjs/convert_py_to_js.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def to_js(value, cache=None, depth=0, max_depth=None):
8080

8181
# # bytestring
8282
elif isinstance(value, bytes):
83-
return internal.bytes_to_typed_array(value).buffer
83+
l = len(value)
84+
return internal.bytes_to_typed_array(value, l).buffer
8485

8586
elif hasattr(value, "explicit_to_js"):
8687
return value.explicit_to_js()

src/convert.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,9 @@ namespace pyjs
205205
}
206206

207207

208-
em::val bytes_to_js(char * binary_string)
208+
em::val bytes_to_js(char * binary_string, std::size_t length)
209209
{
210210
// get the length of the string
211-
std::size_t length = std::strlen(binary_string);
212211
em::val mem_view = em::val(em::typed_memory_view(length, binary_string));
213212
em::val mem_copy = em::val::global("Uint8Array").new_(mem_view);
214213
return mem_copy;

src/js_timestamp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define PYJS_JS_UTC_TIMESTAMP "2025-07-04 07:29:10.089112"
1+
#define PYJS_JS_UTC_TIMESTAMP "2025-09-01 06:22:09.224255"

tests/tests/test_pyjs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ def test_to_js_dict():
246246
assert jsmap.get(1) == "a"
247247

248248

249+
def test_bytes_to_js():
250+
pyjs.to_js(b"\x00").byteLength == 1
251+
252+
253+
249254
def test_to_js_none():
250255

251256
jsval = pyjs.to_js(None)

0 commit comments

Comments
 (0)