From 9f9dfe03a6ded59cf9f41ba922e1fda90b32fb2c Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sun, 25 May 2025 11:51:01 -0500 Subject: [PATCH] removed internal functions from accessible via use --- examples/chess/main.js | 11 ++------ scripts/core/engine.js | 17 +++++++++--- scripts/modules/moth.js | 1 - scripts/modules/resources.js | 53 ------------------------------------ source/jsffi.c | 24 ++++++++++++---- source/qjs_actor.c | 2 -- source/qjs_sdl.c | 13 +++++++++ source/qjs_sdl.h | 1 + 8 files changed, 49 insertions(+), 73 deletions(-) diff --git a/examples/chess/main.js b/examples/chess/main.js index 3abf7408..1621c8ba 100644 --- a/examples/chess/main.js +++ b/examples/chess/main.js @@ -326,15 +326,10 @@ function joinServer() { } var os = use('os') -var actor = use('actor') -for (var i in actor) console.log(i) -// Set up IO actor subscription -var ioguy = { - __ACTORDATA__: { - id: actor.ioactor() - } -}; +var sdl = use('sdl') + +var ioguy = sdl.ioguy() send(ioguy, { type: "subscribe", diff --git a/scripts/core/engine.js b/scripts/core/engine.js index 038765bf..7db07a72 100644 --- a/scripts/core/engine.js +++ b/scripts/core/engine.js @@ -21,9 +21,18 @@ prosperon.dispatch = function(type, data) { for (var callback of arr) callback(data) } -var os = use_embed('os') -var actor_mod = use_embed('actor') +// Get hidden modules from prosperon.hidden before stripping it +var hidden = prosperon.hidden +var actor_mod = hidden.actor +var wota = hidden.wota +var console_mod = hidden.console +var use_embed = hidden.use_embed +var use_dyn = hidden.use_dyn +// Strip hidden from prosperon so nothing else can access it +delete prosperon.hidden + +var os = use_embed('os') var js = use_embed('js') prosperon.on('SIGINT', function() { @@ -42,12 +51,12 @@ prosperon.on('SIGSEGV', function() { var io = use_embed('io') -globalThis.console = use_embed('console') +globalThis.console = console_mod var RESPATH = 'scripts/modules/resources.js' var canonical = io.realdir(RESPATH) + 'resources.js' var content = io.slurp(RESPATH) -var resources = js.eval(RESPATH, `(function setup_resources(){${content}})`).call({}) +var resources = js.eval(RESPATH, `(function setup_resources(io){${content}})`).call({}, io) var use_cache = {} diff --git a/scripts/modules/moth.js b/scripts/modules/moth.js index 0c34a650..cb8bb4a3 100644 --- a/scripts/modules/moth.js +++ b/scripts/modules/moth.js @@ -17,7 +17,6 @@ if (!delay.name || (delay.name !== 'delay' && !delay.name.includes('delay'))) { var os = use('os'); var io = use('io'); var render = use('render'); -var actor = use('actor'); var transform = use('transform'); var gameConfig = {}; diff --git a/scripts/modules/resources.js b/scripts/modules/resources.js index c20ecded..c3f056bc 100644 --- a/scripts/modules/resources.js +++ b/scripts/modules/resources.js @@ -1,7 +1,3 @@ -var io = use_embed('io'); -var miniz = use_embed('miniz'); -var os = use_embed('os'); - Object.defineProperty(Function.prototype, "hashify", { value: function () { var hash = new Map() @@ -18,23 +14,11 @@ Object.defineProperty(Function.prototype, "hashify", { // Merge of the old resources.js and packer.js functionalities var Resources = {} -// Determine the shared library extension based on the OS -var so_ext -switch(os.platform()) { - case 'Windows': - so_ext = '.dll' - break - default: - so_ext = '.so' - break -} - // Recognized resource extensions Resources.scripts = ["js"] Resources.images = ["qoi", "png", "gif", "jpg", "jpeg", "ase", "aseprite"] Resources.sounds = ["wav", "flac", "mp3", "qoa"] Resources.fonts = ["ttf"] -Resources.lib = [so_ext] // Helper function: get extension from path in lowercase (e.g., "image.png" -> "png") function getExtension(path) { @@ -169,11 +153,6 @@ Resources.gatherStats = function(filePaths) { stats.fonts++ continue } - // For so_ext, we store it in Resources.lib as ['.so'] or ['.dll'], so match that form - if (Resources.lib.includes('.' + ext)) { - stats.lib++ - continue - } stats.other++ } return stats @@ -186,36 +165,4 @@ fonts, libs, or other. Return a stats object with these counts and the total. :return: { scripts, images, sounds, fonts, lib, other, total } ` -// Create a ZIP of recognized files in a directory, skipping ignored ones, and write to outPath -Resources.pack = function(dir, outPath) { - if (!io.exists(dir)) - throw Error("Directory does not exist: " + dir) - - var files = Resources.getAllFiles(dir) - var writer = miniz.write(outPath) - - for (var fullPath of files) { - try { - var st = io.stat(fullPath) - if (!st.filesize) continue // skip directories - // Path in the ZIP: remove leading dir + '/' - var pathInZip = fullPath.substring(dir.length + 1) - var data = io.slurpbytes(fullPath) - writer.add_file(pathInZip, data) - } catch(e) { - // Optionally handle or log errors - } - } -} -Resources.pack[prosperon.DOC] = ` -Create a ZIP archive of all recognized files (skipping those matched by .prosperonignore) -in the specified directory and write it to outPath. Recognized extensions are scripts, -images, sounds, fonts, or libs. - -:param dir: The directory to zip. -:param outPath: The path (including filename) for the resulting ZIP file. -:return: None -:raises Error: If the directory does not exist. -` - return Resources diff --git a/source/jsffi.c b/source/jsffi.c index 38383cc3..eb7d5e53 100644 --- a/source/jsffi.c +++ b/source/jsffi.c @@ -2873,7 +2873,7 @@ void ffi_load(JSContext *js) arrput(rt->module_registry, ((ModuleEntry){"io", js_io_use})); arrput(rt->module_registry, ((ModuleEntry){"os", js_os_use})); - arrput(rt->module_registry, ((ModuleEntry){"actor", js_actor_use})); + // actor module moved to hidden_fn arrput(rt->module_registry, ((ModuleEntry){"input", js_input_use})); arrput(rt->module_registry, MISTLINE(time)); arrput(rt->module_registry, ((ModuleEntry){"math", js_math_use})); @@ -2894,15 +2894,15 @@ void ffi_load(JSContext *js) arrput(rt->module_registry, MISTLINE(nota)); arrput(rt->module_registry, MISTLINE(enet)); arrput(rt->module_registry, MISTLINE(qr)); - arrput(rt->module_registry, MISTLINE(wota)); arrput(rt->module_registry, MISTLINE(crypto)); arrput(rt->module_registry, MISTLINE(blob)); arrput(rt->module_registry, MISTLINE(http)); arrput(rt->module_registry, ((ModuleEntry){"sdl_audio", js_sdl_audio_use})); - arrput(rt->module_registry, MISTLINE(console)); + // console module moved to hidden_fn arrput(rt->module_registry, MISTLINE(rtree)); arrput(rt->module_registry, MISTLINE(sprite)); arrput(rt->module_registry, MISTLINE(transform)); + arrput(rt->module_registry, MISTLINE(sdl)); #ifdef TRACY_ENABLE arrput(rt->module_registry, MISTLINE(tracy)); @@ -2927,8 +2927,7 @@ void ffi_load(JSContext *js) QJSCLASSPREP_FUNCS(font); QJSCLASSPREP_FUNCS(datastream); - JS_SetPropertyStr(js, globalThis, "use_dyn", JS_NewCFunction(js,js_os_use_dyn,"use_dyn", 1)); - JS_SetPropertyStr(js, globalThis, "use_embed", JS_NewCFunction(js,js_os_use_embed,"use_embed", 1)); + // use_dyn and use_embed moved to hidden_fn JSValue jsobject = JS_GetPropertyStr(js,globalThis, "Object"); JS_SetPropertyStr(js, jsobject, "id", JS_NewCFunction(js, js_os_value_id, "id", 1)); @@ -2954,6 +2953,21 @@ void ffi_load(JSContext *js) //JS_SetPropertyStr(js,prosp, "version", JS_NewString(js,PROSPERON_VERSION)); //JS_SetPropertyStr(js,prosp,"revision",JS_NewString(js,PROSPERON_COMMIT)); JS_SetPropertyStr(js,prosp,"engine_start", JS_NewCFunction(js,js_os_engine_start, "engine_start", 1)); + + JSValue hidden_fn = JS_NewObject(js); + // add engine.js-only functions to hidden_fn. It should grab them and then remove so nothing else can use them. + + // Add modules that should only be accessible to engine.js + JS_SetPropertyStr(js, hidden_fn, "actor", js_actor_use(js)); + JS_SetPropertyStr(js, hidden_fn, "wota", js_wota_use(js)); + JS_SetPropertyStr(js, hidden_fn, "console", js_console_use(js)); + + // Add functions that should only be accessible to engine.js + JS_SetPropertyStr(js, hidden_fn, "use_dyn", JS_NewCFunction(js, js_os_use_dyn, "use_dyn", 1)); + JS_SetPropertyStr(js, hidden_fn, "use_embed", JS_NewCFunction(js, js_os_use_embed, "use_embed", 1)); + + JS_SetPropertyStr(js, prosp, "hidden", hidden_fn); + JS_FreeValue(js,globalThis); } diff --git a/source/qjs_actor.c b/source/qjs_actor.c index ef23d59c..a8a88c3b 100644 --- a/source/qjs_actor.c +++ b/source/qjs_actor.c @@ -14,7 +14,6 @@ extern prosperon_rt *io_actor; // External function declarations JSValue js_actor_delay(JSContext *js, JSValue self, int argc, JSValue *argv); JSValue js_actor_removetimer(JSContext *js, JSValue self, int argc, JSValue *argv); -JSValue js_os_ioactor(JSContext *js, JSValue self, int argc, JSValue *argv); JSC_CCALL(os_createactor, int margc = JS_ArrayLength(js, argv[0]); @@ -105,7 +104,6 @@ static const JSCFunctionListEntry js_actor_funcs[] = { MIST_FUNC_DEF(os, register_actor, 2), MIST_FUNC_DEF(os, unneeded, 2), MIST_FUNC_DEF(os, destroy, 0), - MIST_FUNC_DEF(os, ioactor, 0), }; JSValue js_actor_use(JSContext *js) { diff --git a/source/qjs_sdl.c b/source/qjs_sdl.c index 15d73bdf..47447e7a 100644 --- a/source/qjs_sdl.c +++ b/source/qjs_sdl.c @@ -473,4 +473,17 @@ JSValue js_sdl_audio_use(JSContext *js) { JSValue mod = JS_NewObject(js); JS_SetPropertyFunctionList(js,mod,js_sdl_audio_funcs,countof(js_sdl_audio_funcs)); return mod; +} + +JSValue js_os_ioactor(JSContext *js, JSValue self, int argc, JSValue *argv); + +static const JSCFunctionListEntry js_sdl_funcs[] = { + MIST_FUNC_DEF(os, ioactor, 0) +}; + +JSValue js_sdl_use(JSContext *js) +{ + JSValue mod = JS_NewObject(js); + JS_SetPropertyFunctionList(js, mod, js_sdl_funcs, countof(js_sdl_funcs)); + return mod; } \ No newline at end of file diff --git a/source/qjs_sdl.h b/source/qjs_sdl.h index 5a87f2e5..3210c046 100644 --- a/source/qjs_sdl.h +++ b/source/qjs_sdl.h @@ -6,5 +6,6 @@ JSValue js_input_use(JSContext *ctx); JSValue js_camera_use(JSContext *ctx); JSValue js_sdl_audio_use(JSContext *ctx); +JSValue js_sdl_use(JSContext *js); #endif /* QJS_SDL_H */