Skip to content

Commit

Permalink
clean up globals
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 17, 2023
1 parent 91ff2f6 commit 142ed96
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 67 deletions.
10 changes: 5 additions & 5 deletions examples/guard_functions/src/index.did
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
service: () -> {
callExpressionWithEmptyOptionsObject: () -> (bool) query;
callExpressionWithoutOptionsObject: () -> (bool) query;
customErrorGuarded: () -> (bool) query;
errorStringGuarded: () -> (bool) query;
getCounter: () -> (nat32) query;
identifierAnnotation: () -> (bool) query;
callExpressionWithoutOptionsObject: () -> (bool) query;
callExpressionWithEmptyOptionsObject: () -> (bool) query;
looselyGuarded: () -> (bool) query;
looselyGuardedManual: () -> (bool) query;
looselyGuardedWithGuardOptionKeyAsString: () -> (bool) query;
modifyStateGuarded: () -> (bool);
tightlyGuarded: () -> (bool) query;
errorStringGuarded: () -> (bool) query;
customErrorGuarded: () -> (bool) query;
nonStringErrValueGuarded: () -> (bool) query;
tightlyGuarded: () -> (bool) query;
}
3 changes: 2 additions & 1 deletion src/compiler/rust/canister_methods/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ fn get_guard_token_stream(
let context = context.as_mut().unwrap();

let global = context.global_object().unwrap();
let guard_function = global.get_property(#guard_name).unwrap();
let guard_functions = global.get_property("_azleGuardFunctions").unwrap();
let guard_function = guard_functions.get_property(#guard_name).unwrap();

// TODO I am not sure what the first parameter to call is supposed to be
let result = guard_function.call(&guard_function, &[]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function createGlobalGuard(

const guardName = `_azleGuard_${guardedMethodName}`;

(globalThis as any)[guardName] = guard;
globalThis._azleGuardFunctions[guardName] = guard;

return guardName;
}
24 changes: 4 additions & 20 deletions src/lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,18 @@ declare global {
var _azleIc: AzleIc;
var _azleResolveIds: { [key: string]: (buf: ArrayBuffer) => void };
var _azleRejectIds: { [key: string]: (err: any) => void };
var icTimers: { [key: string]: string };
var _azleIcTimers: { [key: string]: string };
var _azleTimerCallbackIds: { [key: string]: () => void };
var Buffer: BufferConstructor;
// var console: Console;
// var crypto: Crypto;
var icTimers: {
[key: string]: string;
};
// var TextDecoder: any;
// var TextEncoder: any;
var _azleGuardFunctions: { [key: string]: () => any };
}

// export declare var globalThis: {
// Buffer: BufferConstructor;
// console: any;
// crypto: {
// getRandomValues: () => Uint8Array;
// };
// TextDecoder: any;
// TextEncoder: any;
// };

globalThis.TextDecoder = require('text-encoding').TextDecoder;
globalThis.TextEncoder = require('text-encoding').TextEncoder;
globalThis.icTimers ||= {};
globalThis._azleIcTimers ||= {};
globalThis._azleResolveIds = {};
globalThis._azleRejectIds = {};
globalThis._azleTimerCallbackIds = {};
globalThis._azleGuardFunctions = {};

globalThis.console = {
...globalThis.console,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ic/clear_timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function clearTimer(timerId: TimerId): Void {

globalThis._azleIc.clearTimer(encode(TimerId, timerId).buffer);

const timerCallbackId = globalThis.icTimers[timerId.toString()];
const timerCallbackId = globalThis._azleIcTimers[timerId.toString()];

delete globalThis.icTimers[timerId.toString()];
delete globalThis._azleIcTimers[timerId.toString()];
delete globalThis._azleTimerCallbackIds[timerCallbackId];
}
4 changes: 2 additions & 2 deletions src/lib/ic/set_timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export function setTimer(
)
);

globalThis.icTimers[timerId.toString()] = timerCallbackId;
globalThis._azleIcTimers[timerId.toString()] = timerCallbackId;

globalThis._azleTimerCallbackIds[timerCallbackId] = () => {
try {
callback();
} finally {
delete globalThis.icTimers[timerId.toString()];
delete globalThis._azleIcTimers[timerId.toString()];
delete globalThis._azleTimerCallbackIds[timerCallbackId];
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/set_timer_interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function setTimerInterval(
)
);

globalThis.icTimers[timerId.toString()] = timerCallbackId;
globalThis._azleIcTimers[timerId.toString()] = timerCallbackId;

// We don't delete this even if the callback throws because
// it still needs to be here for the next tick
Expand Down
33 changes: 30 additions & 3 deletions src/lib/ic/types/azle_ic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { text } from '../../candid/types/primitive/text';
import { Void } from '../../candid/types/primitive/void';

/**
* The interface for our rust methods it slightly different than the interface
* we expose to the users. This is the interface for the rust functions.
Expand Down Expand Up @@ -88,4 +85,34 @@ export type AzleIc = {
call128: () => never;
notify: () => never;
reply: () => never;
// Stable B Tree Map Functions
stableBTreeMapInit: (candidEncodedMemoryId: ArrayBufferLike) => void;
stableBTreeMapContainsKey: (
candidEncodedMemoryId: ArrayBufferLike,
candidEncodedKey: ArrayBufferLike
) => boolean;
stableBTreeMapGet: (
candidEncodedMemoryId: ArrayBufferLike,
candidEncodedKey: ArrayBufferLike
) => ArrayBuffer | undefined;
stableBTreeMapInsert: (
candidEncodedMemoryId: ArrayBufferLike,
candidEncodedKey: ArrayBufferLike,
candidEncodedValue: ArrayBufferLike
) => ArrayBuffer | undefined;
stableBTreeMapIsEmpty: (candidEncodedMemoryId: ArrayBuffer) => boolean;
stableBTreeMapItems: (
candidEncodedMemoryId: ArrayBufferLike
) => [ArrayBuffer, ArrayBuffer][];
stableBTreeMapKeys: (
candidEncodedMemoryId: ArrayBufferLike
) => ArrayBuffer[];
stableBTreeMapLen: (candidEncodedMemoryId: ArrayBufferLike) => ArrayBuffer;
stableBTreeMapRemove(
candidEncodedMemoryId: ArrayBufferLike,
candidEncodedKey: ArrayBufferLike
): ArrayBuffer;
stableBTreeMapValues: (
candidEncodedMemoryId: ArrayBufferLike
) => ArrayBuffer[];
};
59 changes: 27 additions & 32 deletions src/lib/stable_b_tree_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export function StableBTreeMap<
>(keyType: Key, valueType: Value, memoryId: nat8) {
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

if ((globalThis as any)._azleIc !== undefined) {
(globalThis as any)._azleIc.stableBTreeMapInit(candidEncodedMemoryId);
if (globalThis._azleIc !== undefined) {
globalThis._azleIc.stableBTreeMapInit(candidEncodedMemoryId);
}

return {
Expand All @@ -24,7 +24,7 @@ export function StableBTreeMap<
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;
const candidEncodedKey = encode(keyType, key).buffer;

return (globalThis as any)._azleIc.stableBTreeMapContainsKey(
return globalThis._azleIc.stableBTreeMapContainsKey(
candidEncodedMemoryId,
candidEncodedKey
);
Expand All @@ -38,9 +38,7 @@ export function StableBTreeMap<
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;
const candidEncodedKey = encode(keyType, key).buffer;

const candidEncodedValue = (
globalThis as any
)._azleIc.stableBTreeMapGet(
const candidEncodedValue = globalThis._azleIc.stableBTreeMapGet(
candidEncodedMemoryId,
candidEncodedKey
);
Expand All @@ -65,13 +63,12 @@ export function StableBTreeMap<
const candidEncodedKey = encode(keyType, key).buffer;
const candidEncodedValue = encode(valueType, value).buffer;

const candidEncodedResultValue = (
globalThis as any
)._azleIc.stableBTreeMapInsert(
candidEncodedMemoryId,
candidEncodedKey,
candidEncodedValue
);
const candidEncodedResultValue =
globalThis._azleIc.stableBTreeMapInsert(
candidEncodedMemoryId,
candidEncodedKey,
candidEncodedValue
);

if (candidEncodedResultValue === undefined) {
return None;
Expand All @@ -86,7 +83,7 @@ export function StableBTreeMap<
isEmpty(): boolean {
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

return (globalThis as any)._azleIc.stableBTreeMapIsEmpty(
return globalThis._azleIc.stableBTreeMapIsEmpty(
candidEncodedMemoryId
);
},
Expand All @@ -97,12 +94,12 @@ export function StableBTreeMap<
items(): [TypeMapping<Key>, TypeMapping<Value>][] {
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

const candidEncodedItems = (
globalThis as any
)._azleIc.stableBTreeMapItems(candidEncodedMemoryId);
const candidEncodedItems = globalThis._azleIc.stableBTreeMapItems(
candidEncodedMemoryId
);

// TODO too much copying
return candidEncodedItems.map((candidEncodedItem: any) => {
return candidEncodedItems.map((candidEncodedItem) => {
return [
decode(keyType, candidEncodedItem[0]),
decode(valueType, candidEncodedItem[1])
Expand All @@ -116,12 +113,12 @@ export function StableBTreeMap<
keys(): TypeMapping<Key>[] {
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

const candidEncodedKeys = (
globalThis as any
)._azleIc.stableBTreeMapKeys(candidEncodedMemoryId);
const candidEncodedKeys = globalThis._azleIc.stableBTreeMapKeys(
candidEncodedMemoryId
);

// TODO too much copying
return candidEncodedKeys.map((candidEncodedKey: any) => {
return candidEncodedKeys.map((candidEncodedKey) => {
return decode(keyType, candidEncodedKey);
});
},
Expand All @@ -132,9 +129,9 @@ export function StableBTreeMap<
len(): nat64 {
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

const candidEncodedLen = (
globalThis as any
)._azleIc.stableBTreeMapLen(candidEncodedMemoryId);
const candidEncodedLen = globalThis._azleIc.stableBTreeMapLen(
candidEncodedMemoryId
);

return decode(nat64, candidEncodedLen);
},
Expand All @@ -147,9 +144,7 @@ export function StableBTreeMap<
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;
const candidEncodedKey = encode(keyType, key).buffer;

const candidEncodedValue = (
globalThis as any
)._azleIc.stableBTreeMapRemove(
const candidEncodedValue = globalThis._azleIc.stableBTreeMapRemove(
candidEncodedMemoryId,
candidEncodedKey
);
Expand All @@ -167,12 +162,12 @@ export function StableBTreeMap<
values(): TypeMapping<Value>[] {
const candidEncodedMemoryId = encode(nat8, memoryId).buffer;

const candidEncodedValues = (
globalThis as any
)._azleIc.stableBTreeMapValues(candidEncodedMemoryId);
const candidEncodedValues = globalThis._azleIc.stableBTreeMapValues(
candidEncodedMemoryId
);

// TODO too much copying
return candidEncodedValues.map((candidEncodedValue: any) => {
return candidEncodedValues.map((candidEncodedValue) => {
return decode(valueType, candidEncodedValue);
});
}
Expand Down

0 comments on commit 142ed96

Please sign in to comment.