An example of linking WASI to the runtime in order to interact with the system. It uses the WAT code from https://github.com/bytecodealliance/wasmtime/blob/main/docs/WASI-tutorial.md#web-assembly-text-example
()
| 582 | // An example of linking WASI to the runtime in order to interact with the system. |
| 583 | // It uses the WAT code from https://github.com/bytecodealliance/wasmtime/blob/main/docs/WASI-tutorial.md#web-assembly-text-example |
| 584 | func ExampleWasiConfig() { |
| 585 | dir, err := os.MkdirTemp("", "out") |
| 586 | if err != nil { |
| 587 | log.Fatal(err) |
| 588 | } |
| 589 | defer os.RemoveAll(dir) |
| 590 | stdoutPath := filepath.Join(dir, "stdout") |
| 591 | |
| 592 | engine := wasmtime.NewEngine() |
| 593 | |
| 594 | // Create our module |
| 595 | wasm, err := wasmtime.Wat2Wasm(` |
| 596 | (module |
| 597 | ;; Import the required fd_write WASI function which will write the given io vectors to stdout |
| 598 | ;; The function signature for fd_write is: |
| 599 | ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written |
| 600 | (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32))) |
| 601 | |
| 602 | (memory 1) |
| 603 | (export "memory" (memory 0)) |
| 604 | |
| 605 | ;; Write 'hello world\n' to memory at an offset of 8 bytes |
| 606 | ;; Note the trailing newline which is required for the text to appear |
| 607 | (data (i32.const 8) "hello world\n") |
| 608 | |
| 609 | (func $main (export "_start") |
| 610 | ;; Creating a new io vector within linear memory |
| 611 | (i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string |
| 612 | (i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string |
| 613 | |
| 614 | (call $fd_write |
| 615 | (i32.const 1) ;; file_descriptor - 1 for stdout |
| 616 | (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0 |
| 617 | (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one. |
| 618 | (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written |
| 619 | ) |
| 620 | drop ;; Discard the number of bytes written from the top of the stack |
| 621 | ) |
| 622 | ) |
| 623 | `) |
| 624 | if err != nil { |
| 625 | log.Fatal(err) |
| 626 | } |
| 627 | module, err := wasmtime.NewModule(engine, wasm) |
| 628 | if err != nil { |
| 629 | log.Fatal(err) |
| 630 | } |
| 631 | |
| 632 | // Create a linker with WASI functions defined within it |
| 633 | linker := wasmtime.NewLinker(engine) |
| 634 | err = linker.DefineWasi() |
| 635 | if err != nil { |
| 636 | log.Fatal(err) |
| 637 | } |
| 638 | |
| 639 | // Configure WASI imports to write stdout into a file, and then create |
| 640 | // a `Store` using this wasi configuration. |
| 641 | wasiConfig := wasmtime.NewWasiConfig() |
nothing calls this directly
no test coverage detected
searching dependent graphs…