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 d24fa59e43
commit 246e21fb0f
20 changed files with 3576 additions and 2707 deletions
+6 -4
View File
@@ -10,6 +10,7 @@ use crate::codegen::{ExportSections, compile_exportable_module};
use crate::dictionary::WordId;
use crate::ir::IrOp;
use crate::outer::ForthVM;
use crate::runtime::Runtime;
/// Configuration for `wafer build`.
pub struct ExportConfig {
@@ -39,14 +40,14 @@ pub struct ExportMetadata {
///
/// Returns the raw `.wasm` bytes ready to write to a file, plus the metadata.
pub fn export_module(
vm: &mut ForthVM,
vm: &mut ForthVM<impl Runtime>,
config: &ExportConfig,
) -> anyhow::Result<(Vec<u8>, ExportMetadata)> {
let mut words = vm.ir_words();
// Determine the entry point.
// Priority: --entry flag > MAIN word > recorded top-level execution.
let toplevel = vm.toplevel_ir();
let toplevel = vm.toplevel_ir().to_vec();
let entry_word_id = if let Some(ref name) = config.entry_word {
Some(
vm.resolve_word(name)
@@ -58,7 +59,7 @@ pub fn export_module(
// Synthesize a _start word from recorded top-level execution.
// Pick a WordId that won't collide (one past the current table size).
let start_id = WordId(vm.current_table_size());
words.push((start_id, toplevel.to_vec()));
words.push((start_id, toplevel.clone()));
Some(start_id)
} else {
None
@@ -361,8 +362,9 @@ mod tests {
fn roundtrip(source: &str) -> String {
use crate::outer::ForthVM;
use crate::runner::run_wasm_bytes;
use crate::runtime_native::NativeRuntime;
let mut vm = ForthVM::new().unwrap();
let mut vm = ForthVM::<NativeRuntime>::new().unwrap();
vm.set_recording(true);
vm.evaluate(source).unwrap();