Skip to content

Instantly share code, notes, and snippets.

Created April 16, 2021 06:11
What would you like to do?
Compile custom V code to WASM

Compile V Code to WASM

As of 2021.02.11 --

If you don't already have V installed, do so by running

git clone
cd v
sudo ./v symlink
# Create file cp.v that copies <input_file> to <output_file>
cat > cp.v <<'EOF'
import os

fn main() {
	if os.args.len != 3 {
		eprintln('usage: ${os.base(os.args[0])}.v <input_file> <output_file>')

	src := os.args[1]
	dest := os.args[2]

	os.cp(src, dest) or {
		eprintln('Error copying input file $src to output file $dest -- $err')

# Compile cp.v to cp.c
v -o cp.c cp.v

# Install wasienv
curl | sh
source ~/.wasienv/
cp cp.c cp2.c  # Create backup

wasicc cp.c -o cp.wasm  # Will likely error out
# If so, manually delete the function containing 'WNOHANG', namely
# os__Process_unix_is_alive
${EDITOR:-nano} cp2.c

Once os__Process_unix_is_alive is deleted, save the file then run

wasicc cp2.c -o cp.wasm
echo 'Hello from newly-created copy of hello.txt!' > hello.txt
wasmer --dir=. cp.wasm hello.txt created.txt
cat created.txt

You should now see

Hello from newly-created copy of hello.txt!

on your screen from our WASM program cp.wasm that copied hello.txt to created.txt! 🎉

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment