diff --git a/canister_templates/experimental.wasm b/canister_templates/experimental.wasm index f9b832b541..463b241234 100644 Binary files a/canister_templates/experimental.wasm and b/canister_templates/experimental.wasm differ diff --git a/canister_templates/stable.wasm b/canister_templates/stable.wasm index 9c9645faef..c2b35ff0ea 100644 Binary files a/canister_templates/stable.wasm and b/canister_templates/stable.wasm differ diff --git a/src/build/experimental/commands/compile/wasm_binary/compile.ts b/src/build/experimental/commands/compile/wasm_binary/compile.ts index f4b66ff208..8daf027410 100644 --- a/src/build/experimental/commands/compile/wasm_binary/compile.ts +++ b/src/build/experimental/commands/compile/wasm_binary/compile.ts @@ -8,7 +8,7 @@ export function compile( ioType: IOType ): void { execSyncPretty( - `CARGO_TARGET_DIR=target cargo build --target wasm32-wasi --manifest-path ${manifestPath} --release --features "experimental"`, + `CARGO_TARGET_DIR=target cargo build --target wasm32-wasi --manifest-path ${manifestPath} --release`, ioType ); diff --git a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/Cargo.toml b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/Cargo.toml index d4d0ae8117..d7c4c0676b 100644 --- a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/Cargo.toml +++ b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/Cargo.toml @@ -6,9 +6,6 @@ edition = "2018" [lib] crate-type = ["cdylib"] -[features] -experimental = ["open_value_sharing", "wasmi"] - [dependencies] anyhow = "1.0.75" ic-cdk = "0.12.2" @@ -17,6 +14,7 @@ ic-cdk-timers = "0.6.0" candid = "0.10.2" candid_parser = "0.1.2" ic-stable-structures = "0.6.5" +open_value_sharing = { path = "../open_value_sharing" } slotmap = "=1.0.6" sha2 = "0.10.8" serde = "1.0.202" @@ -24,11 +22,4 @@ serde_json = "1.0.107" ic-wasi-polyfill = "0.6.1" wasmedge_quickjs = { git = "https://github.com/demergent-labs/wasmedge-quickjs", rev = "573c6c07316de64e4bb9a9561b079f265fd9bcc4" } # wasmedge_quickjs = { path = "/home/wasmedge-quickjs" } - -[dependencies.open_value_sharing] -optional = true -path = "../open_value_sharing" - -[dependencies.wasmi] -optional = true -version = "0.31.2" +wasmi = "0.31.2" diff --git a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/autoreload.rs b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/autoreload.rs index 507a115f32..945245de6f 100644 --- a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/autoreload.rs +++ b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/autoreload.rs @@ -1,6 +1,6 @@ use std::{cell::RefCell, collections::BTreeMap}; -use crate::init_and_post_upgrade::initialize_js; +use crate::{init_and_post_upgrade::initialize_js, WASM_DATA_REF_CELL}; thread_local! { static RELOADED_JS_TIMESTAMP: RefCell = RefCell::new(0); @@ -35,8 +35,11 @@ pub fn reload_js( reloaded_js_mut.values().flat_map(|v| v.clone()).collect(); if reloaded_js_complete_bytes.len() as u64 == total_len { + let wasm_data = WASM_DATA_REF_CELL + .with(|wasm_data_ref_cell| wasm_data_ref_cell.borrow().as_ref().unwrap().clone()); + let js_string = String::from_utf8_lossy(&reloaded_js_complete_bytes); - initialize_js(&js_string, false, function_index, 1); // TODO should the last arg be 0? + initialize_js(&wasm_data, &js_string, false, function_index, 1); // TODO should the last arg be 0? ic_cdk::println!("Azle: Reloaded canister JavaScript"); } }); diff --git a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/candid.rs b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/candid.rs index ba21eed769..604c1a1633 100644 --- a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/candid.rs +++ b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/candid.rs @@ -1,6 +1,10 @@ use wasmedge_quickjs::AsObject; -use crate::{ic, run_event_loop, wasm_binary_manipulation::get_js_code, EXPERIMENTAL, RUNTIME}; +use crate::{ + ic, run_event_loop, + wasm_binary_manipulation::{get_js_code, get_wasm_data}, + RUNTIME, +}; // Heavily inspired by https://stackoverflow.com/a/47676844 #[no_mangle] @@ -32,12 +36,16 @@ pub fn get_candid_and_method_meta_pointer() -> *mut std::os::raw::c_char { ic::register(context); + let wasm_data = get_wasm_data(); let js = get_js_code(); // TODO what do we do if there is an error in here? context.eval_global_str("globalThis.exports = {};".to_string()); - context.eval_global_str(format!("globalThis._azleExperimental = {EXPERIMENTAL};")); - context.eval_module_str(std::str::from_utf8(&js).unwrap().to_string(), "main"); + context.eval_global_str(format!("globalThis._azleExperimental = true;")); + context.eval_module_str( + std::str::from_utf8(&js).unwrap().to_string(), + &wasm_data.main_js_path, + ); run_event_loop(context); diff --git a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/init_and_post_upgrade.rs b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/init_and_post_upgrade.rs index aec3c5526a..1e6658f46c 100644 --- a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/init_and_post_upgrade.rs +++ b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/init_and_post_upgrade.rs @@ -2,12 +2,12 @@ use ic_stable_structures::memory_manager::MemoryId; use wasmedge_quickjs::AsObject; use crate::{ - execute_method_js, ic, run_event_loop, wasm_binary_manipulation::get_js_code, - wasm_binary_manipulation::get_wasm_data, EXPERIMENTAL, MEMORY_MANAGER_REF_CELL, RUNTIME, - WASM_DATA_REF_CELL, + execute_method_js, ic, run_event_loop, + wasm_binary_manipulation::get_wasm_data, + wasm_binary_manipulation::{get_js_code, WasmData}, + MEMORY_MANAGER_REF_CELL, RUNTIME, WASM_DATA_REF_CELL, }; -#[cfg(feature = "experimental")] use crate::{upload_file, web_assembly}; #[inline(never)] @@ -20,7 +20,6 @@ pub extern "C" fn init(function_index: i32, pass_arg_data: i32) { initialize(true, function_index, pass_arg_data); - #[cfg(feature = "experimental")] upload_file::init_hashes().unwrap(); } @@ -69,31 +68,35 @@ fn initialize(init: bool, function_index: i32, pass_arg_data: i32) { MEMORY_MANAGER_REF_CELL.with(|manager| manager.borrow().get(MemoryId::new(254))); ic_wasi_polyfill::init_with_memory(&[], &env_vars, polyfill_memory); - #[cfg(feature = "experimental")] std::fs::write("/candid/icp/management.did", &wasm_data.management_did).unwrap(); let js = get_js_code(); initialize_js( + &wasm_data, std::str::from_utf8(&js).unwrap(), init, function_index, pass_arg_data, ); - #[cfg(feature = "experimental")] ic_cdk::spawn(async move { open_value_sharing::init(&wasm_data.consumer).await; }); } -pub fn initialize_js(js: &str, init: bool, function_index: i32, pass_arg_data: i32) { +pub fn initialize_js( + wasm_data: &WasmData, + js: &str, + init: bool, + function_index: i32, + pass_arg_data: i32, +) { let mut rt = wasmedge_quickjs::Runtime::new(); rt.run_with_context(|context| { ic::register(context); - #[cfg(feature = "experimental")] web_assembly::register(context); let mut env = context.new_object(); @@ -115,18 +118,12 @@ pub fn initialize_js(js: &str, init: bool, function_index: i32, pass_arg_data: i // TODO what do we do if there is an error in here? context.eval_global_str("globalThis.exports = {};".to_string()); - context.eval_global_str(format!("globalThis._azleExperimental = {EXPERIMENTAL};")); - let record_benchmarks = WASM_DATA_REF_CELL.with(|wasm_data_ref_cell| { - wasm_data_ref_cell - .borrow() - .as_ref() - .unwrap() - .record_benchmarks - }); + context.eval_global_str(format!("globalThis._azleExperimental = true;")); context.eval_global_str(format!( - "globalThis._azleRecordBenchmarks = {record_benchmarks};" + "globalThis._azleRecordBenchmarks = {};", + wasm_data.record_benchmarks )); - context.eval_module_str(js.to_string(), "main"); + context.eval_module_str(js.to_string(), &wasm_data.main_js_path); run_event_loop(context); @@ -160,9 +157,9 @@ pub fn initialize_js(js: &str, init: bool, function_index: i32, pass_arg_data: i runtime.run_with_context(|context| { let assignment = if init { - "globalThis._azleInitCalled = true;" + "globalThis._azleInitCalled = true;\nglobalThis._azlePostUpgradeCalled = false;" } else { - "globalThis._azlePostUpgradeCalled = true;" + "globalThis._azleInitCalled = false;\nglobalThis._azlePostUpgradeCalled = true;" }; context.eval_global_str(assignment.to_string()); diff --git a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/lib.rs b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/lib.rs index 61b8204f18..863b859805 100644 --- a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/lib.rs +++ b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/lib.rs @@ -12,7 +12,6 @@ use ic_stable_structures::{ DefaultMemoryImpl, }; -#[cfg(feature = "experimental")] mod autoreload; mod benchmarking; mod candid; @@ -22,10 +21,8 @@ mod guards; mod ic; mod init_and_post_upgrade; mod stable_b_tree_map; -#[cfg(feature = "experimental")] mod upload_file; mod wasm_binary_manipulation; -#[cfg(feature = "experimental")] mod web_assembly; #[allow(unused)] @@ -37,8 +34,6 @@ thread_local! { static WASM_DATA_REF_CELL: RefCell> = RefCell::new(None); } -const EXPERIMENTAL: bool = cfg!(feature = "experimental"); - pub fn run_event_loop(context: &mut wasmedge_quickjs::Context) { context.promise_loop_poll(); @@ -56,7 +51,6 @@ pub fn run_event_loop(context: &mut wasmedge_quickjs::Context) { #[ic_cdk_macros::update] pub fn _azle_chunk() {} -#[cfg(feature = "experimental")] #[ic_cdk_macros::update(guard = guard_against_non_controllers)] fn _azle_reload_js( timestamp: u64, @@ -68,7 +62,6 @@ fn _azle_reload_js( autoreload::reload_js(timestamp, chunk_number, js_bytes, total_len, function_index); } -#[cfg(feature = "experimental")] #[ic_cdk_macros::update(guard = guard_against_non_controllers)] pub async fn _azle_upload_file_chunk( dest_path: String, @@ -87,13 +80,11 @@ pub async fn _azle_upload_file_chunk( .await } -#[cfg(feature = "experimental")] #[ic_cdk_macros::update(guard = guard_against_non_controllers)] pub fn _azle_clear_file_and_info(path: String) { upload_file::reset_for_new_upload(&path, 0).unwrap() } -#[cfg(feature = "experimental")] #[ic_cdk_macros::query(guard = guard_against_non_controllers)] pub fn _azle_get_file_hash(path: String) -> Option { upload_file::get_file_hash(path) diff --git a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/wasm_binary_manipulation.rs b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/wasm_binary_manipulation.rs index 0ccea8881f..07ef24a466 100644 --- a/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/wasm_binary_manipulation.rs +++ b/src/build/experimental/commands/compile/wasm_binary/rust/experimental_canister_template/src/wasm_binary_manipulation.rs @@ -1,19 +1,18 @@ -#[cfg(feature = "experimental")] use open_value_sharing::Consumer; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct WasmData { + pub consumer: Consumer, #[serde(rename = "envVars")] pub env_vars: Vec<(String, String)>, - #[serde(rename = "recordBenchmarks")] - pub record_benchmarks: bool, - #[cfg(feature = "experimental")] - pub consumer: Consumer, - #[cfg(feature = "experimental")] + #[serde(rename = "mainJsPath")] + pub main_js_path: String, #[serde(rename = "managementDid")] pub management_did: String, + #[serde(rename = "recordBenchmarks")] + pub record_benchmarks: bool, } #[inline(never)] diff --git a/src/build/stable/commands/compile/get_context.ts b/src/build/stable/commands/compile/get_context.ts index 14b463e41d..c2785f9a6d 100644 --- a/src/build/stable/commands/compile/get_context.ts +++ b/src/build/stable/commands/compile/get_context.ts @@ -27,6 +27,7 @@ export function getContext( const envVars = getEnvVars(canisterConfig); const wasmData: WasmData = { envVars, + mainJsPath: join(canisterPath, `main.js`), recordBenchmarks: process.env.npm_lifecycle_event === 'pre_tests' || process.env.npm_lifecycle_event === 'pretest' || diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/candid.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/candid.rs index 868ddd26a9..b6e4a51e8e 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/candid.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/candid.rs @@ -7,8 +7,8 @@ use crate::{ error::{handle_promise_error, quickjs_call_with_error_handling}, ic::register, quickjs_with_ctx, - wasm_binary_manipulation::get_js_code, - CONTEXT_REF_CELL, MODULE_NAME, + wasm_binary_manipulation::{get_js_code, get_wasm_data}, + CONTEXT_REF_CELL, }; type CCharPtr = *mut c_char; @@ -42,9 +42,10 @@ fn initialize_and_get_candid() -> Result> { register(ctx.clone())?; + let wasm_data = get_wasm_data()?; let js = get_js_code(); - let promise = Module::evaluate(ctx.clone(), MODULE_NAME, str::from_utf8(&js)?)?; + let promise = Module::evaluate(ctx.clone(), wasm_data.main_js_path, str::from_utf8(&js)?)?; handle_promise_error(ctx.clone(), promise)?; diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/init_and_post_upgrade.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/init_and_post_upgrade.rs index 2f7695502f..9ece16971b 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/init_and_post_upgrade.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/init_and_post_upgrade.rs @@ -10,8 +10,8 @@ use crate::{ execute_method_js::execute_method_js, ic::register, quickjs_with_ctx, - wasm_binary_manipulation::{get_js_code, get_wasm_data}, - CONTEXT_REF_CELL, MEMORY_MANAGER_REF_CELL, MODULE_NAME, WASM_DATA_REF_CELL, + wasm_binary_manipulation::{get_js_code, get_wasm_data, WasmData}, + CONTEXT_REF_CELL, MEMORY_MANAGER_REF_CELL, WASM_DATA_REF_CELL, }; #[inline(never)] @@ -55,12 +55,19 @@ fn initialize(init: bool, function_index: i32, pass_arg_data: i32) -> Result<(), let js = get_js_code(); - initialize_js(str::from_utf8(&js)?, init, function_index, pass_arg_data)?; + initialize_js( + &wasm_data, + str::from_utf8(&js)?, + init, + function_index, + pass_arg_data, + )?; Ok(()) } pub fn initialize_js( + wasm_data: &WasmData, js: &str, init: bool, function_index: i32, @@ -112,7 +119,7 @@ pub fn initialize_js( register(ctx.clone())?; - let promise = Module::evaluate(ctx.clone(), MODULE_NAME, js)?; + let promise = Module::evaluate(ctx.clone(), wasm_data.main_js_path.clone(), js)?; handle_promise_error(ctx.clone(), promise)?; diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/lib.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/lib.rs index 27f98508ad..ad324a5454 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/lib.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/lib.rs @@ -22,10 +22,6 @@ mod quickjs_with_ctx; mod stable_b_tree_map; mod wasm_binary_manipulation; -// TODO dynamically get the canister name -// TODO send it in through the Wasm meta data -const MODULE_NAME: &str = ".azle/[canister_name]/main.js"; - #[allow(unused)] type Memory = VirtualMemory; diff --git a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/wasm_binary_manipulation.rs b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/wasm_binary_manipulation.rs index c844943705..a90d2ee941 100644 --- a/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/wasm_binary_manipulation.rs +++ b/src/build/stable/commands/compile/wasm_binary/rust/stable_canister_template/src/wasm_binary_manipulation.rs @@ -7,6 +7,8 @@ use serde_json; pub struct WasmData { #[serde(rename = "envVars")] pub env_vars: Vec<(String, String)>, + #[serde(rename = "mainJsPath")] + pub main_js_path: String, #[serde(rename = "recordBenchmarks")] pub record_benchmarks: bool, } diff --git a/src/build/stable/utils/types.ts b/src/build/stable/utils/types.ts index 74daf87a13..f2a203b09f 100644 --- a/src/build/stable/utils/types.ts +++ b/src/build/stable/utils/types.ts @@ -65,5 +65,6 @@ export type MethodMeta = { export type WasmData = { envVars: EnvVars; + mainJsPath: string; recordBenchmarks: boolean; }; diff --git a/src/lib/experimental/canister_methods/execute_method.ts b/src/lib/experimental/canister_methods/execute_method.ts index ca0b28ad2d..73bd2796ac 100644 --- a/src/lib/experimental/canister_methods/execute_method.ts +++ b/src/lib/experimental/canister_methods/execute_method.ts @@ -1,9 +1,9 @@ import '../experimental'; +import { handleUncaughtError } from '../../stable/error'; import { CandidType } from '../candid/candid_type'; import { decode } from '../candid/serde/decode'; import { encode } from '../candid/serde/encode'; -import { handleUncaughtError } from '../error'; import { ic } from '../ic'; import { CanisterMethodInfo } from './types/canister_method_info'; diff --git a/src/lib/experimental/error.ts b/src/lib/experimental/error.ts deleted file mode 100644 index 3030d03540..0000000000 --- a/src/lib/experimental/error.ts +++ /dev/null @@ -1,10 +0,0 @@ -// import { trap } from './ic_apis/trap'; // TODO why does this break bitcoin_psbt examples? https://github.com/demergent-labs/azle/issues/2003 -import { trap } from './ic/trap'; - -export function handleUncaughtError(rawError: any): never { - const error = rawError instanceof Error ? rawError : new Error(rawError); - - const azleError = `Uncaught ${error.name}: ${error.message}${error.stack}`; - - trap(azleError); -} diff --git a/src/lib/stable/canister_methods/heartbeat.ts b/src/lib/stable/canister_methods/heartbeat.ts index 80c40fe044..71da83c97a 100644 --- a/src/lib/stable/canister_methods/heartbeat.ts +++ b/src/lib/stable/canister_methods/heartbeat.ts @@ -1,5 +1,5 @@ +import { handleUncaughtError } from '../error'; import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde'; -import { trap } from '../ic_apis'; export function heartbeat( originalMethod: (this: This, ...args: Args) => Return, @@ -25,7 +25,7 @@ export function heartbeat( false ); } catch (error: any) { - trap(`Uncaught Error: ${error.toString()}`); + handleUncaughtError(error); } }; } diff --git a/src/lib/stable/canister_methods/init.ts b/src/lib/stable/canister_methods/init.ts index 3a3936d64b..16cc6c7efa 100644 --- a/src/lib/stable/canister_methods/init.ts +++ b/src/lib/stable/canister_methods/init.ts @@ -1,7 +1,7 @@ import { IDL } from '@dfinity/candid'; +import { handleUncaughtError } from '../error'; import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde'; -import { trap } from '../ic_apis'; export function init( paramIdlTypes: IDL.Type[] @@ -36,7 +36,7 @@ export function init( false ); } catch (error: any) { - trap(`Uncaught Error: ${error.toString()}`); + handleUncaughtError(error); } }; }; diff --git a/src/lib/stable/canister_methods/inspect_message.ts b/src/lib/stable/canister_methods/inspect_message.ts index 0366cf08b5..2de4912147 100644 --- a/src/lib/stable/canister_methods/inspect_message.ts +++ b/src/lib/stable/canister_methods/inspect_message.ts @@ -1,5 +1,5 @@ +import { handleUncaughtError } from '../error'; import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde'; -import { trap } from '../ic_apis'; // TODO explain here in a jsdoc that the dev can get the raw args using argDataRaw export function inspectMessage( @@ -26,7 +26,7 @@ export function inspectMessage( false ); } catch (error: any) { - trap(`Uncaught Error: ${error.toString()}`); + handleUncaughtError(error); } }; } diff --git a/src/lib/stable/canister_methods/post_upgrade.ts b/src/lib/stable/canister_methods/post_upgrade.ts index 07074acf23..c4f742ec0b 100644 --- a/src/lib/stable/canister_methods/post_upgrade.ts +++ b/src/lib/stable/canister_methods/post_upgrade.ts @@ -1,7 +1,7 @@ import { IDL } from '@dfinity/candid'; +import { handleUncaughtError } from '../error'; import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde'; -import { trap } from '../ic_apis'; export function postUpgrade( paramIdlTypes: IDL.Type[] @@ -36,7 +36,7 @@ export function postUpgrade( false ); } catch (error: any) { - trap(`Uncaught Error: ${error.toString()}`); + handleUncaughtError(error); } }; }; diff --git a/src/lib/stable/canister_methods/pre_upgrade.ts b/src/lib/stable/canister_methods/pre_upgrade.ts index 3b28ba5857..ff3e576588 100644 --- a/src/lib/stable/canister_methods/pre_upgrade.ts +++ b/src/lib/stable/canister_methods/pre_upgrade.ts @@ -1,5 +1,5 @@ +import { handleUncaughtError } from '../error'; import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde'; -import { trap } from '../ic_apis'; export function preUpgrade( originalMethod: (this: This, ...args: Args) => Return, @@ -25,7 +25,7 @@ export function preUpgrade( false ); } catch (error: any) { - trap(`Uncaught Error: ${error.toString()}`); + handleUncaughtError(error); } }; } diff --git a/src/lib/stable/canister_methods/query.ts b/src/lib/stable/canister_methods/query.ts index 4de3dbbd89..bad6ef16d3 100644 --- a/src/lib/stable/canister_methods/query.ts +++ b/src/lib/stable/canister_methods/query.ts @@ -1,7 +1,7 @@ import { IDL } from '@dfinity/candid'; +import { handleUncaughtError } from '../error'; import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde'; -import { trap } from '../ic_apis'; export function query( paramIdlTypes: IDL.Type[], @@ -45,7 +45,7 @@ export function query( options?.manual ?? false ); } catch (error: any) { - trap(`Uncaught Error: ${error.toString()}`); + handleUncaughtError(error); } }; }; diff --git a/src/lib/stable/canister_methods/update.ts b/src/lib/stable/canister_methods/update.ts index 9fdcdfa37e..13001656ef 100644 --- a/src/lib/stable/canister_methods/update.ts +++ b/src/lib/stable/canister_methods/update.ts @@ -1,7 +1,7 @@ import { IDL } from '@dfinity/candid'; +import { handleUncaughtError } from '../error'; import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde'; -import { trap } from '../ic_apis'; export function update( paramIdlTypes: IDL.Type[], @@ -37,7 +37,7 @@ export function update( options?.manual ?? false ); } catch (error: any) { - trap(`Uncaught Error: ${error.toString()}`); + handleUncaughtError(error); } }; }; diff --git a/src/lib/stable/error.ts b/src/lib/stable/error.ts new file mode 100644 index 0000000000..94688c431d --- /dev/null +++ b/src/lib/stable/error.ts @@ -0,0 +1,11 @@ +import { trap } from './ic_apis/trap'; + +export function handleUncaughtError(rawError: any): never { + if (rawError instanceof Error) { + const error = rawError; + trap(`Uncaught ${error.name}: ${error.message}\n${error.stack}`); + } else { + const error = new Error(rawError); + trap(`Uncaught: ${error.message}\n${error.stack}`); + } +} diff --git a/src/lib/stable/globals.ts b/src/lib/stable/globals.ts index 23ba0ba67c..4549be163b 100644 --- a/src/lib/stable/globals.ts +++ b/src/lib/stable/globals.ts @@ -104,9 +104,6 @@ if (globalThis._azleInsideCanister === true) { }) as any }; - globalThis._azleInitCalled = false; - globalThis._azlePostUpgradeCalled = false; - const log = (...args: any[]): void => { const jsonStringifiedArgs = args .map((arg) => { diff --git a/tests/end_to_end/candid_rpc/functional_syntax/cross_canister_calls/test/tests.ts b/tests/end_to_end/candid_rpc/functional_syntax/cross_canister_calls/test/tests.ts index 90364cfdb6..61aeaaf8ba 100644 --- a/tests/end_to_end/candid_rpc/functional_syntax/cross_canister_calls/test/tests.ts +++ b/tests/end_to_end/candid_rpc/functional_syntax/cross_canister_calls/test/tests.ts @@ -103,7 +103,7 @@ export function getTests( const canister1Id = getCanisterId('canister1'); const canister2Id = getCanisterId('canister2'); const partialErrorMessage = new RegExp( - `Error from Canister ${canister1Id}: Canister called \`ic0.trap\` with message: Uncaught Error: Error: Rejection code 5, IC0503: Error from Canister ${canister2Id}: Canister called \`ic0.trap\` with message: hahahaha` + `Error from Canister ${canister1Id}: Canister called \`ic0.trap\` with message: Uncaught Error: Rejection code 5, IC0503: Error from Canister ${canister2Id}: Canister called \`ic0.trap\` with message: hahahaha` ); await expect(canister1.trap()).rejects.toThrow(partialErrorMessage); diff --git a/tests/end_to_end/candid_rpc/functional_syntax/primitive_types/test/tests.ts b/tests/end_to_end/candid_rpc/functional_syntax/primitive_types/test/tests.ts index ef70d89d00..b6833fe411 100644 --- a/tests/end_to_end/candid_rpc/functional_syntax/primitive_types/test/tests.ts +++ b/tests/end_to_end/candid_rpc/functional_syntax/primitive_types/test/tests.ts @@ -319,7 +319,7 @@ export function getTests( const rejectionMessage = 'Anything you want'; const canisterId = '[a-z2-7-]{27}'; const expectedErrorMessage = new RegExp( - `Call failed:\\s*Canister: ${canisterId}\\s*Method: getEmpty \\(query\\)\\s*"Status": "rejected"\\s*"Code": "CanisterError"\\s*"Message": "IC0503: Error from Canister ${canisterId}: Canister called \`ic0.trap\` with message: Uncaught Error: ${rejectionMessage}.*"` + `Call failed:\\s*Canister: ${canisterId}\\s*Method: getEmpty \\(query\\)\\s*"Status": "rejected"\\s*"Code": "CanisterError"\\s*"Message": "IC0503: Error from Canister ${canisterId}: Canister called \`ic0.trap\` with message: Uncaught: ${rejectionMessage}.*"` ); await expect(primitiveTypesCanister.getEmpty()).rejects.toThrow( expectedErrorMessage