From 7522618ac3e913b904ada9d2e2529afa7acabad3 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Thu, 18 Dec 2025 15:10:08 -0500 Subject: rust, js: do plain old object translation is wasm. use workers --- src/lib.rs | 233 +++++++++++++++++++++++++------------------------------------ 1 file changed, 97 insertions(+), 136 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 371d30f..7f65a80 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,116 +4,70 @@ use wasm_bindgen::prelude::*; pub mod forth; pub mod robo; -#[wasm_bindgen] -pub struct ExportedInstructionPointer { - pub word: usize, - pub offset: usize, -} - -impl From<&forth::vm::InstructionPointer> for ExportedInstructionPointer { - fn from(ip: &forth::vm::InstructionPointer) -> Self { - Self { - word: ip.word, - offset: ip.offset, - } - } +use web_sys::js_sys; + +fn tr_op(op: &forth::vm::OpCode) -> String { + use forth::vm::OpCode::*; + let s = match op { + If(t, None) => format!("If({t}, none)"), + If(t, Some(f)) => format!("If({t}, {f})"), + TIf(t, None) => format!("TIf({t}, none)"), + TIf(t, Some(f)) => format!("TIf({t}, {f})"), + other => format!("{other:?}"), + }; + s.to_string() } -// wasm can't wrap Vec>, so we need a custom type -// - 23-aug-2025 -#[wasm_bindgen] -pub struct ExportedByteCode(Vec); - -impl ExportedByteCode { - fn tr_op(op: &forth::vm::OpCode) -> String { - use forth::vm::OpCode::*; - let s = match op { - If(t, None) => format!("If({t}, none)"), - If(t, Some(f)) => format!("If({t}, {f})"), - TIf(t, None) => format!("TIf({t}, none)"), - TIf(t, Some(f)) => format!("TIf({t}, {f})"), - other => format!("{other:?}"), - }; - s.to_string() - } +fn map_set>(m: &js_sys::Map, k: &str, v: T) { + let jk: js_sys::JsString = k.to_string().into(); + let jv = v.into(); + m.set(&jk, &jv); } -impl From<&forth::vm::ByteCode> for ExportedByteCode { - fn from(v: &forth::vm::ByteCode) -> Self { - Self(v.iter().map(Self::tr_op).collect()) - } -} +impl From<&forth::vm::InstructionPointer> for js_sys::Map { + fn from(ip: &forth::vm::InstructionPointer) -> Self { + let res = Self::new(); -#[wasm_bindgen] -impl ExportedByteCode { - pub fn len(&self) -> usize { - self.0.len() - } + let w: js_sys::JsString = "word".to_string().into(); + let wv = (ip.word as i32).into(); + res.set(&w, &wv); - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } + let o: js_sys::JsString = "offset".to_string().into(); + let ov = (ip.offset as i32).into(); + res.set(&o, &ov); - pub fn at(&self, offset: usize) -> String { - self.0[offset].clone() + res } } -// ibid. -#[wasm_bindgen] -#[derive(Clone)] -pub struct ExportedAnnotation { - pub start: usize, - pub end: usize, -} - -impl From<&forth::compiler::Annotation> for ExportedAnnotation { - fn from(v: &forth::compiler::Annotation) -> Self { - Self { - start: v.loc.0, - end: v.loc.1, - } - } -} +impl From<&forth::compiler::Annotation> for js_sys::Map { + fn from(anno: &forth::compiler::Annotation) -> Self { + let res = Self::new(); -// ibid. -#[wasm_bindgen] -#[derive(Clone)] -pub struct ExportedWordAnnotations(Vec); + let w: js_sys::JsString = "start".to_string().into(); + let wv = (anno.loc.0 as i32).into(); + res.set(&w, &wv); -impl FromIterator for ExportedWordAnnotations { - fn from_iter>(iter: T) -> Self { - ExportedWordAnnotations(iter.into_iter().collect()) - } -} + let o: js_sys::JsString = "end".to_string().into(); + let ov = (anno.loc.1 as i32).into(); + res.set(&o, &ov); -#[wasm_bindgen] -impl ExportedWordAnnotations { - pub fn len(&self) -> usize { - self.0.len() - } - - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - pub fn at(&self, offset: usize) -> ExportedAnnotation { - self.0[offset].clone() + res } } #[wasm_bindgen] pub struct ExportedVM { - annos: Vec, vm: Option, + annos: Vec>, } #[wasm_bindgen] impl ExportedVM { fn new() -> Self { Self { - annos: vec![], vm: None, + annos: vec![], } } @@ -123,15 +77,10 @@ impl ExportedVM { error!("couldn't compile program text: {e:?}"); return false; } - self.annos = comp - .annotations - .iter() - .map(|word_anno| -> ExportedWordAnnotations { - word_anno.iter().map(|a| a.into()).collect() - }) - .collect(); + let vm = forth::vm::VM::new(comp.wordlist); let _ = self.vm.insert(vm); + self.annos = comp.annotations; true } @@ -152,38 +101,6 @@ impl ExportedVM { vm.run().map_err(|err| format!("runtime error: {err:?}")) } - pub fn stack(&self) -> Vec { - let Some(vm) = &self.vm else { return vec![] }; - - vm.stack.0.clone() - } - - pub fn wordlist(&self) -> Vec { - let Some(vm) = &self.vm else { return vec![] }; - - vm.wordlist.iter().map(|bc| bc.into()).collect() - } - - pub fn annotations(&self) -> Vec { - self.annos.clone() - } - - pub fn annotation_at(&self, ip: &ExportedInstructionPointer) -> ExportedAnnotation { - self.annos[ip.word].0[ip.offset].clone() - } - - pub fn callstack(&self) -> Vec { - let Some(vm) = &self.vm else { return vec![] }; - vm.callstack.0.iter().map(|ip| ip.into()).collect() - } - - pub fn ip(&self) -> ExportedInstructionPointer { - let Some(vm) = &self.vm else { - return ExportedInstructionPointer { word: 0, offset: 0 }; - }; - (&vm.ip).into() - } - pub fn reset_ip(&mut self) { let Some(vm) = &mut self.vm else { return; @@ -199,19 +116,63 @@ impl ExportedVM { res } - pub fn heading(&mut self) -> isize { - let Some(vm) = &self.vm else { return 0 }; - vm.heading - } - - pub fn speed(&mut self) -> isize { - let Some(vm) = &self.vm else { return 0 }; - vm.speed - } - - pub fn doppler(&mut self) -> isize { - let Some(vm) = &self.vm else { return 0 }; - vm.doppler + pub fn trans(&mut self) -> js_sys::Object { + let Some(vm) = &self.vm else { return js_sys::Object::new() }; + + let res = js_sys::Map::new(); + + // wordlist + let wl = vm.wordlist.iter().map( + |dfn| { + let ops = + dfn.iter() + .map(|op| { + Into::::into(tr_op(op)) + }); + js_sys::Array::from_iter(ops) + } + ); + map_set(&res, "wordlist", js_sys::Array::from_iter(wl)); + + // annotations + let annos = self.annos.iter().map( + |dfn| { + let ops = + dfn.iter() + .map(|anno| { + let a: js_sys::Map = anno.into(); + let o: js_sys::Object = js_sys::Object::from_entries(&a).expect("can't make object from anno map"); + o + }); + js_sys::Array::from_iter(ops) + } + ); + map_set(&res, "annos", js_sys::Array::from_iter(annos)); + + // instruction pointer + let ip: js_sys::Map = (&vm.ip).into(); + let o: js_sys::Object = js_sys::Object::from_entries(&ip).expect("can't make object from ip map"); + map_set(&res, "ip", o); + + // stack + let s = vm.stack.0.iter().map(|v| Into::::into(*v as i32)); + map_set(&res, "stack", js_sys::Array::from_iter(s)); + + // callstack + let cs = vm.callstack.0.iter().map(|v| { + let ip: js_sys::Map = v.into(); + let o: js_sys::Object = js_sys::Object::from_entries(&ip).expect("can't make object from ip map"); + o + }); + map_set(&res, "callstack", js_sys::Array::from_iter(cs)); + + let vars = js_sys::Map::new(); + map_set::(&vars, "heading", vm.heading.into()); + map_set::(&vars, "speed", vm.speed.into()); + map_set::(&vars, "doppler", vm.doppler.into()); + map_set(&res, "vars", js_sys::Object::from_entries(&vars).expect("can't make object from vars map")); + + js_sys::Object::from_entries(&res).expect("can't make object from results map") } } -- cgit v1.3