#!/usr/bin/env node /** * Test the full WASM + platform stack in Node. * Loads the kernel, registers FFI stubs, loads .sx web files. */ const path = require('path'); const fs = require('fs'); // Load js_of_ocaml kernel (WASM needs browser; JS works in Node) require(path.join(__dirname, '../_build/default/browser/sx_browser.bc.js')); const K = globalThis.SxKernel; console.log('Engine:', K.engine()); // Register FFI stubs (no real DOM in Node, but the primitives must exist) K.registerNative("host-global", (args) => { const name = args[0]; return globalThis[name] || null; }); K.registerNative("host-get", (args) => { const [obj, prop] = args; if (obj == null) return null; const v = obj[prop]; return v === undefined ? null : v; }); K.registerNative("host-set!", (args) => { const [obj, prop, val] = args; if (obj != null) obj[prop] = val; }); K.registerNative("host-call", (args) => { const [obj, method, ...rest] = args; if (obj == null) return null; if (typeof obj[method] === 'function') { try { return obj[method].apply(obj, rest); } catch(e) { return null; } } return null; }); K.registerNative("host-new", (args) => null); K.registerNative("host-callback", (args) => { const fn = args[0]; if (typeof fn === 'function') return fn; if (fn && fn.__sx_handle !== undefined) return (...a) => K.callFn(fn, a); return () => {}; }); K.registerNative("host-typeof", (args) => { const obj = args[0]; if (obj == null) return "nil"; return typeof obj; }); K.registerNative("host-await", (args) => { const [promise, callback] = args; if (promise && typeof promise.then === 'function') { const cb = typeof callback === 'function' ? callback : (callback && callback.__sx_handle !== undefined) ? (v) => K.callFn(callback, [v]) : () => {}; promise.then(cb); } }); // Load .sx web files in order const root = path.join(__dirname, '../../..'); const sxFiles = [ 'spec/render.sx', // HTML_TAGS, VOID_ELEMENTS, BOOLEAN_ATTRS, parse-element-args 'web/signals.sx', 'web/deps.sx', 'web/router.sx', 'web/page-helpers.sx', 'lib/bytecode.sx', 'lib/compiler.sx', 'lib/vm.sx', 'web/lib/dom.sx', 'web/lib/browser.sx', 'web/adapter-html.sx', 'web/adapter-sx.sx', // Skip adapter-dom.sx, engine.sx, orchestration.sx, boot.sx — need real DOM ]; let totalExprs = 0; for (const f of sxFiles) { const src = fs.readFileSync(path.join(root, f), 'utf8'); const result = K.load(src); if (typeof result === 'string' && result.startsWith('Error')) { console.error(` FAIL loading ${f}: ${result}`); process.exit(1); } totalExprs += (typeof result === 'number' ? result : 0); } console.log(`Loaded ${totalExprs} expressions from ${sxFiles.length} .sx files`); // Test the loaded stack const tests = [ // Signals ['(let ((s (signal 0))) (reset! s 42) (deref s))', 42], ['(let ((s (signal 10))) (swap! s inc) (deref s))', 11], // Computed ['(let ((a (signal 2)) (b (computed (fn () (* (deref a) 3))))) (deref b))', 6], // Render (OCaml renderer uses XHTML-style void tags) ['(render-to-html (quote (div :class "foo" "bar")))', '