/* Host test for js_mu.c (the MuJS-style API over QuickJS) and the engine's * modern-syntax coverage the browser relies on. Run: make test-js */ #include #include #include "js_mu.h" static int fails; static char last_report[256]; static void report(js_State *J, const char *msg) { (void)J; snprintf(last_report, sizeof(last_report), "%s", msg); } /* Evaluate `src` and compare String(result) with `want`. */ static void expect(js_State *J, const char *src, const char *want) { char wrapped[2048]; const char *got; snprintf(wrapped, sizeof(wrapped), "globalThis.__r = (function(){ %s })();", src); if (js_ploadstring(J, "[test]", wrapped)) { printf("FAIL compile: %s\n %s\n", src, js_trystring(J, -1, "?")); js_pop(J, 1); fails++; return; } js_pushundefined(J); if (js_pcall(J, 0)) { printf("FAIL run: %s\n %s\n", src, js_trystring(J, -1, "?")); js_pop(J, 1); fails++; return; } js_pop(J, 1); js_runjobs(J); js_getglobal(J, "__r"); got = js_tostring(J, -1); if (strcmp(got, want)) { printf("FAIL %s\n got %s\n want %s\n", src, got, want); fails++; } js_pop(J, 1); } /* A C function that throws a TypeError for a negative argument. */ static void c_checked(js_State *J) { int v = js_tointeger(J, 1); if (v < 0) js_typeerror(J, "negative %d", v); js_pushnumber(J, v * 2); } /* Used with `new`: returns its own object, like the DOM Event constructors. */ static void c_make(js_State *J) { js_newobject(J); js_copy(J, 1); js_setproperty(J, -2, "type"); } static void c_data(js_State *J) { js_pushnumber(J, (double)(unsigned long)js_currentfunctiondata(J)); } /* Userdata: "kind" is a read-only hook property, "value" is stored in C, * everything else falls through to ordinary expando properties. */ static double ud_value = 1; static int ud_has(js_State *J, void *p, const char *name) { if (!strcmp(name, "kind")) { js_pushstring(J, (const char *)p); return 1; } if (!strcmp(name, "value")) { js_pushnumber(J, ud_value); return 1; } return 0; } static int ud_put(js_State *J, void *p, const char *name) { (void)p; if (!strcmp(name, "value")) { ud_value = js_tonumber(J, -1); return 1; } return 0; } int main(void) { js_State *J = js_newstate(0, 0, 0); js_setreport(J, report); js_newcfunction(J, c_checked, "checked", 1); js_defglobal(J, "checked", JS_DONTENUM); js_newcfunction(J, c_make, "Make", 1); js_defglobal(J, "Make", JS_DONTENUM); js_newcfunctionx(J, c_data, "data", 0, (void *)42, 0); js_defglobal(J, "data", JS_DONTENUM); js_pushnull(J); js_newuserdatax(J, "thing", (void *)"widget", ud_has, ud_put, 0, 0); js_defglobal(J, "thing", JS_DONTENUM); /* modern syntax */ expect(J, "const a = [1, 2, 3]; return a.map(x => x * 2).join(',');", "2,4,6"); expect(J, "let s = 0; for (const v of [1, 2, 3]) s += v; return s;", "6"); expect(J, "const n = 5; return `n=${n + 1}`;", "n=6"); expect(J, "class A { constructor(x) { this.x = x; } get d() { return this.x * 2; } }" " class B extends A {} return new B(4).d;", "8"); expect(J, "const {p, q = 3} = {p: 1}; const [x, ...r] = [7, 8, 9];" " return [p, q, x, r.length].join(',');", "1,3,7,2"); expect(J, "const o = {a: 1, ...{b: 2}}; return Object.entries(o).flat().join('');", "a1b2"); expect(J, "return [1, 2, 3].includes(2) && 'abc'.startsWith('ab') &&" " Array.from(new Set([1, 1, 2])).length === 2;", "true"); expect(J, "const m = new Map([[1, 'x']]); return m.get(1) + (null ?? 'd') + (undefined?.q ?? 'e');", "xde"); expect(J, "let out = ''; Promise.resolve(3).then(v => { globalThis.__p = v; });" " return 'sync';", "sync"); expect(J, "return String(globalThis.__p);", "3"); expect(J, "async function f() { await null; return 7; } f().then(v => globalThis.__q = v); return 0;", "0"); expect(J, "return String(globalThis.__q);", "7"); expect(J, "return 2n ** 64n + 1n;", "18446744073709551617"); expect(J, "return /(?\\d{4})/.exec('in 2026').groups.y;", "2026"); /* C functions: arguments, return value, function data, throws */ expect(J, "return checked(21);", "42"); expect(J, "try { checked(-1); return 'no'; } catch (e) {" " return e instanceof TypeError ? e.message : 'wrong'; }", "negative -1"); expect(J, "return data();", "42"); expect(J, "return new Make('click').type;", "click"); /* userdata hooks and expandos */ expect(J, "return thing.kind;", "widget"); expect(J, "thing.value = 9; return thing.value * 2;", "18"); expect(J, "thing.extra = {k: 1}; return thing.extra.k + ('extra' in thing ? 1 : 0);", "2"); expect(J, "return Object.keys(thing).join(',');", "extra"); expect(J, "delete thing.extra; return typeof thing.extra;", "undefined"); /* registry refs */ js_pushstring(J, "kept"); { char ref[32]; snprintf(ref, sizeof(ref), "%s", js_ref(J)); js_getregistry(J, ref); if (strcmp(js_tostring(J, -1), "kept")) { printf("FAIL registry\n"); fails++; } js_pop(J, 1); js_unref(J, ref); } /* the wall-clock budget turns a runaway loop into an exception */ js_setlimit(J, 200, 0); if (js_ploadstring(J, "[loop]", "for (;;) {}") == 0) { js_pushundefined(J); if (!js_pcall(J, 0)) { printf("FAIL loop was not interrupted\n"); fails++; } js_pop(J, 1); } js_setlimit(J, 0, 0); expect(J, "return 'alive';", "alive"); if (js_gettop(J) != 0) { printf("FAIL stack not balanced: %d\n", js_gettop(J)); fails++; } js_freestate(J); printf("js test: %s (%d failure(s))\n", fails ? "FAIL" : "OK", fails); return fails != 0; }