1
0
forked from ground/ground-old
This commit is contained in:
2026-03-14 13:48:04 +11:00
parent 4be8eeefdf
commit cd59281f0c
3 changed files with 46 additions and 1 deletions

View File

@@ -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));

View File

@@ -2,6 +2,20 @@
#include <inttypes.h>
#include <stdlib.h>
#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;

View File

@@ -10,6 +10,24 @@
#define MAX_ID_LEN 64
// If targeting WASM, define WASM specific stuff
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
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;