Skip to content

Commit

Permalink
Merge pull request #1487 from demergent-labs/console_log
Browse files Browse the repository at this point in the history
Console log
  • Loading branch information
lastmjs authored Dec 8, 2023
2 parents 20bc87f + e1e0522 commit 9850532
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
20 changes: 11 additions & 9 deletions property_tests/arbitraries/stable_b_tree_map_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { UniqueIdentifierArb } from './unique_identifier_arb';
import { createUniquePrimitiveArb } from './unique_primitive_arb';
import { CandidValueAndMetaArb } from './candid/candid_value_and_meta_arb';

type SerializableType = 'STABLE_JSON' | 'CANDID_TYPE_OBJECT';

export const StableBTreeMapArb = fc
.tuple(
CandidValueAndMetaArb(),
Expand All @@ -19,9 +21,9 @@ export const StableBTreeMapArb = fc
.map(
([
keySample,
keyArgumentInfo,
keySerializableType,
valueSample,
valueArgumentInfo,
valueSerializableType,
uniqueIdentifier,
memoryId
]) => {
Expand All @@ -37,8 +39,8 @@ export const StableBTreeMapArb = fc
const serializableArguments = getSerializableArguments(
keySample,
valueSample,
keyArgumentInfo,
valueArgumentInfo
keySerializableType,
valueSerializableType
);

return {
Expand All @@ -58,22 +60,22 @@ export type StableBTreeMap = typeof StableBTreeMap;
function getSerializableArguments(
keySample: StableBTreeMap['keySample'],
valueSample: StableBTreeMap['valueSample'],
keyArgumentInfo: 'STABLE_JSON' | 'CANDID_TYPE_OBJECT',
valueArgumentInfo: 'STABLE_JSON' | 'CANDID_TYPE_OBJECT'
keySerializableType: SerializableType,
valueSerializableType: SerializableType
): string {
const keyArgument =
keyArgumentInfo === 'STABLE_JSON'
keySerializableType === 'STABLE_JSON'
? 'stableJson'
: keySample.src.candidTypeObject;
const valueArgument =
valueArgumentInfo == 'STABLE_JSON'
valueSerializableType == 'STABLE_JSON'
? 'stableJson'
: valueSample.src.candidTypeObject;

return `, ${keyArgument}, ${valueArgument}`;
}

function argumentInfoArb(): fc.Arbitrary<'STABLE_JSON' | 'CANDID_TYPE_OBJECT'> {
function argumentInfoArb(): fc.Arbitrary<SerializableType> {
return fc.oneof(
fc.constant<'STABLE_JSON'>('STABLE_JSON'),
fc.constant<'CANDID_TYPE_OBJECT'>('CANDID_TYPE_OBJECT')
Expand Down
13 changes: 10 additions & 3 deletions src/compiler/rust/canister/src/ic/print.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use std::convert::TryInto;

use quickjs_wasm_rs::{CallbackArg, JSContextRef, JSValueRef};

pub fn native_function<'a>(
context: &'a JSContextRef,
_this: &CallbackArg,
args: &[CallbackArg],
) -> Result<JSValueRef<'a>, anyhow::Error> {
for arg in args {
let value = arg.to_js_value()?;
ic_cdk::println!("{:?} ", value);
let first_arg_option = args.get(0);

if let Some(first_arg) = first_arg_option {
let string_to_print: String = first_arg.to_js_value()?.try_into()?;
ic_cdk::print(string_to_print);
} else {
ic_cdk::print("");
}

context.undefined_value()
}
21 changes: 14 additions & 7 deletions src/lib/globals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ic } from './ic';
import { AzleIc } from './ic/types/azle_ic';
import { Buffer } from 'buffer';
import { replacer } from './stable_structures/stable_json';

declare global {
var _azleIc: AzleIc | undefined;
Expand All @@ -11,6 +12,19 @@ declare global {
var _azleGuardFunctions: { [key: string]: () => any };
}

if (globalThis._azleIc) {
globalThis.console = {
...globalThis.console,
log: (...args: any[]) => {
const jsonStringifiedArgs = args
.map((arg) => JSON.stringify(arg, replacer, 4))
.join(' ');

ic.print(jsonStringifiedArgs);
}
};
}

globalThis.TextDecoder = require('text-encoding').TextDecoder;
globalThis.TextEncoder = require('text-encoding').TextEncoder;
globalThis._azleIcTimers = {};
Expand All @@ -19,13 +33,6 @@ globalThis._azleRejectIds = {};
globalThis._azleTimerCallbacks = {};
globalThis._azleGuardFunctions = {};

globalThis.console = {
...globalThis.console,
log: (...args: any[]) => {
ic.print(...args);
}
};

// TODO be careful we are using a random seed of 0 I think
// TODO the randomness is predictable
globalThis.crypto = {
Expand Down

0 comments on commit 9850532

Please sign in to comment.