Stream: jco

Topic: Generate host bindings for preview1


view this post on Zulip Bennett Hardwick (Apr 28 2025 at 00:45):

Hello! I've got a WIT file I'm using to generate a guest for using wit-bindgen. I want to be able to run this on the web using a host generated by jco. Unfortunately I'm not able to transpile the wasm and since preview2 doesn't work natively in the browser I was hoping there was a way to generate a host using jco for preview1. Is this possible at all or will I need to hand-roll my host code? Thanks.

view this post on Zulip Victor Adossi (Apr 28 2025 at 11:36):

Hey Bennett, right now jco does not have preview1 bindings, but there are a couple projects that might be of help here:

https://github.com/bjorn3/browser_wasi_shim

https://github.com/easywasm/wasi

A WASI shim for in the browser. Contribute to bjorn3/browser_wasi_shim development by creating an account on GitHub.
A working, zero-dependency implementation of WASI preview1, with lots of filesystem options. - easywasm/wasi

view this post on Zulip Bennett Hardwick (Apr 28 2025 at 11:43):

Hi Victor thanks for the response! I've used the shim from bjorn3 a bit and it works great. However if I define a bunch of custom types using WIT there is a lot of glue code that I have to write. If I transpile a preview2 version of the same code with jco it produces all the required JS glue code (object serialization, deserialization, etc.) that would work with my preview1 binary, but it is trapped inside the "instantiate" closure. I've probably reached a dead end but is there any way with jco or with js-component-bindgen that I could just get the glue code? Thanks.

view this post on Zulip Victor Adossi (Apr 28 2025 at 11:45):

Ah unfortunately, you'd need the preivew2 shims to be more developed for that :(

We had some work in the past that filled them out more, but they're still experimental and incomplete

view this post on Zulip Victor Adossi (Apr 28 2025 at 11:45):

Could you list the bits that are missing from the current impl that you depend on?

view this post on Zulip Victor Adossi (Apr 28 2025 at 11:45):

p2-shim usage on the browser is experimental but some of it is there -- maybe we can shift how we prioritize implementing to try to hit your usecases/maybe the work has already been done and just not committed yet

view this post on Zulip Bennett Hardwick (Apr 28 2025 at 11:53):

I think the p2 shims have everything I need, it's just the "glue" that jco generates for custom records, etc. in wit.

For example the following in a .wit file:

record message {
  name: string,
  data: list<u8>
}

load-message: func() -> message

Has code like so generated when transpiled with jco:

const load = () => {
  let ptr = instance.exports.["me:package/load-message"]();
  let namePtr = new DataView(instance.exports.memory).getInt32(ptr, true);
  let nameLen = new DataView(instance.exports.memory).getInt32(ptr + 4, true);
  let name = new TextDecoder().decode(new Uint8Array(..));

 // and so on ..
}

I would love to be able to just generate this glue code.

view this post on Zulip Victor Adossi (Apr 28 2025 at 11:56):

OH, got you! OK in this case would you mind making an issue for this? This probably deserves some discussion, also there's the JS guest languages meeting where this might be worth discussion.

Unfortunately I can't make it this week, but being able to generate just the other bindings is an interesting feature request...

Contribute to bytecodealliance/meetings development by creating an account on GitHub.

view this post on Zulip Bennett Hardwick (Apr 28 2025 at 11:56):

Will do, thanks Victor!

view this post on Zulip Victor Adossi (Apr 28 2025 at 11:56):

Does monkey-patching not work as a solution for this?

If you could explain your ideal workflow here (and in the issue, if you file one!) that would probably help me and others get an idea of what you'd prefer jco to be able to do

view this post on Zulip Victor Adossi (Apr 28 2025 at 11:57):

(I mean up to and including posting the commands as you might imagine them/what would be output!)

view this post on Zulip Bennett Hardwick (Apr 28 2025 at 12:01):

Yeah, I can definitely get stuff working by copy pasting just what I need so it's not too bad.


Last updated: Dec 06 2025 at 07:03 UTC