Hello, is it possible to use wit bindgen in the browser? This is actually two questions:
1) Is it possible to load and use WASM components that use the component model in the browser?
2) Is it possible to load and use "normal" WASM modules that use wit bidgen (but aren't using the component model) in the browser AND is it possible to load and use these modules (again, not using component model, only wit bindgen) in wasmtime and get wit-bindings?
The reason I ask is that I have a video game in Rust and I want to load mods using WASM modules. I also want to run the game on the web.
If I use the component model, then it's probably not going to work on the web, since I didn't find any way to get a WASM component to load and run on the web.
If, on the other hand, I use just wit bindgen without the component model, then I do not know how to load and tun the module from wasmtime, since the only tutorials on how to use wit bindgen I could find use the component model when used with wasmtime host.
Is it possible to load and use WASM components that use the component model in the browser?
For this you'll want to check out the jco
project
Is it possible to load and use "normal" WASM modules that use wit bidgen (but aren't using the component model) in the browser AND is it possible to load and use these modules (again, not using component model, only wit bindgen) in wasmtime and get wit-bindings?
Sort of and sort of not. Today this is technically possible as native compilers are outtping core wasm modules which are then wrapped up into components. There's, strictly speaking, nothing stopping you from taking that core wasm module and running it in a browser and then alternatively also taking the component and running it in Wasmtime.
I say "sort of not" though because this isn't really a supported workflow and will likely have a lot of gotchas and surprises and isn't how things are supposed to work. Instead a component is intended to be the "source of truth" and tooling like jco
enables running that in a browser.
Ok, I guess I will not be able to avoid the component model. But I am having trouble with making jco work. I have a wasm component that I created from a wasm module that was using wit bindgen using this command:
wasm-tools component new plugin_rust.wasm -o component.wasm
This component.wasm file is working correctly in wasmtime runtime. But when I pass it to jco, like this:
jco transpile component.wasm -o out-dir
I get an error:
(jco transpile) ComponentError: failed to extract interface information from component
Caused by:
unknown component version: 0xd (at offset 0x0)
Any help?
This likely means that jco
is still on version 0xc
and doesn't know about the component model version you produced.
Although, that appears to have landed.
https://github.com/bytecodealliance/jco/pull/86
Are you using the latest version of jco?
Kyle Brown said:
Are you using the latest version of jco?
I have version 0.7.0. How do i upgrade to a newer version?
Never mind, I got the new version, but now I have another error. My wit file looks like this:
package example:protocol
world my-world {
import print: func(msg: string)
export run: func()
}
And the first line of the generated component.js file is trying to import the print function:
import import print from 'print';
And it results in an error:
Uncaught SyntaxError: import is an invalid identifier
Can I generate proper TS bindings that would require me to provide the imported "print" function?
The current exported TS bindings only have the exported run function:
export function run(): void;
It may be the case that jco hasn't upgraded from 0xc to 0xd, although @Guy Bedford would know for sure
@Karel Hrkal if you want to provide the "print" function you can use the --instantiation
flag option
@Alex Crichton the PR I link above says that it has
oh oops sorry missed that
Guy Bedford said:
Karel Hrkal if you want to provide the "print" function you can use the
--instantiation
flag option
Ok, that mostly works, but there are few problems.
First, the "print" function is named "default" for some reason in the generated bindings:
export interface ImportObject {
default(msg: string): void,
}
Second, there is a syntax error when getting this imported function, in the generated "component.js" file:
const const print = imports.default;
Removing the extra "const" makes it work correctly. I'm assuming I should create a bug issue on jco's github?
@Karel Hrkal that would be great if you could post a bug, I'll look into it further
@Karel Hrkal I've released jco@0.9.3 which should fix your issues
Guy Bedford said:
Karel Hrkal I've released jco@0.9.3 which should fix your issues
Yes, the "double const" issue is resolved, many thanks. The "import" function is still named "default" in the import object, but changing that would be a breaking change.
Karel Hrkal has marked this topic as resolved.
Hello @Guy Bedford, I think there are more problems when you have more than one import. I have created a new issue on github: https://github.com/bytecodealliance/jco/issues/103
Last updated: Nov 22 2024 at 17:03 UTC