Skip to content

Commit aa2e388

Browse files
committed
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>
1 parent 0148bbb commit aa2e388

File tree

8 files changed

+35
-10
lines changed

8 files changed

+35
-10
lines changed

ports/webassembly/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ context, created and returned by `loadMicroPython()`.
160160
- `replProcessCharWithAsyncify(chr)`: process an incoming character at the REPL,
161161
for use when ASYNCIFY is enabled.
162162

163+
Type conversions
164+
----------------
165+
166+
Read-only objects (booleanns, numbers, strings, etc) are converted when passed between
167+
Python and JavaScript. The conversions are:
168+
169+
- JavaScript `null` converts to/from Python `None`.
170+
- JavaScript `undefined` converts to/from Python `js.undefined`.
171+
172+
The conversion between `null` and `None` matches the behaviour of the Python `json`
173+
module.
174+
163175
Proxying
164176
--------
165177

ports/webassembly/proxy_c.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ enum {
5959
PROXY_KIND_JS_PYPROXY = 7,
6060
};
6161

62+
MP_DEFINE_CONST_OBJ_TYPE(
63+
mp_type_undefined,
64+
MP_QSTR_undefined,
65+
MP_TYPE_FLAG_NONE
66+
);
67+
68+
static const mp_obj_base_t mp_const_undefined_obj = {&mp_type_undefined};
69+
70+
#define mp_const_undefined (MP_OBJ_FROM_PTR(&mp_const_undefined_obj))
71+
6272
MP_DEFINE_EXCEPTION(JsException, Exception)
6373

6474
void proxy_c_init(void) {
@@ -80,7 +90,7 @@ static inline mp_obj_t proxy_c_get_obj(uint32_t c_ref) {
8090

8191
mp_obj_t proxy_convert_js_to_mp_obj_cside(uint32_t *value) {
8292
if (value[0] == PROXY_KIND_JS_UNDEFINED) {
83-
return mp_const_none;
93+
return mp_const_undefined;
8494
} else if (value[0] == PROXY_KIND_JS_NULL) {
8595
return mp_const_none;
8696
} 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) {
122132
const char *str = mp_obj_str_get_data(obj, &len);
123133
out[1] = len;
124134
out[2] = (uintptr_t)str;
135+
} else if (obj == mp_const_undefined) {
136+
kind = PROXY_KIND_MP_JSPROXY;
137+
out[1] = 1;
125138
} else if (mp_obj_is_jsproxy(obj)) {
126139
kind = PROXY_KIND_MP_JSPROXY;
127140
out[1] = mp_obj_jsproxy_get_ref(obj);

ports/webassembly/proxy_js.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class PythonError extends Error {
5656
}
5757

5858
function proxy_js_init() {
59-
globalThis.proxy_js_ref = [globalThis];
59+
globalThis.proxy_js_ref = [globalThis, undefined];
6060
}
6161

6262
function proxy_call_python(target, argumentsList) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
1
22
2
3-
(<JsProxy 6>, 'Error', 'test')
3+
(<JsProxy 7>, 'Error', 'test')
44
3
55
true Error test

tests/ports/webassembly/jsffi_create_proxy.mjs.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
1
2-
<JsProxy 1>
2+
<JsProxy 2>
33
1
44
1
55
PyProxy { _ref: 3 }

tests/ports/webassembly/jsffi_to_js.mjs.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
1
2-
<JsProxy 1>
32
<JsProxy 2>
3+
<JsProxy 3>
44
false
55
1
66
true
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<JsProxy 1>
1+
<JsProxy 2>
22
2

tests/ports/webassembly/run_python_async.mjs.exp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
= TEST 1 ==========
22
1
3-
<JsProxy 1>
3+
<JsProxy 2>
44
py 1
5-
<JsProxy 4>
5+
<JsProxy 5>
66
py 2
77
2
88
resolved 123
99
3
1010
= TEST 2 ==========
1111
1
12-
<JsProxy 5>
12+
<JsProxy 6>
1313
py 1
14-
<JsProxy 8>
14+
<JsProxy 9>
1515
py 2
1616
2
1717
setTimeout resolved

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy