Skip to main content

nifty_rt/abi/
mod.rs

1//! The calls compiled code makes into the runtime, exported unmangled as `nf_*`.
2//!
3//! Ownership follows one rule: a call takes ownership of the values passed to it unless
4//! its parameter is a borrowed `Slot`, documented as such, and gives ownership of the
5//! values it returns, except getters, whose results are borrowed from their container.
6//! A call that can fail returns `false` after recording why in the context; compiled code
7//! then returns `false` itself, up to the runtime.
8//!
9//! Every pointer passed in must be valid for the access the call makes, every `Slot` must
10//! hold a value of the type the call expects, and every `context` must be the one the
11//! runtime passed into the compiled code making the call. `nf build` upholds this for the
12//! code it emits, which `nifty_rt_main`'s caller vouches for.
13
14mod lists;
15mod numbers;
16mod state;
17mod strings;
18mod structs;
19mod values;
20
21use crate::context::Context;
22
23/// # Safety
24/// `context` is the context the runtime passed into the compiled code calling.
25pub(crate) unsafe fn borrow_context<'a>(context: *const Context) -> &'a Context {
26    // SAFETY: by the caller's contract.
27    unsafe { &*context }
28}
29
30/// A list's length, or a string's, as an `int`. Allocations never exceed `isize::MAX`
31/// bytes, so the length always fits.
32fn count(len: usize) -> i64 {
33    i64::try_from(len).unwrap_or(i64::MAX)
34}
35
36pub(crate) use lists::*;
37pub(crate) use numbers::*;
38pub(crate) use state::*;
39pub(crate) use strings::*;
40pub(crate) use structs::*;
41pub(crate) use values::*;