Skip to main content

nifty_rt/abi/
state.rs

1//! The app's state, calls between functions, and failures compiled code detects itself.
2
3use nifty_runtime::Failure;
4
5use super::borrow_context;
6use crate::context::Context;
7use crate::value::Slot;
8
9/// The value of state `state`, borrowed from the app.
10///
11/// # Safety
12/// See the module's contract.
13#[unsafe(no_mangle)]
14pub(crate) unsafe extern "C" fn nf_state(context: *const Context, state: u32) -> Slot {
15    // SAFETY: by the caller's contract.
16    unsafe { borrow_context(context) }.state(state as usize)
17}
18
19/// The slot of state `state`, marked changed, for compiled code to write.
20///
21/// # Safety
22/// See the module's contract.
23#[unsafe(no_mangle)]
24pub(crate) unsafe extern "C" fn nf_state_mut(context: *const Context, state: u32) -> *mut Slot {
25    // SAFETY: by the caller's contract.
26    unsafe { borrow_context(context) }.state_mut(state as usize)
27}
28
29/// Counts a call into function `function` from the call at `span`, failing when calls
30/// are too deep. Each successful call is matched by `nf_leave`.
31///
32/// # Safety
33/// See the module's contract.
34#[unsafe(no_mangle)]
35pub(crate) unsafe extern "C" fn nf_enter(
36    context: *const Context,
37    function: u32,
38    span: u32,
39) -> bool {
40    // SAFETY: by the caller's contract.
41    unsafe { borrow_context(context) }.enter(function, span)
42}
43
44/// # Safety
45/// See the module's contract.
46#[unsafe(no_mangle)]
47pub(crate) unsafe extern "C" fn nf_leave(context: *const Context) {
48    // SAFETY: by the caller's contract.
49    unsafe { borrow_context(context) }.leave();
50}
51
52/// # Safety
53/// See the module's contract.
54#[unsafe(no_mangle)]
55pub(crate) unsafe extern "C" fn nf_fail_divide_by_zero(context: *const Context, span: u32) {
56    // SAFETY: by the caller's contract.
57    unsafe { borrow_context(context) }.fail(Failure::DivideByZero, span);
58}
59
60/// # Safety
61/// See the module's contract.
62#[unsafe(no_mangle)]
63pub(crate) unsafe extern "C" fn nf_fail_too_large(context: *const Context, span: u32) {
64    // SAFETY: by the caller's contract.
65    unsafe { borrow_context(context) }.fail(Failure::TooLarge, span);
66}