open-standards/wai/wai-web/reach/test/node-host.js
Transaction Science c4e592cfa4 wai-web: reach builds — L1 legalised, L2 MVP-lowered, J wasm2js, and an ES5 loader
The shipped wai-web package needs WebAssembly with sign-extension, non-trapping
float-to-int and bulk memory, and i64 crossing the JS boundary as BigInt.
reach/build.sh derives three more builds from that package with pinned tools
(binaryen version_123 by archive SHA-256; TypeScript, esbuild and acorn by
package-lock.json):

- L1: the shipped binary legalised at the JS interface, for engines where i64
  does not cross as BigInt;
- L2: MVP-lowered and legalised, at four profiles (full, verify_decode, verify,
  receipt), for MVP-only WebAssembly;
- J: the wasm2js translation, one ES5 script per profile, with an ES5-floor
  runtime and glue: memory views refreshed when the buffer changes, i64 values
  carried as Numbers where there is no BigInt.

wai-load.js (ES5) picks shipped, L1, L2 or J from feature probes alone and
says why; a rung can be forced for testing.

test/conformance.js (ES5) runs the workloads over the repository's corpora on
any rung and compares every output with the shipped build's; run-modern.mjs
forces every rung and profile on a modern Node and runs the demo verify
scripts against each full build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-09 18:36:04 -04:00

49 lines
1.7 KiB
JavaScript

/* The reach loader's host under Node, in ECMAScript 5 so that it runs on every Node line
* the conformance proxies use (0.12 onward).
*
* var H = require('./node-host.js');
* H.host { loadScript(file, cb), fetchBytes(file, cb), importModule(file, cb) }
* H.loadLoader(dist) evaluates dist/wai-load.js as a classic script; returns WaiLoad
* H.bytes(file) the file as a Uint8Array
*/
'use strict';
var fs = require('fs'), vm = require('vm'), path = require('path');
function toBytes(buf) {
var u;
try { u = new Uint8Array(buf); if (u.length === buf.length) return u; } catch (e) { /* engines without array-like construction */ }
u = new Uint8Array(buf.length);
for (var i = 0; i < buf.length; i++) u[i] = buf[i];
return u;
}
function bytes(file) { return toBytes(fs.readFileSync(file)); }
function runScript(file) {
vm.runInThisContext(fs.readFileSync(file, 'utf8'), file);
}
var host = {
loadScript: function (file, cb) {
try { runScript(file); } catch (e) { cb(e); return; }
cb(null);
},
fetchBytes: function (file, cb) {
var u;
try { u = bytes(file); } catch (e) { cb(e); return; }
cb(null, u);
},
importModule: function (file, cb) {
var dyn;
try { dyn = new Function('u', 'return import(u)'); } catch (e) { cb(new Error('no dynamic import on this engine: ' + e.message)); return; }
var url = 'file://' + path.resolve(file).split(path.sep).join('/');
dyn(url).then(function (m) { cb(null, m); }, function (e) { cb(e); });
}
};
function loadLoader(dist) {
runScript(path.join(dist, 'wai-load.js'));
return global.WaiLoad;
}
module.exports = { host: host, loadLoader: loadLoader, bytes: bytes, toBytes: toBytes, runScript: runScript };