Skip to content
pi-c Artifact [318af5f8fd]
Login

Artifact [318af5f8fd]

Artifact 318af5f8fd798813b39969c711fea3438947124db40036bdd30e31b6cef811ae:


/* The JavaScript engine seam (jsengine.h) on Micro QuickJS (vendor/mquickjs), for the no-OS build
 * (PIC_CODEMODE_MICRO): no processes to put a worker in, so the engine runs in pic's own process,
 * in an arena of its own; it allocates nothing else. ES5 with for...of and **, no promises. Its
 * standard library is tools/codemode-mqjs-stdlib.c, built into C tables at compile time. */
#include "jsengine.h"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

#include <mquickjs.h>

/* A heap past this is not asked for: the whole arena is taken when the engine is made. */
#define MICRO_HEAP_MAX ((size_t)64 << 20)

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

/* The engine reaches its host through the one context it knows. */
static pic_js *micro_current;

static int64_t micro_date_ms(void) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

/* The library's Date (only Date.now() and new Date(ms)), as the mqjs shell defines it. */
JSValue js_date_constructor(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv);
JSValue js_date_constructor(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
    double val;
    (void)this_val;
    argc &= ~FRAME_CF_CTOR;
    if (argc == 0) {
        val = (double)micro_date_ms();
    } else if (argc == 1 && JS_IsNumber(ctx, argv[0])) {
        if (JS_ToNumber(ctx, &val, argv[0])) {
            return JS_EXCEPTION;
        }
    } else {
        return JS_ThrowTypeError(ctx, "unsupported Date() parameter");
    }
    return JS_NewDate(ctx, val);
}

JSValue js_date_now(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv);
JSValue js_date_now(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
    (void)this_val;
    (void)argc;
    (void)argv;
    return JS_NewInt64(ctx, micro_date_ms());
}

/* A string value as a NUL-terminated copy (malloc'd); NULL for undefined and null. Copied at
 * once: the collector may move what the engine handed out. */
static char *micro_string(JSContext *ctx, JSValue v) {
    JSCStringBuf buf;
    const char *s;
    size_t len = 0;
    char *out;
    if (JS_IsUndefined(v) || JS_IsNull(v)) {
        return NULL;
    }
    if (!(s = JS_ToCStringLen(ctx, &len, v, &buf)) || !(out = malloc(len + 1))) {
        return NULL;
    }
    memcpy(out, s, len);
    out[len] = '\0';
    return out;
}

JSValue js_pic_host(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv);
JSValue js_pic_host(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
    pic_js *js = micro_current;
    char *args[8] = {NULL}, *answer;
    int n = (argc & ~FRAME_CF_CTOR) < 8 ? (argc & ~FRAME_CF_CTOR) : 8, thrown = 0;
    JSValue out;
    (void)this_val;
    for (int i = 0; i < n; i++) {
        args[i] = micro_string(ctx, argv[i]);
    }
    answer = js->host(js->hctx, n, (const char *const *)args, &thrown);
    for (int i = 0; i < n; i++) {
        free(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;
}

extern const JSSTDLibraryDef js_stdlib;

static int micro_interrupt(JSContext *ctx, void *opaque) {
    (void)ctx;
    (void)opaque;
    return micro_current && micro_current->interrupt ? micro_current->interrupt(micro_current->hctx) : 0;
}

pic_js *pic_js_new(size_t heap, size_t stack, int (*interrupt)(void *ctx), pic_js_host host, void *ctx) {
    pic_js *js = calloc(1, sizeof *js);
    (void)stack; /* the VM keeps its own stack in the arena */
    if (heap > MICRO_HEAP_MAX) {
        heap = MICRO_HEAP_MAX;
    }
    if (!js || micro_current || !(js->arena = malloc(heap))) {
        free(js);
        return NULL;
    }
    js->interrupt = interrupt;
    js->host = host;
    js->hctx = ctx;
    micro_current = js;
    if (!(js->ctx = JS_NewContext(js->arena, heap, &js_stdlib))) {
        micro_current = NULL;
        free(js->arena);
        free(js);
        return NULL;
    }
    JS_SetInterruptHandler(js->ctx, micro_interrupt);
    return js;
}

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, 0);
    *err = NULL;
    if (JS_IsException(v)) {
        JSValue e = JS_GetException(js->ctx);
        char *head = micro_string(js->ctx, JS_ToString(js->ctx, e));
        *err = head ? head : strdup("Error");
        return -1;
    }
    return 0;
}

int pic_js_pump(pic_js *js) {
    (void)js; /* no promises, so no jobs */
    return 0;
}

void pic_js_free(pic_js *js) {
    if (!js) {
        return;
    }
    JS_FreeContext(js->ctx);
    free(js->arena);
    if (micro_current == js) {
        micro_current = NULL;
    }
    free(js);
}

int pic_js_async(void) { return 0; }

const char *pic_js_engine(void) { return "Micro QuickJS 2026-11-21"; }

/* The library's tables (built by the Makefile from tools/codemode-mqjs-stdlib.c), after the
 * functions they name. */
#include <codemode_stdlib.h>