diff --git a/src/lib/candid/recursive.ts b/src/lib/candid/recursive.ts index 9eb11a3042..cc7afcd684 100644 --- a/src/lib/candid/recursive.ts +++ b/src/lib/candid/recursive.ts @@ -4,8 +4,8 @@ import { CandidType, Parent } from './index'; export type _AzleRecursiveFunction = { (...args: any[]): CandidType; - _azleName?: string; - _azleIsRecursive?: boolean; + name?: string; + isRecursive?: boolean; getIdl?: (parents: Parent[]) => IDL.Type; }; @@ -14,18 +14,18 @@ export function Recursive(candidTypeCallback: any): any { let result: _AzleRecursiveFunction = (...args: any[]) => { const candidType = candidTypeCallback(); - if (candidType._azleIsCanister) { + if (candidType.isCanister) { return candidType(...args); } return candidType; }; - result._azleName = name; - result._azleIsRecursive = true; + result.name = name; + result.isRecursive = true; result.getIdl = (parents: Parent[]) => { const idl = IDL.Rec(); let filler = candidTypeCallback(); - if (filler._azleIsCanister) { + if (filler.isCanister) { filler = filler(result); } idl.fill(filler.getIdl([...parents, { idl: idl, name }])); diff --git a/src/lib/candid/serde/visitors/decode_visitor.ts b/src/lib/candid/serde/visitors/decode_visitor.ts index c97d94f91b..4b9f3ce790 100644 --- a/src/lib/candid/serde/visitors/decode_visitor.ts +++ b/src/lib/candid/serde/visitors/decode_visitor.ts @@ -58,7 +58,7 @@ export class DecodeVisitor extends IDL.Visitor { const candid = ty.accept(this, { js_data: data.js_data[0], - candidType: data.candidType._azleType + candidType: data.candidType.innerType }); return { diff --git a/src/lib/candid/serde/visitors/encode_visitor.ts b/src/lib/candid/serde/visitors/encode_visitor.ts index e931383364..8a4689fb77 100644 --- a/src/lib/candid/serde/visitors/encode_visitor.ts +++ b/src/lib/candid/serde/visitors/encode_visitor.ts @@ -57,7 +57,7 @@ export class EncodeVisitor extends IDL.Visitor { if ('Some' in data.js_data) { const candid = ty.accept(this, { js_data: data.js_data.Some, - candidType: data.candidType._azleType + candidType: data.candidType.innerType }); return [candid]; diff --git a/src/lib/candid/serde/visitors/index.ts b/src/lib/candid/serde/visitors/index.ts index b84f56710b..c7573cc3bc 100644 --- a/src/lib/candid/serde/visitors/index.ts +++ b/src/lib/candid/serde/visitors/index.ts @@ -30,7 +30,7 @@ export function visitTuple( const fields = components.map((value, index) => value.accept(visitor, { js_data: data.js_data[index], - candidType: data.candidType._azleTypes[index] + candidType: data.candidType.innerTypes[index] }) ); return [...fields]; @@ -47,7 +47,7 @@ export function visitVec( return data.js_data.map((array_elem: any) => { return ty.accept(visitor, { js_data: array_elem, - candidType: data.candidType._azleType + candidType: data.candidType.innerType }); }); } @@ -90,7 +90,7 @@ export function visitRec( data: VisitorData ): VisitorResult { let candidType = data.candidType(); - if (candidType._azleIsCanister) { + if (candidType.isCanister) { candidType = candidType([]); } return ty.accept(visitor, { @@ -108,7 +108,7 @@ function visitAzleResult( const OK_FIELD_INDEX = 0; const okField = fields[OK_FIELD_INDEX]; const okData = data.js_data['Ok']; - const okClass = data.candidType._azleOk; + const okClass = data.candidType.Ok; return Result.Ok( okField[1].accept(visitor, { @@ -121,7 +121,7 @@ function visitAzleResult( const ERR_FIELD_INDEX = 1; const errField = fields[ERR_FIELD_INDEX]; const errData = data.js_data['Err']; - const errClass = data.candidType._azleErr; + const errClass = data.candidType.Err; return Result.Err( errField[1].accept(visitor, { js_data: errData, diff --git a/src/lib/candid/to_Idl.ts b/src/lib/candid/to_Idl.ts index 1b6ff9c6af..5c6ef56c74 100644 --- a/src/lib/candid/to_Idl.ts +++ b/src/lib/candid/to_Idl.ts @@ -10,9 +10,9 @@ export function toIdl( candidType: CandidType, parents: Parent[] = [] ): IDL.Type { - if ('_azleName' in candidType) { + if ('name' in candidType) { const parent = parents.find( - (parent) => parent.name === candidType._azleName + (parent) => parent.name === candidType.name ); // If the parent isn't undefined (ie we found one with the same name) // this is a recursive type and we should return the parent rec idl @@ -21,7 +21,7 @@ export function toIdl( return parent.idl; } } - if ('_azleIsCanister' in candidType && candidType._azleIsCanister) { + if ('isCanister' in candidType && candidType.isCanister) { return toIdl((candidType as any)(), parents); } // All CandidTypes ought to have a getIdl function defined for them diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index 510f8981a8..2ac67f194b 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -28,14 +28,14 @@ export function Opt(t: T): AzleOpt { export class AzleOpt { constructor(t: any) { - this._azleType = t; + this.innerType = t; } - _azleType: CandidType; + innerType: CandidType; _azleCandidType?: '_azleCandidType'; _kind: 'AzleOpt' = 'AzleOpt'; getIdl(parents: Parent[]) { - return IDL.Opt(toIdl(this._azleType, parents)); + return IDL.Opt(toIdl(this.innerType, parents)); } } diff --git a/src/lib/candid/types/constructed/tuple.ts b/src/lib/candid/types/constructed/tuple.ts index 8465379e01..ae1cb84584 100644 --- a/src/lib/candid/types/constructed/tuple.ts +++ b/src/lib/candid/types/constructed/tuple.ts @@ -4,14 +4,14 @@ import { IDL } from '@dfinity/candid'; export class AzleTuple { constructor(t: CandidType[]) { - this._azleTypes = t; + this.innerTypes = t; } - _azleTypes: CandidType[]; + innerTypes: CandidType[]; _azleCandidType?: '_azleCandidType'; getIdl(parents: Parent[]) { - const idls = this._azleTypes.map((value) => { + const idls = this.innerTypes.map((value) => { return toIdl(value, parents); }); return IDL.Tuple(...idls); diff --git a/src/lib/candid/types/constructed/vec.ts b/src/lib/candid/types/constructed/vec.ts index 3901fc4c92..df0adfd809 100644 --- a/src/lib/candid/types/constructed/vec.ts +++ b/src/lib/candid/types/constructed/vec.ts @@ -4,14 +4,14 @@ import { IDL } from '@dfinity/candid'; export class AzleVec { constructor(t: any) { - this._azleType = t; + this.innerType = t; } - _azleType: CandidType; + innerType: CandidType; _azleCandidType?: '_azleCandidType'; getIdl(parents: Parent[]) { - return IDL.Vec(toIdl(this._azleType, parents)); + return IDL.Vec(toIdl(this.innerType, parents)); } } diff --git a/src/lib/candid/types/reference/service/index.ts b/src/lib/candid/types/reference/service/index.ts index 0dbbe61bb2..40069b63c9 100644 --- a/src/lib/candid/types/reference/service/index.ts +++ b/src/lib/candid/types/reference/service/index.ts @@ -25,7 +25,7 @@ type CallableObject = { type _AzleCanisterReturnType = { (parentOrPrincipal: _AzleRecursiveFunction | Principal): void; - _azleIsCanister?: boolean; + isCanister?: boolean; }; export function Canister( @@ -40,6 +40,6 @@ export function Canister( return canisterFunction; }; - result._azleIsCanister = true; + result.isCanister = true; return result as any; } diff --git a/src/lib/stable_b_tree_map.ts b/src/lib/stable_b_tree_map.ts index d9540f792f..b6f05c8540 100644 --- a/src/lib/stable_b_tree_map.ts +++ b/src/lib/stable_b_tree_map.ts @@ -8,11 +8,12 @@ export function StableBTreeMap< Key extends CandidType, Value extends CandidType >(keyType: Key, valueType: Value, memoryId: nat8) { + if (globalThis._azleIc === undefined) { + return undefined as any; + } const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - if (globalThis._azleIc !== undefined) { - globalThis._azleIc.stableBTreeMapInit(candidEncodedMemoryId); - } + globalThis._azleIc.stableBTreeMapInit(candidEncodedMemoryId); return { /** diff --git a/src/lib/system_types/result.ts b/src/lib/system_types/result.ts index d8c860a66b..73cf8f20cb 100644 --- a/src/lib/system_types/result.ts +++ b/src/lib/system_types/result.ts @@ -2,21 +2,21 @@ import { Parent, toIdl, CandidType } from '../candid'; import { RequireExactlyOne } from '../candid/types/constructed/variant'; import { IDL } from '@dfinity/candid'; -export class AzleResult { - constructor(ok: any, err: any) { - this._azleOk = ok; - this._azleErr = err; +export class AzleResult { + constructor(ok: T, err: K) { + this.Ok = ok; + this.Err = err; } - _azleOk: any; - _azleErr: any; + Ok: T; + Err: K; _azleCandidType?: '_azleCandidType'; getIdl(parents: Parent[]) { return IDL.Variant({ - Ok: toIdl(this._azleOk, parents), - Err: toIdl(this._azleErr, parents) + Ok: toIdl(this.Ok, parents), + Err: toIdl(this.Err, parents) }); } }