Stream: wasmtime

Topic: trying to hand create a component


view this post on Zulip Dd (May 11 2024 at 11:33):

I've got a newbe question, I'm sorry if it's not the right place to ask.

My primary goal is to run some JS code inside of project written in Go. I've heard that it can be done by compiling JS code to WASM (using wasm components) and than use wasm runtime to then run it inside target codebase.

I've found this tutorial: https://github.com/bytecodealliance/jco/blob/main/docs/src/example.md

I've created wasm file from js code as was describe there. So as a result, I've got cowsay.wasm file:

$ jco wit cowsay.wasm
package root:component;

world root {
  import wasi:io/poll@0.2.0;
  import wasi:clocks/monotonic-clock@0.2.0;
  import wasi:io/error@0.2.0;
  import wasi:io/streams@0.2.0;
  import wasi:cli/stdin@0.2.0;
  import wasi:cli/stdout@0.2.0;
  import wasi:cli/stderr@0.2.0;
  import wasi:cli/terminal-input@0.2.0;
  import wasi:cli/terminal-output@0.2.0;
  import wasi:cli/terminal-stdin@0.2.0;
  import wasi:cli/terminal-stdout@0.2.0;
  import wasi:cli/terminal-stderr@0.2.0;
  import wasi:clocks/wall-clock@0.2.0;
  import wasi:filesystem/types@0.2.0;
  import wasi:filesystem/preopens@0.2.0;
  import wasi:random/random@0.2.0;

  export cow: interface {
    enum cows {
      default,
      owl,
    }

    say: func(text: string, cow: option<cows>) -> string;
  }
}

The important step is omitted in this tutorial, which is: how to execute it?

I've tried using wasmtime, but result looks like this:

$ wasmtime cowsay.wasm
Error: failed to run main module `cowsay.wasm`

Caused by:
    exported instance `wasi:cli/run@0.2.0` not present

I've tried jco run, but result looks like this:

$ jco run cowsay.wasm
Not a valid command component to execute.
JavaScript tooling for working with WebAssembly Components - bytecodealliance/jco

view this post on Zulip bjorn3 (May 11 2024 at 14:06):

You probably need something likewasmtime run --invoke say cowsay.wasm arg1 arg2. Without --invoke wasmtime will invoke the run method of the wasi:cli interface.

view this post on Zulip Dd (May 11 2024 at 15:51):

Thank you for reply, but I tried that and the result is:

$ wasmtime run --invoke say cowsay.wasm arg1 arg2
Error: failed to run main module `cowsay.wasm`

Caused by:
    using `--invoke` with components is not supported

view this post on Zulip Pat Hickey (May 11 2024 at 16:22):

Yeah we haven’t gotten to invoke for components yet, it requires a little more machinery than we have right at the moment. If you want to invoke that function you’ll need to write a little bit of rust code using wasmtime the crate, or alternatively js using jco transpile

view this post on Zulip Pat Hickey (May 11 2024 at 16:24):

Here’s one example of how you use wasmtime for that https://github.com/bytecodealliance/wasmtime/blob/main/examples/component/main.rs

A fast and secure runtime for WebAssembly. Contribute to bytecodealliance/wasmtime development by creating an account on GitHub.

view this post on Zulip Brandon Bloom (May 12 2024 at 00:13):

Trying to wrap my head around wasi preview 2 & struggling with basic imports. Am I missing something obvious here?

/tmp $ cat exit.wat
(module

  (import "wasi:cli/exit" "exit"
    (func $exit (param i32)))

  (func (export "main")
    i32.const 42
    call $exit)

  )
/tmp $ wat2wasm exit.wat
/tmp $ wasmtime run -S cli exit.wasm
Error: failed to run main module `exit.wasm`

Caused by:
    0: failed to instantiate "exit.wasm"
    1: unknown import: `wasi:cli/exit::exit` has not been defined

view this post on Zulip Pat Hickey (May 12 2024 at 00:55):

That’s a wasm module, which is a distinct binary (or wat) representation from a component. If you use wasm-tools (to substitute for wat2wasm) to convert between text and binary you can write your own that way. There’s examples of using wasm-tools dump here https://github.com/bytecodealliance/wasm-tools/tree/main/tests/cli/dump, the inverse operation is available as well, ask —help because I’m on mobile

CLI and Rust libraries for low-level manipulation of WebAssembly modules - bytecodealliance/wasm-tools

view this post on Zulip Brandon Bloom (May 12 2024 at 01:46):

Thanks. I've seen the WIT syntax, but couldn't find any documentation of the s-expr WAT-style syntax for components. Given the lack of mention of the syntax or encoding, I just kind of assumed components "macro-expanded" down to normal wasm modules. Sounds like you're saying that's not true. Could you please point me to where these new formats are documented?

view this post on Zulip Brandon Bloom (May 12 2024 at 05:52):

I guess it doesn't exist yet, but will go here: https://github.com/WebAssembly/component-model/tree/main/spec

I did manage to find https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md and https://github.com/WebAssembly/component-model/blob/main/design/mvp/Binary.md now... so I guess I'll study those. Sorry for the beginner question in what is probably the wrong channel :)

Repository for design and specification of the Component Model - WebAssembly/component-model
Repository for design and specification of the Component Model - WebAssembly/component-model
Repository for design and specification of the Component Model - WebAssembly/component-model

Last updated: Dec 23 2024 at 13:07 UTC