From 53085d7e3a2fb345529cae9cace9bda79737b35a Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sun, 30 Nov 2025 20:43:25 -0600 Subject: [PATCH] remove jsffi --- Makefile | 2 +- meson.build | 3 +- source/cell.c | 70 ++++++++++++++++++++--------- source/jsffi.c | 119 ------------------------------------------------- source/jsffi.h | 8 ---- 5 files changed, 50 insertions(+), 152 deletions(-) delete mode 100644 source/jsffi.c delete mode 100644 source/jsffi.h diff --git a/Makefile b/Makefile index 36c17b74..22b97c04 100755 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ debug: FORCE meson setup build_dbg -Dbuildtype=debugoptimized - meson install --only-changed -C --verbose build_dbg + meson install --only-changed -C build_dbg fast: FORCE meson setup build_fast diff --git a/meson.build b/meson.build index 99a6f1ed..65caba6b 100644 --- a/meson.build +++ b/meson.build @@ -213,7 +213,6 @@ src += [ # core 'qjs_blob.c', 'monocypher.c', 'timer.c', - 'jsffi.c', 'cell.c', 'wildmatch.c', 'qjs_actor.c', @@ -313,7 +312,7 @@ cell = custom_target('cell', ) # Install headers for building dynamic libraries using Cell -install_headers('source/cell.h', 'source/jsffi.h') +install_headers('source/cell.h') install_headers('source/quickjs.h') install_headers('source/wota.h') diff --git a/source/cell.c b/source/cell.c index 3ac8e841..34e2cb88 100644 --- a/source/cell.c +++ b/source/cell.c @@ -1,16 +1,3 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #ifdef HAVE_MIMALLOC #include #endif @@ -802,6 +789,8 @@ static int actor_interrupt_cb(JSRuntime *rt, cell_rt *crt) return SDL_GetAtomicInt(&shutting_down) || crt->disrupt; } +JSValue js_os_use(JSContext *js); + void script_startup(cell_rt *prt) { JSRuntime *rt; @@ -830,17 +819,43 @@ void script_startup(cell_rt *prt) JS_SetContextOpaque(js, prt); prt->context = js; - ffi_load(js); - // Add core QOP blob to hidden + cell_rt *crt = JS_GetContextOpaque(js); + JS_FreeValue(js, js_blob_use(js)); + JSValue globalThis = JS_GetGlobalObject(js); - JSValue cell = JS_GetPropertyStr(js, globalThis, "cell"); - JSValue hidden = JS_GetPropertyStr(js, cell, "hidden"); + + JSValue cell = JS_NewObject(js); + JS_SetPropertyStr(js,globalThis,"cell", cell); + + JSValue hidden_fn = JS_NewObject(js); + + JS_SetPropertyStr(js, cell, "hidden", hidden_fn); + JS_SetPropertyStr(js, hidden_fn, "os", js_os_use(js)); + + const char actorsym_script[] = "var sym = Symbol(`actordata`); sym;"; + + JSValue actorsym = JS_Eval(js, actorsym_script, sizeof(actorsym_script)-1, "internal", 0); + + JS_SetPropertyStr(js, hidden_fn, "actorsym", actorsym); + + crt->actor_sym = JS_ValueToAtom(js, actorsym); + + if (crt->init_wota) { + JS_SetPropertyStr(js, hidden_fn, "init", wota2value(js, crt->init_wota)); + // init wota can now be freed + free(crt->init_wota); + crt->init_wota = NULL; + } + + JSValue js_cell = JS_GetPropertyStr(js, globalThis, "cell"); + JSValue hidden = JS_GetPropertyStr(js, js_cell, "hidden"); size_t archive_size = qop_core.data_size - qop_core.files_offset; JSValue blob = js_new_blob_stoned_copy(js, qop_core.data + qop_core.files_offset, archive_size); JS_SetPropertyStr(js, hidden, "core_qop_blob", blob); JS_FreeValue(js, hidden); - JS_FreeValue(js, cell); + JS_FreeValue(js, js_cell); + JS_FreeValue(js, globalThis); // Find and load engine.cm from QOP archive @@ -864,11 +879,11 @@ void script_startup(cell_rt *prt) } data[engine_file->size] = 0; - prt->state = ACTOR_RUNNING; + crt->state = ACTOR_RUNNING; JSValue v = JS_Eval(js, data, (size_t)engine_file->size, ENGINE, 0); uncaught_exception(js, v); - prt->state = ACTOR_IDLE; - set_actor_state(prt); + crt->state = ACTOR_IDLE; + set_actor_state(crt); } int uncaught_exception(JSContext *js, JSValue v) @@ -1046,4 +1061,15 @@ int cell_random() { uint8_t buf[4]; randombytes(buf, sizeof(buf)); return *(int32_t *)buf; -} \ No newline at end of file +} + +int js2bool(JSContext *js, JSValue v) { return JS_ToBool(js,v); } +JSValue bool2js(JSContext *js, int b) { return JS_NewBool(js,b); } + +JSValue number2js(JSContext *js, double g) { return JS_NewFloat64(js,g); } +double js2number(JSContext *js, JSValue v) { + double g; + JS_ToFloat64(js, &g, v); + if (isnan(g)) g = 0; + return g; +} diff --git a/source/jsffi.c b/source/jsffi.c deleted file mode 100644 index 4b95a774..00000000 --- a/source/jsffi.c +++ /dev/null @@ -1,119 +0,0 @@ -#include "jsffi.h" -#include "stb_ds.h" -#include "string.h" -#include -#include -#include -#include -#include -#include -#include -#include "HandmadeMath.h" -#include -#include "cell.h" - -#if defined(_WIN32) -#include -#else -#include -#endif - -#define JS_GETNUM(JS,VAL,I,TO,TYPE) { \ -JSValue val = JS_GetPropertyUint32(JS,VAL,I); \ -TO = js2##TYPE(JS, val); \ -JS_FreeValue(JS, val); } \ - -int js2bool(JSContext *js, JSValue v) { return JS_ToBool(js,v); } -JSValue bool2js(JSContext *js, int b) { return JS_NewBool(js,b); } - -JSValue number2js(JSContext *js, double g) { return JS_NewFloat64(js,g); } -double js2number(JSContext *js, JSValue v) { - double g; - JS_ToFloat64(js, &g, v); - if (isnan(g)) g = 0; - return g; -} - -JSValue js_getproperty(JSContext *js, JSValue v, const char *prop) -{ - JSValue ret = JS_GetPropertyStr(js, v, prop); - JS_FreeValue(js,ret); - return ret; -} - -JSValue make_quad_indices_buffer(JSContext *js, int quads) -{ - cell_rt *rt = JS_GetContextOpaque(js); - int count = quads*6; - if (!JS_IsNull(rt->idx_buffer) && rt->idx_count >= count) - return JS_DupValue(js,rt->idx_buffer); - - int verts = quads*4; - uint16_t *indices = malloc(sizeof(*indices)*count); - for (int i = 0, v = 0; v < verts; i +=6, v += 4) { - indices[i] = v; - indices[i+1] = v+2; - indices[i+2] = v+1; - indices[i+3] = v+2; - indices[i+4] = v+3; - indices[i+5] = v+1; - } - - if (!JS_IsNull(rt->idx_buffer)) - JS_FreeValue(js,rt->idx_buffer); - -// rt->idx_buffer = make_gpu_buffer(js,indices, sizeof(*indices)*count, JS_TYPED_ARRAY_UINT16, 1,0,1); - rt->idx_count = count; - return JS_DupValue(js,rt->idx_buffer); -} - -char *js2strdup(JSContext *js, JSValue v) { - const char *str = JS_ToCString(js, v); - char *ret = strdup(str); - JS_FreeCString(js, str); - return ret; -} - -JSValue angle2js(JSContext *js,double g) { - return number2js(js,g*HMM_RadToTurn); -} - -double js2angle(JSContext *js,JSValue v) { - double n = js2number(js,v); - return n * HMM_TurnToRad; -} - -JSValue js_os_use(JSContext *js); - -void ffi_load(JSContext *js) -{ - cell_rt *rt = JS_GetContextOpaque(js); - JS_FreeValue(js, js_blob_use(js)); - - JSValue globalThis = JS_GetGlobalObject(js); - - JSValue cell = JS_NewObject(js); - JS_SetPropertyStr(js,globalThis,"cell", cell); - - JSValue hidden_fn = JS_NewObject(js); - - JS_SetPropertyStr(js, cell, "hidden", hidden_fn); - JS_SetPropertyStr(js, hidden_fn, "os", js_os_use(js)); - - const char actorsym_script[] = "var sym = Symbol(`actordata`); sym;"; - - JSValue actorsym = JS_Eval(js, actorsym_script, sizeof(actorsym_script)-1, "internal", 0); - - JS_SetPropertyStr(js, hidden_fn, "actorsym", actorsym); - - rt->actor_sym = JS_ValueToAtom(js, actorsym); - - if (rt->init_wota) { - JS_SetPropertyStr(js, hidden_fn, "init", wota2value(js, rt->init_wota)); - // init wota can now be freed - free(rt->init_wota); - rt->init_wota = NULL; - } - - JS_FreeValue(js,globalThis); -} diff --git a/source/jsffi.h b/source/jsffi.h deleted file mode 100644 index 76459698..00000000 --- a/source/jsffi.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef FFI_H -#define FFI_H - -#include "cell.h" - -void ffi_load(JSContext *js); - -#endif