add fstat; tests/cat as an example
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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) {
|
||||
|
||||
21
tests/cat.ce
21
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()
|
||||
Reference in New Issue
Block a user