diff --git a/src/lib/globals.ts b/src/lib/globals.ts index 09867011ad..1617e016a1 100644 --- a/src/lib/globals.ts +++ b/src/lib/globals.ts @@ -3,7 +3,7 @@ import { AzleIc } from './ic/types/azle_ic'; import { Buffer } from 'buffer'; declare global { - var _azleIc: AzleIc; + var _azleIc: AzleIc | undefined; var _azleResolveIds: { [key: string]: (buf: ArrayBuffer) => void }; var _azleRejectIds: { [key: string]: (err: any) => void }; var _azleIcTimers: { [key: string]: string }; @@ -13,7 +13,7 @@ declare global { globalThis.TextDecoder = require('text-encoding').TextDecoder; globalThis.TextEncoder = require('text-encoding').TextEncoder; -globalThis._azleIcTimers ||= {}; +globalThis._azleIcTimers = {}; globalThis._azleResolveIds = {}; globalThis._azleRejectIds = {}; globalThis._azleTimerCallbackIds = {}; diff --git a/src/lib/ic/call_raw_128.ts b/src/lib/ic/call_raw_128.ts index b1c6ea90e3..5c4d4bc778 100644 --- a/src/lib/ic/call_raw_128.ts +++ b/src/lib/ic/call_raw_128.ts @@ -28,6 +28,10 @@ export function callRaw128( // TODO this should use a Result remember return new Promise((resolve, reject) => { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const promiseId = v4(); const globalResolveId = `_resolve_${promiseId}`; const globalRejectId = `_reject_${promiseId}`; diff --git a/src/lib/stable_b_tree_map.ts b/src/lib/stable_b_tree_map.ts index b6f05c8540..bd41ff57b1 100644 --- a/src/lib/stable_b_tree_map.ts +++ b/src/lib/stable_b_tree_map.ts @@ -22,6 +22,10 @@ export function StableBTreeMap< * @returns `true` if the key exists in the map, `false` otherwise. */ containsKey(key: TypeMapping): boolean { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; const candidEncodedKey = encode(keyType, key).buffer; @@ -36,6 +40,10 @@ export function StableBTreeMap< * @returns the value associated with the given key, if it exists. */ get(key: TypeMapping): Opt> { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; const candidEncodedKey = encode(keyType, key).buffer; @@ -60,6 +68,10 @@ export function StableBTreeMap< key: TypeMapping, value: TypeMapping ): Opt> { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; const candidEncodedKey = encode(keyType, key).buffer; const candidEncodedValue = encode(valueType, value).buffer; @@ -82,6 +94,10 @@ export function StableBTreeMap< * @returns `true` if the map contains no elements, `false` otherwise. */ isEmpty(): boolean { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; return globalThis._azleIc.stableBTreeMapIsEmpty( @@ -93,6 +109,10 @@ export function StableBTreeMap< * @returns tuples representing key/value pairs. */ items(): [TypeMapping, TypeMapping][] { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; const candidEncodedItems = globalThis._azleIc.stableBTreeMapItems( @@ -112,6 +132,10 @@ export function StableBTreeMap< * @returns they keys in the map. */ keys(): TypeMapping[] { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; const candidEncodedKeys = globalThis._azleIc.stableBTreeMapKeys( @@ -128,6 +152,10 @@ export function StableBTreeMap< * @returns the number of elements in the map. */ len(): nat64 { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; const candidEncodedLen = globalThis._azleIc.stableBTreeMapLen( @@ -142,6 +170,10 @@ export function StableBTreeMap< * @returns the previous value at the key if it exists, `null` otherwise. */ remove(key: TypeMapping): Opt> { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; const candidEncodedKey = encode(keyType, key).buffer; @@ -161,6 +193,10 @@ export function StableBTreeMap< * @returns the values in the map. */ values(): TypeMapping[] { + if (globalThis._azleIc === undefined) { + return undefined as any; + } + const candidEncodedMemoryId = encode(nat8, memoryId).buffer; const candidEncodedValues = globalThis._azleIc.stableBTreeMapValues(