How to import WASI and write a simple console log function in WASM WAT

Viewed 242

I want to understand the WebAssembly text format for educational purposes and for writing a POC compiler that compiles straight to WASM.

I want to understand how to properly import the necessary WASI functions to create a console log function.

I'd love to see an example written in somewhat readable WAT of a module that imports WASI and implements a simple console log/printf/echo function that can run outside the browser.

e.g.,

(module
  (func $wasi_snapshot_preview1.fd_close (;0;) (import "wasi_snapshot_preview1" "fd_close") (param i32) (result i32))
  (func $wasi_snapshot_preview1.fd_seek (;1;) (import "wasi_snapshot_preview1" "fd_seek") (param i32 i64 i32 i32) (result i32))
  (func $wasi_snapshot_preview1.fd_write (;2;) (import "wasi_snapshot_preview1" "fd_write") (param i32 i32 i32 i32) (result i32))
  (func $wasi_snapshot_preview1.proc_exit (;3;) (import "wasi_snapshot_preview1" "proc_exit") (param i32))
  (data (; ... ;) )
  (memory (; ... ;) )
  (func $log (; ... ;))
)
0 Answers
Related