Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console log #1487

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions property_tests/arbitraries/stable_b_tree_map_arb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,76 @@ import { CandidValueAndMetaArb } from './candid/candid_value_and_meta_arb';
export const StableBTreeMapArb = fc
.tuple(
CandidValueAndMetaArb(),
argumentInfoArb(),
CandidValueAndMetaArb(),
argumentInfoArb(),
UniqueIdentifierArb('stableBTreeMap'),
createUniquePrimitiveArb(
fc.nat({
max: 254
})
)
)
.map(([keySample, valueSample, uniqueIdentifier, memoryId]) => {
const name = `stableBTreeMap${uniqueIdentifier}`;

const imports = new Set([
...keySample.src.imports,
...valueSample.src.imports,
'stableJson',
'StableBTreeMap'
]);

return {
name,
imports,
definition: `let ${name} = StableBTreeMap<${keySample.src.candidTypeAnnotation}, ${valueSample.src.candidTypeAnnotation}>(${memoryId});`,
.map(
([
keySample,
valueSample
};
});
keyArgumentInfo,
valueSample,
valueArgumentInfo,
uniqueIdentifier,
memoryId
]) => {
const name = `stableBTreeMap${uniqueIdentifier}`;

const imports = new Set([
...keySample.src.imports,
...valueSample.src.imports,
'stableJson',
'StableBTreeMap'
]);

const serializableArguments = getSerializableArguments(
keySample,
valueSample,
keyArgumentInfo,
valueArgumentInfo
);

return {
name,
imports,
definition: `let ${name} = StableBTreeMap<${keySample.src.candidTypeAnnotation}, ${valueSample.src.candidTypeAnnotation}>(${memoryId}${serializableArguments});`,
keySample,
valueSample
};
}
);

// TODO there must be a better way to get this type out, the sample will actually run
export const StableBTreeMap = fc.sample(StableBTreeMapArb)[0];
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'
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
): string {
const keyArgument =
keyArgumentInfo === 'STABLE_JSON'
? 'stableJson'
: keySample.src.candidTypeObject;
const valueArgument =
valueArgumentInfo == 'STABLE_JSON'
? 'stableJson'
: valueSample.src.candidTypeObject;

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

function argumentInfoArb(): fc.Arbitrary<'STABLE_JSON' | 'CANDID_TYPE_OBJECT'> {
return fc.oneof(
fc.constant<'STABLE_JSON'>('STABLE_JSON'),
fc.constant<'CANDID_TYPE_OBJECT'>('CANDID_TYPE_OBJECT')
);
}
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/contains_key.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';

export function ContainsKeyTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/get.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function GetTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/insert.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function InsertTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/is_empty.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function IsEmptyTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/items.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function ItemsTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/keys.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { getArrayForCandidType, getArrayStringForCandidType } from './utils';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function KeysTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/len.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function LenTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/remove.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function RemoveTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
14 changes: 7 additions & 7 deletions property_tests/tests/stable_b_tree_map/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fc from 'fast-check';

import { runPropTests } from '../../../../property_tests';
import { CanisterArb } from '../../../arbitraries/canister_arb';
import { StableBTreeMapArb } from '../../../arbitraries/stable_b_tree_map_arb';
import { runPropTests } from 'azle/property_tests';
import { CanisterArb } from 'azle/property_tests/arbitraries/canister_arb';
import { StableBTreeMapArb } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { ContainsKeyTestArb } from './contains_key';
import { GetTestArb } from './get';
import { IsEmptyTestArb } from './is_empty';
Expand Down Expand Up @@ -42,11 +42,11 @@ const StableBTreeMapTestArb = fc
]) => {
return {
globalDeclarations: [
stableBTreeMap.definition,
...stableBTreeMap.keySample.src
.variableAliasDeclarations,
...stableBTreeMap.valueSample.src
.variableAliasDeclarations
.variableAliasDeclarations,
stableBTreeMap.definition
],
queryMethods: [],
updateMethods: [
Expand All @@ -65,8 +65,8 @@ const StableBTreeMapTestArb = fc
);
}),
{
minLength: 1,
maxLength: 50
minLength: 20,
maxLength: 100
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
}
)
.map((canisterConfigs) => {
Expand Down
10 changes: 5 additions & 5 deletions property_tests/tests/stable_b_tree_map/test/values.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fc from 'fast-check';
import { deepEqual } from 'fast-equals';

import { StableBTreeMap } from '../../../arbitraries/stable_b_tree_map_arb';
import { getActor } from '../../../../property_tests';
import { Test } from '../../../../test';
import { StableBTreeMap } from 'azle/property_tests/arbitraries/stable_b_tree_map_arb';
import { getActor } from 'azle/property_tests';
import { Test } from 'azle/test';
import { getArrayForCandidType, getArrayStringForCandidType } from './utils';
import { UniqueIdentifierArb } from '../../../arbitraries/unique_identifier_arb';
import { QueryMethod } from '../../../arbitraries/canister_methods/query_method_arb';
import { UniqueIdentifierArb } from 'azle/property_tests/arbitraries/unique_identifier_arb';
import { QueryMethod } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb';

export function ValuesTestArb(stableBTreeMap: StableBTreeMap) {
return fc
Expand Down
14 changes: 10 additions & 4 deletions src/compiler/rust/canister/src/ic/print.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
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 string_to_print: String = args
.get(0)
.expect("console.log must have at least one argument")
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
.to_js_value()?
.try_into()?;

ic_cdk::print(string_to_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(', ');
lastmjs marked this conversation as resolved.
Show resolved Hide resolved

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
Loading