From 7d29faead63abf66a9ef35606a732ca3493c6fbe Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Sat, 17 Oct 2026 14:06:37 +0200 Subject: 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... --- Makefile | 10 +--------- src/env.c | 21 +++++++++++++++++++++ src/env.h | 8 ++++++++ src/game.c | 2 +- 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 + #include #include #include @@ -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 @@ -32,6 +32,14 @@ struct w2c_env { uint32_t main_loop; }; +/** + * The Web Assembly host globals. + */ +struct w2c_global { + double infinity; + double nan; +}; + /** * Allocates a new host environment with the specified Web Assembly memory size. */ 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; { -- cgit v1.2.3