Skip to content
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

Load WASM/JS modules dynamically using importScripts #40

Merged
merged 1 commit into from
Aug 14, 2024

Conversation

ianthomas23
Copy link
Member

Up until now we have been importing the JavaScript-with-embedded-WebAssembly modules statically at build time just like any other TypeScript/JavaScript code. This PR changes this to dynamically import such code on demand using the WebWorker importScripts function. There are two benefits:

  1. It will support (with a little more work) separate JS and WASM files. This will be in a follow-up PR.
  2. It will be possible (with a larger amount of more work) to register the WASM commands available at runtime, probably when constructing a Shell object, to support arbitrary terminal functionality written by ourselves or any user.

To dynamically import such modules they have to be available at some URL. At this stage I have implemented a really simple solution to this in the test suite and cockle-playground (Jupyter terminal extension to follow) of copying the JS/WASM files from the cockle npm package into the webpack/rspack statically-served assets directory.

The imported JS/WASM modules are cached to make subsequent command calls faster. The first time coreutils is used it takes on my dev machine ~150 ms to import compared to the ~60 ms previously. But subsequent calls only take ~15 ms compared to the ~30 ms previously. grep is faster as it is a much smaller module with just a single command, taking ~60 ms to load the first time and <10 ms subsequently. If these times get worse and feel like they are too long then we can always pre-emptively warm up the cache at the start. Also note that the browser caches the JS/WASM files and if you have a copy of coreutils already cached in the browser it only takes ~60 ms to import the first time.

@ianthomas23 ianthomas23 added the enhancement New feature or request label Aug 13, 2024
@ianthomas23 ianthomas23 merged commit 95e66f7 into main Aug 14, 2024
4 checks passed
@ianthomas23 ianthomas23 deleted the importScripts branch August 14, 2024 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant