From 580df9f2333929d74687bd207273591e0d67ce7b Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 6 Jun 2025 16:49:19 -0500 Subject: [PATCH] add fstat; tests/cat as an example --- scripts/engine.cm | 12 +++++++++--- source/qjs_actor.c | 14 ++++++++++++++ tests/cat.ce | 21 ++++----------------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/scripts/engine.cm b/scripts/engine.cm index 15d6f91d..091d8bea 100644 --- a/scripts/engine.cm +++ b/scripts/engine.cm @@ -360,9 +360,10 @@ $_.random[cell.DOC] = "returns a number between 0 and 1. There is a 50% chance t $_.random_fit = crypto.random_fit $_.clock = function(fn) { - $_.delay(_ => { + actor_mod.clock(_ => { fn(time.number()) - }, 0) + send_messages() + }) } $_.clock[cell.DOC] = "takes a function input value that will eventually be called with the current time in number form." @@ -521,7 +522,12 @@ $_.unneeded = function unneeded(fn, seconds) { actor_mod.unneeded(fn, seconds) } -$_.delay = function delay(fn, seconds) { +$_.delay = function delay(fn, seconds = 0) { + if (seconds <= 0) { + $_.clock(fn) + return + } + function delay_turn() { fn() send_messages() diff --git a/source/qjs_actor.c b/source/qjs_actor.c index 1f6ebd6b..0299af6d 100644 --- a/source/qjs_actor.c +++ b/source/qjs_actor.c @@ -163,6 +163,19 @@ JSC_CCALL(actor_on_exception, rt->on_exception = JS_DupValue(js,argv[0]); ) +JSC_CCALL(actor_clock, + if (!JS_IsFunction(js, argv[0])) + return JS_ThrowReferenceError(js, "Argument must be a function."); + + cell_rt *actor = JS_GetContextOpaque(js); + SDL_LockMutex(actor->msg_mutex); + letter l; + l.type = LETTER_CALLBACK; + l.callback = JS_DupValue(js, argv[0]); + arrput(actor->letters, l); + SDL_UnlockMutex(actor->msg_mutex); +) + static const JSCFunctionListEntry js_actor_funcs[] = { MIST_FUNC_DEF(os, createactor, 1), MIST_FUNC_DEF(os, mailbox_push, 2), @@ -174,6 +187,7 @@ static const JSCFunctionListEntry js_actor_funcs[] = { MIST_FUNC_DEF(actor, disrupt, 0), MIST_FUNC_DEF(actor, setname, 1), MIST_FUNC_DEF(actor, on_exception, 1), + MIST_FUNC_DEF(actor, clock, 1), }; JSValue js_actor_use(JSContext *js) { diff --git a/tests/cat.ce b/tests/cat.ce index 0b592750..981bb998 100644 --- a/tests/cat.ce +++ b/tests/cat.ce @@ -1,24 +1,11 @@ var fd = use('fd') var time = use('time') -var blob = use('blob') var st = time.number() -var data = new blob var f = fd.open(arg[0], 'r') var stat = fd.fstat(f) +var data = fd.read(f,stat.size); +fd.close(f) +log.console(text(data)) -function getchunk() -{ - var chunk = fd.read(f,stat.blksize) - data.write_blob(chunk) - if (chunk.length == stat.blksize*8) - $_.clock(getchunk) - else { - log.console(`fd read took ${time.number()-st}`) - log.console(stat.blksize) - log.console(data.length/8) - fd.close(f) - } -} - -getchunk() +$_.stop() \ No newline at end of file