Stream: general

Topic: Generating a component from hand written WAT


view this post on Zulip Piotr Sarnacki (Nov 02 2024 at 22:24):

In my quest to understand WASM better I got to components and it's quite a lot to take in, so I wanted to first have a working example. In one of the discussions I found @Alex Crichton's example of using some of the component model exported functions:

;; foo.wat
(module
  (import "wasi:io/streams@0.2.1" "[method]output-stream.blocking-write-and-flush" (func $blocking-write-and-flush (param i32 i32 i32 i32)))
  (import "wasi:cli/stdout@0.2.1" "get-stdout" (func $get-stdout (result i32)))
  (func (export "wasi:cli/run@0.2.1|run") (result i32)
    (call $blocking-write-and-flush
      (call $get-stdout)
      (i32.const 100)
      (i32.const 6)
      (i32.const 200))
    (i32.const 0)
  )

  (memory (export "memory") 1)

  (data (i32.const 100) "hello\n")
)

and then running:

 wasm-tools component embed ./wit --world just-testing foo.wat -o foo.core.wasm
wasm-tools component new foo.core.wasm -o foo.component.wasm
wasmtime run foo.component.wasm

This results in:

Error: failed to run main module `foo.component.wasm`

Caused by:
    no exported instance named `wasi:cli/run@0.2.1`

I'm guessing the wat module is missing some instruction for exporting the run function, but my understanding of the component related instructions is still somewhat lacking, so I can't tell what's missing. I was trying to modify the wat file for the component by hand, but the closest I got to re-exporting the run function was using (alias export 3 "wasi:cli/run@0.2.1|run" (func)), but again, not fully understanding what needs to be re-exported it just resulted in the following error:

Caused by:
    type mismatch for export `0` of module instantiation argument ``
    expected: [i32 i32 i32 i32] -> []
    found:    [] -> [i32] (at offset 0x505)

I'll be re-reading the component model explainer and hopefully I'll find an answer there, but if anyone knows how to fix it, it would help me immensely in continuing my experiments.


Last updated: Nov 22 2024 at 16:03 UTC