Mozilla shared WASI and how to use Wasmtime to run .wasm file in their blog post. The programming language they demonstrated is Rust:
#[wasm_bindgen]
pub fn render(input: &str) -> String {
let parser = Parser::new(input);
let mut html_output = String::new();
html::push_html(&mut html_output, parser);
return html_output;
}
However, I want to do the same thing in C.
I've downloaded wasi-libc and tried to build a 'hello world' program with Clang.
I created two functions in test.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int foo1()
{
printf("Hello foo1()\n");
return 0;
}
int foo2(char* filename)
{
printf("Hello foo2()\n");
printf("filename is %s\n", filename);
return 0;
}
Build it with the command:
clang --target=wasm32-wasi --sysroot=/mnt/d/code/wasi-libc/sysroot test.c -o test.wasm -nostartfiles -Wl,--no-entry,--export=foo1,--export=foo2
Run the wasm file to invoke functions:
$ wasmtime test.wasm --invoke foo1
Hello foo1()
warning: using `--render` with a function that returns values is experimental and may break in the future
0
$ wasmtime test.wasm --invoke foo2 "hello"
warning: using `--render` with a function that takes arguments is experimental and may break in the future
error: failed to process main module `test.wasm`
caused by: invalid digit found in string
I failed to invoke the function with an input parameter.
What's the difference between Rust and C? Is Rust currently the only way to build wasm lib file?
The difference is that the Rust toolchain has experimental support for Interface Types, whereas that doesn't yet exist for C, unfortunately. The
#[wasm_bindgen]
above therender
function is what turnsrender
into a function exported with Interface Types bindings.