protoverse

A metaverse protocol
git clone git://jb55.com/protoverse
Log | Files | Refs | README | LICENSE

commit e42994f98ba262fb6d6d7bfdf27b4e0bcbd7fca6
parent bf9d77d9520811a070fb26a83fe7912a8969dc14
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 31 Jul 2021 16:02:03 -0700

retval for testing

Diffstat:
MMakefile | 3++-
Aruntests | 8++++++++
Msrc/bench.c | 3++-
Msrc/protoverse.c | 6+++---
Msrc/wasm.c | 15++++++++++++---
Msrc/wasm.h | 4++--
Awasm/test/ifelse.wat | 42++++++++++++++++++++++++++++++++++++++++++
Awasm/test/ifelse2.wat | 40++++++++++++++++++++++++++++++++++++++++
8 files changed, 111 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile @@ -63,8 +63,9 @@ test: src/test.c $(OBJS) @echo "ld $@" @$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@ -check: test +check: test protoverse @./test + ./runtests tags: fake ctags src/*.c src/*.h > $@ diff --git a/runtests b/runtests @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +printf "\nWASM TESTS\n" +for test in wasm/test/*.wasm +do + printf 'running %s... ' $test + ./protoverse run $test >/dev/null && echo "ok." || echo 'fail' +done diff --git a/src/bench.c b/src/bench.c @@ -13,6 +13,7 @@ static int bench_wasm(unsigned char *wasm, unsigned long len, int times, struct wasm_interp interp; struct timespec t1, t2; int i, ops = 0; + int retval; long nanos, ms; wasm_parser_init(&p, wasm, len, len*16); @@ -31,7 +32,7 @@ static int bench_wasm(unsigned char *wasm, unsigned long len, int times, clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1); for (i = 0; i < times; i++) { - if (!interp_wasm_module(&interp)) { + if (!interp_wasm_module(&interp, &retval)) { print_error_backtrace(&interp.errors); } ops += interp.ops; diff --git a/src/protoverse.c b/src/protoverse.c @@ -111,6 +111,7 @@ int main(int argc, const char *argv[]) struct protoverse_server server; u16 root; int ok; + int retval; size_t len; if (argc < 2) @@ -159,10 +160,9 @@ int main(int argc, const char *argv[]) perror("mmap"); return 1; } - if (!run_wasm(wasm_data, len, argc - 2, argv + 2, env)) { - return 2; - } + run_wasm(wasm_data, len, argc - 2, argv + 2, env, &retval); munmap(wasm_data, len); + return retval; } return 0; diff --git a/src/wasm.c b/src/wasm.c @@ -411,7 +411,11 @@ static INLINE int stack_pushval(struct wasm_interp *interp, struct val *val) static int interp_exit(struct wasm_interp *interp) { + struct val *vals = NULL; + if (!get_params(interp, &vals, 1)) + return interp_error(interp, "exit param missing?"); interp->quitting = 1; + stack_push_i32(interp, vals[0].num.i32); return 0; } @@ -6463,9 +6467,10 @@ void wasm_interp_free(struct wasm_interp *interp) free(interp->memory.start); } -int interp_wasm_module(struct wasm_interp *interp) +int interp_wasm_module(struct wasm_interp *interp, int *retval) { interp->ops = 0; + *retval = 0; if (interp->module->code_section.num_funcs == 0) { interp_error(interp, "empty module"); @@ -6494,10 +6499,13 @@ int interp_wasm_module(struct wasm_interp *interp) } if (interp_code(interp)) { + *retval = 0; debug("interp success!!\n"); } else if (interp->quitting) { + stack_pop_i32(interp, retval); debug("finished running via process exit\n"); } else { + *retval = 8; return interp_error(interp, "interp_code"); } @@ -6505,7 +6513,8 @@ int interp_wasm_module(struct wasm_interp *interp) } int run_wasm(unsigned char *wasm, unsigned long len, - int argc, const char **argv, char **env) + int argc, const char **argv, char **env, + int *retval) { struct wasm_parser p; struct wasm_interp interp; @@ -6524,7 +6533,7 @@ int run_wasm(unsigned char *wasm, unsigned long len, setup_wasi(&interp, argc, argv, env); - if (!interp_wasm_module(&interp)) { + if (!interp_wasm_module(&interp, retval)) { print_error_backtrace(&interp.errors); } diff --git a/src/wasm.h b/src/wasm.h @@ -701,13 +701,13 @@ struct wasm_parser { }; -int run_wasm(unsigned char *wasm, unsigned long len, int argc, const char **argv, char **env); +int run_wasm(unsigned char *wasm, unsigned long len, int argc, const char **argv, char **env, int *retval); int parse_wasm(struct wasm_parser *p); int wasm_interp_init(struct wasm_interp *interp, struct module *module); void wasm_parser_free(struct wasm_parser *parser); void wasm_parser_init(struct wasm_parser *parser, u8 *wasm, size_t wasm_len, size_t arena_size); void wasm_interp_free(struct wasm_interp *interp); -int interp_wasm_module(struct wasm_interp *interp); +int interp_wasm_module(struct wasm_interp *interp, int *retval); void print_error_backtrace(struct errors *errors); void setup_wasi(struct wasm_interp *interp, int argc, const char **argv, char **env); diff --git a/wasm/test/ifelse.wat b/wasm/test/ifelse.wat @@ -0,0 +1,42 @@ +(module + (func $add (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.add) + (func $sub (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.sub + ) + (func $start (result i32) + (local i32 i32) + i32.const 0 + local.set 0 + block + local.get 0 + i32.const 1 + i32.add + local.set 0 + i32.const 4 + local.get 0 + i32.gt_u + if + nop + br 0 + if + nop + else + nop + end + else + unreachable + block + nop + end + end + end + i32.const 0 + ) + (export "_start" (func $start)) + (export "add" (func $add)) + (export "sub" (func $sub))) diff --git a/wasm/test/ifelse2.wat b/wasm/test/ifelse2.wat @@ -0,0 +1,40 @@ +(module + (func $add (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.add) + (func $sub (param $lhs i32) (param $rhs i32) (result i32) + local.get $lhs + local.get $rhs + i32.sub + ) + (func $start (result i32) + (local i32 i32) + i32.const 0 + local.set 0 + i32.const 0 + if + (block (result i32) + i32.const 0 + if + nop + end + local.get 0 + i32.const 1 + i32.add + local.set 0 + i32.const 4 + local.get 0 + i32.gt_u + (if (result i32) + (then local.get 0) + (else + local.get 1)) + ) + local.set 1 + end + i32.const 0 + ) + (export "_start" (func $start)) + (export "add" (func $add)) + (export "sub" (func $sub)))