Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions wasm2c/examples/hello-wasi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.o
hello-wasi
hello-wasi.wasm
hello-wasi-wasm.*
test.out

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually tmp files goes to the bin/ build/ or out/ directories when wabt is compiled. Can these files goes there as well (and no need to add them to gitignore)?

21 changes: 21 additions & 0 deletions wasm2c/examples/hello-wasi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
WABT_ROOT=../../..
CC=clang
CFLAGS=-I$(WABT_ROOT)/wasm2c -I$(WABT_ROOT)/third_party/uvwasi/include -O3
LDFLAGS=-L$(WABT_ROOT)/build/_deps/libuv-build -L$(WABT_ROOT)/build/third_party/uvwasi
LDLIBS=-luvwasi_a -luv_a -lm -lpthread

all: hello-wasi

clean:
rm -rf hello-wasi hello-wasi.wasm hello-wasi-wasm.c hello-wasi-wasm.h test.out *.o

hello-wasi.wasm: hello-wasi.c
/opt/wasi-sdk/bin/clang -O3 $< -o $@

hello-wasi-wasm.c: hello-wasi.wasm $(WABT_ROOT)/bin/wasm2c
$(WABT_ROOT)/bin/wasm2c $< -n hello_wasi -o $@

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
$(CC) $(LDFLAGS) $(CFLAGS) $^ -o $@ $(LDLIBS)

.PHONY: all clean
33 changes: 33 additions & 0 deletions wasm2c/examples/hello-wasi/hello-wasi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
printf("printing args\n");
for (int i = 0; i < argc; i++) {
printf("[%d] %s \n", i, argv[i]);
}

printf("Writing to stdout\n");
char* hello_str = strdup("hello world!");
puts(hello_str);

printf("writing test.out\n");
FILE* fp = fopen("test.out", "w+");
assert(fp != NULL);
fprintf(fp, "hello filesystem\n");
fclose(fp);
printf("removing test.out\n");
assert(!unlink("test.out"));

printf("writing /tmp/test.out\n");
fp = fopen("/tmp/test.out", "w+");
assert(fp != NULL);
fprintf(fp, "hello filesystem\n");
fclose(fp);
printf("removing /tmp/test.out\n");
assert(!unlink("/tmp/test.out"));

return 0;
}
49 changes: 49 additions & 0 deletions wasm2c/examples/hello-wasi/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>

#include "hello-wasi-wasm.h"
#include "wasm-rt-uvwasi-adapter.h"
#include "uvwasi.h"

int main(int argc, const char** argv) {
w2c_hello__wasi hello = {0};
struct w2c_wasi__snapshot__preview1 wasi = {0};
uvwasi_t uvwasi = {0};
uvwasi_options_t init_options = {0};
uvwasi_preopen_t preopens[2] = {0};

init_options.in = 0;
init_options.out = 1;
init_options.err = 2;
init_options.fd_table_size = 10;

extern const char** environ;
init_options.argc = argc;
init_options.argv = argv;
init_options.envp = (const char**)environ;

preopens[0].mapped_path = "/tmp";
preopens[0].real_path = "/tmp";
preopens[1].mapped_path = ".";
preopens[1].real_path = ".";
init_options.preopenc = 2;
init_options.preopens = preopens;
init_options.allocator = NULL;

wasm_rt_init();
uvwasi_errno_t ret = uvwasi_init(&uvwasi, &init_options);
if (ret != UVWASI_ESUCCESS) {
fprintf(stderr, "uvwasi_init failed with error %u\n", ret);
return 1;
}

wasm2c_uvwasi_link(&wasi, &hello.w2c_memory, &uvwasi);
wasm2c_hello__wasi_instantiate(&hello, &wasi);
w2c_hello__wasi_0x5Fstart(&hello);
wasm2c_hello__wasi_free(&hello);

uvwasi_destroy(&uvwasi);
wasm_rt_free();

return 0;
}
Loading
Loading