From aa2e3880c13036c65aa3381ed5f104c59ebad86e Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 14 May 2024 21:56:36 +1000 Subject: [PATCH] webassembly/proxy_js: Create a special "undefined" type for Python. This adds a new undefined singleton to Python, that corresponds directly to JavaScript `undefined`. It's accessible via `js.undefined`. Signed-off-by: Damien George --- ports/webassembly/README.md | 12 ++++++++++++ ports/webassembly/proxy_c.c | 15 ++++++++++++++- ports/webassembly/proxy_js.js | 2 +- .../webassembly/await_error_handling.mjs.exp | 2 +- .../ports/webassembly/jsffi_create_proxy.mjs.exp | 2 +- tests/ports/webassembly/jsffi_to_js.mjs.exp | 2 +- tests/ports/webassembly/register_js_module.js.exp | 2 +- tests/ports/webassembly/run_python_async.mjs.exp | 8 ++++---- 8 files changed, 35 insertions(+), 10 deletions(-) diff --git a/ports/webassembly/README.md b/ports/webassembly/README.md index af395778a7..97cb397fd4 100644 --- a/ports/webassembly/README.md +++ b/ports/webassembly/README.md @@ -160,6 +160,18 @@ context, created and returned by `loadMicroPython()`. - `replProcessCharWithAsyncify(chr)`: process an incoming character at the REPL, for use when ASYNCIFY is enabled. +Type conversions +---------------- + +Read-only objects (booleanns, numbers, strings, etc) are converted when passed between +Python and JavaScript. The conversions are: + +- JavaScript `null` converts to/from Python `None`. +- JavaScript `undefined` converts to/from Python `js.undefined`. + +The conversion between `null` and `None` matches the behaviour of the Python `json` +module. + Proxying -------- diff --git a/ports/webassembly/proxy_c.c b/ports/webassembly/proxy_c.c index f8d6d85303..b55740175b 100644 --- a/ports/webassembly/proxy_c.c +++ b/ports/webassembly/proxy_c.c @@ -59,6 +59,16 @@ enum { PROXY_KIND_JS_PYPROXY = 7, }; +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_undefined, + MP_QSTR_undefined, + MP_TYPE_FLAG_NONE + ); + +static const mp_obj_base_t mp_const_undefined_obj = {&mp_type_undefined}; + +#define mp_const_undefined (MP_OBJ_FROM_PTR(&mp_const_undefined_obj)) + MP_DEFINE_EXCEPTION(JsException, Exception) void proxy_c_init(void) { @@ -80,7 +90,7 @@ static inline mp_obj_t proxy_c_get_obj(uint32_t c_ref) { mp_obj_t proxy_convert_js_to_mp_obj_cside(uint32_t *value) { if (value[0] == PROXY_KIND_JS_UNDEFINED) { - return mp_const_none; + return mp_const_undefined; } else if (value[0] == PROXY_KIND_JS_NULL) { return mp_const_none; } else if (value[0] == PROXY_KIND_JS_BOOLEAN) { @@ -122,6 +132,9 @@ void proxy_convert_mp_to_js_obj_cside(mp_obj_t obj, uint32_t *out) { const char *str = mp_obj_str_get_data(obj, &len); out[1] = len; out[2] = (uintptr_t)str; + } else if (obj == mp_const_undefined) { + kind = PROXY_KIND_MP_JSPROXY; + out[1] = 1; } else if (mp_obj_is_jsproxy(obj)) { kind = PROXY_KIND_MP_JSPROXY; out[1] = mp_obj_jsproxy_get_ref(obj); diff --git a/ports/webassembly/proxy_js.js b/ports/webassembly/proxy_js.js index f1f273d6b3..2a34f01a7b 100644 --- a/ports/webassembly/proxy_js.js +++ b/ports/webassembly/proxy_js.js @@ -56,7 +56,7 @@ class PythonError extends Error { } function proxy_js_init() { - globalThis.proxy_js_ref = [globalThis]; + globalThis.proxy_js_ref = [globalThis, undefined]; } function proxy_call_python(target, argumentsList) { diff --git a/tests/ports/webassembly/await_error_handling.mjs.exp b/tests/ports/webassembly/await_error_handling.mjs.exp index 149f8914d4..0c8eb52f20 100644 --- a/tests/ports/webassembly/await_error_handling.mjs.exp +++ b/tests/ports/webassembly/await_error_handling.mjs.exp @@ -1,5 +1,5 @@ 1 2 -(, 'Error', 'test') +(, 'Error', 'test') 3 true Error test diff --git a/tests/ports/webassembly/jsffi_create_proxy.mjs.exp b/tests/ports/webassembly/jsffi_create_proxy.mjs.exp index a3b38a78b1..5e541f0b0d 100644 --- a/tests/ports/webassembly/jsffi_create_proxy.mjs.exp +++ b/tests/ports/webassembly/jsffi_create_proxy.mjs.exp @@ -1,5 +1,5 @@ 1 - + 1 1 PyProxy { _ref: 3 } diff --git a/tests/ports/webassembly/jsffi_to_js.mjs.exp b/tests/ports/webassembly/jsffi_to_js.mjs.exp index 399dd0aa8c..6cfd123c01 100644 --- a/tests/ports/webassembly/jsffi_to_js.mjs.exp +++ b/tests/ports/webassembly/jsffi_to_js.mjs.exp @@ -1,6 +1,6 @@ 1 - + false 1 true diff --git a/tests/ports/webassembly/register_js_module.js.exp b/tests/ports/webassembly/register_js_module.js.exp index bb45f4ce00..6e2bad3ce5 100644 --- a/tests/ports/webassembly/register_js_module.js.exp +++ b/tests/ports/webassembly/register_js_module.js.exp @@ -1,2 +1,2 @@ - + 2 diff --git a/tests/ports/webassembly/run_python_async.mjs.exp b/tests/ports/webassembly/run_python_async.mjs.exp index f441bc5cf1..ad6c49e336 100644 --- a/tests/ports/webassembly/run_python_async.mjs.exp +++ b/tests/ports/webassembly/run_python_async.mjs.exp @@ -1,17 +1,17 @@ = TEST 1 ========== 1 - + py 1 - + py 2 2 resolved 123 3 = TEST 2 ========== 1 - + py 1 - + py 2 2 setTimeout resolved