Skip to content

Commit

Permalink
webassembly: Make mp_js_do_str asynchronous.
Browse files Browse the repository at this point in the history
This fixes a bug where `gc.collect()` would crash due to
emscripten_scan_stack being called synchronously within mp_js_do_str.  The
fix is to make mp_js_do_str asynchronous.

Fixes #10692.

Signed-off-by: Eli Bierman <eli@elib.dev>
  • Loading branch information
elibdev authored and dpgeorge committed Jun 27, 2023
1 parent 0e215a9 commit b2ad7e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions ports/webassembly/README.md
Expand Up @@ -49,7 +49,7 @@ using the require command and the general API outlined below. For example:
var mp_js = require('./build/micropython.js');

mp_js_init(64 * 1024);
mp_js_do_str("print('hello world')\n");
await mp_js_do_str("print('hello world')\n");
```

Running with HTML
Expand Down Expand Up @@ -77,7 +77,7 @@ something to stdout. The following code demonstrates basic functionality:
Module["onRuntimeInitialized"] = async function() {
mp_js_startup();
mp_js_init(64 * 1024);
mp_js_do_str("print('hello world')");
await mp_js_do_str("print('hello world')");
};
</script>
</body>
Expand Down Expand Up @@ -108,7 +108,7 @@ Initialize MicroPython with the given stack size in bytes. This must be
called before attempting to interact with MicroPython.

```
mp_js_do_str(code)
await mp_js_do_str(code)
```

Execute the input code. `code` must be a `string`.
Expand Down
6 changes: 4 additions & 2 deletions ports/webassembly/wrapper.js
Expand Up @@ -29,7 +29,7 @@ var Module = {};
var mainProgram = function()
{
mp_js_init = Module.cwrap('mp_js_init', 'null', ['number']);
mp_js_do_str = Module.cwrap('mp_js_do_str', 'number', ['string']);
mp_js_do_str = Module.cwrap('mp_js_do_str', 'number', ['string'], {async: true});
mp_js_init_repl = Module.cwrap('mp_js_init_repl', 'null', ['null']);
mp_js_process_char = Module.cwrap('mp_js_process_char', 'number', ['number']);

Expand Down Expand Up @@ -75,7 +75,9 @@ var mainProgram = function()
}
});
} else {
process.exitCode = mp_js_do_str(contents);
mp_js_do_str(contents).then(exitCode => {
process.exitCode = exitCode
})
}
}
}
Expand Down

0 comments on commit b2ad7e2

Please sign in to comment.