8000 webassembly/api: Allocate code data on C heap when running Python code. · micropython/micropython@a5ad308 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5ad308

Browse files
committed
webassembly/api: Allocate code data on C heap when running Python code.
Otherwise Emscripten allocates it on the Emscripten C stack, which will overflow for large amounts of code. Fixes issue #14307. Signed-off-by: Damien George <damien@micropython.org>
1 parent 49ce7a6 commit a5ad308

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

ports/webassembly/api.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,31 @@ export async function loadMicroPython(options) {
131131
},
132132
pyimport: pyimport,
133133
runPython(code) {
134+
const len = Module.lengthBytesUTF8(code);
135+
const buf = Module._malloc(len + 1);
136+
Module.stringToUTF8(code, buf, len + 1);
134137
const value = Module._malloc(3 * 4);
135138
Module.ccall(
136139
"mp_js_do_exec",
137140
"number",
138-
["string", "pointer"],
139-
[code, value],
141+
["pointer", "number", "pointer"],
142+
[buf, len, value],
140143
);
144+
Module._free(buf);
141145
return proxy_convert_mp_to_js_obj_jsside_with_free(value);
142146
},
143147
runPythonAsync(code) {
148+
const len = Module.lengthBytesUTF8(code);
149+
const buf = Module._malloc(len + 1);
150+
Module.stringToUTF8(code, buf, len + 1);
144151
const value = Module._malloc(3 * 4);
145152
Module.ccall(
146153
"mp_js_do_exec_async",
147154
"number",
148-
["string", "pointer"],
149-
[code, value],
155+
["pointer", "number", "pointer"],
156+
[buf, len, value],
150157
);
158+
Module._free(buf);
151159
return proxy_convert_mp_to_js_obj_jsside_with_free(value);
152160
},
153161
replInit() {

ports/webassembly/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ void mp_js_do_import(const char *name, uint32_t *out) {
104104
}
105105
}
106106

107-
void mp_js_do_exec(const char *src, uint32_t *out) {
107+
void mp_js_do_exec(const char *src, size_t len, uint32_t *out) {
108108
// Collect at the top-level, where there are no root pointers from stack/registers.
109109
gc_collect_start();
110110
gc_collect_end();
111111

112112
mp_parse_input_kind_t input_kind = MP_PARSE_FILE_INPUT;
113113
nlr_buf_t nlr;
114114
if (nlr_push(&nlr) == 0) {
115-
mp_lexer_t *lex = mp_lexer_new_from_str_len_dedent(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
115+
mp_lexer_t *lex = mp_lexer_new_from_str_len_dedent(MP_QSTR__lt_stdin_gt_, src, len, 0);
116116
qstr source_name = lex->source_name;
117117
mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
118118
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false);
@@ -125,9 +125,9 @@ void mp_js_do_exec(const char *src, uint32_t *out) {
125125
}
126126
}
127127

128-
void mp_js_do_exec_async(const char *src, uint32_t *out) {
128+
void mp_js_do_exec_async(const char *src, size_t len, uint32_t *out) {
129129
mp_compile_allow_top_level_await = true;
130-
mp_js_do_exec(src, out);
130+
mp_js_do_exec(src, len, out);
131131
mp_compile_allow_top_level_await = false;
132132
}
133133

0 commit comments

Comments
 (0)
0