Runtime abstraction + browser REPL

Decouple ForthVM from wasmtime via a Runtime trait so the same outer
interpreter, compiler, and 200+ word definitions work on both native
(wasmtime) and browser (js-sys WebAssembly API) backends.

Runtime trait (runtime.rs):
- HostAccess trait for memory/global ops inside host function closures
- HostFn type: Box<dyn Fn(&mut dyn HostAccess) -> Result<()>>
- Runtime trait: memory, globals, table, instantiate, call, register

NativeRuntime (runtime_native.rs):
- Wraps wasmtime Engine/Store/Memory/Table/Global/Func
- CallerHostAccess bridges HostAccess to wasmtime Caller API
- Feature-gated behind "native" (default)

outer.rs refactor:
- ForthVM<R: Runtime> — generic over execution backend
- All 87 host functions converted from Func::new closures to HostFn
- All memory access via rt.mem_read/write_*, global access via rt.get/set_*
- Zero logic changes — pure API conversion

wafer-core feature gates:
- default = ["native"] includes wasmtime + all native modules
- Without "native": pure Rust only (outer, codegen, optimizer, dictionary)

Browser REPL (crates/web):
- WebRuntime: js-sys WebAssembly.Memory/Table/Global/Module/Instance
- WaferRepl: wasm-bindgen entry point (evaluate, data_stack, reset)
- WebAssembly.Function with Safari fallback (wrapper module)
- Frontend: dark terminal UI, word panel, init code editor, history
- Build: wasm-pack build --target web

All 452 tests pass (431 unit + 1 benchmark + 9 comparison + 11 compliance).
This commit is contained in:
2026-04-13 10:06:37 +02:00
parent 7780ea3ab3
commit 321f001232
20 changed files with 3576 additions and 2707 deletions
+7 -4
View File
@@ -6,6 +6,7 @@
use std::time::Instant;
use wafer_core::config::WaferConfig;
use wafer_core::outer::ForthVM;
use wafer_core::runtime_native::NativeRuntime;
// -----------------------------------------------------------------------
// Benchmark definitions
@@ -203,7 +204,8 @@ struct BenchResult {
fn run_benchmark(config: &WaferConfig, bench: &Benchmark) -> BenchResult {
// Compile
let compile_start = Instant::now();
let mut vm = ForthVM::new_with_config(config.clone()).expect("VM creation failed");
let mut vm =
ForthVM::<NativeRuntime>::new_with_config(config.clone()).expect("VM creation failed");
for line in bench.define.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() {
@@ -246,7 +248,8 @@ fn correctness_all_configs() {
for (cfg_name, config) in &configs {
for bench in &benches {
let mut vm = ForthVM::new_with_config(config.clone()).expect("VM creation failed");
let mut vm = ForthVM::<NativeRuntime>::new_with_config(config.clone())
.expect("VM creation failed");
let mut define_ok = true;
for line in bench.define.lines() {
let trimmed = line.trim();
@@ -427,8 +430,8 @@ fn optimization_report() {
let result_all = run_benchmark(&all_config, bench);
// With CONSOLIDATE
let mut vm_consol =
ForthVM::new_with_config(all_config.clone()).expect("VM creation failed");
let mut vm_consol = ForthVM::<NativeRuntime>::new_with_config(all_config.clone())
.expect("VM creation failed");
for line in bench.define.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() {
+5 -3
View File
@@ -12,6 +12,7 @@ use std::sync::OnceLock;
use wafer_core::config::WaferConfig;
use wafer_core::outer::ForthVM;
use wafer_core::runtime_native::NativeRuntime;
// -----------------------------------------------------------------------
// Gforth discovery (cached)
@@ -74,7 +75,7 @@ struct EngineResult {
/// Run Forth code through WAFER (in-process via `ForthVM`).
fn run_wafer(code: &str) -> EngineResult {
let mut vm = ForthVM::new().expect("Failed to create ForthVM");
let mut vm = ForthVM::<NativeRuntime>::new().expect("Failed to create ForthVM");
let mut output = String::new();
for line in code.lines() {
let trimmed = line.trim();
@@ -99,7 +100,8 @@ fn run_wafer(code: &str) -> EngineResult {
/// Run Forth code through WAFER with all optimizations enabled.
fn run_wafer_optimized(code: &str) -> EngineResult {
let mut vm = ForthVM::new_with_config(WaferConfig::all()).expect("Failed to create ForthVM");
let mut vm = ForthVM::<NativeRuntime>::new_with_config(WaferConfig::all())
.expect("Failed to create ForthVM");
let mut output = String::new();
for line in code.lines() {
let trimmed = line.trim();
@@ -809,7 +811,7 @@ fn performance_report() {
// Verify correctness first
for bench in &benchmarks {
let mut vm = ForthVM::new().expect("VM creation failed");
let mut vm = ForthVM::<NativeRuntime>::new().expect("VM creation failed");
for line in bench.define.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() {
+8 -7
View File
@@ -5,6 +5,7 @@
//! asserting 0 test failures.
use wafer_core::outer::ForthVM;
use wafer_core::runtime_native::NativeRuntime;
/// Path to the test suite source directory.
const SUITE_DIR: &str = concat!(
@@ -13,7 +14,7 @@ const SUITE_DIR: &str = concat!(
);
/// Load a file and evaluate it line by line, ignoring errors on individual lines.
fn load_file(vm: &mut ForthVM, path: &str) {
fn load_file(vm: &mut ForthVM<NativeRuntime>, path: &str) {
let source = std::fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read {path}"));
for line in source.lines() {
let _ = vm.evaluate(line);
@@ -22,8 +23,8 @@ fn load_file(vm: &mut ForthVM, path: &str) {
}
/// Boot a WAFER VM with full prerequisites loaded.
fn boot_with_prerequisites() -> ForthVM {
let mut vm = ForthVM::new().expect("Failed to create ForthVM");
fn boot_with_prerequisites() -> ForthVM<NativeRuntime> {
let mut vm = ForthVM::<NativeRuntime>::new().expect("Failed to create ForthVM");
// Load test framework
load_file(&mut vm, &format!("{SUITE_DIR}/tester.fr"));
@@ -40,7 +41,7 @@ fn boot_with_prerequisites() -> ForthVM {
}
/// Run a test suite file and return the #ERRORS count.
fn run_suite(vm: &mut ForthVM, test_file: &str) -> u32 {
fn run_suite(vm: &mut ForthVM<NativeRuntime>, test_file: &str) -> u32 {
// Reset error counter
let _ = vm.evaluate("DECIMAL 0 #ERRORS !");
vm.take_output();
@@ -74,7 +75,7 @@ fn run_suite(vm: &mut ForthVM, test_file: &str) -> u32 {
#[test]
fn compliance_core() {
let mut vm = ForthVM::new().expect("Failed to create ForthVM");
let mut vm = ForthVM::<NativeRuntime>::new().expect("Failed to create ForthVM");
load_file(&mut vm, &format!("{SUITE_DIR}/tester.fr"));
load_file(&mut vm, &format!("{SUITE_DIR}/core.fr"));
@@ -94,7 +95,7 @@ fn compliance_core_plus() {
fn compliance_core_ext() {
// Core Extensions are loaded as part of prerequisites.
// Run from scratch to get a clean error count.
let mut vm = ForthVM::new().expect("Failed to create ForthVM");
let mut vm = ForthVM::<NativeRuntime>::new().expect("Failed to create ForthVM");
load_file(&mut vm, &format!("{SUITE_DIR}/tester.fr"));
load_file(&mut vm, &format!("{SUITE_DIR}/core.fr"));
let _ = vm.evaluate("DECIMAL");
@@ -162,7 +163,7 @@ fn compliance_search_order() {
fn compliance_string() {
// Run from scratch -- the stringtest includes CoreExt tests that
// cascade failures when run on top of an already-loaded CoreExt suite.
let mut vm = ForthVM::new().expect("Failed to create ForthVM");
let mut vm = ForthVM::<NativeRuntime>::new().expect("Failed to create ForthVM");
load_file(&mut vm, &format!("{SUITE_DIR}/tester.fr"));
load_file(&mut vm, &format!("{SUITE_DIR}/core.fr"));
let _ = vm.evaluate("DECIMAL");