Skip to content

Commit

Permalink
ic apis return default values instead of undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jul 10, 2024
1 parent cfee35b commit 44ff71b
Show file tree
Hide file tree
Showing 78 changed files with 172 additions and 202 deletions.
5 changes: 4 additions & 1 deletion src/compiler/rust/canister/src/ic/reply_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ impl JsFn for NativeFunction {

match arg_0_js_value {
JsValue::ArrayBuffer(js_array_buffer) => {
ic_cdk::api::print(format!("hello world"));
let buf = js_array_buffer.to_vec();

ic_cdk::api::call::reply_raw(&buf);
}
_ => {}
_ => {
ic_cdk::api::print(format!("hello from the other side"));
}
};

JsValue::UnDefined
Expand Down
6 changes: 3 additions & 3 deletions src/lib/ic/arg_data_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { blob } from '../candid/types/constructed/blob';
* @returns the argument data
*/
export function argDataRaw(): blob {
return globalThis._azleIc
? new Uint8Array(globalThis._azleIc.argDataRaw())
: (undefined as any);
return new Uint8Array(
globalThis._azleIc ? globalThis._azleIc.argDataRaw() : []
);
}
4 changes: 1 addition & 3 deletions src/lib/ic/arg_data_raw_size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ import { nat32 } from '../candid/types/primitive/nats/nat32';
* @returns the data size
*/
export function argDataRawSize(): nat32 {
return globalThis._azleIc
? Number(globalThis._azleIc.argDataRawSize())
: (undefined as any);
return globalThis._azleIc ? Number(globalThis._azleIc.argDataRawSize()) : 0;
}
2 changes: 1 addition & 1 deletion src/lib/ic/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function call<T extends (...args: any[]) => any>(
}
): ReturnTypeOf<T> {
if (globalThis._azleIc === undefined) {
return undefined as any;
return {} as any;
}

const { callFunction, cycles } = getCallFunctionAndCycles(config?.cycles);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ic/call_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export function callRaw(
payment: nat
): Promise<blob> {
if (globalThis._azleIc === undefined) {
return undefined as any;
return new Promise(() => new Uint8Array());
}

// TODO this should use a Result remember
return new Promise((resolve, reject) => {
if (globalThis._azleIc === undefined) {
return undefined as any;
return new Uint8Array();
}

const promiseId = v4();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Principal } from '../candid/types/reference/principal';
*/
export function caller(): Principal {
if (globalThis._azleIc === undefined) {
return undefined as any;
return Principal.fromHex('0x04');
}

const callerBytes = globalThis._azleIc.caller();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/candid_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { text } from '../candid/types/primitive/text';
*/
export function candidCompiler(candidPath: text): string {
if (globalThis._azleIc === undefined) {
return undefined as any;
return '';
}

return globalThis._azleIc.candidCompiler(candidPath);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/candid_decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { text } from '../candid/types/primitive/text';
*/
export function candidDecode(candidEncoded: blob): text {
if (globalThis._azleIc === undefined) {
return undefined as any;
return '';
}

return globalThis._azleIc.candidDecode(candidEncoded.buffer);
Expand Down
8 changes: 3 additions & 5 deletions src/lib/ic/candid_encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { text } from '../candid/types/primitive/text';
* @returns the candid value as bytes
*/
export function candidEncode(candidString: text): blob {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

return new Uint8Array(globalThis._azleIc.candidEncode(candidString));
return new Uint8Array(
globalThis._azleIc ? globalThis._azleIc.candidEncode(candidString) : []
);
}
2 changes: 1 addition & 1 deletion src/lib/ic/canister_balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function canisterBalance(): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

return BigInt(globalThis._azleIc.canisterBalance());
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/canister_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function canisterVersion(): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

return BigInt(globalThis._azleIc.canisterVersion());
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ic } from './';

export async function chunk(): Promise<void> {
if (globalThis._azleIc === undefined) {
return undefined as any;
return undefined;
}

await ic.callRaw(ic.id(), '_azle_chunk', ic.candidEncode('()'), 0n);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/clear_timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Void } from '../candid/types/primitive/void';
*/
export function clearTimer(timerId: bigint): Void {
if (globalThis._azleIc === undefined) {
return undefined as any;
return undefined;
}

globalThis._azleIc.clearTimer(timerId.toString());
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/data_certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { None, Opt, Some } from '../candid/types/constructed/opt';
*/
export function dataCertificate(): Opt<blob> {
if (globalThis._azleIc === undefined) {
return undefined as any;
return None;
}

const rawRustValue = globalThis._azleIc.dataCertificate();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Principal } from '../candid/types/reference/principal';
*/
export function id(): Principal {
if (globalThis._azleIc === undefined) {
return undefined as any;
return Principal.fromHex('0x04');
}

// TODO consider bytes instead of string, just like with caller
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/instruction_counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function instructionCounter(): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

return BigInt(globalThis._azleIc.instructionCounter());
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/is_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Principal } from '../candid/types/reference/principal';
/** Determine if a {@link Principal} is a controller of the canister. */
export function isController(principal: Principal): bool {
if (globalThis._azleIc === undefined) {
return undefined as any;
return false;
}

return globalThis._azleIc.isController(principal.toUint8Array().buffer);
Expand Down
4 changes: 1 addition & 3 deletions src/lib/ic/method_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ import { text } from '../candid/types/primitive/text';
* @returns the current canister method
*/
export function methodName(): text {
return globalThis._azleIc
? globalThis._azleIc.methodName()
: (undefined as any);
return globalThis._azleIc ? globalThis._azleIc.methodName() : '';
}
2 changes: 1 addition & 1 deletion src/lib/ic/msg_cycles_accept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function msgCyclesAccept(maxAmount: nat64): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

const msgCyclesAcceptAmountMovedString = globalThis._azleIc.msgCyclesAccept(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/msg_cycles_available.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function msgCyclesAvailable(): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

const msgCyclesAvailableAmountString =
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/msg_cycles_refunded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function msgCyclesRefunded(): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

const msgCyclesRefundedAmountString =
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function notify<T extends (...args: any[]) => any>(
}
): Void {
if (globalThis._azleIc === undefined) {
return undefined as any;
return undefined;
}

return method(true, notifyRaw, config?.cycles ?? 0n, config?.args ?? []);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/notify_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function notifyRaw(
payment: nat
): Void {
if (globalThis._azleIc === undefined) {
return undefined as any;
return undefined;
}

const canisterIdBytes = canisterId.toUint8Array().buffer;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/performance_counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function performanceCounter(counterType: nat32): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

return BigInt(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/reject_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RejectionCode } from '../system_types';
*/
export function rejectCode(): RejectionCode {
if (globalThis._azleIc === undefined) {
return undefined as any;
return { Unknown: null };
}

const rejectCodeNumber = Number(globalThis._azleIc.rejectCode());
Expand Down
4 changes: 1 addition & 3 deletions src/lib/ic/reject_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ import { text } from '../candid/types/primitive/text';
* @returns the rejection message
*/
export function rejectMessage(): text {
return globalThis._azleIc
? globalThis._azleIc.rejectMessage()
: (undefined as any);
return globalThis._azleIc ? globalThis._azleIc.rejectMessage() : '';
}
4 changes: 2 additions & 2 deletions src/lib/ic/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { Void } from '../candid/types/primitive/void';
/**
* Used to manually reply to an ingress message. Intended to be used in
* canister methods with a {@link Manual} return type.
* @param reply the value with which to reply. Must by of type `T` where `T`
* @param reply the value with which to reply. Must be of type `T` where `T`
* is the generic type supplied to `Manual<T>`. Otherwise will result in an
* uncaught `TypeError`.
*/
export function reply(data: any, type: CandidType): Void {
if (globalThis._azleIc === undefined) {
return undefined as any;
return undefined;
}

return globalThis._azleIc.replyRaw(encode(type, data).buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/set_certified_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { Void } from '../candid/types/primitive/void';
*/
export function setCertifiedData(data: blob): Void {
if (globalThis._azleIc === undefined) {
return undefined as any;
return undefined;
}

return globalThis._azleIc.setCertifiedData(data.buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/set_timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function setTimer(
callback: () => void | Promise<void>
): bigint {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

const timerCallbackId = `_timer_${v4()}`;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/set_timer_interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function setTimerInterval(
callback: () => void | Promise<void>
): bigint {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

const timerCallbackId = `_interval_timer_${v4()}`;
Expand Down
8 changes: 3 additions & 5 deletions src/lib/ic/stable_bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { blob } from '../candid/types/constructed/blob';
* @returns a copy of the stable memory
*/
export function stableBytes(): blob {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

return new Uint8Array(globalThis._azleIc.stableBytes());
return new Uint8Array(
globalThis._azleIc ? globalThis._azleIc.stableBytes() : []
);
}
3 changes: 2 additions & 1 deletion src/lib/ic/stable_grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Attempts to grow the stable memory by `newPages`.
* Supports 64-bit addressed memory.
* @param newPages
* @returns the previous size that was reserved.
*/
export function stableGrow(newPages: nat64): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

return BigInt(globalThis._azleIc.stableGrow(newPages.toString()));
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ic/stable_read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Reads data from the stable memory location specified by an offset
* Supports 64-bit addressed memory.
* @param offset the location from which to read
* @param length the length of buffer to read
* @returns the raw bytes in stable memory
*/
export function stableRead(offset: nat64, length: nat64): Uint8Array {
if (globalThis._azleIc === undefined) {
return undefined as any;
return new Uint8Array();
}

return new Uint8Array(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ic/stable_size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Gets current size of the stable memory (in WASM pages)
* Supports 64-bit addressed memory.
* @returns the current memory size
*/
export function stableSize(): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

return BigInt(globalThis._azleIc.stableSize());
Expand Down
5 changes: 3 additions & 2 deletions src/lib/ic/stable_write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { blob } from '../candid/types/constructed/blob';
import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Writes data to the stable memory location specified by an offset
* Writes data to the stable memory location specified by an offset.
* Supports 64-bit addressed memory.
*
* **Warning:** this will panic if `offset` + `buffer.length` exceeds the
* current size of stable memory. Use {@link ic.stableGrow} to request more
Expand All @@ -12,7 +13,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function stableWrite(offset: nat64, buf: blob): void {
if (globalThis._azleIc === undefined) {
return undefined as any;
return undefined;
}

return globalThis._azleIc.stableWrite(offset.toString(), buf.buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
*/
export function time(): nat64 {
if (globalThis._azleIc === undefined) {
return undefined as any;
return 0n;
}

return BigInt(globalThis._azleIc.time());
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { text } from '../candid/types/primitive/text';
*/
export function trap(message: text): empty {
if (globalThis._azleIc === undefined) {
return undefined as never;
throw new Error();
}

return globalThis._azleIc.trap(message);
Expand Down
Loading

0 comments on commit 44ff71b

Please sign in to comment.