diff --git a/canister_templates/stable.wasm b/canister_templates/stable.wasm index 0a3425f5d2..2116ba7dde 100644 Binary files a/canister_templates/stable.wasm and b/canister_templates/stable.wasm differ diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_accept.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_accept.rs index 95bfe3b268..28f67bdbd4 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_accept.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_accept.rs @@ -1,10 +1,10 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::call::msg_cycles_accept128; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, |max_amount_string: String| { +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, |max_amount_string: String| { let max_amount: u128 = max_amount_string.parse().unwrap(); - ic_cdk::api::call::msg_cycles_accept128(max_amount).to_string() + msg_cycles_accept128(max_amount).to_string() }) - .unwrap() } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_available.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_available.rs index eaba647961..8846a4be03 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_available.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_available.rs @@ -1,8 +1,6 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::call::msg_cycles_available128; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, || { - ic_cdk::api::call::msg_cycles_available128().to_string() - }) - .unwrap() +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, || msg_cycles_available128().to_string()) } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_refunded.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_refunded.rs index 1eaeaa9194..b19e3cf209 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_refunded.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/msg_cycles_refunded.rs @@ -1,8 +1,6 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::call::msg_cycles_refunded128; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, || { - ic_cdk::api::call::msg_cycles_refunded128().to_string() - }) - .unwrap() +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, || msg_cycles_refunded128().to_string()) } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/performance_counter.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/performance_counter.rs index b7af20f182..f2e8b7e9c9 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/performance_counter.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/performance_counter.rs @@ -1,8 +1,7 @@ -use rquickjs::{Ctx, Function}; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, |counter_type: u32| { +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, |counter_type: u32| -> u64 { ic_cdk::api::performance_counter(counter_type) }) - .unwrap() } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/print.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/print.rs index f35d190399..f2ebd9da52 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/print.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/print.rs @@ -1,8 +1,7 @@ -use rquickjs::{Ctx, Function}; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> rquickjs::Function { - Function::new(context, |message: String| { +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, |message: String| { ic_cdk::print(message); }) - .unwrap() } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject.rs index a43452c82a..891aed3612 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject.rs @@ -1,8 +1,6 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::call::reject; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, |message: String| { - ic_cdk::api::call::reject(&message); - }) - .unwrap() +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, |message: String| reject(&message)) } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject_code.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject_code.rs index 0ceb9b6d22..9395bee260 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject_code.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject_code.rs @@ -1,10 +1,11 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::call::reject_code; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, || { - let reject_code = ic_cdk::api::call::reject_code(); +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, || { + let reject_code = reject_code(); - let reject_code_number = match reject_code { + match reject_code { ic_cdk::api::call::RejectionCode::NoError => 0, ic_cdk::api::call::RejectionCode::SysFatal => 1, ic_cdk::api::call::RejectionCode::SysTransient => 2, @@ -12,9 +13,6 @@ pub fn get_function(context: Ctx) -> Function { ic_cdk::api::call::RejectionCode::CanisterReject => 4, ic_cdk::api::call::RejectionCode::CanisterError => 5, ic_cdk::api::call::RejectionCode::Unknown => 6, - }; - - reject_code_number + } }) - .unwrap() } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject_message.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject_message.rs index 5fa76ffa33..ae217c9180 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject_message.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reject_message.rs @@ -1,5 +1,6 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::call::reject_message; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, || ic_cdk::api::call::reject_message()).unwrap() +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, || reject_message()) } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reply_raw.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reply_raw.rs index 4ef5b4295d..c3a4f5cedd 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reply_raw.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/reply_raw.rs @@ -1,8 +1,6 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::call::reply_raw; +use rquickjs::{Ctx, Function, Result, TypedArray}; -pub fn get_function(context: Ctx) -> rquickjs::Function { - Function::new(context, |bytes: rquickjs::TypedArray| { - ic_cdk::api::call::reply_raw(bytes.as_ref()); - }) - .unwrap() +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, |bytes: TypedArray| reply_raw(bytes.as_ref())) } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_certified_data.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_certified_data.rs index 94a4fb1c67..2b6bb48639 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_certified_data.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_certified_data.rs @@ -1,8 +1,8 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::set_certified_data; +use rquickjs::{Ctx, Function, Result, TypedArray}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, |bytes: rquickjs::TypedArray| { - ic_cdk::api::set_certified_data(bytes.as_ref()); +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, |bytes: TypedArray| { + set_certified_data(bytes.as_ref()) }) - .unwrap() } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_timer.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_timer.rs index 8e432829b8..815eec1fa9 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_timer.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_timer.rs @@ -1,37 +1,47 @@ -use rquickjs::{Ctx, Function}; -use slotmap::Key; - -use crate::{error::quickjs_call_with_error_handling, quickjs_with_ctx}; - -pub fn get_function(ctx: Ctx) -> Function { - Function::new(ctx, |delay: String, callback_id: String| { - let delay: u64 = delay.parse().unwrap(); - let delay_duration = core::time::Duration::new(delay, 0); +use core::time::Duration; - let closure = move || { - quickjs_with_ctx(|ctx| { - let global = ctx.globals(); - let timer_callbacks = global - .get::<_, rquickjs::Object>("_azleTimerCallbacks") - .unwrap(); - let timer_callback: Function = timer_callbacks.get(callback_id.as_str()).unwrap(); - - let result = - quickjs_call_with_error_handling(ctx.clone(), timer_callback, ()).unwrap(); +use ic_cdk_timers::{set_timer, TimerId}; +use rquickjs::{Ctx, Function, Object, Result}; +use slotmap::Key; - if result.is_exception() { - panic!("Timer callback threw an exception"); +use crate::{error::quickjs_call_with_error_handling, ic::throw_error, quickjs_with_ctx}; + +pub fn get_function(ctx: Ctx) -> Result { + Function::new( + ctx.clone(), + move |delay: String, callback_id: String| -> Result { + let delay: u64 = delay.parse().map_err(|e| throw_error(ctx.clone(), e))?; + let delay_duration = Duration::new(delay, 0); + + let closure = move || { + let result = quickjs_with_ctx(|ctx| { + let globals = ctx.globals(); + + let timer_callbacks: Object = + globals.get("_azleTimerCallbacks").map_err(|e| { + format!("Failed to get globalThis._azleTimerCallbacks: {e}") + })?; + let timer_callback: Function = + timer_callbacks.get(callback_id.as_str()).map_err(|e| { + format!( + "Failed to get globalThis._azleTimerCallbacks[{callback_id}]: {e}" + ) + })?; + + quickjs_call_with_error_handling(ctx, timer_callback, ())?; + + Ok(()) + }); + + if let Err(e) = result { + ic_cdk::trap(&format!("Azle TimerError: {e}")); } + }; - Ok(()) - }) - .unwrap(); - }; - - let timer_id: ic_cdk_timers::TimerId = ic_cdk_timers::set_timer(delay_duration, closure); - let timer_id_u64: u64 = timer_id.data().as_ffi(); + let timer_id: TimerId = set_timer(delay_duration, closure); + let timer_id_u64: u64 = timer_id.data().as_ffi(); - timer_id_u64 - }) - .unwrap() + Ok(timer_id_u64) + }, + ) } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_timer_interval.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_timer_interval.rs index 47ac950cfa..e4ac19f4ca 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_timer_interval.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/set_timer_interval.rs @@ -1,38 +1,47 @@ -use rquickjs::{Ctx, Function}; -use slotmap::Key; - -use crate::{error::quickjs_call_with_error_handling, quickjs_with_ctx}; - -pub fn get_function(ctx: Ctx) -> Function { - Function::new(ctx, |interval: String, callback_id: String| { - let interval: u64 = interval.parse().unwrap(); - let interval_duration = core::time::Duration::new(interval, 0); +use core::time::Duration; - let closure = move || { - quickjs_with_ctx(|ctx| { - let global = ctx.globals(); - let timer_callbacks = global - .get::<_, rquickjs::Object>("_azleTimerCallbacks") - .unwrap(); - let timer_callback: Function = timer_callbacks.get(callback_id.as_str()).unwrap(); - - let result = - quickjs_call_with_error_handling(ctx.clone(), timer_callback, ()).unwrap(); +use ic_cdk_timers::{set_timer_interval, TimerId}; +use rquickjs::{Ctx, Function, Object, Result}; +use slotmap::Key; - if result.is_exception() { - panic!("Timer interval callback threw an exception"); +use crate::{error::quickjs_call_with_error_handling, ic::throw_error, quickjs_with_ctx}; + +pub fn get_function(ctx: Ctx) -> Result { + Function::new( + ctx.clone(), + move |interval: String, callback_id: String| -> Result { + let interval: u64 = interval.parse().map_err(|e| throw_error(ctx.clone(), e))?; + let interval_duration = Duration::new(interval, 0); + + let closure = move || { + let result = quickjs_with_ctx(|ctx| { + let globals = ctx.globals(); + + let timer_callbacks: Object = + globals.get("_azleTimerCallbacks").map_err(|e| { + format!("Failed to get globalThis._azleTimerCallbacks: {e}") + })?; + let timer_callback: Function = + timer_callbacks.get(callback_id.as_str()).map_err(|e| { + format!( + "Failed to get globalThis._azleTimerCallbacks[{callback_id}]: {e}" + ) + })?; + + quickjs_call_with_error_handling(ctx, timer_callback, ())?; + + Ok(()) + }); + + if let Err(e) = result { + ic_cdk::trap(&format!("Azle TimerIntervalError: {e}")); } + }; - Ok(()) - }) - .unwrap(); - }; - - let timer_id: ic_cdk_timers::TimerId = - ic_cdk_timers::set_timer_interval(interval_duration, closure); - let timer_id_u64: u64 = timer_id.data().as_ffi(); + let timer_id: TimerId = set_timer_interval(interval_duration, closure); + let timer_id_u64: u64 = timer_id.data().as_ffi(); - timer_id_u64 - }) - .unwrap() + Ok(timer_id_u64) + }, + ) } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/time.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/time.rs index 43a150f23c..ff960cb5fb 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/time.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/time.rs @@ -1,5 +1,6 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::time; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, || ic_cdk::api::time()).unwrap() +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, || time()) } diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/trap.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/trap.rs index 572aa5719f..a67c220e13 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/trap.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/ic/trap.rs @@ -1,8 +1,6 @@ -use rquickjs::{Ctx, Function}; +use ic_cdk::api::trap; +use rquickjs::{Ctx, Function, Result}; -pub fn get_function(context: Ctx) -> Function { - Function::new(context, |message: String| { - ic_cdk::api::trap(&message); - }) - .unwrap() +pub fn get_function(ctx: Ctx) -> Result { + Function::new(ctx, |message: String| trap(&message)) }