fd now uses numbers to fix the inability to exit
This commit is contained in:
@@ -371,8 +371,6 @@ var underlings = new Set() // this is more like "all actors that are notified wh
|
||||
var overling = undefined
|
||||
var root = undefined
|
||||
|
||||
// Don't make $_ global - it should only be available to actor scripts
|
||||
|
||||
var receive_fn = undefined
|
||||
var greeters = {} // Router functions for when messages are received for a specific actor
|
||||
|
||||
@@ -821,11 +819,11 @@ var prog_script = `(function ${cell.args.program.name()}_start($_, arg) { var ar
|
||||
// queue up its first turn instead of run immediately
|
||||
|
||||
var startfn = js.eval(cell.args.program, prog_script);
|
||||
$_.delay(_ => {
|
||||
$_.clock(_ => {
|
||||
var val = startfn($_, cell.args.arg);
|
||||
|
||||
if (val)
|
||||
throw new Error('Program must not return anything');
|
||||
}, 0)
|
||||
})
|
||||
|
||||
})()
|
||||
@@ -506,7 +506,9 @@ void actor_turn(cell_rt *actor)
|
||||
uncaught_exception(actor->context, result);
|
||||
JS_FreeValue(actor->context, arg);
|
||||
} else if (l.type == LETTER_CALLBACK) {
|
||||
printf("running a callback\n");
|
||||
result = JS_Call(actor->context, l.callback, JS_UNDEFINED, 0, NULL);
|
||||
printf("turn is over for %s\n", actor->id);
|
||||
uncaught_exception(actor->context, result);
|
||||
JS_FreeValue(actor->context, l.callback);
|
||||
}
|
||||
|
||||
103
source/qjs_fd.c
103
source/qjs_fd.c
@@ -11,55 +11,15 @@
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// File descriptor wrapper structure
|
||||
typedef struct {
|
||||
// Helper to convert JS value to file descriptor
|
||||
static int js2fd(JSContext *ctx, JSValueConst val)
|
||||
{
|
||||
int fd;
|
||||
} FDWrapper;
|
||||
|
||||
// Free function for file descriptor
|
||||
static void FD_free(JSRuntime *rt, FDWrapper *fdw)
|
||||
{
|
||||
if (fdw->fd >= 0) {
|
||||
close(fdw->fd);
|
||||
if (JS_ToInt32(ctx, &fd, val) < 0) {
|
||||
JS_ThrowTypeError(ctx, "Expected file descriptor number");
|
||||
return -1;
|
||||
}
|
||||
js_free_rt(rt, fdw);
|
||||
}
|
||||
|
||||
// Class definition for file descriptor
|
||||
static JSClassID js_fd_class_id;
|
||||
|
||||
static JSClassDef js_fd_class = {
|
||||
"FileDescriptor",
|
||||
.finalizer = (JSClassFinalizer *)FD_free,
|
||||
};
|
||||
|
||||
// Helper to convert JS value to FDWrapper
|
||||
static FDWrapper *js2fd(JSContext *ctx, JSValueConst obj)
|
||||
{
|
||||
FDWrapper *fdw = JS_GetOpaque2(ctx, obj, js_fd_class_id);
|
||||
if (!fdw) {
|
||||
JS_ThrowTypeError(ctx, "Expected file descriptor object");
|
||||
return NULL;
|
||||
}
|
||||
return fdw;
|
||||
}
|
||||
|
||||
// Helper to create JS FDWrapper object
|
||||
static JSValue fd2js(JSContext *ctx, int fd)
|
||||
{
|
||||
FDWrapper *fdw = js_mallocz(ctx, sizeof(FDWrapper));
|
||||
if (!fdw) return JS_EXCEPTION;
|
||||
|
||||
fdw->fd = fd;
|
||||
|
||||
JSValue obj = JS_NewObjectClass(ctx, js_fd_class_id);
|
||||
if (JS_IsException(obj)) {
|
||||
js_free(ctx, fdw);
|
||||
return obj;
|
||||
}
|
||||
|
||||
JS_SetOpaque(obj, fdw);
|
||||
return obj;
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Helper function for writing
|
||||
@@ -105,14 +65,14 @@ JSC_SCALL(fd_open,
|
||||
if (fd < 0)
|
||||
ret = JS_ThrowReferenceError(js, "open failed: %s", strerror(errno));
|
||||
else
|
||||
ret = fd2js(js, fd);
|
||||
ret = JS_NewInt32(js, fd);
|
||||
)
|
||||
|
||||
JSC_CCALL(fd_write,
|
||||
FDWrapper *fdw = js2fd(js, argv[0]);
|
||||
if (!fdw) return JS_EXCEPTION;
|
||||
int fd = js2fd(js, argv[0]);
|
||||
if (fd < 0) return JS_EXCEPTION;
|
||||
|
||||
ssize_t wrote = js_fd_write_helper(js, fdw->fd, argv[1]);
|
||||
ssize_t wrote = js_fd_write_helper(js, fd, argv[1]);
|
||||
if (wrote < 0)
|
||||
return JS_ThrowReferenceError(js, "write failed: %s", strerror(errno));
|
||||
|
||||
@@ -120,8 +80,8 @@ JSC_CCALL(fd_write,
|
||||
)
|
||||
|
||||
JSC_CCALL(fd_read,
|
||||
FDWrapper *fdw = js2fd(js, argv[0]);
|
||||
if (!fdw) return JS_EXCEPTION;
|
||||
int fd = js2fd(js, argv[0]);
|
||||
if (fd < 0) return JS_EXCEPTION;
|
||||
|
||||
size_t size = 4096;
|
||||
if (argc > 1)
|
||||
@@ -131,7 +91,7 @@ JSC_CCALL(fd_read,
|
||||
if (!buf)
|
||||
return JS_ThrowReferenceError(js, "malloc failed");
|
||||
|
||||
ssize_t bytes_read = read(fdw->fd, buf, size);
|
||||
ssize_t bytes_read = read(fd, buf, size);
|
||||
if (bytes_read < 0) {
|
||||
free(buf);
|
||||
return JS_ThrowReferenceError(js, "read failed: %s", strerror(errno));
|
||||
@@ -143,8 +103,8 @@ JSC_CCALL(fd_read,
|
||||
)
|
||||
|
||||
JSC_CCALL(fd_lseek,
|
||||
FDWrapper *fdw = js2fd(js, argv[0]);
|
||||
if (!fdw) return JS_EXCEPTION;
|
||||
int fd = js2fd(js, argv[0]);
|
||||
if (fd < 0) return JS_EXCEPTION;
|
||||
|
||||
off_t offset = js2number(js, argv[1]);
|
||||
int whence = SEEK_SET;
|
||||
@@ -156,7 +116,7 @@ JSC_CCALL(fd_lseek,
|
||||
JS_FreeCString(js, whence_str);
|
||||
}
|
||||
|
||||
off_t new_pos = lseek(fdw->fd, offset, whence);
|
||||
off_t new_pos = lseek(fd, offset, whence);
|
||||
if (new_pos < 0)
|
||||
return JS_ThrowReferenceError(js, "lseek failed: %s", strerror(errno));
|
||||
|
||||
@@ -181,33 +141,31 @@ JSC_SCALL(fd_mkdir,
|
||||
)
|
||||
|
||||
JSC_CCALL(fd_fsync,
|
||||
FDWrapper *fdw = js2fd(js, argv[0]);
|
||||
if (!fdw) return JS_EXCEPTION;
|
||||
int fd = js2fd(js, argv[0]);
|
||||
if (fd < 0) return JS_EXCEPTION;
|
||||
|
||||
if (fsync(fdw->fd) != 0)
|
||||
if (fsync(fd) != 0)
|
||||
return JS_ThrowReferenceError(js, "fsync failed: %s", strerror(errno));
|
||||
|
||||
return JS_UNDEFINED;
|
||||
)
|
||||
|
||||
JSC_CCALL(fd_close,
|
||||
FDWrapper *fdw = js2fd(js, argv[0]);
|
||||
if (!fdw) return JS_EXCEPTION;
|
||||
int fd = js2fd(js, argv[0]);
|
||||
if (fd < 0) return JS_EXCEPTION;
|
||||
|
||||
if (fdw->fd >= 0) {
|
||||
close(fdw->fd);
|
||||
fdw->fd = -1;
|
||||
}
|
||||
if (close(fd) != 0)
|
||||
return JS_ThrowReferenceError(js, "close failed: %s", strerror(errno));
|
||||
|
||||
return JS_UNDEFINED;
|
||||
)
|
||||
|
||||
JSC_CCALL(fd_fstat,
|
||||
FDWrapper *fdw = js2fd(js, argv[0]);
|
||||
if (!fdw) return JS_EXCEPTION;
|
||||
int fd = js2fd(js, argv[0]);
|
||||
if (fd < 0) return JS_EXCEPTION;
|
||||
|
||||
struct stat st;
|
||||
if (fstat(fdw->fd, &st) != 0)
|
||||
if (fstat(fd, &st) != 0)
|
||||
return JS_ThrowReferenceError(js, "fstat failed: %s", strerror(errno));
|
||||
|
||||
JSValue obj = JS_NewObject(js);
|
||||
@@ -252,13 +210,6 @@ static const JSCFunctionListEntry js_fd_funcs[] = {
|
||||
};
|
||||
|
||||
JSValue js_fd_use(JSContext *js) {
|
||||
// Initialize the file descriptor class
|
||||
JS_NewClassID(&js_fd_class_id);
|
||||
JS_NewClass(JS_GetRuntime(js), js_fd_class_id, &js_fd_class);
|
||||
|
||||
JSValue proto = JS_NewObject(js);
|
||||
JS_SetClassProto(js, js_fd_class_id, proto);
|
||||
|
||||
JSValue mod = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, mod, js_fd_funcs, countof(js_fd_funcs));
|
||||
return mod;
|
||||
|
||||
@@ -8,4 +8,4 @@ var data = fd.read(f,stat.size);
|
||||
fd.close(f)
|
||||
log.console(text(data))
|
||||
|
||||
$_.stop()
|
||||
$_.stop()
|
||||
|
||||
Reference in New Issue
Block a user