From 88513d12268f5a5c3f3488f1a2d70cb605129537 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 19 Jun 2024 15:17:21 +1000 Subject: [PATCH] webassembly/api: Allow specifying the pystack size. This allows increasing the Python recursion depth if needed. Also increase the default to 2k words. There is enough RAM in the browser/node context for this to be increased, and having a larger pystack allows more complex code to run without hitting the limit. Signed-off-by: Damien George --- ports/webassembly/api.js | 17 ++++++++++++----- ports/webassembly/main.c | 12 ++++++------ tests/ports/webassembly/heap_expand.mjs.exp | 10 +++++----- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/ports/webassembly/api.js b/ports/webassembly/api.js index c00edf575..0718d13d6 100644 --- a/ports/webassembly/api.js +++ b/ports/webassembly/api.js @@ -25,6 +25,7 @@ */ // Options: +// - pystack: size in words of the MicroPython Python stack. // - heapsize: size in bytes of the MicroPython GC heap. // - url: location to load `micropython.mjs`. // - stdin: function to return input characters. @@ -34,10 +35,11 @@ // - stderr: same behaviour as stdout but for error output. // - linebuffer: whether to buffer line-by-line to stdout/stderr. export async function loadMicroPython(options) { - const { heapsize, url, stdin, stdout, stderr, linebuffer } = Object.assign( - { heapsize: 1024 * 1024, linebuffer: true }, - options, - ); + const { pystack, heapsize, url, stdin, stdout, stderr, linebuffer } = + Object.assign( + { pystack: 2 * 1024, heapsize: 1024 * 1024, linebuffer: true }, + options, + ); let Module = {}; Module.locateFile = (path, scriptDirectory) => url || scriptDirectory + path; @@ -96,7 +98,12 @@ export async function loadMicroPython(options) { ); return proxy_convert_mp_to_js_obj_jsside_with_free(value); }; - Module.ccall("mp_js_init", "null", ["number"], [heapsize]); + Module.ccall( + "mp_js_init", + "null", + ["number", "number"], + [pystack, heapsize], + ); Module.ccall("proxy_c_init", "null", [], []); return { _module: Module, diff --git a/ports/webassembly/main.c b/ports/webassembly/main.c index 6502ed16d..c542f0cd7 100644 --- a/ports/webassembly/main.c +++ b/ports/webassembly/main.c @@ -66,7 +66,12 @@ void external_call_depth_dec(void) { --external_call_depth; } -void mp_js_init(int heap_size) { +void mp_js_init(int pystack_size, int heap_size) { + #if MICROPY_ENABLE_PYSTACK + mp_obj_t *pystack = (mp_obj_t *)malloc(pystack_size * sizeof(mp_obj_t)); + mp_pystack_init(pystack, pystack + pystack_size); + #endif + #if MICROPY_ENABLE_GC char *heap = (char *)malloc(heap_size * sizeof(char)); gc_init(heap, heap + heap_size); @@ -80,11 +85,6 @@ void mp_js_init(int heap_size) { MP_STATE_MEM(gc_alloc_threshold) = 16 * 1024 / MICROPY_BYTES_PER_GC_BLOCK; #endif - #if MICROPY_ENABLE_PYSTACK - static mp_obj_t pystack[1024]; - mp_pystack_init(pystack, &pystack[MP_ARRAY_SIZE(pystack)]); - #endif - mp_init(); #if MICROPY_VFS_POSIX diff --git a/tests/ports/webassembly/heap_expand.mjs.exp b/tests/ports/webassembly/heap_expand.mjs.exp index ee1490840..5efa8567f 100644 --- a/tests/ports/webassembly/heap_expand.mjs.exp +++ b/tests/ports/webassembly/heap_expand.mjs.exp @@ -17,11 +17,11 @@ 135109888 134978800 134716640 -135216848 -136217216 -138218032 -142219616 -150222864 +135216784 +136217152 +138217840 +142219296 +150222224 1 2 4