aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-17 14:06:37 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-17 14:06:37 +0200
commit7d29faead63abf66a9ef35606a732ca3493c6fbe (patch)
tree2927b97ef942d3aa3f8c4f2ac4dacab6693d5c4e
parent1aa6444759c2e564f5b99c0cd65f7c2bb134758a (diff)
Add Globals
Note that we fail to build on linking errors - we have a lot of missing `env` functions to implement since moving away from the stub. Now the real fun begins...
-rw-r--r--Makefile10
-rw-r--r--src/env.c21
-rw-r--r--src/env.h8
-rw-r--r--src/game.c2
4 files changed, 31 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 426fc9d..ed56369 100644
--- a/Makefile
+++ b/Makefile
@@ -6,8 +6,6 @@ HOSTCC ?= cc
PICO8 ?= pico8
WASM2C ?= wasm2c
-WAT2WASM ?= wat2wasm
-
ARCH ?= $(shell uname -m)
CFLAGS ?= -std=c99 -Wall -Wextra -O2 -MMD -Isrc -Ilib -I$(SYSTEM) \
$(shell pkg-config sdl3 --cflags)
@@ -38,8 +36,7 @@ $(OUT)/system/%.o: $(SYSTEM)/%.c
@ mkdir -p $(dir $@)
@ $(CC) $(CFLAGS) -Wno-unused -Wno-unused-parameter -c $< -o $@
-$(SYSTEM)/pico8.h $(SYSTEM)/pico8.c: $(SYSTEM)/stub/pico8.wasm \
- $(SYSTEM)/pico8.wasm
+$(SYSTEM)/pico8.h $(SYSTEM)/pico8.c: $(SYSTEM)/pico8.wasm
@ echo "WASM2C\t$(SYSTEM)/pico8.h $(SYSTEM)/pico8.c"
@ $(WASM2C) $(W2CFLAGS) $< -o $(SYSTEM)/pico8.c
@ echo '#include "wasmrt.h"' > $(SYSTEM)/wasm-rt.h
@@ -70,11 +67,6 @@ $(SYSTEM)/%.png: carts/%.p8
@ mkdir -p $(dir $@)
@ $(PICO8) $< -export $@ >/dev/null
-$(SYSTEM)/stub/%.wasm: stub/%.wat
- @ echo "WAT2WASM\t$@"
- @ mkdir -p $(dir $@)
- @ $(WAT2WASM) $< -o $@
-
$(TOOLS)/%: tools/%.c
@ echo "CC\t$@"
@ mkdir -p $(dir $@)
diff --git a/src/env.c b/src/env.c
index 71858e3..6d5078a 100644
--- a/src/env.c
+++ b/src/env.c
@@ -1,3 +1,5 @@
+#include <math.h>
+
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_timer.h>
@@ -244,3 +246,22 @@ void w2c_env_0x5Femscripten_set_main_loop(struct w2c_env *env, uint32_t func,
(void)fps;
(void)simulate_infinite_loop;
}
+
+static struct w2c_global shared_global = {
+ .infinity = INFINITY,
+ .nan = NAN,
+};
+
+double *w2c_global_Infinity(struct w2c_global *global) {
+ if (!global) {
+ global = &shared_global;
+ }
+ return &global->infinity;
+}
+
+double *w2c_global_NaN(struct w2c_global *global) {
+ if (!global) {
+ global = &shared_global;
+ }
+ return &global->nan;
+}
diff --git a/src/env.h b/src/env.h
index 4f99d2e..54d159f 100644
--- a/src/env.h
+++ b/src/env.h
@@ -33,6 +33,14 @@ struct w2c_env {
};
/**
+ * The Web Assembly host globals.
+ */
+struct w2c_global {
+ double infinity;
+ double nan;
+};
+
+/**
* Allocates a new host environment with the specified Web Assembly memory size.
*/
struct w2c_env *pw_env_init(size_t memory);
diff --git a/src/game.c b/src/game.c
index fa031a0..84b17f7 100644
--- a/src/game.c
+++ b/src/game.c
@@ -80,7 +80,7 @@ bool pw_game_start(struct pw_game *game, struct pw_multicart *carts) {
.carts = game->carts,
.instance = &game->instance,
});
- wasm2c_pico8_instantiate(&game->instance, game->env);
+ wasm2c_pico8_instantiate(&game->instance, game->env, NULL);
uint32_t argv;
{