Open6

v wasi

zztkmzztkm

とりあえず v help build-c の通りにやってみた

❯ v -os wasm32-wasi -o hello.wasm main.v
Please use `-os wasm32_wasi` instead.
Note: V can only cross compile to windows and linux for now by default.
It will use `cc` as a cross compiler for now, although that will probably fail.
Set `VCROSS_COMPILER_NAME` to the name of your cross compiler, for your target OS: WebAssembly(WASI) .
builder error: Header file <gc.h>, needed for module `builtin` was not found. Please install the corresponding development headers.

-os wasm32_wasi を指定しろと言われた、まあ変えたところでエラーになるけど

❯ v -os wasm32_wasi -o hello.wasm main.v
Note: V can only cross compile to windows and linux for now by default.
It will use `cc` as a cross compiler for now, although that will probably fail.
Set `VCROSS_COMPILER_NAME` to the name of your cross compiler, for your target OS: WebAssembly(WASI) .
builder error: Header file <gc.h>, needed for module `builtin` was not found. Please install the corresponding development headers.

Header file <gc.h> が必要らしいのでインストールする

 sudo apt-get install libgc-dev

またやってみる

❯ v -os wasm32_wasi -o hello.wasm main.v
Note: V can only cross compile to windows and linux for now by default.
It will use `cc` as a cross compiler for now, although that will probably fail.
Set `VCROSS_COMPILER_NAME` to the name of your cross compiler, for your target OS: WebAssembly(WASI) .
❯ ls
 hello.wasm   main.v

お!出力されてる!

zztkmzztkm

wasmer で試してみた

❯ wasmer hello.wasm
error: Unable to compile "hello.wasm"
╰─▶ 1: WebAssembly translation error: Error when converting wat: input bytes aren't valid utf-8

ほう...

zztkmzztkm

別のビルド方法を試す

v -b wasm -os wasi -o hello.wasm main.v
The WebAssembly backend requires `binaryen`, an external library dependency
This can be installed with `./cmd/tools/install_binaryen.vsh`, to download prebuilt libraries for your platform

言われたとおりに install_binaryen.vsh を実行してみる

❯ ~/dev/github.com/vlang/v/cmd/tools/install_binaryen.vsh
Downloading archive: /home/zztkm/dev/github.com/vlang/v/thirdparty/binaryen-version_112.tar.gz, from url: https://github.com/WebAssembly/binaryen/releases/download/version_112/binaryen-version_112-x86_64-linux.tar.gz ...
Extracting `/home/zztkm/dev/github.com/vlang/v/thirdparty/binaryen-version_112` to `/home/zztkm/dev/github.com/vlang/v/thirdparty/binaryen` ...
binaryen-version_112/
binaryen-version_112/lib/
binaryen-version_112/lib/libbinaryen.a
binaryen-version_112/include/
binaryen-version_112/include/binaryen-c.h
binaryen-version_112/include/wasm-delegations.def
binaryen-version_112/bin/
binaryen-version_112/bin/wasm2js
binaryen-version_112/bin/wasm-reduce
binaryen-version_112/bin/wasm-metadce
binaryen-version_112/bin/wasm-emscripten-finalize
binaryen-version_112/bin/wasm-as
binaryen-version_112/bin/wasm-split
binaryen-version_112/bin/wasm-shell
binaryen-version_112/bin/binaryen-unittests
binaryen-version_112/bin/wasm-dis
binaryen-version_112/bin/wasm-opt
binaryen-version_112/bin/wasm-fuzz-types
binaryen-version_112/bin/wasm-ctor-eval
tar -xvf /home/zztkm/dev/github.com/vlang/v/thirdparty/binaryen-version_112.tar.gz --directory /home/zztkm/dev/github.com/vlang/v/thirdparty
Moving `/home/zztkm/dev/github.com/vlang/v/thirdparty/binaryen-version_112` to `/home/zztkm/dev/github.com/vlang/v/thirdparty/binaryen` ...
Done. You can now use `v -b wasm file.v` .

良さそう。

ちょっと時間かかったけどビルドできたかも?

v -b wasm -os wasi -o hello.wasm main.v
❯ ls
 hello.wasm   main.v

wasmer で実行

❯ wasmer hello.wasm
Hello, wasi!
zztkmzztkm

Go の WASI ビルドとバイナリサイズを比較してみた。

module main

fn main() {
	println("Hello, wasi!")
}
package main

import "fmt"

func main() {
	fmt.Println("Hello, wasi!")
}

それぞれビルド

v -b wasm -os wasi -o hello-v main.v
❯ GOOS=wasip1 GOARCH=wasm go1.21rc2 build -o hello-go main.go

サイズを見てみる

2.0M zztkm 30 Jun 01:28  hello-go*
3.1k zztkm 30 Jun 01:28  hello-v.wasm

これだけ見ると V のほうが小さいバイナリを吐ける。