webassembly/proxy_js: Convert JS undefined and JS null to Py None.

And change Py None conversion so it converts to JS undefined.

The semantics for conversion of these objects are then:
- Python None           -> JavaScript undefined
- JavaScript undefined  -> Python None
- JavaScript null       -> Python None

This follows Pyodide:
https://pyodide.org/en/stable/usage/type-conversions.html

Signed-off-by: Damien George <damien@micropython.org>
pull/14457/head
Damien George 2024-05-09 15:36:30 +10:00
rodzic a67e326cb9
commit fa23e4b093
4 zmienionych plików z 13 dodań i 7 usunięć

Wyświetl plik

@ -49,6 +49,7 @@ enum {
};
enum {
PROXY_KIND_JS_UNDEFINED = 0,
PROXY_KIND_JS_NULL = 1,
PROXY_KIND_JS_BOOLEAN = 2,
PROXY_KIND_JS_INTEGER = 3,
@ -78,7 +79,9 @@ 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_NULL) {
if (value[0] == PROXY_KIND_JS_UNDEFINED) {
return mp_const_none;
} else if (value[0] == PROXY_KIND_JS_NULL) {
return mp_const_none;
} else if (value[0] == PROXY_KIND_JS_BOOLEAN) {
return mp_obj_new_bool(value[1]);

Wyświetl plik

@ -38,6 +38,7 @@ const PROXY_KIND_MP_GENERATOR = 7;
const PROXY_KIND_MP_OBJECT = 8;
const PROXY_KIND_MP_JSPROXY = 9;
const PROXY_KIND_JS_UNDEFINED = 0;
const PROXY_KIND_JS_NULL = 1;
const PROXY_KIND_JS_BOOLEAN = 2;
const PROXY_KIND_JS_INTEGER = 3;
@ -109,7 +110,9 @@ function proxy_call_python(target, argumentsList) {
function proxy_convert_js_to_mp_obj_jsside(js_obj, out) {
let kind;
if (js_obj === null) {
if (js_obj === undefined) {
kind = PROXY_KIND_JS_UNDEFINED;
} else if (js_obj === null) {
kind = PROXY_KIND_JS_NULL;
} else if (typeof js_obj === "boolean") {
kind = PROXY_KIND_JS_BOOLEAN;
@ -185,7 +188,7 @@ function proxy_convert_mp_to_js_obj_jsside(value) {
}
if (kind === PROXY_KIND_MP_NONE) {
// None
obj = null;
obj = undefined;
} else if (kind === PROXY_KIND_MP_BOOL) {
// bool
obj = Module.getValue(value + 4, "i32") ? true : false;

Wyświetl plik

@ -1,4 +1,4 @@
false 1
true [ 1, 2, 3 ]
true [ null, true, 1.2 ]
true { tuple: [ 1, 2, 3 ], one: 1, list: [ null, true, 1.2 ] }
true [ undefined, true, 1.2 ]
true { tuple: [ 1, 2, 3 ], one: 1, list: [ undefined, true, 1.2 ] }

Wyświetl plik

@ -23,7 +23,7 @@ py 1
setTimeout resolved
resolved value: 123
py 2
2 null
2 undefined
= TEST 4 ==========
1
py 1
@ -35,4 +35,4 @@ py 3
setTimeout B resolved
resolved value: 456
py 4
2 null
2 undefined