Skip to content

Conversation

@jgonet
Copy link
Contributor

@jgonet jgonet commented Dec 1, 2025

This PR upstreams API we're using in Popcorn.
It doesn't have tests or extended docs. I'd gladly take some instructions about testing Wasm. Docs will be added after first round of review – I need to know if approach is sane at all.

Design notes

We needed some API to track values in JS with their lifetime in the VM. This was achieved by using new type of resource – TrackedValue. Popcorn also wants to customize JS behavior without maintaining custom Emscripten hooks in .c files – we extracted them to functions in atomvm.pre.js. You can see our implementation in Popcorn repo.

We also need JS to have access to DOM. We hardcoded thread executing JS to be main thread (or in our case, iframe thread).
We were thinking about relaxing that constraint but it'd need js_tracked_eval to forward script to it's destination JS context (e.g. postMessage() to iframe).

js_get_tracked_objects works by batching TrackedObjects and their status (missing, bad type, etc). This allows to reduce communication overhead between JS and Wasm.

We also needed to use ES modules instead of current, IIFE format. It makes it easier to load in iframes and to use in modern JS projects.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
It's a field in emscripten platform struct. It could be a callback entirely in JS but we use threads.
Threads are emulated via webworkers which have their own contexts and variables aren't shared with each other.
This means that we would lose uniqueness of the keys.

This commit also changes output format from file loaded and ran at <script src=...> to ES6 classes
which have greater control over initialization of Wasm.

Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
@jgonet
Copy link
Contributor Author

jgonet commented Dec 1, 2025

cc @pguyot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant