Skip to content

Commit

Permalink
bytes converter, better pyodide polyfill (#55)
Browse files Browse the repository at this point in the history
* added bytes converter
* improved pyodide polyfill by using correct error messages
  • Loading branch information
DerThorsten authored Feb 14, 2024
1 parent d1db2fa commit e1dc468
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(PYCPPSCRIPTS
error_handling.py
extend_js_val.py
webloop.py
pyodide_polyfill.py
)

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pycpp_includes)
Expand Down
1 change: 1 addition & 0 deletions include/pyjs/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ namespace pyjs


em::val py_1d_buffer_to_typed_array(py::buffer buffer, bool view);
em::val bytes_to_js(char* data);

}
6 changes: 6 additions & 0 deletions include/pyjs/pycpp/convert_py_to_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,11 @@ def to_js(value, cache=None, depth=0, max_depth=None):
return js_undefined()
elif isinstance(value, (int, float, str, bool)):
return JsValue(value)

# # bytestring
elif isinstance(value, bytes):
return internal.bytes_to_typed_array(value).buffer


else:
raise RuntimeError(f"no registerd converted for {value} of type {type(value)}")
21 changes: 2 additions & 19 deletions include/pyjs/pycpp/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ast



def install_submodules():
def _js_mod__getattr__(name: str) -> Any:
ret = internal.global_property(name)
Expand All @@ -25,29 +26,11 @@ def _module_mod__getattr__(name: str) -> Any:
_module = sys.modules["pyjs._module"] = types.ModuleType("_module")
_module.__getattr__ = _module_mod__getattr__

# Expose a small pyodide polyfill
def _pyodide__getattr__(name: str) -> Any:
if name == "to_js":
from pyjs import to_js
return to_js

raise AttributeError(
"This is not the real Pyodide. We are providing a small Pyodide polyfill for conveniance."
"If you are missing an important Pyodide feature, please open an issue in https://github.com/emscripten-forge/pyjs/issues"
)

pyodide = sys.modules["pyodide"] = types.ModuleType("pyodide")
pyodide.ffi = sys.modules["pyodide.ffi"] = types.ModuleType("ffi")
pyodide.ffi.JsException = RuntimeError
pyodide.ffi.JsArray = object
pyodide.ffi.JsProxy = object
pyodide.__getattr__ = _pyodide__getattr__
pyodide.ffi.__getattr__ = _pyodide__getattr__


install_submodules()
del install_submodules


js = sys.modules["pyjs.js"]
_module = sys.modules["pyjs._module"]

Expand Down
26 changes: 26 additions & 0 deletions include/pyjs/pycpp/pyodide_polyfill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


def install_pyodide_polyfill():
# Expose a small pyodide polyfill
def _pyodide__getattr__(name: str) -> Any:
if name == "to_js":
from pyjs import to_js
return to_js

raise AttributeError(
"This is not the real Pyodide. We are providing a small Pyodide polyfill for conveniance."
"If you are missing an important Pyodide feature, please open an issue in https://github.com/emscripten-forge/pyjs/issues"
)

pyodide = sys.modules["pyodide"] = types.ModuleType("pyodide")
pyodide.ffi = sys.modules["pyodide.ffi"] = types.ModuleType("ffi")
pyodide.ffi.JsException = JsException
pyodide.ffi.JsArray = object
pyodide.ffi.JsProxy = object
pyodide.__getattr__ = _pyodide__getattr__
pyodide.ffi.__getattr__ = _pyodide__getattr__



install_pyodide_polyfill()
del install_pyodide_polyfill
10 changes: 10 additions & 0 deletions src/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ namespace pyjs
}


em::val bytes_to_js(char * binary_string)
{
// get the length of the string
std::size_t length = std::strlen(binary_string);
em::val mem_view = em::val(em::typed_memory_view(length, binary_string));
em::val mem_copy = em::val::global("Uint8Array").new_(mem_view);
return mem_copy;
}

template <class T>
em::val py_1d_buffer_to_typed_array_t(const std::size_t size,
Expand All @@ -215,6 +223,8 @@ namespace pyjs
}




em::val py_1d_buffer_to_typed_array(py::buffer buffer, bool view)
{
/* Request a buffer descriptor from Python */
Expand Down
1 change: 1 addition & 0 deletions src/export_js_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ namespace pyjs


m_internal.def("py_1d_buffer_to_typed_array", &py_1d_buffer_to_typed_array);
m_internal.def("bytes_to_typed_array", &bytes_to_js);

}

Expand Down
3 changes: 2 additions & 1 deletion src/export_pyjs_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void pyjs_error_handling_pseudo_init(py::module_&);
void pyjs_convert_pseudo_init(py::module_&);
void pyjs_convert_py_to_js_pseudo_init(py::module_&);
void pyjs_webloop_pseudo_init(py::module_&);

void pyjs_pyodide_polyfill_pseudo_init(py::module_&);

namespace pyjs
{
Expand All @@ -36,6 +36,7 @@ namespace pyjs
pyjs_convert_pseudo_init(pyjs_module);
pyjs_convert_py_to_js_pseudo_init(pyjs_module);
pyjs_webloop_pseudo_init(pyjs_module);
pyjs_pyodide_polyfill_pseudo_init(pyjs_module);
}
catch (py::error_already_set& e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/js_timestamp.cpp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define PYJS_JS_UTC_TIMESTAMP "2024-02-05 11:33:27.581566"
#define PYJS_JS_UTC_TIMESTAMP "2024-02-14 09:09:18.446638"

0 comments on commit e1dc468

Please sign in to comment.