From 690faa6b79f6f70743fcc68b9a0a2730f0228019 Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Sat, 23 Nov 2024 12:22:54 +1100 Subject: [PATCH] add roc_expect_failed --- crates/roc_host/src/glue.rs | 20 ++++++++++++++++++++ crates/roc_host/src/lib.rs | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/crates/roc_host/src/glue.rs b/crates/roc_host/src/glue.rs index f81c9c3..f10a0ee 100644 --- a/crates/roc_host/src/glue.rs +++ b/crates/roc_host/src/glue.rs @@ -92,3 +92,23 @@ impl From for IOErr { } } } + +#[repr(C)] +pub struct Variable { + pub name: RocStr, + pub value: RocStr, +} + +impl roc_std::RocRefcounted for Variable { + fn inc(&mut self) { + self.name.inc(); + self.value.inc(); + } + fn dec(&mut self) { + self.name.dec(); + self.value.dec(); + } + fn is_refcounted() -> bool { + true + } +} diff --git a/crates/roc_host/src/lib.rs b/crates/roc_host/src/lib.rs index dca4167..c130d76 100644 --- a/crates/roc_host/src/lib.rs +++ b/crates/roc_host/src/lib.rs @@ -129,6 +129,29 @@ pub unsafe extern "C" fn roc_dbg(loc: &RocStr, msg: &RocStr, src: &RocStr) { eprintln!("[{}] {} = {}", loc, src, msg); } +/// # Safety +/// +/// This function is unsafe. +#[no_mangle] +pub unsafe extern "C" fn roc_expect_failed( + loc: &RocStr, + src: &RocStr, + variables: &RocList, +) { + eprintln!("\nExpectation failed at {}:", loc.as_str()); + eprintln!("\nExpression:\n\t{}\n", src.as_str()); + + if !variables.is_empty() { + eprintln!("With values:"); + for var in variables.iter() { + eprintln!("\t{} = {}", var.name.as_str(), var.value.as_str()); + } + eprintln!(); + } + + std::process::exit(1); +} + /// # Safety /// /// This function is unsafe.