From 79054080c08f0f42b76adf830bfeb2bcd64c05c9 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 6 Jun 2026 16:39:27 -0500 Subject: [PATCH] Cleanup a bit. --- assets/init.js | 13 +-- src/dusk/input/input.c | 16 +++- src/dusk/input/inputaction.h | 10 ++- .../script/module/require/modulerequire.c | 85 ++++++++++++------- .../script/module/require/modulerequire.h | 58 +++++++++++++ 5 files changed, 132 insertions(+), 50 deletions(-) diff --git a/assets/init.js b/assets/init.js index 230eb3a6..3709b379 100644 --- a/assets/init.js +++ b/assets/init.js @@ -1,5 +1,3 @@ - - const platformNames = { [System.PLATFORM_LINUX]: 'Linux', [System.PLATFORM_KNULLI]: 'Knulli', @@ -8,13 +6,4 @@ const platformNames = { [System.PLATFORM_WII]: 'Wii', }; -Console.print('Platform: ' + (platformNames[System.platform] || 'Unknown')); - -async function testRequireAsync() { - Console.print('Loading testscene.js...'); - const scene = await requireAsync('testscene.js'); - Console.print('Loaded!'); - Console.print(scene.test()); -} - -testRequireAsync(); \ No newline at end of file +Console.print('Platform: ' + (platformNames[System.platform] || 'Unknown')); \ No newline at end of file diff --git a/src/dusk/input/input.c b/src/dusk/input/input.c index 70cf416a..2ed9a04c 100644 --- a/src/dusk/input/input.c +++ b/src/dusk/input/input.c @@ -22,8 +22,20 @@ errorret_t inputInit(void) { INPUT.actions[i].action = (inputaction_t)i; INPUT.actions[i].lastValue = 0.0f; INPUT.actions[i].currentValue = 0.0f; - eventInit(&INPUT.actions[i].onPressed, INPUT.actions[i].onPressedCallbacks, INPUT.actions[i].onPressedUsers, 4); - eventInit(&INPUT.actions[i].onReleased, INPUT.actions[i].onReleasedCallbacks, INPUT.actions[i].onReleasedUsers, 4); + + eventInit( + &INPUT.actions[i].onPressed, + INPUT.actions[i].onPressedCallbacks, + INPUT.actions[i].onPressedUsers, + INPUT_ACTION_CALLBACK_COUNT_MAX + ); + + eventInit( + &INPUT.actions[i].onReleased, + INPUT.actions[i].onReleasedCallbacks, + INPUT.actions[i].onReleasedUsers, + INPUT_ACTION_CALLBACK_COUNT_MAX + ); } #ifdef inputInitPlatform diff --git a/src/dusk/input/inputaction.h b/src/dusk/input/inputaction.h index 22fd68fe..345d4021 100644 --- a/src/dusk/input/inputaction.h +++ b/src/dusk/input/inputaction.h @@ -10,6 +10,8 @@ #include "input/inputactiondefs.h" #include "event/event.h" +#define INPUT_ACTION_CALLBACK_COUNT_MAX 4 + typedef struct { inputaction_t action; float_t lastValue; @@ -20,11 +22,11 @@ typedef struct { float_t currentDynamicValue; #endif - eventcallback_t onPressedCallbacks[4]; - void *onPressedUsers[4]; + eventcallback_t onPressedCallbacks[INPUT_ACTION_CALLBACK_COUNT_MAX]; + void *onPressedUsers[INPUT_ACTION_CALLBACK_COUNT_MAX]; event_t onPressed; - eventcallback_t onReleasedCallbacks[4]; - void *onReleasedUsers[4]; + eventcallback_t onReleasedCallbacks[INPUT_ACTION_CALLBACK_COUNT_MAX]; + void *onReleasedUsers[INPUT_ACTION_CALLBACK_COUNT_MAX]; event_t onReleased; } inputactiondata_t; diff --git a/src/dusk/script/module/require/modulerequire.c b/src/dusk/script/module/require/modulerequire.c index dd5eff48..58602ac8 100644 --- a/src/dusk/script/module/require/modulerequire.c +++ b/src/dusk/script/module/require/modulerequire.c @@ -6,56 +6,58 @@ */ #include "modulerequire.h" -#include "asset/asset.h" #include "util/memory.h" #include "util/string.h" #include "assert/assert.h" -#define MODULE_REQUIRE_ASYNC_MAX 16 +modulerequireasyncpending_t MODULE_REQUIRE_ASYNC_PENDING[ + MODULE_REQUIRE_ASYNC_MAX +]; +uint32_t MODULE_REQUIRE_ASYNC_PENDING_COUNT = 0; -typedef struct { - assetentry_t *entry; - jerry_value_t promise; -} modulerequireasyncpending_t; - -static modulerequireasyncpending_t MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_MAX]; -static uint32_t MODULE_REQUIRE_ASYNC_PENDING_COUNT = 0; - -/* Resolves or rejects every promise associated with entry, then unsubscribes. */ -static void moduleRequireAsyncOnError(void *params, void *user); - -static void moduleRequireAsyncOnLoaded(void *params, void *user) { +void moduleRequireAsyncOnLoaded(void *params, void *user) { assetentry_t *entry = (assetentry_t *)params; - (void)user; eventUnsubscribe(&entry->onLoaded, moduleRequireAsyncOnLoaded); eventUnsubscribe(&entry->onError, moduleRequireAsyncOnError); - jerry_value_t exports = jerry_value_is_undefined(entry->data.script.exports) - ? jerry_undefined() - : jerry_value_copy(entry->data.script.exports); + jerry_value_t exports = ( + jerry_value_is_undefined(entry->data.script.exports) ? + jerry_undefined() : + jerry_value_copy(entry->data.script.exports) + ); uint32_t i = 0; while(i < MODULE_REQUIRE_ASYNC_PENDING_COUNT) { - if(MODULE_REQUIRE_ASYNC_PENDING[i].entry != entry) { i++; continue; } + if(MODULE_REQUIRE_ASYNC_PENDING[i].entry != entry) { + i++; + continue; + } + assetUnlockEntry(entry); + jerry_value_t copy = jerry_value_copy(exports); - jerry_value_t ret = jerry_promise_resolve(MODULE_REQUIRE_ASYNC_PENDING[i].promise, copy); + jerry_value_t ret = jerry_promise_resolve( + MODULE_REQUIRE_ASYNC_PENDING[i].promise, copy + ); jerry_value_free(ret); jerry_value_free(copy); jerry_value_free(MODULE_REQUIRE_ASYNC_PENDING[i].promise); + MODULE_REQUIRE_ASYNC_PENDING_COUNT--; + if(i < MODULE_REQUIRE_ASYNC_PENDING_COUNT) { - MODULE_REQUIRE_ASYNC_PENDING[i] = MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_PENDING_COUNT]; + MODULE_REQUIRE_ASYNC_PENDING[i] = ( + MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_PENDING_COUNT] + ); } } jerry_value_free(exports); } -static void moduleRequireAsyncOnError(void *params, void *user) { +void moduleRequireAsyncOnError(void *params, void *user) { assetentry_t *entry = (assetentry_t *)params; - (void)user; eventUnsubscribe(&entry->onLoaded, moduleRequireAsyncOnLoaded); eventUnsubscribe(&entry->onError, moduleRequireAsyncOnError); @@ -66,14 +68,20 @@ static void moduleRequireAsyncOnError(void *params, void *user) { while(i < MODULE_REQUIRE_ASYNC_PENDING_COUNT) { if(MODULE_REQUIRE_ASYNC_PENDING[i].entry != entry) { i++; continue; } assetUnlockEntry(entry); + jerry_value_t copy = jerry_value_copy(errStr); - jerry_value_t ret = jerry_promise_reject(MODULE_REQUIRE_ASYNC_PENDING[i].promise, copy); + jerry_value_t ret = jerry_promise_reject( + MODULE_REQUIRE_ASYNC_PENDING[i].promise, copy + ); jerry_value_free(ret); jerry_value_free(copy); jerry_value_free(MODULE_REQUIRE_ASYNC_PENDING[i].promise); MODULE_REQUIRE_ASYNC_PENDING_COUNT--; + if(i < MODULE_REQUIRE_ASYNC_PENDING_COUNT) { - MODULE_REQUIRE_ASYNC_PENDING[i] = MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_PENDING_COUNT]; + MODULE_REQUIRE_ASYNC_PENDING[i] = ( + MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_PENDING_COUNT] + ); } } @@ -147,9 +155,11 @@ jerry_value_t moduleRequireAsyncFunc( // Already loaded — resolve immediately. if(entry->state == ASSET_ENTRY_STATE_LOADED) { - jerry_value_t exports = jerry_value_is_undefined(entry->data.script.exports) - ? jerry_undefined() - : jerry_value_copy(entry->data.script.exports); + jerry_value_t exports = ( + jerry_value_is_undefined(entry->data.script.exports) ? + jerry_undefined() : + jerry_value_copy(entry->data.script.exports) + ); assetUnlockEntry(entry); jerry_value_t ret = jerry_promise_resolve(promise, exports); jerry_value_free(ret); @@ -170,8 +180,12 @@ jerry_value_t moduleRequireAsyncFunc( // Subscribe to entry events only if this is the first pending await for it. bool_t subscribed = false; for(uint32_t i = 0; i < MODULE_REQUIRE_ASYNC_PENDING_COUNT; i++) { - if(MODULE_REQUIRE_ASYNC_PENDING[i].entry == entry) { subscribed = true; break; } + if(MODULE_REQUIRE_ASYNC_PENDING[i].entry == entry) { + subscribed = true; + break; + } } + if(!subscribed) { if(entry->onLoaded.count >= entry->onLoaded.size) { assetUnlockEntry(entry); @@ -187,8 +201,12 @@ jerry_value_t moduleRequireAsyncFunc( eventSubscribe(&entry->onError, moduleRequireAsyncOnError, NULL); } - MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_PENDING_COUNT].entry = entry; - MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_PENDING_COUNT].promise = jerry_value_copy(promise); + MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_PENDING_COUNT].entry = ( + entry + ); + MODULE_REQUIRE_ASYNC_PENDING[MODULE_REQUIRE_ASYNC_PENDING_COUNT].promise = ( + jerry_value_copy(promise) + ); MODULE_REQUIRE_ASYNC_PENDING_COUNT++; return promise; } @@ -204,7 +222,10 @@ void moduleRequireDispose(void) { assetentry_t *entry = MODULE_REQUIRE_ASYNC_PENDING[i].entry; bool_t alreadyUnsub = false; for(uint32_t j = 0; j < i; j++) { - if(MODULE_REQUIRE_ASYNC_PENDING[j].entry == entry) { alreadyUnsub = true; break; } + if(MODULE_REQUIRE_ASYNC_PENDING[j].entry == entry) { + alreadyUnsub = true; + break; + } } if(!alreadyUnsub) { eventUnsubscribe(&entry->onLoaded, moduleRequireAsyncOnLoaded); diff --git a/src/dusk/script/module/require/modulerequire.h b/src/dusk/script/module/require/modulerequire.h index f4c24b1d..ef3445d4 100644 --- a/src/dusk/script/module/require/modulerequire.h +++ b/src/dusk/script/module/require/modulerequire.h @@ -7,6 +7,64 @@ #pragma once #include "script/module/modulebase.h" +#include "asset/asset.h" + +#define MODULE_REQUIRE_ASYNC_MAX 16 + +typedef struct { + assetentry_t *entry; + jerry_value_t promise; +} modulerequireasyncpending_t; + +extern modulerequireasyncpending_t MODULE_REQUIRE_ASYNC_PENDING[ + MODULE_REQUIRE_ASYNC_MAX +]; + +extern uint32_t MODULE_REQUIRE_ASYNC_PENDING_COUNT; + +/** + * Success loaded callback for async module loading. + * + * @param params The asset entry that finished loading. + * @param user Unused. + */ +void moduleRequireAsyncOnLoaded(void *params, void *user); + +/** + * Error callback for when an async loaded module fails. + * + * @param params The asset entry that failed to load. + * @param user Unused. + */ +void moduleRequireAsyncOnError(void *params, void *user); + +/** + * Synchronous require function exposed to scripts. + * + * @param callInfo JerryScript call info. + * @param args The arguments passed to the function. + * @param argc The number of arguments passed. + * @return The exports of the required module. + */ +jerry_value_t moduleRequireFunc( + const jerry_call_info_t *callInfo, + const jerry_value_t args[], + const jerry_length_t argc +); + +/** + * Asynchronous require function exposed to scripts. + * + * @param callInfo JerryScript call info. + * @param args The arguments passed to the function. + * @param argc The number of arguments passed. + * @return A promise that resolves with the exports of the required module. + */ +jerry_value_t moduleRequireAsyncFunc( + const jerry_call_info_t *callInfo, + const jerry_value_t args[], + const jerry_length_t argc +); /** * Initializes the require module.