~jb55/protoverse

45e611a89ce65ada4eea978bab827cef3623a3bd — William Casarin 2 months ago 4ae260e
print callstack
2 files changed, 18 insertions(+), 2 deletions(-)

M src/wasm.c
M src/wasm.h
M src/wasm.c => src/wasm.c +17 -2
@@ 41,12 41,10 @@ struct expr_parser {
	struct cursor *stack; // optional
};

#ifdef DEBUG
static INLINE struct callframe *top_callframes(struct cursor *cur, int top)
{
	return (struct callframe*)cursor_topn(cur, sizeof(struct callframe), top);
}
#endif

static INLINE struct callframe *top_callframe(struct cursor *cur)
{


@@ 429,6 427,22 @@ static void print_stack(struct cursor *stack)
	stack->p = p;
}

void print_callstack(struct wasm_interp *interp)
{
	int i = 0;
	struct callframe *frame;
	struct func *func;

	printf("callstack:\n");
	while ((frame = top_callframes(&interp->callframes, i++))) {
		func = get_fn(interp->module, frame->fn);
		if (!func)
			printf("??\n");
		else
			printf("%d %s:%d\n", i, func->name, frame->fn);
	}
}

static INLINE int cursor_pushval(struct cursor *cur, struct val *val)
{
	return cursor_push(cur, (u8*)val, sizeof(*val));


@@ 7035,6 7049,7 @@ int run_wasm(unsigned char *wasm, unsigned long len,
	setup_wasi(&interp, argc, argv, env);

	if (!interp_wasm_module(&interp, retval)) {
		print_callstack(&interp);
		print_error_backtrace(&interp.errors);
	}


M src/wasm.h => src/wasm.h +1 -0
@@ 724,5 724,6 @@ void wasm_interp_free(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);
void print_callstack(struct wasm_interp *interp);

#endif /* PROTOVERSE_WASM_H */