From f3da2dd76a19f2e3fb6ece1084faee1d158a68a2 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Tue, 3 Oct 2023 15:39:49 -0600 Subject: [PATCH] fixup --- src/compiler/rust/canister/src/ic/call_raw.rs | 2 + .../rust/canister/src/ic/call_raw128.rs | 2 + .../rust/canister/src/ic/set_timer.rs | 1 + .../canister/src/ic/set_timer_interval.rs | 1 + src/lib/globals.ts | 47 ++++++++++++------- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/compiler/rust/canister/src/ic/call_raw.rs b/src/compiler/rust/canister/src/ic/call_raw.rs index 125dc37941..2d5f9b4d71 100644 --- a/src/compiler/rust/canister/src/ic/call_raw.rs +++ b/src/compiler/rust/canister/src/ic/call_raw.rs @@ -69,11 +69,13 @@ pub fn native_function<'a>( if should_resolve { let resolve = global + .get_property("_azleResolveIds").unwrap() .get_property(format!("_resolve_{promise_id}").as_str()) .unwrap(); resolve.call(&resolve, &[js_value_ref]).unwrap(); } else { let reject = global + .get_property("_azleRejectIds").unwrap() .get_property(format!("_reject_{promise_id}").as_str()) .unwrap(); reject.call(&reject, &[js_value_ref]).unwrap(); diff --git a/src/compiler/rust/canister/src/ic/call_raw128.rs b/src/compiler/rust/canister/src/ic/call_raw128.rs index adaf3b7e7b..b7da057038 100644 --- a/src/compiler/rust/canister/src/ic/call_raw128.rs +++ b/src/compiler/rust/canister/src/ic/call_raw128.rs @@ -69,11 +69,13 @@ pub fn native_function<'a>( if should_resolve { let resolve = global + .get_property("_azleResolveIds").unwrap() .get_property(format!("_resolve_{promise_id}").as_str()) .unwrap(); resolve.call(&resolve, &[js_value_ref]).unwrap(); } else { let reject = global + .get_property("_azleRejectIds").unwrap() .get_property(format!("_reject_{promise_id}").as_str()) .unwrap(); reject.call(&reject, &[js_value_ref]).unwrap(); diff --git a/src/compiler/rust/canister/src/ic/set_timer.rs b/src/compiler/rust/canister/src/ic/set_timer.rs index 8c42e3f1bb..94858b6101 100644 --- a/src/compiler/rust/canister/src/ic/set_timer.rs +++ b/src/compiler/rust/canister/src/ic/set_timer.rs @@ -34,6 +34,7 @@ pub fn native_function<'a>( let global = context.global_object().unwrap(); let timer_callback = global + .get_property("_azleTimerCallbackIds").unwrap() .get_property(callback_id.as_str()) .unwrap_or_else(|e| ic_cdk::api::trap(e.to_string().as_str())); diff --git a/src/compiler/rust/canister/src/ic/set_timer_interval.rs b/src/compiler/rust/canister/src/ic/set_timer_interval.rs index 856000629f..78b8f8e399 100644 --- a/src/compiler/rust/canister/src/ic/set_timer_interval.rs +++ b/src/compiler/rust/canister/src/ic/set_timer_interval.rs @@ -34,6 +34,7 @@ pub fn native_function<'a>( let global = context.global_object().unwrap(); let timer_callback = global + .get_property("_azleTimerCallbackIds").unwrap() .get_property(callback_id.as_str()) .unwrap_or_else(|e| ic_cdk::api::trap(e.to_string().as_str())); diff --git a/src/lib/globals.ts b/src/lib/globals.ts index c94b92d5bd..eb62194a47 100644 --- a/src/lib/globals.ts +++ b/src/lib/globals.ts @@ -8,29 +8,41 @@ declare global { var _azleRejectIds: { [key: string]: (err: any) => void }; var icTimers: { [key: string]: string }; var _azleTimerCallbackIds: { [key: string]: () => void }; -} - -interface global {} - -export declare var globalThis: { - _azleCandidInitParams: any[]; - _azleCandidMethods: any[]; - _azleCandidTypes: any[]; - Buffer: BufferConstructor; - console: any; - crypto: { - getRandomValues: () => Uint8Array; - }; - icTimers: { + var _azleCandidInitParams: any[]; + var _azleCandidMethods: any[]; + var _azleCandidTypes: any[]; + var Buffer: BufferConstructor; + // var console: Console; + // var crypto: Crypto; + var icTimers: { [key: string]: string; }; - TextDecoder: any; - TextEncoder: any; -}; + // var TextDecoder: any; + // var TextEncoder: any; +} + +// export declare var globalThis: { +// _azleCandidInitParams: any[]; +// _azleCandidMethods: any[]; +// _azleCandidTypes: any[]; +// Buffer: BufferConstructor; +// console: any; +// crypto: { +// getRandomValues: () => Uint8Array; +// }; +// icTimers: { +// [key: string]: string; +// }; +// TextDecoder: any; +// TextEncoder: any; +// }; globalThis.TextDecoder = require('text-encoding').TextDecoder; globalThis.TextEncoder = require('text-encoding').TextEncoder; globalThis.icTimers ||= {}; +globalThis._azleResolveIds = {}; +globalThis._azleRejectIds = {}; +globalThis._azleTimerCallbackIds = {}; globalThis.console = { ...globalThis.console, @@ -42,6 +54,7 @@ globalThis.console = { // TODO be careful we are using a random seed of 0 I think // TODO the randomness is predictable globalThis.crypto = { + ...globalThis.crypto, getRandomValues: () => { let array = new Uint8Array(32);