From cd59281f0c67906d779a7aea35c5e425852c9c7f Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 14 Mar 2026 13:48:04 +1100 Subject: [PATCH] Stuff --- src/interface.c | 15 ++++++++++++++- src/types.c | 14 ++++++++++++++ src/types.h | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/interface.c b/src/interface.c index 460576e..b5d58bb 100644 --- a/src/interface.c +++ b/src/interface.c @@ -150,7 +150,20 @@ GroundValue groundRunFunction(GroundFunction* function, size_t argc, ...) { va_list args; va_start(args, argc); - GroundScope callScope = copyGroundScope(&function->closure); + if (function == NULL) { + return createErrorGroundValue(createGroundError("Null function passed to groundRunFunction", "valueError", NULL, NULL)); + } + + // Seems to crash some functions + // GroundScope callScope = copyGroundScope(&function->closure); + + GroundScope callScope = { + .labels = malloc(sizeof(GroundLabel*)), + .variables = malloc(sizeof(GroundVariable*)), + .isMainScope = false + }; + *callScope.variables = NULL; + *callScope.labels = NULL; if (argc != function->argSize) { return createErrorGroundValue(createGroundError("Too few or too many arguments for function", "callError", NULL, NULL)); diff --git a/src/types.c b/src/types.c index 2e3e5da..6123b32 100644 --- a/src/types.c +++ b/src/types.c @@ -2,6 +2,20 @@ #include #include +#ifdef __EMSCRIPTEN__ +static char out_buf[65536]; +static int out_pos = 0; + +void wasm_print(const char* str) { + int len = strlen(str); + if (out_pos + len < (int)sizeof(out_buf) - 1) { + memcpy(out_buf + out_pos, str, len); + out_pos += len; + out_buf[out_pos] = '\0'; + } +} +#endif + GroundValue createIntGroundValue(int64_t in) { GroundValue gv; gv.data.intVal = in; diff --git a/src/types.h b/src/types.h index 458327a..862ad4d 100644 --- a/src/types.h +++ b/src/types.h @@ -10,6 +10,24 @@ #define MAX_ID_LEN 64 +// If targeting WASM, define WASM specific stuff +#ifdef __EMSCRIPTEN__ +#include + +void wasm_print(const char* str); + +#undef printf +#define printf(fmt, ...) do { \ + int __needed = snprintf(NULL, 0, fmt, ##__VA_ARGS__) + 1; \ + char* __buf = malloc(__needed); \ + if (__buf) { \ + snprintf(__buf, __needed, fmt, ##__VA_ARGS__); \ + wasm_print(__buf); \ + free(__buf); \ + } \ +} while(0) +#endif + typedef enum GroundInstType { IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, ITOC, CTOI, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD } GroundInstType;