Content-Length: 514680 | pFad | http://github.com/micropython/micropython/commit/aa2e3880c13036c65aa3381ed5f104c59ebad86e

CD webassembly/proxy_js: Create a special "undefined" type for Python. · micropython/micropython@aa2e388 · GitHub
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

8 files changed

+35
-10
lines changed

Diff for: ports/webassembly/README.md

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

Diff for: ports/webassembly/proxy_c.c

+14-1
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);

Diff for: ports/webassembly/proxy_js.js

+1-1
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) {

Diff for: tests/ports/webassembly/await_error_handling.mjs.exp

+1-1
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

Diff for: tests/ports/webassembly/jsffi_create_proxy.mjs.exp

+1-1
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 }

Diff for: tests/ports/webassembly/jsffi_to_js.mjs.exp

+1-1
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

Diff for: tests/ports/webassembly/register_js_module.js.exp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<JsProxy 1>
1+
<JsProxy 2>
22
2

Diff for: tests/ports/webassembly/run_python_async.mjs.exp

+4-4
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)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/commit/aa2e3880c13036c65aa3381ed5f104c59ebad86e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy