Skip to content

Commit

Permalink
Merge pull request #1449 from demergent-labs/string_serde_for_bigint
Browse files Browse the repository at this point in the history
String serde for bigint
  • Loading branch information
lastmjs authored Nov 16, 2023
2 parents 08edcf2 + fb0c532 commit 2616415
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/compiler/rust/canister/src/ic/instruction_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pub fn native_function<'a>(
_args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let instruction_counter_js_value: JSValue =
candid::encode_one(ic_cdk::api::instruction_counter())?.into();
ic_cdk::api::instruction_counter().to_string().into();
to_qjs_value(&context, &instruction_counter_js_value)
}
9 changes: 5 additions & 4 deletions src/compiler/rust/canister/src/ic/performance_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ pub fn native_function<'a>(
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let counter_type_vec_u8: Vec<u8> = args
let counter_type_string: String = args
.get(0)
.expect("performanceCounter must have one argument")
.to_js_value()?
.try_into()?;

let counter_type: u32 = candid::decode_one(&counter_type_vec_u8)?;

let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::call::performance_counter(counter_type))?.into();
ic_cdk::api::call::performance_counter(counter_type_string.parse()?)
.to_string()
.into();

to_qjs_value(&context, &return_js_value)
}
8 changes: 4 additions & 4 deletions src/compiler/rust/canister/src/ic/stable64_grow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ pub fn native_function<'a>(
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let new_pages_bytes: Vec<u8> = args
let new_pages_string: String = args
.get(0)
.expect("stable64Grow must have one argument")
.to_js_value()?
.try_into()?;

let new_pages: u64 = candid::decode_one(&new_pages_bytes)?;
let return_js_value: JSValue = ic_cdk::api::stable::stable64_grow(new_pages_string.parse()?)?
.to_string()
.into();

let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::stable::stable64_grow(new_pages)?)?.into();
to_qjs_value(&context, &return_js_value)
}
12 changes: 8 additions & 4 deletions src/compiler/rust/canister/src/ic/stable64_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ pub fn native_function<'a>(
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let params_bytes: Vec<u8> = args
let offset_string: String = args
.get(0)
.expect("stable64Read must have two arguments")
.to_js_value()?
.try_into()?;

let (offset, length): (u64, u64) = candid::decode_args(&params_bytes)?;
let length_string: String = args
.get(1)
.expect("stable64Read must have two arguments")
.to_js_value()?
.try_into()?;

let mut buf: Vec<u8> = vec![0; length as usize];
ic_cdk::api::stable::stable64_read(offset, &mut buf);
let mut buf: Vec<u8> = vec![0; length_string.parse()?];
ic_cdk::api::stable::stable64_read(offset_string.parse()?, &mut buf);
to_qjs_value(&context, &buf.into())
}
2 changes: 1 addition & 1 deletion src/compiler/rust/canister/src/ic/stable64_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pub fn native_function<'a>(
_this: &CallbackArg,
_args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let return_js_value: JSValue = candid::encode_one(ic_cdk::api::stable::stable64_size())?.into();
let return_js_value: JSValue = ic_cdk::api::stable::stable64_size().to_string().into();
to_qjs_value(&context, &return_js_value)
}
10 changes: 7 additions & 3 deletions src/compiler/rust/canister/src/ic/stable64_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ pub fn native_function<'a>(
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let params_bytes: Vec<u8> = args
let offset_string: String = args
.get(0)
.expect("stable64Write must have two arguments")
.to_js_value()?
.try_into()?;

let (offset, buf): (u64, Vec<u8>) = candid::decode_args(&params_bytes)?;
let buf: Vec<u8> = args
.get(1)
.expect("stable64Write must have two arguments")
.to_js_value()?
.try_into()?;

ic_cdk::api::stable::stable64_write(offset, &buf);
ic_cdk::api::stable::stable64_write(offset_string.parse()?, &buf);

context.undefined_value()
}
8 changes: 4 additions & 4 deletions src/compiler/rust/canister/src/ic/stable_grow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ pub fn native_function<'a>(
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let new_pages_bytes: Vec<u8> = args
let new_pages_string: String = args
.get(0)
.expect("stableGrow must have one argument")
.to_js_value()?
.try_into()?;

let new_pages: u32 = candid::decode_one(&new_pages_bytes)?;
let return_js_value: JSValue = ic_cdk::api::stable::stable_grow(new_pages_string.parse()?)?
.to_string()
.into();

let return_js_value: JSValue =
candid::encode_one(ic_cdk::api::stable::stable_grow(new_pages)?)?.into();
to_qjs_value(&context, &return_js_value)
}
12 changes: 8 additions & 4 deletions src/compiler/rust/canister/src/ic/stable_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ pub fn native_function<'a>(
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let params_bytes: Vec<u8> = args
let offset_string: String = args
.get(0)
.expect("stableRead must have two arguments")
.to_js_value()?
.try_into()?;

let (offset, length): (u32, u32) = candid::decode_args(&params_bytes)?;
let length_string: String = args
.get(1)
.expect("stableRead must have two arguments")
.to_js_value()?
.try_into()?;

let mut buf: Vec<u8> = vec![0; length as usize];
ic_cdk::api::stable::stable_read(offset, &mut buf);
let mut buf: Vec<u8> = vec![0; length_string.parse()?];
ic_cdk::api::stable::stable_read(offset_string.parse()?, &mut buf);

to_qjs_value(&context, &buf.into())
}
2 changes: 1 addition & 1 deletion src/compiler/rust/canister/src/ic/stable_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pub fn native_function<'a>(
_this: &CallbackArg,
_args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let return_js_value: JSValue = candid::encode_one(ic_cdk::api::stable::stable_size())?.into();
let return_js_value: JSValue = ic_cdk::api::stable::stable_size().to_string().into();
to_qjs_value(&context, &return_js_value)
}
10 changes: 7 additions & 3 deletions src/compiler/rust/canister/src/ic/stable_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ pub fn native_function<'a>(
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let params_bytes: Vec<u8> = args
let offset_string: String = args
.get(0)
.expect("stableWrite must have two arguments")
.to_js_value()?
.try_into()?;

let (offset, buf): (u32, Vec<u8>) = candid::decode_args(&params_bytes)?;
let buf: Vec<u8> = args
.get(1)
.expect("stableWrite must have two arguments")
.to_js_value()?
.try_into()?;

ic_cdk::api::stable::stable_write(offset, &buf);
ic_cdk::api::stable::stable_write(offset_string.parse()?, &buf);

context.undefined_value()
}
2 changes: 1 addition & 1 deletion src/compiler/rust/canister/src/ic/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pub fn native_function<'a>(
_this: &CallbackArg,
_args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
let time_js_value: JSValue = candid::encode_one(ic_cdk::api::time())?.into();
let time_js_value: JSValue = ic_cdk::api::time().to_string().into();
to_qjs_value(&context, &time_js_value)
}
5 changes: 2 additions & 3 deletions src/lib/ic/instruction_counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export function instructionCounter(): nat64 {
return undefined as any;
}

const instructionCounterCandidBytes =
globalThis._azleIc.instructionCounter();
return decode(nat64, instructionCounterCandidBytes);
const instructionCounterString = globalThis._azleIc.instructionCounter();
return BigInt(instructionCounterString);
}
10 changes: 3 additions & 7 deletions src/lib/ic/performance_counter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { nat32 } from '../candid/types/primitive/nats/nat32';
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { decode } from '../candid/serde/decode';
import { encode } from '../candid/serde/encode';

/**
* Gets the value of the specified performance counter
Expand All @@ -17,11 +15,9 @@ export function performanceCounter(counterType: nat32): nat64 {
return undefined as any;
}

const counterTypeCandidBytes = encode(nat32, counterType).buffer;

const performanceCounterCandidBytes = globalThis._azleIc.performanceCounter(
counterTypeCandidBytes
const performanceCounterString = globalThis._azleIc.performanceCounter(
counterType.toString()
);

return decode(nat64, performanceCounterCandidBytes);
return BigInt(performanceCounterString);
}
6 changes: 1 addition & 5 deletions src/lib/ic/stable_64_grow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { decode } from '../candid/serde/decode';
import { encode } from '../candid/serde/encode';

/**
* Attempts to grow the stable memory by `newPages`.
Expand All @@ -13,7 +11,5 @@ export function stable64Grow(newPages: nat64): nat64 {
return undefined as any;
}

const newPagesCandidBytes = encode(nat64, newPages).buffer;

return decode(nat64, globalThis._azleIc.stable64Grow(newPagesCandidBytes));
return BigInt(globalThis._azleIc.stable64Grow(newPages.toString()));
}
4 changes: 3 additions & 1 deletion src/lib/ic/stable_64_read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ export function stable64Read(offset: nat64, length: nat64): Uint8Array {

const paramsCandidBytes = encode([nat64, nat64], [offset, length]).buffer;

return new Uint8Array(globalThis._azleIc.stable64Read(paramsCandidBytes));
return new Uint8Array(
globalThis._azleIc.stable64Read(offset.toString(), length.toString())
);
}
2 changes: 1 addition & 1 deletion src/lib/ic/stable_64_size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export function stable64Size(): nat64 {
return undefined as any;
}

return decode(nat64, globalThis._azleIc.stable64Size());
return BigInt(globalThis._azleIc.stable64Size());
}
7 changes: 2 additions & 5 deletions src/lib/ic/stable_64_write.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { blob } from '../candid/types/constructed/blob';
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { encode } from '../candid/serde/encode';

/**
* Writes data to the stable memory location specified by an offset.
Expand All @@ -12,12 +11,10 @@ import { encode } from '../candid/serde/encode';
* @param offset the location at which to write
* @param buffer the data to write
*/
export function stable64Write(offset: nat64, buffer: blob): void {
export function stable64Write(offset: nat64, buf: blob): void {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const paramsCandidBytes = encode([nat64, blob], [offset, buffer]).buffer;

return globalThis._azleIc.stable64Write(paramsCandidBytes);
return globalThis._azleIc.stable64Write(offset.toString(), buf.buffer);
}
9 changes: 1 addition & 8 deletions src/lib/ic/stable_grow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { nat32 } from '../candid/types/primitive/nats/nat32';
import { decode } from '../candid/serde/decode';
import { encode } from '../candid/serde/encode';

/**
* Attempts to grow the stable memory by `newPages`.
Expand All @@ -12,10 +10,5 @@ export function stableGrow(newPages: nat32): nat32 {
return undefined as any;
}

const newPagesCandidBytes = encode(nat32, newPages).buffer;

return decode(
nat32,
globalThis._azleIc.stableGrow(newPagesCandidBytes)
) as number;
return Number(globalThis._azleIc.stableGrow(newPages.toString()));
}
6 changes: 3 additions & 3 deletions src/lib/ic/stable_read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function stableRead(offset: nat32, length: nat32): Uint8Array {
return undefined as any;
}

const paramsCandidBytes = encode([nat32, nat32], [offset, length]).buffer;

return new Uint8Array(globalThis._azleIc.stableRead(paramsCandidBytes));
return new Uint8Array(
globalThis._azleIc.stableRead(offset.toString(), length.toString())
);
}
2 changes: 1 addition & 1 deletion src/lib/ic/stable_size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export function stableSize(): nat32 {
return undefined as any;
}

return decode(nat32, globalThis._azleIc.stableSize()) as number;
return Number(globalThis._azleIc.stableSize());
}
6 changes: 2 additions & 4 deletions src/lib/ic/stable_write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import { encode } from '../candid/serde/encode';
* @param offset the location at which to write
* @param buffer the data to write
*/
export function stableWrite(offset: nat32, buffer: blob): void {
export function stableWrite(offset: nat32, buf: blob): void {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

const paramsCandidBytes = encode([nat32, blob], [offset, buffer]).buffer;

return globalThis._azleIc.stableWrite(paramsCandidBytes);
return globalThis._azleIc.stableWrite(offset.toString(), buf.buffer);
}
3 changes: 1 addition & 2 deletions src/lib/ic/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ export function time(): nat64 {
return undefined as any;
}

const timeCandidBytes = globalThis._azleIc.time();
return decode(nat64, timeCandidBytes);
return BigInt(globalThis._azleIc.time());
}
24 changes: 11 additions & 13 deletions src/lib/ic/types/azle_ic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type AzleIc = {
clearTimer: (timerIdBytes: ArrayBufferLike) => void;
dataCertificate: () => ArrayBufferLike | undefined;
id: () => string;
instructionCounter: () => ArrayBufferLike;
instructionCounter: () => string;
isController: (principalBytes: ArrayBufferLike) => boolean;
msgCyclesAccept: (maxAmountCandidBytes: ArrayBufferLike) => ArrayBufferLike;
msgCyclesAccept128: (
Expand All @@ -44,9 +44,7 @@ export type AzleIc = {
argsRawBuffer: ArrayBufferLike,
paymentCandidBytes: ArrayBufferLike
) => void;
performanceCounter: (
counterTypeCandidBytes: ArrayBufferLike
) => ArrayBufferLike;
performanceCounter: (counterType: string) => string;
rejectCode: () => number;
replyRaw: (bytes: ArrayBufferLike) => void;
setCertifiedData: (dataBytes: ArrayBufferLike) => void;
Expand All @@ -59,15 +57,15 @@ export type AzleIc = {
timerCallbackId: string
) => ArrayBufferLike;
stableBytes: () => ArrayBufferLike;
stableGrow: (newPagesCandidBytes: ArrayBufferLike) => ArrayBufferLike;
stableRead: (paramsCandidBytes: ArrayBufferLike) => ArrayBufferLike;
stableSize: () => ArrayBufferLike;
stableWrite: (paramsCandidBytes: ArrayBufferLike) => void;
stable64Grow: (newPagesCandidBytes: ArrayBufferLike) => ArrayBufferLike;
stable64Read: (paramsCandidBytes: ArrayBufferLike) => ArrayBufferLike;
stable64Size: () => ArrayBufferLike;
stable64Write: (paramsCandidBytes: ArrayBufferLike) => void;
time: () => ArrayBufferLike;
stableGrow: (newPages: string) => string;
stableRead: (offset: string, length: string) => ArrayBufferLike;
stableSize: () => string;
stableWrite: (offset: string, buf: ArrayBufferLike) => void;
stable64Grow: (newPages: string) => string;
stable64Read: (offset: string, length: string) => ArrayBufferLike;
stable64Size: () => string;
stable64Write: (offset: string, buf: ArrayBufferLike) => void;
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
Expand Down

0 comments on commit 2616415

Please sign in to comment.