add chunked reading; example with cat.ce

This commit is contained in:
2025-06-06 16:15:39 -05:00
parent cd05ab97b5
commit d5d17560f9
3 changed files with 62 additions and 0 deletions

View File

@@ -819,6 +819,8 @@ int main(int argc, char **argv)
printf("CRITICAL ERROR: %s\n", SDL_GetError());
exit(1);
}
printf("main thread is %d\n", SDL_GetThreadID(NULL));
#ifdef TRACY_ENABLE
tracy_profiling_enabled = profile_enabled;

View File

@@ -202,6 +202,41 @@ JSC_CCALL(fd_close,
return JS_UNDEFINED;
)
JSC_CCALL(fd_fstat,
FDWrapper *fdw = js2fd(js, argv[0]);
if (!fdw) return JS_EXCEPTION;
struct stat st;
if (fstat(fdw->fd, &st) != 0)
return JS_ThrowReferenceError(js, "fstat failed: %s", strerror(errno));
JSValue obj = JS_NewObject(js);
JS_SetPropertyStr(js, obj, "size", JS_NewInt64(js, st.st_size));
JS_SetPropertyStr(js, obj, "mode", JS_NewInt32(js, st.st_mode));
JS_SetPropertyStr(js, obj, "uid", JS_NewInt32(js, st.st_uid));
JS_SetPropertyStr(js, obj, "gid", JS_NewInt32(js, st.st_gid));
JS_SetPropertyStr(js, obj, "atime", JS_NewInt64(js, st.st_atime));
JS_SetPropertyStr(js, obj, "mtime", JS_NewInt64(js, st.st_mtime));
JS_SetPropertyStr(js, obj, "ctime", JS_NewInt64(js, st.st_ctime));
JS_SetPropertyStr(js, obj, "nlink", JS_NewInt32(js, st.st_nlink));
JS_SetPropertyStr(js, obj, "ino", JS_NewInt64(js, st.st_ino));
JS_SetPropertyStr(js, obj, "dev", JS_NewInt32(js, st.st_dev));
JS_SetPropertyStr(js, obj, "rdev", JS_NewInt32(js, st.st_rdev));
JS_SetPropertyStr(js, obj, "blksize", JS_NewInt32(js, st.st_blksize));
JS_SetPropertyStr(js, obj, "blocks", JS_NewInt64(js, st.st_blocks));
// Add boolean properties for file type
JS_SetPropertyStr(js, obj, "isFile", JS_NewBool(js, S_ISREG(st.st_mode)));
JS_SetPropertyStr(js, obj, "isDirectory", JS_NewBool(js, S_ISDIR(st.st_mode)));
JS_SetPropertyStr(js, obj, "isSymlink", JS_NewBool(js, S_ISLNK(st.st_mode)));
JS_SetPropertyStr(js, obj, "isFIFO", JS_NewBool(js, S_ISFIFO(st.st_mode)));
JS_SetPropertyStr(js, obj, "isSocket", JS_NewBool(js, S_ISSOCK(st.st_mode)));
JS_SetPropertyStr(js, obj, "isCharDevice", JS_NewBool(js, S_ISCHR(st.st_mode)));
JS_SetPropertyStr(js, obj, "isBlockDevice", JS_NewBool(js, S_ISBLK(st.st_mode)));
return obj;
)
static const JSCFunctionListEntry js_fd_funcs[] = {
MIST_FUNC_DEF(fd, open, 2),
@@ -213,6 +248,7 @@ static const JSCFunctionListEntry js_fd_funcs[] = {
MIST_FUNC_DEF(fd, mkdir, 1),
MIST_FUNC_DEF(fd, fsync, 1),
MIST_FUNC_DEF(fd, close, 1),
MIST_FUNC_DEF(fd, fstat, 1),
};
JSValue js_fd_use(JSContext *js) {

24
tests/cat.ce Normal file
View File

@@ -0,0 +1,24 @@
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)
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()