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

Improve JS and WebAssembly support #65

Open
2 tasks
booniepepper opened this issue Nov 9, 2023 · 0 comments
Open
2 tasks

Improve JS and WebAssembly support #65

booniepepper opened this issue Nov 9, 2023 · 0 comments

Comments

@booniepepper
Copy link
Collaborator

booniepepper commented Nov 9, 2023

Running dt from WebAssembly and calling the main() function (via _start()) is a bit involved.

(This is my first time ever trying to use WebAssembly APIs btw)

dt = null

errstub = called => (...args) => { throw {called, args}; };
logstub = called => (...args) => console.log({called, args});

args_sizes_get = () => 1;
fd_read = fd => fd == 0 ? "1 1 + pl" : "";
fd_fdstat_get = fd => ({ fd });

fetch("static/dt.wasm")
  .then(r => r.arrayBuffer())
  .then(bytes => WebAssembly.instantiate(bytes, {
    wasi_snapshot_preview1: {
      args_get: errstub("args_get"),
      args_sizes_get,
      fd_close: errstub("fd_close"),
      fd_fdstat_get,
      fd_read,
      fd_readdir: errstub("fd_readdir"),
      fd_seek: errstub("fd_seek"),
      fd_write: errstub("fd_write"),
      path_open: errstub("path_open"),
      proc_exit: errstub("proc_exit"),
      random_get: errstub("random_get"),
  }}))
  .then(module => dt = module.instance.exports)

dt._start()

This can run in a browser console on https://dt.plumbing and will fail (for now on purpose) when trying to call fd_write

The above code could be fleshed out and refactored into something reasonable. It could be TS-ified and etc.

That said, it will be a kinda gross JS inter-op to try to fit everything into stdin/stdout file read and writes... Not fun in a browser context, although fine for something that interprets in an OS context, like Wasmer (probably?).


A better interface:

  • A one-shot WebAssembly function that interprets code
    • Maybe: export fn interpret(code: []const u8) []const u8 { ... }
  • A WebAssembly function that can export a Dt instance
    • Maybe: export fn instance() Dt { ... }
    • ...And then is it straightforward to send JS strings? Does it require an extra layer of abstraction?
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

No branches or pull requests

1 participant