WIP: pre-existing changes from WASM browser work + test infrastructure
Accumulated changes from WASM browser development sessions: - sx_runtime.ml: signal subscription + notify, env unwrap tolerance - sx_browser.bc.js: rebuilt js_of_ocaml browser kernel - sx_browser.bc.wasm.js + assets: WASM browser kernel build - sx-platform.js browser tests (test_js, test_platform, test_wasm) - Playwright sx-inspect.js: interactive page inspector tool - harness-web.sx: DOM assertion updates - deploy.sh, Dockerfile, dune-project: build config updates - test-stepper.sx: stepper unit tests - reader-macro-demo plan update Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -306,6 +306,31 @@ if (fullBuild) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Load web harnesses (DOM mocking, signals, rendering awareness)
|
||||
const webDir = path.join(projectDir, "web");
|
||||
for (const webFile of ["harness-web.sx", "harness-reactive.sx"]) {
|
||||
const wp = path.join(webDir, webFile);
|
||||
if (fs.existsSync(wp)) {
|
||||
const src = fs.readFileSync(wp, "utf8");
|
||||
const exprs = Sx.parse(src);
|
||||
for (const expr of exprs) {
|
||||
try { Sx.eval(expr, env); } catch (e) {
|
||||
console.error(`Error loading ${webFile}: ${e.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Load stepper-lib (shared stepper functions used by lib/tests/test-stepper.sx)
|
||||
const stepperLibPath = path.join(projectDir, "sx", "sx", "stepper-lib.sx");
|
||||
if (fs.existsSync(stepperLibPath)) {
|
||||
const src = fs.readFileSync(stepperLibPath, "utf8");
|
||||
const exprs = Sx.parse(src);
|
||||
for (const expr of exprs) {
|
||||
try { Sx.eval(expr, env); } catch (e) {
|
||||
console.error(`Error loading stepper-lib.sx: ${e.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Determine which tests to run
|
||||
|
||||
62
hosts/ocaml/browser/test_js.js
Normal file
62
hosts/ocaml/browser/test_js.js
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env node
|
||||
// Test js_of_ocaml build of SX kernel
|
||||
const path = require('path');
|
||||
require(path.join(__dirname, '../_build/default/browser/sx_browser.bc.js'));
|
||||
|
||||
const sx = globalThis.SxKernel;
|
||||
console.log('Engine:', sx.engine());
|
||||
|
||||
const tests = [
|
||||
['(+ 1 2)', 3],
|
||||
['(- 10 3)', 7],
|
||||
['(* 6 7)', 42],
|
||||
['(/ 10 2)', 5],
|
||||
['(= 5 5)', true],
|
||||
['(< 3 5)', true],
|
||||
['(> 5 3)', true],
|
||||
['(not false)', true],
|
||||
['(inc 5)', 6],
|
||||
['(dec 5)', 4],
|
||||
['(len (list 1 2 3))', 3],
|
||||
['(len "hello")', 5],
|
||||
['(first (list 10 20))', 10],
|
||||
['(nth "hello" 0)', 'h'],
|
||||
['(nth "hello" 4)', 'o'],
|
||||
['(str "a" "b")', 'ab'],
|
||||
['(join ", " (list "a" "b" "c"))', 'a, b, c'],
|
||||
['(let ((x 10) (y 20)) (+ x y))', 30],
|
||||
['(if true "yes" "no")', 'yes'],
|
||||
['(cond (= 1 2) "one" :else "other")', 'other'],
|
||||
['(case 2 1 "one" 2 "two" :else "other")', 'two'],
|
||||
['(render-to-html (list (quote div) "hello"))', '<div>hello</div>'],
|
||||
['(render-to-html (list (quote span) (list (quote b) "bold")))', '<span><b>bold</b></span>'],
|
||||
// Lambda + closure
|
||||
['(let ((add (fn (a b) (+ a b)))) (add 3 4))', 7],
|
||||
['(let ((x 10)) (let ((f (fn () x))) (f)))', 10],
|
||||
// Higher-order
|
||||
['(len (filter (fn (x) (> x 2)) (list 1 2 3 4 5)))', 3],
|
||||
// Recursion
|
||||
['(let ((fact (fn (n) (if (<= n 1) 1 (* n (fact (- n 1))))))) (fact 5))', 120],
|
||||
];
|
||||
|
||||
let passed = 0, failed = 0;
|
||||
for (const [expr, expected] of tests) {
|
||||
try {
|
||||
const result = sx.eval(expr);
|
||||
const ok = typeof expected === 'object'
|
||||
? result && result._type === expected._type
|
||||
: result === expected;
|
||||
if (ok) {
|
||||
passed++;
|
||||
} else {
|
||||
console.log(` FAIL: ${expr} = ${JSON.stringify(result)} (expected ${JSON.stringify(expected)})`);
|
||||
failed++;
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(` ERROR: ${expr}: ${e.message || e}`);
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`${passed} passed, ${failed} failed`);
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
134
hosts/ocaml/browser/test_platform.js
Normal file
134
hosts/ocaml/browser/test_platform.js
Normal file
@@ -0,0 +1,134 @@
|
||||
#!/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")))', '<div class="foo">bar</div>'],
|
||||
['(render-to-html (quote (br)))', '<br />'],
|
||||
// Compiler + VM
|
||||
['(let ((c (compile (quote (+ 1 2))))) (get c "bytecode"))', { check: v => v && v._type === 'list' }],
|
||||
// dom.sx loaded (functions exist even without real DOM)
|
||||
['(type-of dom-create-element)', 'lambda'],
|
||||
['(type-of dom-listen)', 'lambda'],
|
||||
// browser.sx loaded
|
||||
['(type-of console-log)', 'lambda'],
|
||||
];
|
||||
|
||||
let passed = 0, failed = 0;
|
||||
for (const [expr, expected] of tests) {
|
||||
try {
|
||||
const result = K.eval(expr);
|
||||
let ok;
|
||||
if (expected && typeof expected === 'object' && expected.check) {
|
||||
ok = expected.check(result);
|
||||
} else {
|
||||
ok = result === expected;
|
||||
}
|
||||
if (ok) {
|
||||
passed++;
|
||||
} else {
|
||||
console.log(` FAIL: ${expr}`);
|
||||
console.log(` got: ${JSON.stringify(result)}, expected: ${JSON.stringify(expected)}`);
|
||||
failed++;
|
||||
}
|
||||
} catch(e) {
|
||||
console.log(` ERROR: ${expr}: ${e.message || e}`);
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n${passed} passed, ${failed} failed`);
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
73
hosts/ocaml/browser/test_wasm.js
Normal file
73
hosts/ocaml/browser/test_wasm.js
Normal file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env node
|
||||
// Test WASM build of SX kernel
|
||||
const path = require('path');
|
||||
const build_dir = path.join(__dirname, '../_build/default/browser');
|
||||
|
||||
async function main() {
|
||||
// Load WASM module — require.main.filename must point to build dir
|
||||
// so the WASM loader finds .wasm assets via path.dirname(require.main.filename)
|
||||
require.main.filename = path.join(build_dir, 'test_wasm.js');
|
||||
require(path.join(build_dir, 'sx_browser.bc.wasm.js'));
|
||||
|
||||
// Wait for WASM init
|
||||
await new Promise(r => setTimeout(r, 2000));
|
||||
|
||||
const sx = globalThis.SxKernel;
|
||||
if (!sx) {
|
||||
console.error('FAIL: SxKernel not available');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('Engine:', sx.engine());
|
||||
|
||||
// Basic tests
|
||||
const tests = [
|
||||
['(+ 1 2)', 3],
|
||||
['(- 10 3)', 7],
|
||||
['(* 6 7)', 42],
|
||||
['(/ 10 2)', 5],
|
||||
['(= 5 5)', true],
|
||||
['(< 3 5)', true],
|
||||
['(> 5 3)', true],
|
||||
['(not false)', true],
|
||||
['(inc 5)', 6],
|
||||
['(dec 5)', 4],
|
||||
['(len (list 1 2 3))', 3],
|
||||
['(len "hello")', 5],
|
||||
['(first (list 10 20))', 10],
|
||||
['(nth "hello" 0)', 'h'],
|
||||
['(nth "hello" 4)', 'o'],
|
||||
['(str "a" "b")', 'ab'],
|
||||
['(join ", " (list "a" "b" "c"))', 'a, b, c'],
|
||||
['(let ((x 10) (y 20)) (+ x y))', 30],
|
||||
['(if true "yes" "no")', 'yes'],
|
||||
['(cond (= 1 2) "one" :else "other")', 'other'],
|
||||
['(case 2 1 "one" 2 "two" :else "other")', 'two'],
|
||||
['(render-to-html (list (quote div) "hello"))', '<div>hello</div>'],
|
||||
['(render-to-html (list (quote span) (list (quote b) "bold")))', '<span><b>bold</b></span>'],
|
||||
['(let ((add (fn (a b) (+ a b)))) (add 3 4))', 7],
|
||||
['(let ((x 10)) (let ((f (fn () x))) (f)))', 10],
|
||||
['(len (filter (fn (x) (> x 2)) (list 1 2 3 4 5)))', 3],
|
||||
['(let ((fact (fn (n) (if (<= n 1) 1 (* n (fact (- n 1))))))) (fact 5))', 120],
|
||||
];
|
||||
|
||||
let passed = 0, failed = 0;
|
||||
for (const [expr, expected] of tests) {
|
||||
const result = sx.eval(expr);
|
||||
const ok = typeof expected === 'object'
|
||||
? result && result._type === expected._type
|
||||
: result === expected;
|
||||
if (ok) {
|
||||
console.log(` PASS: ${expr} = ${JSON.stringify(result)}`);
|
||||
passed++;
|
||||
} else {
|
||||
console.log(` FAIL: ${expr} = ${JSON.stringify(result)} (expected ${JSON.stringify(expected)})`);
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n${passed} passed, ${failed} failed`);
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
main().catch(e => { console.error(e); process.exit(1); });
|
||||
@@ -1,2 +1,2 @@
|
||||
(lang dune 3.0)
|
||||
(lang dune 3.19)
|
||||
(name sx)
|
||||
|
||||
@@ -361,11 +361,44 @@ let signal_value s = match s with
|
||||
| _ -> raise (Eval_error "not a signal")
|
||||
let signal_set_value s v = match s with Signal sig' -> sig'.s_value <- v; v | _ -> raise (Eval_error "not a signal")
|
||||
let signal_subscribers s = match s with Signal sig' -> List (List.map (fun _ -> Nil) sig'.s_subscribers) | _ -> List []
|
||||
let signal_add_sub_b _s _f = Nil
|
||||
let signal_remove_sub_b _s _f = Nil
|
||||
let signal_deps _s = List []
|
||||
let signal_set_deps _s _d = Nil
|
||||
let notify_subscribers _s = Nil
|
||||
let signal_add_sub_b s f =
|
||||
match s with
|
||||
| Dict d ->
|
||||
(match Hashtbl.find_opt d "subscribers" with
|
||||
| Some (ListRef r) -> r := !r @ [f]; Nil
|
||||
| Some (List items) -> Hashtbl.replace d "subscribers" (ListRef (ref (items @ [f]))); Nil
|
||||
| _ -> Hashtbl.replace d "subscribers" (ListRef (ref [f])); Nil)
|
||||
| _ -> Nil
|
||||
|
||||
let signal_remove_sub_b s f =
|
||||
match s with
|
||||
| Dict d ->
|
||||
(match Hashtbl.find_opt d "subscribers" with
|
||||
| Some (ListRef r) -> r := List.filter (fun x -> x != f) !r; Nil
|
||||
| Some (List items) -> Hashtbl.replace d "subscribers" (List (List.filter (fun x -> x != f) items)); Nil
|
||||
| _ -> Nil)
|
||||
| _ -> Nil
|
||||
|
||||
let signal_deps s =
|
||||
match s with
|
||||
| Dict d -> (match Hashtbl.find_opt d "deps" with Some v -> v | None -> List [])
|
||||
| _ -> List []
|
||||
|
||||
let signal_set_deps s d =
|
||||
match s with
|
||||
| Dict tbl -> Hashtbl.replace tbl "deps" d; Nil
|
||||
| _ -> Nil
|
||||
|
||||
let notify_subscribers s =
|
||||
let subs = match s with
|
||||
| Dict d -> (match Hashtbl.find_opt d "subscribers" with
|
||||
| Some (ListRef { contents = items }) | Some (List items) -> items
|
||||
| _ -> [])
|
||||
| _ -> []
|
||||
in
|
||||
List.iter (fun f -> ignore (sx_call f [])) subs;
|
||||
Nil
|
||||
|
||||
let flush_subscribers _s = Nil
|
||||
let dispose_computed _s = Nil
|
||||
|
||||
|
||||
Reference in New Issue
Block a user