Browse Source

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 <damien@micropython.org>
pull/14483/head
Damien George 6 months ago
parent
commit
aa2e3880c1
  1. 12
      ports/webassembly/README.md
  2. 15
      ports/webassembly/proxy_c.c
  3. 2
      ports/webassembly/proxy_js.js
  4. 2
      tests/ports/webassembly/await_error_handling.mjs.exp
  5. 2
      tests/ports/webassembly/jsffi_create_proxy.mjs.exp
  6. 2
      tests/ports/webassembly/jsffi_to_js.mjs.exp
  7. 2
      tests/ports/webassembly/register_js_module.js.exp
  8. 8
      tests/ports/webassembly/run_python_async.mjs.exp

12
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
--------

15
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);

2
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) {

2
tests/ports/webassembly/await_error_handling.mjs.exp

@ -1,5 +1,5 @@
1
2
(<JsProxy 6>, 'Error', 'test')
(<JsProxy 7>, 'Error', 'test')
3
true Error test

2
tests/ports/webassembly/jsffi_create_proxy.mjs.exp

@ -1,5 +1,5 @@
1
<JsProxy 1>
<JsProxy 2>
1
1
PyProxy { _ref: 3 }

2
tests/ports/webassembly/jsffi_to_js.mjs.exp

@ -1,6 +1,6 @@
1
<JsProxy 1>
<JsProxy 2>
<JsProxy 3>
false
1
true

2
tests/ports/webassembly/register_js_module.js.exp

@ -1,2 +1,2 @@
<JsProxy 1>
<JsProxy 2>
2

8
tests/ports/webassembly/run_python_async.mjs.exp

@ -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

Loading…
Cancel
Save