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>
This commit is contained in:
parent
0148bbb495
commit
aa2e3880c1
@ -160,6 +160,18 @@ context, created and returned by `loadMicroPython()`.
|
|||||||
- `replProcessCharWithAsyncify(chr)`: process an incoming character at the REPL,
|
- `replProcessCharWithAsyncify(chr)`: process an incoming character at the REPL,
|
||||||
for use when ASYNCIFY is enabled.
|
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
|
Proxying
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
|||||||
@ -59,6 +59,16 @@ enum {
|
|||||||
PROXY_KIND_JS_PYPROXY = 7,
|
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)
|
MP_DEFINE_EXCEPTION(JsException, Exception)
|
||||||
|
|
||||||
void proxy_c_init(void) {
|
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) {
|
mp_obj_t proxy_convert_js_to_mp_obj_cside(uint32_t *value) {
|
||||||
if (value[0] == PROXY_KIND_JS_UNDEFINED) {
|
if (value[0] == PROXY_KIND_JS_UNDEFINED) {
|
||||||
return mp_const_none;
|
return mp_const_undefined;
|
||||||
} else if (value[0] == PROXY_KIND_JS_NULL) {
|
} else if (value[0] == PROXY_KIND_JS_NULL) {
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
} else if (value[0] == PROXY_KIND_JS_BOOLEAN) {
|
} 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);
|
const char *str = mp_obj_str_get_data(obj, &len);
|
||||||
out[1] = len;
|
out[1] = len;
|
||||||
out[2] = (uintptr_t)str;
|
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)) {
|
} else if (mp_obj_is_jsproxy(obj)) {
|
||||||
kind = PROXY_KIND_MP_JSPROXY;
|
kind = PROXY_KIND_MP_JSPROXY;
|
||||||
out[1] = mp_obj_jsproxy_get_ref(obj);
|
out[1] = mp_obj_jsproxy_get_ref(obj);
|
||||||
|
|||||||
@ -56,7 +56,7 @@ class PythonError extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function proxy_js_init() {
|
function proxy_js_init() {
|
||||||
globalThis.proxy_js_ref = [globalThis];
|
globalThis.proxy_js_ref = [globalThis, undefined];
|
||||||
}
|
}
|
||||||
|
|
||||||
function proxy_call_python(target, argumentsList) {
|
function proxy_call_python(target, argumentsList) {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
1
|
1
|
||||||
2
|
2
|
||||||
(<JsProxy 6>, 'Error', 'test')
|
(<JsProxy 7>, 'Error', 'test')
|
||||||
3
|
3
|
||||||
true Error test
|
true Error test
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
1
|
1
|
||||||
<JsProxy 1>
|
<JsProxy 2>
|
||||||
1
|
1
|
||||||
1
|
1
|
||||||
PyProxy { _ref: 3 }
|
PyProxy { _ref: 3 }
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
1
|
1
|
||||||
<JsProxy 1>
|
|
||||||
<JsProxy 2>
|
<JsProxy 2>
|
||||||
|
<JsProxy 3>
|
||||||
false
|
false
|
||||||
1
|
1
|
||||||
true
|
true
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
<JsProxy 1>
|
<JsProxy 2>
|
||||||
2
|
2
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
= TEST 1 ==========
|
= TEST 1 ==========
|
||||||
1
|
1
|
||||||
<JsProxy 1>
|
<JsProxy 2>
|
||||||
py 1
|
py 1
|
||||||
<JsProxy 4>
|
<JsProxy 5>
|
||||||
py 2
|
py 2
|
||||||
2
|
2
|
||||||
resolved 123
|
resolved 123
|
||||||
3
|
3
|
||||||
= TEST 2 ==========
|
= TEST 2 ==========
|
||||||
1
|
1
|
||||||
<JsProxy 5>
|
<JsProxy 6>
|
||||||
py 1
|
py 1
|
||||||
<JsProxy 8>
|
<JsProxy 9>
|
||||||
py 2
|
py 2
|
||||||
2
|
2
|
||||||
setTimeout resolved
|
setTimeout resolved
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user