From 0b287510e2fb9c65881c8376f934f789c67e95c6 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 17 Jan 2024 15:23:28 -0600 Subject: [PATCH] attempt to fix number conversions, always use strings --- .../rust/canister/src/ic/arg_data_raw_size.rs | 4 ++-- .../rust/canister/src/ic/canister_balance.rs | 4 +++- .../rust/canister/src/ic/canister_version.rs | 4 +++- .../canister/src/ic/instruction_counter.rs | 4 +++- .../canister/src/ic/performance_counter.rs | 7 +++++- .../rust/canister/src/ic/reject_code.rs | 2 +- .../rust/canister/src/ic/stable64_grow.rs | 8 +++++-- .../rust/canister/src/ic/stable64_size.rs | 4 +++- .../canister/src/ic/stable_b_tree_map_len.rs | 2 +- src/compiler/rust/canister/src/ic/time.rs | 2 +- src/lib/ic/arg_data_raw_size.ts | 2 +- src/lib/ic/canister_balance.ts | 2 +- src/lib/ic/canister_version.ts | 2 +- src/lib/ic/instruction_counter.ts | 2 +- src/lib/ic/performance_counter.ts | 4 +++- src/lib/ic/reject_code.ts | 2 +- src/lib/ic/stable_64_grow.ts | 2 +- src/lib/ic/stable_64_size.ts | 2 +- src/lib/ic/time.ts | 2 +- src/lib/ic/types/azle_ic.ts | 22 +++++++++---------- .../stable_structures/stable_b_tree_map.ts | 2 +- 21 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/compiler/rust/canister/src/ic/arg_data_raw_size.rs b/src/compiler/rust/canister/src/ic/arg_data_raw_size.rs index 2a4bfa16db..a8710975d7 100644 --- a/src/compiler/rust/canister/src/ic/arg_data_raw_size.rs +++ b/src/compiler/rust/canister/src/ic/arg_data_raw_size.rs @@ -5,8 +5,8 @@ use wasmedge_quickjs::{Context, JsFn, JsValue}; pub struct NativeFunction; impl JsFn for NativeFunction { fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { - i32::try_from(ic_cdk::api::call::arg_data_raw_size()) - .unwrap() + context + .new_string(&ic_cdk::api::call::arg_data_raw_size().to_string()) .into() } } diff --git a/src/compiler/rust/canister/src/ic/canister_balance.rs b/src/compiler/rust/canister/src/ic/canister_balance.rs index 8b84240953..f775f0d608 100644 --- a/src/compiler/rust/canister/src/ic/canister_balance.rs +++ b/src/compiler/rust/canister/src/ic/canister_balance.rs @@ -3,6 +3,8 @@ use wasmedge_quickjs::{Context, JsFn, JsValue}; pub struct NativeFunction; impl JsFn for NativeFunction { fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { - ic_cdk::api::canister_balance().into() + context + .new_string(&ic_cdk::api::canister_balance().to_string()) + .into() } } diff --git a/src/compiler/rust/canister/src/ic/canister_version.rs b/src/compiler/rust/canister/src/ic/canister_version.rs index 686366a7e4..bde085dd31 100644 --- a/src/compiler/rust/canister/src/ic/canister_version.rs +++ b/src/compiler/rust/canister/src/ic/canister_version.rs @@ -3,6 +3,8 @@ use wasmedge_quickjs::{Context, JsFn, JsValue}; pub struct NativeFunction; impl JsFn for NativeFunction { fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { - ic_cdk::api::canister_version().into() + context + .new_string(&ic_cdk::api::canister_version().to_string()) + .into() } } diff --git a/src/compiler/rust/canister/src/ic/instruction_counter.rs b/src/compiler/rust/canister/src/ic/instruction_counter.rs index 8699d75469..c274545a3e 100644 --- a/src/compiler/rust/canister/src/ic/instruction_counter.rs +++ b/src/compiler/rust/canister/src/ic/instruction_counter.rs @@ -3,6 +3,8 @@ use wasmedge_quickjs::{Context, JsFn, JsValue}; pub struct NativeFunction; impl JsFn for NativeFunction { fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { - ic_cdk::api::instruction_counter().into() + context + .new_string(&ic_cdk::api::instruction_counter().to_string()) + .into() } } diff --git a/src/compiler/rust/canister/src/ic/performance_counter.rs b/src/compiler/rust/canister/src/ic/performance_counter.rs index c80ed06361..953c199f4b 100644 --- a/src/compiler/rust/canister/src/ic/performance_counter.rs +++ b/src/compiler/rust/canister/src/ic/performance_counter.rs @@ -9,6 +9,11 @@ impl JsFn for NativeFunction { panic!("conversion from JsValue to JsString failed") }; - ic_cdk::api::call::performance_counter(counter_type_string.parse().unwrap()).into() + context + .new_string( + &ic_cdk::api::call::performance_counter(counter_type_string.parse().unwrap()) + .to_string(), + ) + .into() } } diff --git a/src/compiler/rust/canister/src/ic/reject_code.rs b/src/compiler/rust/canister/src/ic/reject_code.rs index 4da4e927dc..9c8b8e1b78 100644 --- a/src/compiler/rust/canister/src/ic/reject_code.rs +++ b/src/compiler/rust/canister/src/ic/reject_code.rs @@ -15,6 +15,6 @@ impl JsFn for NativeFunction { ic_cdk::api::call::RejectionCode::Unknown => 6, }; - reject_code_number.into() + context.new_string(&reject_code_number.to_string()).into() } } diff --git a/src/compiler/rust/canister/src/ic/stable64_grow.rs b/src/compiler/rust/canister/src/ic/stable64_grow.rs index 7e31e49d13..8e648eee3d 100644 --- a/src/compiler/rust/canister/src/ic/stable64_grow.rs +++ b/src/compiler/rust/canister/src/ic/stable64_grow.rs @@ -9,8 +9,12 @@ impl JsFn for NativeFunction { panic!("conversion from JsValue to JsString failed") }; - ic_cdk::api::stable::stable64_grow(new_pages_string.parse().unwrap()) - .unwrap() + context + .new_string( + &ic_cdk::api::stable::stable64_grow(new_pages_string.parse().unwrap()) + .unwrap() + .to_string(), + ) .into() } } diff --git a/src/compiler/rust/canister/src/ic/stable64_size.rs b/src/compiler/rust/canister/src/ic/stable64_size.rs index 1373639ba6..4b131d021b 100644 --- a/src/compiler/rust/canister/src/ic/stable64_size.rs +++ b/src/compiler/rust/canister/src/ic/stable64_size.rs @@ -3,6 +3,8 @@ use wasmedge_quickjs::{Context, JsFn, JsValue}; pub struct NativeFunction; impl JsFn for NativeFunction { fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { - ic_cdk::api::stable::stable64_size().into() + context + .new_string(&ic_cdk::api::stable::stable64_size().to_string()) + .into() } } diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_len.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_len.rs index 555c39ea55..588099b6f6 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_len.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_len.rs @@ -18,6 +18,6 @@ impl JsFn for NativeFunction { stable_b_tree_maps[&memory_id].len() }); - len.into() + context.new_string(&len.to_string()).into() } } diff --git a/src/compiler/rust/canister/src/ic/time.rs b/src/compiler/rust/canister/src/ic/time.rs index 4acc845feb..a45dea02cf 100644 --- a/src/compiler/rust/canister/src/ic/time.rs +++ b/src/compiler/rust/canister/src/ic/time.rs @@ -3,6 +3,6 @@ use wasmedge_quickjs::{Context, JsFn, JsValue}; pub struct NativeFunction; impl JsFn for NativeFunction { fn call(context: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue { - ic_cdk::api::time().into() + context.new_string(&ic_cdk::api::time().to_string()).into() } } diff --git a/src/lib/ic/arg_data_raw_size.ts b/src/lib/ic/arg_data_raw_size.ts index 103cfef538..9e90ae7d45 100644 --- a/src/lib/ic/arg_data_raw_size.ts +++ b/src/lib/ic/arg_data_raw_size.ts @@ -6,6 +6,6 @@ import { nat32 } from '../candid/types/primitive/nats/nat32'; */ export function argDataRawSize(): nat32 { return globalThis._azleIc - ? globalThis._azleIc.argDataRawSize() + ? Number(globalThis._azleIc.argDataRawSize()) : (undefined as any); } diff --git a/src/lib/ic/canister_balance.ts b/src/lib/ic/canister_balance.ts index 50129968d1..a7e46706b0 100644 --- a/src/lib/ic/canister_balance.ts +++ b/src/lib/ic/canister_balance.ts @@ -9,5 +9,5 @@ export function canisterBalance(): nat64 { return undefined as any; } - return globalThis._azleIc.canisterBalance(); + return BigInt(globalThis._azleIc.canisterBalance()); } diff --git a/src/lib/ic/canister_version.ts b/src/lib/ic/canister_version.ts index 8bea175405..5e04d65070 100644 --- a/src/lib/ic/canister_version.ts +++ b/src/lib/ic/canister_version.ts @@ -10,5 +10,5 @@ export function canisterVersion(): nat64 { return undefined as any; } - return globalThis._azleIc.canisterVersion(); + return BigInt(globalThis._azleIc.canisterVersion()); } diff --git a/src/lib/ic/instruction_counter.ts b/src/lib/ic/instruction_counter.ts index 3f457f5d26..6daaa3ddf7 100644 --- a/src/lib/ic/instruction_counter.ts +++ b/src/lib/ic/instruction_counter.ts @@ -13,5 +13,5 @@ export function instructionCounter(): nat64 { return undefined as any; } - return globalThis._azleIc.instructionCounter(); + return BigInt(globalThis._azleIc.instructionCounter()); } diff --git a/src/lib/ic/performance_counter.ts b/src/lib/ic/performance_counter.ts index 3cb48b9e80..5d67a8d981 100644 --- a/src/lib/ic/performance_counter.ts +++ b/src/lib/ic/performance_counter.ts @@ -15,5 +15,7 @@ export function performanceCounter(counterType: nat32): nat64 { return undefined as any; } - return globalThis._azleIc.performanceCounter(counterType.toString()); + return BigInt( + globalThis._azleIc.performanceCounter(counterType.toString()) + ); } diff --git a/src/lib/ic/reject_code.ts b/src/lib/ic/reject_code.ts index db8c3d45cf..cad186ab33 100644 --- a/src/lib/ic/reject_code.ts +++ b/src/lib/ic/reject_code.ts @@ -10,7 +10,7 @@ export function rejectCode(): RejectionCode { return undefined as any; } - const rejectCodeNumber = globalThis._azleIc.rejectCode(); + const rejectCodeNumber = Number(globalThis._azleIc.rejectCode()); switch (rejectCodeNumber) { case 0: diff --git a/src/lib/ic/stable_64_grow.ts b/src/lib/ic/stable_64_grow.ts index 2c305be5f7..67253b1ffe 100644 --- a/src/lib/ic/stable_64_grow.ts +++ b/src/lib/ic/stable_64_grow.ts @@ -11,5 +11,5 @@ export function stable64Grow(newPages: nat64): nat64 { return undefined as any; } - return globalThis._azleIc.stable64Grow(newPages.toString()); + return BigInt(globalThis._azleIc.stable64Grow(newPages.toString())); } diff --git a/src/lib/ic/stable_64_size.ts b/src/lib/ic/stable_64_size.ts index 28610dd8d0..bc8ed9d40c 100644 --- a/src/lib/ic/stable_64_size.ts +++ b/src/lib/ic/stable_64_size.ts @@ -10,5 +10,5 @@ export function stable64Size(): nat64 { return undefined as any; } - return globalThis._azleIc.stable64Size(); + return BigInt(globalThis._azleIc.stable64Size()); } diff --git a/src/lib/ic/time.ts b/src/lib/ic/time.ts index 28f4ec09be..ce18b02de3 100644 --- a/src/lib/ic/time.ts +++ b/src/lib/ic/time.ts @@ -9,5 +9,5 @@ export function time(): nat64 { return undefined as any; } - return globalThis._azleIc.time(); + return BigInt(globalThis._azleIc.time()); } diff --git a/src/lib/ic/types/azle_ic.ts b/src/lib/ic/types/azle_ic.ts index 76cce17de0..638e4d7b64 100644 --- a/src/lib/ic/types/azle_ic.ts +++ b/src/lib/ic/types/azle_ic.ts @@ -4,7 +4,7 @@ */ export type AzleIc = { argDataRaw: () => ArrayBufferLike; - argDataRawSize: () => number; + argDataRawSize: () => string; callRaw: ( promiseId: string, canisterIdBytes: ArrayBufferLike, @@ -22,19 +22,19 @@ export type AzleIc = { caller: () => ArrayBufferLike; candidDecode: (candidBytes: ArrayBufferLike) => string; candidEncode: (candidString: string) => ArrayBufferLike; - canisterBalance: () => bigint; + canisterBalance: () => string; canisterBalance128: () => string; - canisterVersion: () => bigint; + canisterVersion: () => string; clearTimer: (timerIdString: string) => void; dataCertificate: () => ArrayBufferLike | undefined; id: () => string; - instructionCounter: () => bigint; + instructionCounter: () => string; isController: (principalBytes: ArrayBufferLike) => boolean; msgCyclesAccept: (maxAmountString: string) => string; msgCyclesAccept128: (maxAmountString: string) => string; msgCyclesAvailable: () => string; msgCyclesAvailable128: () => string; - msgCyclesRefunded: () => bigint; + msgCyclesRefunded: () => string; msgCyclesRefunded128: () => string; notifyRaw: ( canisterIdBytes: ArrayBufferLike, @@ -42,8 +42,8 @@ export type AzleIc = { argsRawBuffer: ArrayBufferLike, paymentString: string ) => void; - performanceCounter: (counterType: string) => bigint; - rejectCode: () => number; + performanceCounter: (counterType: string) => string; + rejectCode: () => string; replyRaw: (bytes: ArrayBufferLike) => void; setCertifiedData: (dataBytes: ArrayBufferLike) => void; setTimer: (delayString: string, timerCallbackId: string) => string; @@ -56,11 +56,11 @@ export type AzleIc = { stableRead: (offset: string, length: string) => ArrayBufferLike; stableSize: () => string; stableWrite: (offset: string, buf: ArrayBufferLike) => void; - stable64Grow: (newPages: string) => bigint; + stable64Grow: (newPages: string) => string; stable64Read: (offset: string, length: string) => ArrayBufferLike; - stable64Size: () => bigint; + stable64Size: () => string; stable64Write: (offset: string, buf: ArrayBufferLike) => void; - time: () => bigint; + time: () => string; // These calls aren't intercepted by our IC object, they go right to the // rust version and come out. Since they don't need to be intercepted I am // assuming that their types are the same as the types declared by our @@ -104,7 +104,7 @@ export type AzleIc = { startIndex: string, length: string ) => ArrayBuffer[]; - stableBTreeMapLen: (memoryId: string) => bigint; + stableBTreeMapLen: (memoryId: string) => string; stableBTreeMapRemove( memoryId: string, encodedKey: ArrayBufferLike diff --git a/src/lib/stable_structures/stable_b_tree_map.ts b/src/lib/stable_structures/stable_b_tree_map.ts index 62d9b7bca0..46d65e1357 100644 --- a/src/lib/stable_structures/stable_b_tree_map.ts +++ b/src/lib/stable_structures/stable_b_tree_map.ts @@ -163,7 +163,7 @@ export function StableBTreeMap( return undefined as any; } - return globalThis._azleIc.stableBTreeMapLen(memoryId); + return BigInt(globalThis._azleIc.stableBTreeMapLen(memoryId)); }, /** * Removes a key from the map.