aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-23 17:39:00 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-23 17:39:00 +0200
commit7c789a974625bcee34a22757344cf4fbe268e0b5 (patch)
tree75e1a3bbc7deee71e142ac938816ec63424ea582
parent99fa88f971f23a3422f370d6f05b277c102ee9c3 (diff)
Implement the `invoke_vii` Primitive
We use a linked list of frames and unwind to the latest. The code turned out to be very simple, just following what the Emscripten export does.
-rw-r--r--src/env.c39
-rw-r--r--src/env.h9
2 files changed, 37 insertions, 11 deletions
diff --git a/src/env.c b/src/env.c
index 34b8a0b..a854ced 100644
--- a/src/env.c
+++ b/src/env.c
@@ -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) {
diff --git a/src/env.h b/src/env.h
index e81f569..7bbc8c6 100644
--- a/src/env.h
+++ b/src/env.h
@@ -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;