Skip to content

ports/webassembly/proxy_c.c: Allow python bytes() to be proxied to/from JS. #17044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ports/webassembly/proxy_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ enum {
PROXY_KIND_MP_OBJECT = 8,
PROXY_KIND_MP_JSPROXY = 9,
PROXY_KIND_MP_EXISTING = 10,
PROXY_KIND_MP_BYTES = 11,
};

enum {
Expand All @@ -61,6 +62,7 @@ enum {
PROXY_KIND_JS_STRING = 5,
PROXY_KIND_JS_OBJECT = 6,
PROXY_KIND_JS_PYPROXY = 7,
PROXY_KIND_JS_BYTES = 8,
};

MP_DEFINE_CONST_OBJ_TYPE(
Expand Down Expand Up @@ -170,6 +172,10 @@ mp_obj_t proxy_convert_js_to_mp_obj_cside(uint32_t *value) {
mp_obj_t s = mp_obj_new_str((void *)value[2], value[1]);
free((void *)value[2]);
return s;
} else if (value[0] == PROXY_KIND_JS_BYTES) {
mp_obj_t s = mp_obj_new_bytes((void *)value[2], value[1]);
free((void *)value[2]);
return s;
} else if (value[0] == PROXY_KIND_JS_PYPROXY) {
return proxy_c_get_obj(value[1]);
} else {
Expand Down Expand Up @@ -200,6 +206,14 @@ 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);
out[1] = len;
out[2] = (uintptr_t)str;
} else if (mp_obj_get_type(obj) == &mp_type_bytes || mp_obj_get_type(obj) == &mp_type_bytearray) {
kind = PROXY_KIND_MP_BYTES;
mp_buffer_info_t bufinfo;
mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ);
size_t len = bufinfo.len;
const uint8_t *buf = bufinfo.buf;
out[1] = len;
out[2] = (uintptr_t)buf;
} else if (obj == mp_const_undefined) {
kind = PROXY_KIND_MP_JSPROXY;
out[1] = 1;
Expand Down
15 changes: 15 additions & 0 deletions ports/webassembly/proxy_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const PROXY_KIND_MP_GENERATOR = 7;
const PROXY_KIND_MP_OBJECT = 8;
const PROXY_KIND_MP_JSPROXY = 9;
const PROXY_KIND_MP_EXISTING = 10;
const PROXY_KIND_MP_BYTES = 11;

const PROXY_KIND_JS_UNDEFINED = 0;
const PROXY_KIND_JS_NULL = 1;
Expand All @@ -50,6 +51,7 @@ const PROXY_KIND_JS_DOUBLE = 4;
const PROXY_KIND_JS_STRING = 5;
const PROXY_KIND_JS_OBJECT = 6;
const PROXY_KIND_JS_PYPROXY = 7;
const PROXY_KIND_JS_BYTES = 8;

class PythonError extends Error {
constructor(exc_type, exc_details) {
Expand Down Expand Up @@ -143,6 +145,7 @@ function proxy_call_python(target, argumentsList) {
"null",
["number", "number", "number", "pointer"],
[target, argumentsList.length, args, value],
{ async: true },
);
if (argumentsList.length > 0) {
Module._free(args);
Expand Down Expand Up @@ -195,6 +198,13 @@ function proxy_convert_js_to_mp_obj_jsside(js_obj, out) {
Module.stringToUTF8(js_obj, buf, len + 1);
Module.setValue(out + 4, len, "i32");
Module.setValue(out + 8, buf, "i32");
} else if (js_obj instanceof Uint8Array) {
kind = PROXY_KIND_JS_BYTES;
const len = js_obj.length;
const buf = Module._malloc(len);
for (let i = 0; i < len; i++) Module.HEAPU8[buf + i] = js_obj[i];
Module.setValue(out + 4, len, "i32");
Module.setValue(out + 8, buf, "i32");
} else if (
js_obj instanceof PyProxy ||
(typeof js_obj === "function" && "_ref" in js_obj) ||
Expand Down Expand Up @@ -264,6 +274,11 @@ function proxy_convert_mp_to_js_obj_jsside(value) {
const str_len = Module.getValue(value + 4, "i32");
const str_ptr = Module.getValue(value + 8, "i32");
obj = Module.UTF8ToString(str_ptr, str_len);
} else if (kind === PROXY_KIND_MP_BYTES) {
// bytes
const buf_len = Module.getValue(value + 4, "i32");
const buf_ptr = Module.getValue(value + 8, "i32");
obj = new Uint8Array(Module.HEAPU8.buffer, buf_ptr, buf_len);
} else if (kind === PROXY_KIND_MP_JSPROXY) {
// js proxy
const id = Module.getValue(value + 4, "i32");
Expand Down
Loading
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