-u.unneeded

This commit is contained in:
2025-03-22 22:27:36 -05:00
parent f9c1a3e71a
commit f9100da8a2
6 changed files with 50 additions and 16 deletions

View File

@@ -761,7 +761,7 @@ $_.stop = function(actor) {
$_.stop[prosperon.DOC] = "The stop function stops an underling."
$_.unneeded = function(fn, seconds) {
os.unnneeded(fn, seconds)
os.unneeded(fn, seconds)
}
$_.unneeded[prosperon.DOC] = "registers a function that is called when the actor..."
@@ -909,6 +909,7 @@ function handle_actor_disconnect(id) {
}
function handle_message(msg) {
console.log(json.encode(msg))
if (msg.target) {
if (msg.target !== prosperon.id) {
os.mailbox_push(msg.target, msg)
@@ -960,8 +961,6 @@ function enet_check()
$_.delay(enet_check, service_delay);
}
$_.delay(enet_check, service_delay);
console.log(`actor ${prosperon.id} online.`)
send_messages();

View File

@@ -6775,8 +6775,24 @@ JSC_CCALL(os_mailbox_exist,
JSC_CCALL(os_unneeded,
prosperon_rt *actor = JS_GetContextOpaque(js);
actor->unneeded = JS_DupValue(js, argv[1]);
actor->unneeded_secs = js2number(js, argv[2]);
SDL_LockMutex(actor->msg_mutex);
JS_FreeValue(js, actor->unneeded);
if (!JS_IsFunction(js, argv[0])) {
actor->unneeded = JS_UNDEFINED;
goto END;
}
actor->unneeded = JS_DupValue(js, argv[0]);
actor->unneeded_secs = js2number(js, argv[1]);
END:
if (actor->ar) {
SDL_RemoveTimer(actor->ar);
actor->ar = 0;
}
set_actor_state(actor);
SDL_UnlockMutex(actor->msg_mutex);
)
JSValue js_os_get_trace(JSContext *js, JSValue self)

View File

@@ -54,11 +54,18 @@ static Uint32 queue_event;
static SDL_AtomicInt shutdown;
static SDL_Thread **runners = NULL;
static void set_actor_state(prosperon_rt *actor);
static Uint32 actor_remove_cb(prosperon_rt *actor, Uint32 id, Uint32 interval)
{
actor_free(actor);
if (JS_IsUndefined(actor->unneeded))
actor_free(actor);
else {
SDL_LockMutex(actor->mutex);
JSValue ret = JS_Call(actor->context, actor->unneeded, JS_UNDEFINED, 0, NULL);
uncaught_exception(actor->context, ret);
SDL_UnlockMutex(actor->mutex);
set_actor_state(actor);
}
return 0;
}
@@ -256,7 +263,7 @@ char *send_message(char *id, void *msg)
}
/* set_actor_state should check if either messages or events are pending. */
static void set_actor_state(prosperon_rt *actor)
void set_actor_state(prosperon_rt *actor)
{
SDL_LockMutex(actor->msg_mutex);
if (actor->need_stop) {
@@ -300,8 +307,12 @@ static void set_actor_state(prosperon_rt *actor)
}
END:
if (actor->state == ACTOR_IDLE && !actor->ar && !has_upcoming)
actor->ar = SDL_AddTimerNS(SDL_SECONDS_TO_NS(1), actor_remove_cb, actor);
if (actor->state == ACTOR_IDLE && !actor->ar && !has_upcoming) {
if (JS_IsUndefined(actor->unneeded))
actor->ar = SDL_AddTimerNS(SDL_SECONDS_TO_NS(1), actor_remove_cb, actor);
else
actor->ar = SDL_AddTimerNS(SDL_SECONDS_TO_NS(actor->unneeded_secs), actor_remove_cb, actor);
}
SDL_UnlockMutex(actor->msg_mutex);
}
@@ -497,8 +508,7 @@ JSValue js_actor_delay(JSContext *js, JSValue self, int argc, JSValue *argv)
prosperon_rt *actor = JS_GetContextOpaque(js);
double seconds;
JS_ToFloat64(js, &seconds, argv[1]);
Uint64 ns = (Uint64)(seconds * 1000000000.0);
Uint32 id = SDL_AddTimerNS(ns, actor_timer_cb, actor);
Uint32 id = SDL_AddTimerNS(SDL_SECONDS_TO_NS(seconds), actor_timer_cb, actor);
SDL_LockMutex(actor->msg_mutex);
JSValue cb = JS_DupValue(js, argv[0]);
@@ -1311,7 +1321,6 @@ int main(int argc, char **argv)
return 1;
}
main_thread = SDL_GetCurrentThreadID();
printf("main is %u\n", main_thread);
if (!main_thread) {
printf("Platform does not support threads!\n");
return 1;
@@ -1362,8 +1371,9 @@ int main(int argc, char **argv)
SDL_LockMutex(actors_mutex);
for (int i = 0; i < shlen(actors); i++) {
// Make a fresh copy so each actor owns and frees it independently
void *msg = malloc(wb.size);
memcpy(msg, wb.data, wb.size);
size_t total_bytes = wb.size * sizeof(uint64_t);
void *msg = malloc(total_bytes);
memcpy(msg, wb.data, total_bytes);
send_message(actors[i].key, msg);
}

View File

@@ -80,6 +80,7 @@ void script_evalf(JSContext *js, const char *format, ...);
JSValue script_eval(JSContext *js, const char *file, const char *script);
int uncaught_exception(JSContext *js, JSValue v);
int actor_exists(char *id);
void set_actor_state(prosperon_rt *actor);
int prosperon_mount_core(void);

4
tests/unneeded.js Normal file
View File

@@ -0,0 +1,4 @@
$_.unneeded(_ => {
console.log("Unneded function fired.");
$_.unneeded($_.stop, 1);
}, 1);

View File

@@ -25,3 +25,7 @@ function loop() {
loop()
$_.delay($_.stop, 3)
$_.receiver(e => {
console.log(json.encode(e))
})