From 3913254215e69cbf853f64ceff21c3491074ae51 Mon Sep 17 00:00:00 2001 From: liquidev Date: Thu, 22 Aug 2024 17:50:44 +0200 Subject: [PATCH] fix updated defs not being applied properly to VM in frontend and backend this fixes the case where (def botsbuildbots (fn () (botsbuildbots))) (botsbuildbots) would not run properly (return with a "set def index out of bounds" error) also make exceptions store String instead of &'static str for better error reporting closes #33 --- crates/haku-wasm/src/lib.rs | 2 ++ crates/haku/src/render.rs | 4 ++-- crates/haku/src/vm.rs | 14 ++++++++------ crates/haku/tests/language.rs | 13 +++++++++++++ crates/rkgk/src/haku.rs | 2 ++ 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/crates/haku-wasm/src/lib.rs b/crates/haku-wasm/src/lib.rs index dc94179..92dc725 100644 --- a/crates/haku-wasm/src/lib.rs +++ b/crates/haku-wasm/src/lib.rs @@ -328,6 +328,8 @@ unsafe extern "C" fn haku_compile_brush( }; brush.state = BrushState::Ready(chunk_id); + instance.vm.apply_defs(&instance.defs); + info!("brush compiled into {chunk_id:?}"); StatusCode::Ok diff --git a/crates/haku/src/render.rs b/crates/haku/src/render.rs index 6a43e43..1ba1a21 100644 --- a/crates/haku/src/render.rs +++ b/crates/haku/src/render.rs @@ -43,8 +43,8 @@ impl<'a> Renderer<'a> { } } - fn create_exception(_vm: &Vm, _at: Value, message: &'static str) -> Exception { - Exception { message } + fn create_exception(vm: &Vm, _at: Value, message: &'static str) -> Exception { + vm.create_exception(message) } fn transform(&self) -> Transform { diff --git a/crates/haku/src/vm.rs b/crates/haku/src/vm.rs index 49e1b10..d9cb816 100644 --- a/crates/haku/src/vm.rs +++ b/crates/haku/src/vm.rs @@ -4,7 +4,7 @@ use core::{ iter, }; -use alloc::vec::Vec; +use alloc::{string::String, vec::Vec}; use crate::{ bytecode::{self, Defs, Opcode, CAPTURE_CAPTURE, CAPTURE_LOCAL}, @@ -416,8 +416,10 @@ impl Vm { } } - pub fn create_exception(&self, message: &'static str) -> Exception { - Exception { message } + pub fn create_exception(&self, message: impl Into) -> Exception { + Exception { + message: message.into(), + } } pub fn track_array(&mut self, array: &[T]) -> Result<(), Exception> { @@ -492,15 +494,15 @@ impl FnArgs { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Exception { - pub message: &'static str, + pub message: String, } impl From for Exception { fn from(_: bytecode::ReadError) -> Self { Self { - message: "corrupted bytecode", + message: "corrupted bytecode".into(), } } } diff --git a/crates/haku/tests/language.rs b/crates/haku/tests/language.rs index 48d52e2..594d549 100644 --- a/crates/haku/tests/language.rs +++ b/crates/haku/tests/language.rs @@ -167,6 +167,19 @@ fn def_mutually_recursive() { expect_number(code, 14.0, 0.0001); } +#[test] +fn def_botsbuildbots() { + let result = eval("(def botsbuildbots (fn () (botsbuildbots))) (botsbuildbots)"); + if let Err(error) = result { + assert_eq!( + error.to_string(), + "Exception {\n message: \"too much recursion\",\n}" + ); + } else { + panic!("error expected"); + } +} + #[test] fn let_single() { let code = r#" diff --git a/crates/rkgk/src/haku.rs b/crates/rkgk/src/haku.rs index 073d381..6a5e4ae 100644 --- a/crates/rkgk/src/haku.rs +++ b/crates/rkgk/src/haku.rs @@ -111,6 +111,8 @@ impl Haku { let chunk_id = self.system.add_chunk(chunk).context("too many chunks")?; self.brush = Some(chunk_id); + self.vm.apply_defs(&self.defs); + Ok(()) }