From 6cf5fe636f8993e1c28b91541afbd89e3a70ab49 Mon Sep 17 00:00:00 2001 From: Matteo Almanza Date: Mon, 16 Sep 2024 12:56:38 +0200 Subject: [PATCH 1/2] feat: add error mapping for already used arg throw --- clarity/src/vm/clarity_wasm.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/clarity/src/vm/clarity_wasm.rs b/clarity/src/vm/clarity_wasm.rs index e92f9d6858..5cc402976b 100644 --- a/clarity/src/vm/clarity_wasm.rs +++ b/clarity/src/vm/clarity_wasm.rs @@ -7651,6 +7651,8 @@ mod error_mapping { use crate::vm::types::ResponseData; use crate::vm::Value; + use super::read_identifier_from_wasm; + const LOG2_ERROR_MESSAGE: &str = "log2 must be passed a positive integer"; const SQRTI_ERROR_MESSAGE: &str = "sqrti must be passed a positive integer"; const POW_ERROR_MESSAGE: &str = "Power argument to (pow ...) must be a u32 integer"; @@ -7666,6 +7668,7 @@ mod error_mapping { Panic = 6, ShortReturnAssertionFailure = 7, ArithmeticPowError = 8, + NameAlreadyUsed = 9, NotMapped = 99, } @@ -7682,6 +7685,7 @@ mod error_mapping { 6 => ErrorMap::Panic, 7 => ErrorMap::ShortReturnAssertionFailure, 8 => ErrorMap::ArithmeticPowError, + 9 => ErrorMap::NameAlreadyUsed, _ => ErrorMap::NotMapped, } } @@ -7804,6 +7808,34 @@ mod error_mapping { RuntimeErrorType::Arithmetic(POW_ERROR_MESSAGE.into()), Some(Vec::new()), ), + ErrorMap::NameAlreadyUsed => { + let runtime_error_arg_offset = instance + .get_global(&mut store, "runtime-error-arg-offset") + .and_then(|glob| glob.get(&mut store).i32()) + .unwrap_or_else(|| { + panic!("Could not find $runtime-error-arg-offset global with i32 value") + }); + + let runtime_error_arg_len = instance + .get_global(&mut store, "runtime-error-arg-len") + .and_then(|glob| glob.get(&mut store).i32()) + .unwrap_or_else(|| { + panic!("Could not find $runtime-error-arg-len global with i32 value") + }); + + let memory = instance + .get_memory(&mut store, "memory") + .unwrap_or_else(|| panic!("Could not find wasm instance memory")); + let arg_name = read_identifier_from_wasm( + memory, + &mut store, + runtime_error_arg_offset, + runtime_error_arg_len, + ) + .unwrap_or_else(|e| panic!("Could not recover arg_name: {e}")); + + Error::Unchecked(CheckErrors::NameAlreadyUsed(arg_name)) + } _ => panic!("Runtime error code {} not supported", runtime_error_code), } } From 0a282a9831b7a434adcc501b33469d32adc6caa3 Mon Sep 17 00:00:00 2001 From: Matteo Almanza Date: Mon, 16 Sep 2024 13:01:45 +0200 Subject: [PATCH 2/2] chore: fmt --- clarity/src/vm/clarity_wasm.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clarity/src/vm/clarity_wasm.rs b/clarity/src/vm/clarity_wasm.rs index 5cc402976b..7883c08859 100644 --- a/clarity/src/vm/clarity_wasm.rs +++ b/clarity/src/vm/clarity_wasm.rs @@ -7647,12 +7647,11 @@ mod tests { mod error_mapping { use wasmtime::{AsContextMut, Instance, Trap}; + use super::read_identifier_from_wasm; use crate::vm::errors::{CheckErrors, Error, RuntimeErrorType, ShortReturnType, WasmError}; use crate::vm::types::ResponseData; use crate::vm::Value; - use super::read_identifier_from_wasm; - const LOG2_ERROR_MESSAGE: &str = "log2 must be passed a positive integer"; const SQRTI_ERROR_MESSAGE: &str = "sqrti must be passed a positive integer"; const POW_ERROR_MESSAGE: &str = "Power argument to (pow ...) must be a u32 integer";