Skip to content
pi-c Documentation
Login

Documentation

/* The JavaScript engine seam (jsengine.h) on Bellard's QuickJS (vendor/quickjs): in codemode's
 * worker process on hosted builds, in pic's own process on the no-OS build. */
#include "jsengine.h"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "quickjs.h"

struct pic_js {
    JSRuntime *rt;
    JSContext *ctx;
    int (*interrupt)(void *ctx);
    pic_js_host host;
    void *hctx;
};

static int qjs_interrupt(JSRuntime *rt, void *opaque) {
    pic_js *js = opaque;
    (void)rt;
    return js->interrupt ? js->interrupt(js->hctx) : 0;
}

static JSValue qjs_host(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
    pic_js *js = JS_GetContextOpaque(ctx);
    const char *args[8] = {NULL};
    char *answer;
    int n = argc < 8 ? argc : 8, thrown = 0;
    JSValue out;
    (void)this_val;
    for (int i = 0; i < n; i++) {
        args[i] = JS_IsUndefined(argv[i]) || JS_IsNull(argv[i]) ? NULL : JS_ToCString(ctx, argv[i]);
    }
    answer = js->host(js->hctx, n, args, &thrown);
    for (int i = 0; i < n; i++) {
        JS_FreeCString(ctx, args[i]);
    }
    if (thrown) {
        out = JS_ThrowInternalError(ctx, "%s", answer ? answer : "the host failed");
    } else {
        out = answer ? JS_NewString(ctx, answer) : JS_UNDEFINED;
    }
    free(answer);
    return out;
}

pic_js *pic_js_new(size_t heap, size_t stack, int (*interrupt)(void *ctx), pic_js_host hostfn, void *ctx) {
    pic_js *js = calloc(1, sizeof *js);
    JSValue global;
    if (!js || !(js->rt = JS_NewRuntime())) {
        free(js);
        return NULL;
    }
    js->interrupt = interrupt;
    js->host = hostfn;
    js->hctx = ctx;
    JS_SetMemoryLimit(js->rt, heap);
    JS_SetMaxStackSize(js->rt, stack);
    JS_SetInterruptHandler(js->rt, qjs_interrupt, js);
    if (!(js->ctx = JS_NewContext(js->rt))) {
        JS_FreeRuntime(js->rt);
        free(js);
        return NULL;
    }
    JS_SetContextOpaque(js->ctx, js);
    global = JS_GetGlobalObject(js->ctx);
    JS_SetPropertyStr(js->ctx, global, "__host", JS_NewCFunction(js->ctx, qjs_host, "__host", 3));
    JS_FreeValue(js->ctx, global);
    return js;
}

/* The pending exception as "Name: message" and its stack (malloc'd). */
static char *qjs_error(JSContext *ctx) {
    JSValue e = JS_GetException(ctx), stack = JS_GetPropertyStr(ctx, e, "stack");
    const char *head = JS_ToCString(ctx, e), *frames = JS_IsString(stack) ? JS_ToCString(ctx, stack) : NULL;
    size_t n = strlen(head ? head : "Error") + (frames ? strlen(frames) : 0) + 2;
    char *out = malloc(n);
    if (out) {
        strcpy(out, head ? head : "Error");
        if (frames && *frames) {
            strcat(out, "\n");
            strcat(out, frames);
        }
    }
    JS_FreeCString(ctx, head);
    JS_FreeCString(ctx, frames);
    JS_FreeValue(ctx, stack);
    JS_FreeValue(ctx, e);
    return out;
}

int pic_js_eval(pic_js *js, const char *src, const char *file, char **err) {
    JSValue v = JS_Eval(js->ctx, src, strlen(src), file, JS_EVAL_TYPE_GLOBAL);
    *err = NULL;
    if (JS_IsException(v)) {
        *err = qjs_error(js->ctx);
        return -1;
    }
    JS_FreeValue(js->ctx, v);
    return 0;
}

int pic_js_pump(pic_js *js) {
    JSContext *job;
    int r;
    while ((r = JS_ExecutePendingJob(js->rt, &job)) != 0) {
        if (r < 0) {
            JS_FreeValue(js->ctx, JS_GetException(js->ctx));
            return -1;
        }
    }
    return 0;
}

void pic_js_free(pic_js *js) {
    if (!js) {
        return;
    }
    JS_FreeContext(js->ctx);
    JS_FreeRuntime(js->rt);
    free(js);
}

int pic_js_async(void) { return 1; }

const char *pic_js_engine(void) { return "QuickJS 2026-06-04"; }