diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/env.c | 39 | ||||
| -rw-r--r-- | src/env.h | 9 |
2 files changed, 37 insertions, 11 deletions
@@ -55,6 +55,7 @@ struct w2c_env *pw_env_init(size_t memory) { } env->memory.size = pages * wasm2c_pico8_pagesize_env_memory; env->memory.data_end = env->memory.data + env->memory.size; + SDL_Log("allocated %llu", env->memory.size); env->table.data = SDL_calloc(wasm2c_pico8_min_env_table, sizeof(*env->table.data)); @@ -935,10 +936,17 @@ uint32_t w2c_env_0x5Flocaltime(struct w2c_env *env, uint32_t time) { } void w2c_env_0x5Flongjmp(struct w2c_env *env, uint32_t jmpbuf, uint32_t value) { - (void)env; - (void)jmpbuf; - (void)value; - env_unimplemented("_longjmp"); + struct pw_env_frame *frame = env->data.frame; + if (!frame) { + SDL_LogWarn(0, "attempt to longjmp without invocation handler setup"); + env_abort(); + } + + // Make sure that the value cannot be 0. + value = SDL_max(value, 1); + + w2c_pico8_0x5FsetThrew(env->context.instance, jmpbuf, value); + longjmp(frame->buf, value); } uint32_t w2c_env_0x5Fstrftime(struct w2c_env *env, uint32_t buf, uint32_t sz, @@ -1007,13 +1015,22 @@ uint32_t w2c_env_abortOnCannotGrowMemory(struct w2c_env *env, uint32_t size) { uint32_t w2c_env_getTempRet0(struct w2c_env *env) { return env->data.tempret; } -void w2c_env_invoke_vii(struct w2c_env *env, uint32_t a, uint32_t b, - uint32_t c) { - (void)env; - (void)a; - (void)b; - (void)c; - env_unimplemented("invoke_vii"); +void w2c_env_invoke_vii(struct w2c_env *env, uint32_t fn, uint32_t a, + uint32_t b) { + struct pw_env_frame frame = { + .parent = env->data.frame, + }; + + uint32_t sp = w2c_pico8_stackSave(env->context.instance); + env->data.frame = &frame; + if (setjmp(frame.buf)) { + w2c_pico8_dynCall_vii(env->context.instance, fn, a, b); + } else { + // Stack unwinding - restore the stack pointer. + w2c_pico8_stackRestore(env->context.instance, sp); + w2c_pico8_0x5FsetThrew(env->context.instance, 1, 0); + } + env->data.frame = frame.parent; } void w2c_env_setTempRet0(struct w2c_env *env, uint32_t value) { @@ -25,6 +25,14 @@ struct pw_env_ctx { }; /** + * An invocation frame exception handler. + */ +struct pw_env_frame { + struct pw_env_frame *parent; + jmp_buf buf; +}; + +/** * The Web Assembly host environment. * * This holds the required state to implement the host functions imported by @@ -36,6 +44,7 @@ struct w2c_env { struct pw_env_ctx context; struct pw_env_data { + struct pw_env_frame *frame; uint32_t dynamictop; uint32_t tablebase; uint32_t mainloop; |
