Skip to content

Commit 9a65c41

Browse files
feat: Implement low wasm memory hook (#528)
1 parent 13d01ed commit 9a65c41

File tree

6 files changed

+56
-8
lines changed

6 files changed

+56
-8
lines changed

src/ic-cdk-macros/src/export.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ enum MethodType {
3333
Query,
3434
Heartbeat,
3535
InspectMessage,
36+
OnLowWasmMemory,
3637
}
3738

3839
impl MethodType {
3940
pub fn is_lifecycle(&self) -> bool {
40-
matches!(
41-
self,
41+
match self {
4242
MethodType::Init
43-
| MethodType::PreUpgrade
44-
| MethodType::PostUpgrade
45-
| MethodType::Heartbeat
46-
| MethodType::InspectMessage
47-
)
43+
| MethodType::PreUpgrade
44+
| MethodType::PostUpgrade
45+
| MethodType::Heartbeat
46+
| MethodType::InspectMessage
47+
| MethodType::OnLowWasmMemory => true,
48+
MethodType::Update | MethodType::Query => false,
49+
}
4850
}
4951
}
5052

@@ -58,6 +60,7 @@ impl std::fmt::Display for MethodType {
5860
MethodType::Update => f.write_str("update"),
5961
MethodType::Heartbeat => f.write_str("heartbeat"),
6062
MethodType::InspectMessage => f.write_str("inspect_message"),
63+
MethodType::OnLowWasmMemory => f.write_str("on_low_wasm_memory"),
6164
}
6265
}
6366
}
@@ -309,6 +312,13 @@ pub(crate) fn ic_inspect_message(
309312
dfn_macro(MethodType::InspectMessage, attr, item)
310313
}
311314

315+
pub(crate) fn ic_on_low_wasm_memory(
316+
attr: TokenStream,
317+
item: TokenStream,
318+
) -> Result<TokenStream, Error> {
319+
dfn_macro(MethodType::OnLowWasmMemory, attr, item)
320+
}
321+
312322
#[cfg(test)]
313323
mod test {
314324
use super::*;

src/ic-cdk-macros/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ pub fn heartbeat(attr: TokenStream, item: TokenStream) -> TokenStream {
9191
pub fn inspect_message(attr: TokenStream, item: TokenStream) -> TokenStream {
9292
handle_debug_and_errors(export::ic_inspect_message, "ic_inspect_message", attr, item)
9393
}
94+
95+
#[proc_macro_attribute]
96+
pub fn on_low_wasm_memory(attr: TokenStream, item: TokenStream) -> TokenStream {
97+
handle_debug_and_errors(
98+
export::ic_on_low_wasm_memory,
99+
"ic_on_low_wasm_memory",
100+
attr,
101+
item,
102+
)
103+
}

src/ic-cdk/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212

1313
### Added
1414

15+
- Attribute `#[on_low_wasm_memory]` for low-memory hook. (#528)
1516
- Support Threshold Schnorr signing management canister API. (#518)
1617

1718
## [0.16.0] - 2024-08-27

src/ic-cdk/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ These macros are directly related to the [Internet Computer Specification](https
5858
* [`post_upgrade`](https://docs.rs/ic-cdk/latest/ic_cdk/attr.post_upgrade.html)
5959
* [`inspect_message`](https://docs.rs/ic-cdk/latest/ic_cdk/attr.inspect_message.html)
6060
* [`heartbeat`](https://docs.rs/ic-cdk/latest/ic_cdk/attr.heartbeat.html)
61+
* [`on_low_wasm_memory`](https://docs.rs/ic-cdk/latest/ic_cdk/attr.on_low_wasm_memory.html)
6162
* [`update`](https://docs.rs/ic-cdk/latest/ic_cdk/attr.update.html)
6263
* [`query`](https://docs.rs/ic-cdk/latest/ic_cdk/attr.query.html)
6364

src/ic-cdk/src/macros.rs

+21
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,24 @@ pub use ic_cdk_macros::heartbeat;
313313
/// }
314314
/// ```
315315
pub use ic_cdk_macros::inspect_message;
316+
317+
/// Register the `canister_on_low_wasm_memory` entry point of a canister.
318+
///
319+
/// This attribute macro will export the function `canister_on_low_wasm_memory`
320+
/// in the canister module.
321+
///
322+
/// The function under this attribute must have no return value.
323+
///
324+
/// Each canister can only have one `canister_on_low_wasm_memory` entry point.
325+
///
326+
/// # Example
327+
///
328+
/// ```rust
329+
/// # use ic_cdk::on_low_wasm_memory;
330+
/// #[on_low_wasm_memory]
331+
/// fn low_memory_handler() {
332+
/// // ...
333+
/// # unimplemented!()
334+
/// }
335+
/// ```
336+
pub use ic_cdk_macros::on_low_wasm_memory;

src/ic-cdk/tests/pass/blank_methods.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use ic_cdk::{heartbeat, init, inspect_message, post_upgrade, pre_upgrade, query, update};
1+
use ic_cdk::{
2+
heartbeat, init, inspect_message, on_low_wasm_memory, post_upgrade, pre_upgrade, query, update,
3+
};
24

35
#[init]
46
fn init() {}
@@ -30,4 +32,7 @@ fn heartbeat() {}
3032
#[inspect_message]
3133
fn inspect_message() {}
3234

35+
#[on_low_wasm_memory]
36+
fn on_low_wasm_memory() {}
37+
3338
fn main() {}

0 commit comments

Comments
 (0)