Skip to content

Commit

Permalink
Implement low wasm memory hook
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Nov 1, 2024
1 parent 13d01ed commit 3792fd9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/ic-cdk-macros/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ enum MethodType {
Query,
Heartbeat,
InspectMessage,
OnLowWasmMemory,
}

impl MethodType {
pub fn is_lifecycle(&self) -> bool {
matches!(
self,
match self {
MethodType::Init
| MethodType::PreUpgrade
| MethodType::PostUpgrade
| MethodType::Heartbeat
| MethodType::InspectMessage
)
| MethodType::PreUpgrade
| MethodType::PostUpgrade
| MethodType::Heartbeat
| MethodType::InspectMessage
| MethodType::OnLowWasmMemory => true,
MethodType::Update | MethodType::Query => false,
}
}
}

Expand All @@ -58,6 +60,7 @@ impl std::fmt::Display for MethodType {
MethodType::Update => f.write_str("update"),
MethodType::Heartbeat => f.write_str("heartbeat"),
MethodType::InspectMessage => f.write_str("inspect_message"),
MethodType::OnLowWasmMemory => f.write_str("on_low_wasm_memory"),
}
}
}
Expand Down Expand Up @@ -309,6 +312,13 @@ pub(crate) fn ic_inspect_message(
dfn_macro(MethodType::InspectMessage, attr, item)
}

pub(crate) fn ic_on_low_wasm_memory(
attr: TokenStream,
item: TokenStream,
) -> Result<TokenStream, Error> {
dfn_macro(MethodType::OnLowWasmMemory, attr, item)
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
10 changes: 10 additions & 0 deletions src/ic-cdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ pub fn heartbeat(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn inspect_message(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(export::ic_inspect_message, "ic_inspect_message", attr, item)
}

#[proc_macro_attribute]
pub fn on_low_wasm_memory(attr: TokenStream, item: TokenStream) -> TokenStream {
handle_debug_and_errors(
export::ic_on_low_wasm_memory,
"ic_on_low_wasm_memory",
attr,
item,
)
}
21 changes: 21 additions & 0 deletions src/ic-cdk/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,24 @@ pub use ic_cdk_macros::heartbeat;
/// }
/// ```
pub use ic_cdk_macros::inspect_message;

/// Register the `canister_on_low_wasm_memory` entry point of a canister.
///
/// This attribute macro will export the function `canister_on_low_wasm_memory`
/// in the canister module.
///
/// The function under this attribute must have no return value.
///
/// Each canister can only have one `canister_on_low_wasm_memory` entry point.
///
/// # Example
///
/// ```rust
/// # use ic_cdk::on_low_wasm_memory;
/// #[on_low_wasm_memory]
/// fn low_memory_handler() {
/// // ...
/// # unimplemented!()
/// }
/// ```
pub use ic_cdk_macros::on_low_wasm_memory;
7 changes: 6 additions & 1 deletion src/ic-cdk/tests/pass/blank_methods.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use ic_cdk::{heartbeat, init, inspect_message, post_upgrade, pre_upgrade, query, update};
use ic_cdk::{
heartbeat, init, inspect_message, on_low_wasm_memory, post_upgrade, pre_upgrade, query, update,
};

#[init]
fn init() {}
Expand Down Expand Up @@ -30,4 +32,7 @@ fn heartbeat() {}
#[inspect_message]
fn inspect_message() {}

#[on_low_wasm_memory]
fn on_low_wasm_memory() {}

fn main() {}

0 comments on commit 3792fd9

Please sign in to comment.