Skip to content

Commit

Permalink
Merge pull request #2461 from demergent-labs/experimental_globals
Browse files Browse the repository at this point in the history
completely remove the global index for the canister methods, combine …
  • Loading branch information
lastmjs authored Jan 7, 2025
2 parents 32027e3 + ba7282b commit 05297e9
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 164 deletions.
Binary file modified canister_templates/experimental.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ async function handleHttp(): Promise<CandidAndMethodMeta> {
).toString();

const methodMeta: MethodMeta = {
init: { name: 'init', index: 0 },
post_upgrade: { name: 'postUpgrade', index: 1 },
queries: [
{
name: 'http_request',
index: 0,
index: 2,
composite: true
}
],
updates: [
{
name: 'http_request_update',
index: 1
index: 3
}
],
init: { name: 'init', index: 2 },
post_upgrade: { name: 'postUpgrade', index: 3 }
]
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ pub fn get_candid_and_method_meta_pointer() -> *mut std::os::raw::c_char {
.get_global()
.set("_azleCallbacks", context.new_object().into());

context.get_global().set(
"_azleCanisterMethodsIndex",
wasmedge_quickjs::JsValue::Int(0),
);

context.get_global().set("_azleMethodMeta", {
let mut method_meta = context.new_object();
method_meta.set("queries", context.new_array().into());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,6 @@ pub fn initialize_js(
.get_global()
.set("_azleCallbacks", context.new_object().into());

context.get_global().set(
"_azleCanisterMethodsIndex",
wasmedge_quickjs::JsValue::Int(0),
);

context.get_global().set("_azleMethodMeta", {
let mut method_meta = context.new_object();
method_meta.set("queries", context.new_array().into());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import '../../../../../experimental';

import { IDL } from '@dfinity/candid';

import { MethodMeta } from '../../../../../../../build/stable/utils/types';
import {
Method,
MethodMeta
} from '../../../../../../../build/stable/utils/types';
import { CanisterMethodInfo } from '../../../../../canister_methods/types/canister_method_info';
import { Callbacks } from '../../../../../globals';
import { ic } from '../../../../../ic';
import { CandidType, Parent, toIdlTypeArray } from '../../../../index';
import { _AzleRecursiveFunction } from '../../../../recursive';
import { decode, encode } from '../../../../serde';
import { Principal } from '../../principal';
import { createQueryMethods, createUpdateMethods } from './query_update';
import {
createGetSystemFunctionIdlTypeFunction,
createSystemMethod
} from './system_methods';
import { createGetSystemFunctionIdlTypeFunction } from './system_methods';

export type CanisterOptions = {
[key: string]: CanisterMethodInfo<any, any>;
Expand Down Expand Up @@ -52,28 +52,11 @@ export function createCanisterFunction(
): _AzleFunctionReturnType {
let canister = createCanisterFunctionBase(canisterOptions);

canister.methodMeta = {};
canister.methodMeta.init = createSystemMethod('init', canisterOptions);
canister.methodMeta.heartbeat = createSystemMethod(
'heartbeat',
canisterOptions
);
canister.methodMeta.post_upgrade = createSystemMethod(
'postUpgrade',
canisterOptions
);
canister.methodMeta.pre_upgrade = createSystemMethod(
'preUpgrade',
canisterOptions
);
canister.methodMeta.inspect_message = createSystemMethod(
'inspectMessage',
canisterOptions
);
canister.methodMeta.queries = createQueryMethods(canisterOptions);
canister.methodMeta.updates = createUpdateMethods(canisterOptions);
const { callbacks, methodMeta } =
createCallbacksAndMethodMeta(canisterOptions);

canister.callbacks = createCallbacks(canisterOptions);
canister.callbacks = callbacks;
canister.methodMeta = methodMeta;

canister.getIdlType = createGetIdlTypeFunction(canisterOptions);
canister.getSystemFunctionIdlTypes =
Expand Down Expand Up @@ -138,17 +121,107 @@ function createUpdateOrQueryFunctionIdlType(
return IDL.Func(paramIdlTypes, returnIdlType, annotations);
}

function createCallbacks(
canisterOptions: CanisterOptions
): Record<string, ((...args: any) => any) | undefined> {
return Object.entries(canisterOptions).reduce((acc, entry) => {
const canisterMethod = entry[1];
function createCallbacksAndMethodMeta(canisterOptions: CanisterOptions): {
callbacks: Callbacks;
methodMeta: MethodMeta;
} {
return Object.entries(canisterOptions).reduce(
(acc, [canisterMethodName, canisterMethodInfo], index) => {
const methodMeta = getMethodMeta(
canisterMethodName,
index,
canisterMethodInfo
);

const queries = [
...(acc.methodMeta.queries ?? []),
...(methodMeta.queries ?? [])
];

const updates = [
...(acc.methodMeta.updates ?? []),
...(methodMeta.updates ?? [])
];

return {
callbacks: {
...acc.callbacks,
[index.toString()]: canisterMethodInfo.callback!
},
methodMeta: {
...acc.methodMeta,
...methodMeta,
queries,
updates
}
};
},
{
callbacks: {} as Callbacks,
methodMeta: {} as MethodMeta
}
);
}

function getMethodMeta(
canisterMethodName: string,
index: number,
canisterMethodInfo: CanisterMethodInfo<any, any>
): MethodMeta {
const method: Method = {
name: canisterMethodName,
index,
composite:
canisterMethodInfo.mode === 'query'
? canisterMethodInfo.async
: undefined
};

if (canisterMethodInfo.mode === 'init') {
return {
init: method
};
}

if (canisterMethodInfo.mode === 'postUpgrade') {
return {
post_upgrade: method
};
}

if (canisterMethodInfo.mode === 'preUpgrade') {
return {
pre_upgrade: method
};
}

if (canisterMethodInfo.mode === 'inspectMessage') {
return {
inspect_message: method
};
}

if (canisterMethodInfo.mode === 'heartbeat') {
return {
...acc,
[canisterMethod.index.toString()]: canisterMethod.callback
heartbeat: method
};
}, {});
}

if (canisterMethodInfo.mode === 'query') {
return {
queries: [method]
};
}

if (canisterMethodInfo.mode === 'update') {
return {
updates: [method]
};
}

throw new Error(
`Invalid method mode: ${canisterMethodInfo.mode} for method: ${canisterMethodName}`
);
}

function createCanisterFunctionBase(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@ import { Parent, toIdlTypeArray } from '../../../../index';
import { _AzleRecursiveFunction } from '../../../../recursive';
import { CanisterOptions, ServiceFunctionInfo } from '.';

type SystemMethod = { name: string; index: number } | undefined;

export function createSystemMethod(
mode:
| 'init'
| 'postUpgrade'
| 'preUpgrade'
| 'heartbeat'
| 'inspectMessage',
canisterOptions: CanisterOptions
): SystemMethod {
const methodOption = Object.entries(canisterOptions).find(
([_methodName, canisterMethod]) => canisterMethod.mode === mode
);
if (methodOption === undefined) {
return undefined;
}
return {
name: methodOption[0],
index: methodOption[1].index
};
}

export function createGetSystemFunctionIdlTypeFunction(
canisterOptions: CanisterOptions
) {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/experimental/canister_methods/methods/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ export function heartbeat(
callback: () => executeHeartbeat(callback),
paramCandidTypes: [],
returnCandidType: Void,
async: isAsync(callback),
index: globalThis._azleCanisterMethodsIndex++
async: isAsync(callback)
};
}

Expand Down
6 changes: 2 additions & 4 deletions src/lib/experimental/canister_methods/methods/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export function init<
paramCandidTypes: Params,
callback?: Awaited<ReturnType<GenericCallback>> extends TypeMapping<Void>
? GenericCallback
: never,
noop?: boolean
: never
): CanisterMethodInfo<Params, Void> {
const finalCallback =
callback === undefined
Expand All @@ -36,7 +35,6 @@ export function init<
callback: finalCallback,
paramCandidTypes: paramCandidTypes as any,
returnCandidType: Void,
async: false,
index: noop === true ? 0 : globalThis._azleCanisterMethodsIndex++
async: false
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function inspectMessage(
callback: finalCallback,
paramCandidTypes: [],
returnCandidType: Void,
async: false,
index: globalThis._azleCanisterMethodsIndex++
async: false
};
}
6 changes: 2 additions & 4 deletions src/lib/experimental/canister_methods/methods/post_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export function postUpgrade<
paramCandidTypes: Params,
callback?: Awaited<ReturnType<GenericCallback>> extends TypeMapping<Void>
? GenericCallback
: never,
noop?: boolean
: never
): CanisterMethodInfo<Params, Void> {
const finalCallback =
callback === undefined
Expand All @@ -36,7 +35,6 @@ export function postUpgrade<
callback: finalCallback,
paramCandidTypes: paramCandidTypes as unknown as CandidType[],
returnCandidType: Void,
async: false,
index: noop === true ? 0 : globalThis._azleCanisterMethodsIndex++
async: false
};
}
3 changes: 1 addition & 2 deletions src/lib/experimental/canister_methods/methods/pre_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function preUpgrade(
callback,
paramCandidTypes: [],
returnCandidType: Void,
async: isAsync(callback),
index: globalThis._azleCanisterMethodsIndex++
async: isAsync(callback)
};
}
6 changes: 2 additions & 4 deletions src/lib/experimental/canister_methods/methods/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export function query<
callback?: Awaited<ReturnType<GenericCallback>> extends TypeMapping<Return>
? GenericCallback
: never,
methodArgs?: MethodArgs,
noop?: boolean
methodArgs?: MethodArgs
): CanisterMethodInfo<Params, Return> {
// TODO maybe the cross canister callback should be made here?
const finalCallback =
Expand All @@ -41,7 +40,6 @@ export function query<
callback: finalCallback,
paramCandidTypes: paramCandidTypes as unknown as CandidType[],
returnCandidType,
async: callback === undefined ? false : isAsync(callback),
index: noop === true ? 0 : globalThis._azleCanisterMethodsIndex++
async: callback === undefined ? false : isAsync(callback)
};
}
Loading

0 comments on commit 05297e9

Please sign in to comment.