Skip to content

Commit

Permalink
Merge pull request #1225 from demergent-labs/inspect_message
Browse files Browse the repository at this point in the history
Inspect message
  • Loading branch information
lastmjs authored Sep 10, 2023
2 parents 3fa87dc + 43b0408 commit dae1ed9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
# "examples/guard_functions",
# "examples/ic_api",
# "examples/inline_types",
# "examples/inspect_message",
# "examples/motoko_examples/http_counter",
# "examples/motoko_examples/persistent-storage",
# "examples/motoko_examples/phone-book",
Expand Down Expand Up @@ -165,8 +164,9 @@ jobs:
"examples/cycles",
"examples/date",
"examples/heartbeat",
"examples/init",
"examples/imports",
"examples/init",
"examples/inspect_message",
"examples/key_value_store",
"examples/ledger_canister",
"examples/list_of_lists",
Expand Down
10 changes: 5 additions & 5 deletions examples/inspect_message/src/inspect_message.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
service : () -> {
accessible : () -> (bool);
alsoInaccessible : () -> (bool);
inaccessible : () -> (bool);
}
service: () -> {
accessible: () -> (bool);
inaccessible: () -> (bool);
alsoInaccessible: () -> (bool);
}
50 changes: 26 additions & 24 deletions examples/inspect_message/src/inspect_message.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { ic, $inspectMessage, $update } from 'azle';
import { bool, ic, inspectMessage, Service, update } from 'azle';

$inspectMessage;
export function inspectMessage(): void {
console.log('inspectMessage called');
export default class extends Service {
@inspectMessage
inspectMessage() {
console.log('inspectMessage called');

if (ic.methodName() === 'accessible') {
ic.acceptMessage();
return;
}
if (ic.methodName() === 'accessible') {
ic.acceptMessage();
return;
}

if (ic.methodName() === 'inaccessible') {
return;
}
if (ic.methodName() === 'inaccessible') {
return;
}

throw `Method "${ic.methodName()}" not allowed`;
}
throw `Method "${ic.methodName()}" not allowed`;
}

$update;
export function accessible(): boolean {
return true;
}
@update([], bool)
accessible(): bool {
return true;
}

$update;
export function inaccessible(): boolean {
return false;
}
@update([], bool)
inaccessible(): bool {
return false;
}

$update;
export function alsoInaccessible(): boolean {
return false;
@update([], bool)
alsoInaccessible(): bool {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,23 @@ fn main() -> Result<(), String> {
quote! {
#[ic_cdk_macros::heartbeat]
fn #rust_function_name() {
ic_cdk::println!("inside of the heartbeat");
execute_js(#js_function_name, false);
}
}
});

let inspect_message_method = compiler_info.canister_methods.inspect_message.map(|canister_method| {
let rust_function_name = canister_method.name.to_ident();
let js_function_name = &canister_method.name;

quote! {
#[ic_cdk_macros::inspect_message]
fn #rust_function_name() {
execute_js(#js_function_name, true);
}
}
});

let lib_file = quote! {
#![allow(non_snake_case)]
use quickjs_wasm_rs::{JSContextRef, JSValueRef, JSValue, from_qjs_value, to_qjs_value, CallbackArg};
Expand Down Expand Up @@ -202,6 +213,8 @@ fn main() -> Result<(), String> {

#heartbeat_method

#inspect_message_method

#(#query_methods)*

#(#update_methods)*
Expand Down
36 changes: 34 additions & 2 deletions src/lib_new/method_decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import { serviceCall, serviceDecorator } from './service';

export type Manual<T> = void;

type Mode = 'init' | 'postUpgrade' | 'query' | 'update' | 'heartbeat';
type Mode =
| 'init'
| 'postUpgrade'
| 'query'
| 'update'
| 'heartbeat'
| 'inspectMessage';

const modeToCandid = {
query: ' query',
Expand Down Expand Up @@ -72,6 +78,22 @@ export function heartbeat(
);
}

export function inspectMessage(
target: any,
key: string,
descriptor?: PropertyDescriptor
) {
return setupCanisterMethod(
target,
[],
[],
'inspectMessage',
false,
key,
descriptor
);
}

// Until we can figure how how to type check Funcs, Variants, and Records we are just going to have to use any here
// export function query(paramsIdls: CandidClass[], returnIdl: ReturnCandidClass) {
export function query(
Expand Down Expand Up @@ -210,6 +232,12 @@ function setupCanisterMethod(
};
}

if (mode === 'inspectMessage') {
target.constructor._azleCanisterMethods.inspect_message = {
name: key
};
}

const originalMethod = descriptor.value;

// This must remain a function and not an arrow function
Expand All @@ -229,7 +257,11 @@ function setupCanisterMethod(

const result = originalMethod.apply(this, decoded);

if (mode === 'init' || mode === 'postUpgrade') {
if (
mode === 'init' ||
mode === 'postUpgrade' ||
mode === 'inspectMessage'
) {
return;
}

Expand Down

0 comments on commit dae1ed9

Please sign in to comment.