Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jan 20, 2025
1 parent caca051 commit cac781b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/build/experimental/commands/compile/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function getPrelude(main: string): string {
const candid = idlToString(canister.getIdlType([]), {
...getDefaultVisitorData(),
isFirstService: true,
initAndPostUpgradeParams: canister.getInitAndPostUpgradeParamIdlTypes()
initAndPostUpgradeParamIdlTypes: canister.getInitAndPostUpgradeParamIdlTypes()
});
globalThis._azleCallbacks = canister.callbacks;
Expand Down
2 changes: 1 addition & 1 deletion src/build/stable/commands/compile/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function handleClassApiCanister(): string {
const candid = idlToString(canisterIdlType, {
...getDefaultVisitorData(),
isFirstService: true,
initAndPostUpgradeParams: exportedCanisterClassInstance._azleInitAndPostUpgradeIdlTypes
initAndPostUpgradeParamIdlTypes: exportedCanisterClassInstance._azleInitAndPostUpgradeIdlTypes
});
globalThis._azleGetCandidAndMethodMeta = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Method,
MethodMeta
} from '../../../../../../../build/stable/utils/types';
import { CanisterMethodMode } from '../../../../../../stable/execute_with_candid_serde';
import { CanisterMethodInfo } from '../../../../../canister_methods/types/canister_method_info';
import { Callbacks } from '../../../../../globals';
import { ic } from '../../../../../ic';
Expand All @@ -30,17 +31,8 @@ type _AzleFunctionReturnType = {
type CallRawFunction = typeof ic.callRaw;
type NotifyRawFunction = typeof ic.notifyRaw;

type Mode =
| 'query'
| 'update'
| 'init'
| 'postUpgrade'
| 'preUpgrade'
| 'inspectMessage'
| 'heartbeat';

type FunctionInfo = {
mode: Mode;
mode: CanisterMethodMode;
paramCandidTypes: CandidType[];
returnCandidType: CandidType;
};
Expand Down Expand Up @@ -106,7 +98,7 @@ function createGetIdlTypeFunction(
};
}

function createAnnotation(mode: Mode): string[] {
function createAnnotation(mode: CanisterMethodMode): string[] {
if (mode === 'query') {
return ['query'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,49 @@ export function createGetInitAndPostUpgradeParamIdlTypes(
) {
return (parents: Parent[]): IDL.Type[] => {
const serviceFunctionInfo = canisterOptions as ServiceFunctionInfo;
let foundInit = false;
let foundPostUpgrade = false;

return Object.entries(serviceFunctionInfo).reduce(
(accumulator, [_methodName, functionInfo]) => {
const mode = functionInfo.mode;
const isInitOrPostUpgradeMethod =
mode === 'init' || mode === 'postUpgrade';
if (!isInitOrPostUpgradeMethod) {
return accumulator;
}

if (mode === 'init') {
if (foundInit === true) {
throw new Error(
'Init method already found in canister options'
);
}
foundInit = true;
} else if (mode === 'postUpgrade') {
if (foundPostUpgrade === true) {
throw new Error(
'PostUpgrade method already found in canister options'
);
}
foundPostUpgrade = true;
}
const initMethod = Object.entries(serviceFunctionInfo).find(
([_, info]) => info.mode === 'init'
);
const postUpgradeMethod = Object.entries(serviceFunctionInfo).find(
([_, info]) => info.mode === 'postUpgrade'
);

const paramIdlTypes = toIdlTypeArray(
functionInfo.paramCandidTypes,
parents
if (initMethod !== undefined && postUpgradeMethod !== undefined) {
const initParams = toIdlTypeArray(
initMethod[1].paramCandidTypes,
parents
);
const postUpgradeParams = toIdlTypeArray(
postUpgradeMethod[1].paramCandidTypes,
parents
);

const initSignature = idlToString(IDL.Func(initParams, []));
const postUpgradeSignature = idlToString(
IDL.Func(postUpgradeParams, [])
);

if (initSignature !== postUpgradeSignature) {
throw new Error(
`'init' and 'postUpgrade' methods must have the same parameters.\nFound:\n${initSignature}\n${postUpgradeSignature}`
);
if (
(mode === 'init' && foundPostUpgrade) ||
(mode === 'postUpgrade' && foundInit)
) {
const functionSignature = idlToString(
IDL.Func(paramIdlTypes, [])
);
}

const accumulatorSignature = idlToString(
IDL.Func(accumulator, [])
);
return initParams;
}

if (functionSignature !== accumulatorSignature) {
throw new Error(
'Init and postUpgrade methods must have the same function signature'
);
}
}
if (initMethod !== undefined) {
return toIdlTypeArray(initMethod[1].paramCandidTypes, parents);
}

return paramIdlTypes;
},
[] as IDL.Type[]
);
if (postUpgradeMethod !== undefined) {
return toIdlTypeArray(
postUpgradeMethod[1].paramCandidTypes,
parents
);
}

return [];
};
}
28 changes: 14 additions & 14 deletions src/lib/stable/canister_methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type DefinedSystemMethods = {
init: boolean;
postUpgrade: boolean;
preUpgrade: boolean;
heartbeat: boolean;
inspectMessage: boolean;
heartbeat: boolean;
};

export type MethodType<This, Args extends unknown[], Return> = (
Expand Down Expand Up @@ -262,31 +262,31 @@ function decoratorImplementation<This, Args extends unknown[], Return>(
};
}

if (canisterMethodMode === 'heartbeat') {
if (canisterMethodMode === 'inspectMessage') {
throwIfMethodAlreadyDefined(
'heartbeat',
'inspectMessage',
exportedCanisterClassInstance._azleDefinedSystemMethods
.heartbeat
.inspectMessage
);

exportedCanisterClassInstance._azleDefinedSystemMethods.heartbeat =
exportedCanisterClassInstance._azleDefinedSystemMethods.inspectMessage =
true;
exportedCanisterClassInstance._azleMethodMeta.heartbeat = {
exportedCanisterClassInstance._azleMethodMeta.inspect_message = {
name,
index
};
}

if (canisterMethodMode === 'inspectMessage') {
if (canisterMethodMode === 'heartbeat') {
throwIfMethodAlreadyDefined(
'inspectMessage',
'heartbeat',
exportedCanisterClassInstance._azleDefinedSystemMethods
.inspectMessage
.heartbeat
);

exportedCanisterClassInstance._azleDefinedSystemMethods.inspectMessage =
exportedCanisterClassInstance._azleDefinedSystemMethods.heartbeat =
true;
exportedCanisterClassInstance._azleMethodMeta.inspect_message = {
exportedCanisterClassInstance._azleMethodMeta.heartbeat = {
name,
index
};
Expand Down Expand Up @@ -348,7 +348,7 @@ function isDecoratorOverloadedWithoutParams<
/**
* @internal
*
* Uses the candid string of the init and post-upgrade methods to verify that
* Uses the candid string of the `init` and `postUpgrade` methods to verify that
* they have matching parameter signatures.
*
* @param idlTypes - Array of IDL function types representing canister methods
Expand Down Expand Up @@ -378,12 +378,12 @@ function verifyInitAndPostUpgradeHaveTheSameParams(
* @throws {Error} If the method is already defined
*/
function throwIfMethodAlreadyDefined(
methodName: string,
methodName: keyof DefinedSystemMethods,
isDefined: boolean
): void {
if (isDefined === true) {
throw new Error(
`'${methodName}' method has multiple definitions in the class`
`'${methodName}' method can only have one definition in the exported canister class`
);
}
}
2 changes: 1 addition & 1 deletion src/lib/stable/did_file/idl_to_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
* @returns The Candid string representation of the type
*
* @example
* const recordType = IDL.Record({ 'name': IDL.Text, 'age': IDL.Nat8 });
* const recordType = IDL.Record({ name: IDL.Text, age: IDL.Nat8 });
* const candidStr = idlToString(recordType);
* // Result: "record { name : text; age : nat8 }"
*/
Expand Down
4 changes: 2 additions & 2 deletions src/lib/stable/did_file/visitor/did_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type VisitorData = {
/** Indicates if this is the first/primary service being processed */
isFirstService: boolean;
/** Collection of system functions (init, postUpgrade) to process */
initAndPostUpgradeParams: IDL.FuncClass[];
initAndPostUpgradeParamIdlTypes: IDL.FuncClass[];
};

/**
Expand Down Expand Up @@ -60,7 +60,7 @@ export function getDefaultVisitorData(): VisitorData {
usedRecClasses: [],
isOnService: false,
isFirstService: false,
initAndPostUpgradeParams: []
initAndPostUpgradeParamIdlTypes: []
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/stable/did_file/visitor/visit/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function visitService(
function serviceToCandidString(
t: IDL.ServiceClass,
canisterMethodCandidStrings: string[],
initOrPostUpgradeMethodCandidString: string[],
initAndPostUpgradeMethodCandidString: string[],
candidTypes: CandidTypesDefs,
isFirstService: boolean
): [CandidDef, CandidTypesDefs] {
Expand All @@ -57,7 +57,7 @@ function serviceToCandidString(
.join(func_separator);

const canisterParamsString = createCanisterParamsString(
initOrPostUpgradeMethodCandidString
initAndPostUpgradeMethodCandidString
);

if (isFirstService === true) {
Expand All @@ -73,7 +73,7 @@ function getInitAndPostUpgradeMethodCandid(
didVisitor: DidVisitor,
data: VisitorData
): [CandidDef[], CandidTypesDefs] {
const result = IDL.Func(data.initAndPostUpgradeParams, []).accept(
const result = IDL.Func(data.initAndPostUpgradeParamIdlTypes, []).accept(
didVisitor,
{
...data,
Expand Down

0 comments on commit cac781b

Please sign in to comment.