Skip to content

Commit

Permalink
webassembly/proxy_js: Create a special "undefined" type for Python.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dpgeorge committed May 16, 2024
1 parent 0148bbb commit aa2e388
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 10 deletions.
12 changes: 12 additions & 0 deletions ports/webassembly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------

Expand Down
15 changes: 14 additions & 1 deletion ports/webassembly/proxy_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ports/webassembly/proxy_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ports/webassembly/await_error_handling.mjs.exp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1
2
(<JsProxy 6>, 'Error', 'test')
(<JsProxy 7>, 'Error', 'test')
3
true Error test
2 changes: 1 addition & 1 deletion tests/ports/webassembly/jsffi_create_proxy.mjs.exp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1
<JsProxy 1>
<JsProxy 2>
1
1
PyProxy { _ref: 3 }
Expand Down
2 changes: 1 addition & 1 deletion tests/ports/webassembly/jsffi_to_js.mjs.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
1
<JsProxy 1>
<JsProxy 2>
<JsProxy 3>
false
1
true
Expand Down
2 changes: 1 addition & 1 deletion tests/ports/webassembly/register_js_module.js.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<JsProxy 1>
<JsProxy 2>
2
8 changes: 4 additions & 4 deletions tests/ports/webassembly/run_python_async.mjs.exp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
= TEST 1 ==========
1
<JsProxy 1>
<JsProxy 2>
py 1
<JsProxy 4>
<JsProxy 5>
py 2
2
resolved 123
3
= TEST 2 ==========
1
<JsProxy 5>
<JsProxy 6>
py 1
<JsProxy 8>
<JsProxy 9>
py 2
2
setTimeout resolved
Expand Down

0 comments on commit aa2e388

Please sign in to comment.