Skip to content

Commit d1ad36c

Browse files
committed
wasm2c: uvwasi adapter layer for wasm2c output
1 parent 23b62e9 commit d1ad36c

6 files changed

Lines changed: 1050 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.o
2+
hello-wasi
3+
hello-wasi.wasm
4+
hello-wasi-wasm.*
5+
test.out
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
WABT_ROOT=../../..
2+
CC=clang
3+
CFLAGS=-I$(WABT_ROOT)/wasm2c -I$(WABT_ROOT)/third_party/uvwasi/include -O3
4+
LDFLAGS=-L$(WABT_ROOT)/build/_deps/libuv-build -L$(WABT_ROOT)/build/third_party/uvwasi
5+
LDLIBS=-luvwasi_a -luv_a -lm -lpthread
6+
7+
all: hello-wasi
8+
9+
clean:
10+
rm -rf hello-wasi hello-wasi.wasm hello-wasi-wasm.c hello-wasi-wasm.h test.out *.o
11+
12+
hello-wasi.wasm: hello-wasi.c
13+
/opt/wasi-sdk/bin/clang -O3 $< -o $@
14+
15+
hello-wasi-wasm.c: hello-wasi.wasm $(WABT_ROOT)/bin/wasm2c
16+
$(WABT_ROOT)/bin/wasm2c $< -n hello_wasi -o $@
17+
18+
hello-wasi: main.c hello-wasi-wasm.c $(WABT_ROOT)/wasm2c/wasm-rt-uvwasi-adapter.c $(WABT_ROOT)/wasm2c/wasm-rt-impl.c $(WABT_ROOT)/wasm2c/wasm-rt-mem-impl.c
19+
$(CC) $(LDFLAGS) $(CFLAGS) $^ -o $@ $(LDLIBS)
20+
21+
.PHONY: all clean
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
6+
int main(int argc, char* argv[]) {
7+
printf("printing args\n");
8+
for (int i = 0; i < argc; i++) {
9+
printf("[%d] %s \n", i, argv[i]);
10+
}
11+
12+
printf("Writing to stdout\n");
13+
char* hello_str = strdup("hello world!");
14+
puts(hello_str);
15+
16+
printf("writing test.out\n");
17+
FILE* fp = fopen("test.out", "w+");
18+
assert(fp != NULL);
19+
fprintf(fp, "hello filesystem\n");
20+
fclose(fp);
21+
printf("removing test.out\n");
22+
assert(!unlink("test.out"));
23+
24+
printf("writing /tmp/test.out\n");
25+
fp = fopen("/tmp/test.out", "w+");
26+
assert(fp != NULL);
27+
fprintf(fp, "hello filesystem\n");
28+
fclose(fp);
29+
printf("removing /tmp/test.out\n");
30+
assert(!unlink("/tmp/test.out"));
31+
32+
return 0;
33+
}

wasm2c/examples/hello-wasi/main.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include "hello-wasi-wasm.h"
5+
#include "wasm-rt-uvwasi-adapter.h"
6+
#include "uvwasi.h"
7+
8+
int main(int argc, const char** argv) {
9+
w2c_hello__wasi hello = {0};
10+
struct w2c_wasi__snapshot__preview1 wasi = {0};
11+
uvwasi_t uvwasi = {0};
12+
uvwasi_options_t init_options = {0};
13+
uvwasi_preopen_t preopens[2] = {0};
14+
15+
init_options.in = 0;
16+
init_options.out = 1;
17+
init_options.err = 2;
18+
init_options.fd_table_size = 10;
19+
20+
extern const char** environ;
21+
init_options.argc = argc;
22+
init_options.argv = argv;
23+
init_options.envp = (const char**)environ;
24+
25+
preopens[0].mapped_path = "/tmp";
26+
preopens[0].real_path = "/tmp";
27+
preopens[1].mapped_path = ".";
28+
preopens[1].real_path = ".";
29+
init_options.preopenc = 2;
30+
init_options.preopens = preopens;
31+
init_options.allocator = NULL;
32+
33+
wasm_rt_init();
34+
uvwasi_errno_t ret = uvwasi_init(&uvwasi, &init_options);
35+
if (ret != UVWASI_ESUCCESS) {
36+
fprintf(stderr, "uvwasi_init failed with error %u\n", ret);
37+
return 1;
38+
}
39+
40+
wasm2c_uvwasi_link(&wasi, &hello.w2c_memory, &uvwasi);
41+
wasm2c_hello__wasi_instantiate(&hello, &wasi);
42+
w2c_hello__wasi_0x5Fstart(&hello);
43+
wasm2c_hello__wasi_free(&hello);
44+
45+
uvwasi_destroy(&uvwasi);
46+
wasm_rt_free();
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)