Commit fa23e4b0 authored by Damien George's avatar Damien George

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.htmlSigned-off-by: default avatarDamien George <damien@micropython.org>
parent a67e326c
......@@ -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]);
......
......@@ -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;
......
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 ] }
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment