diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4581a7e962..a7f8fdbbdb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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", @@ -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", diff --git a/examples/inspect_message/src/inspect_message.did b/examples/inspect_message/src/inspect_message.did index 816a2bf576..e731956a3a 100644 --- a/examples/inspect_message/src/inspect_message.did +++ b/examples/inspect_message/src/inspect_message.did @@ -1,5 +1,5 @@ -service : () -> { - accessible : () -> (bool); - alsoInaccessible : () -> (bool); - inaccessible : () -> (bool); -} \ No newline at end of file +service: () -> { + accessible: () -> (bool); + inaccessible: () -> (bool); + alsoInaccessible: () -> (bool); +} diff --git a/examples/inspect_message/src/inspect_message.ts b/examples/inspect_message/src/inspect_message.ts index 7e02271213..dbdc9aa89d 100644 --- a/examples/inspect_message/src/inspect_message.ts +++ b/examples/inspect_message/src/inspect_message.ts @@ -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; + } } diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs index 9a5860960c..a8492c29d3 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs @@ -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}; @@ -202,6 +213,8 @@ fn main() -> Result<(), String> { #heartbeat_method + #inspect_message_method + #(#query_methods)* #(#update_methods)* diff --git a/src/lib_new/method_decorators.ts b/src/lib_new/method_decorators.ts index bfdf906cd1..e671a0a56b 100644 --- a/src/lib_new/method_decorators.ts +++ b/src/lib_new/method_decorators.ts @@ -15,7 +15,13 @@ import { serviceCall, serviceDecorator } from './service'; export type Manual = void; -type Mode = 'init' | 'postUpgrade' | 'query' | 'update' | 'heartbeat'; +type Mode = + | 'init' + | 'postUpgrade' + | 'query' + | 'update' + | 'heartbeat' + | 'inspectMessage'; const modeToCandid = { query: ' query', @@ -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( @@ -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 @@ -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; }