Skip to main content

leanfran_runtime/
lib.rs

1//! Runtime of the Rust code generated from Lean IR by leanfran: the object model of `lean.h`
2//! and the `lean_*` functions of the C runtime, under their C names.
3//!
4//! # Safety
5//!
6//! Every function follows the calling convention of its C counterpart: object arguments are
7//! consumed unless the C declaration borrows them (`b_lean_obj_arg`, `@&` in Lean), results
8//! are owned unless documented as borrowed, and every `Obj` must be a valid Lean value of the
9//! type the function expects. Objects use single-threaded reference counts and must stay on
10//! the thread that created them.
11
12#![allow(clippy::missing_safety_doc, clippy::too_many_arguments, non_snake_case)]
13
14pub mod array;
15pub mod closure;
16pub mod float;
17pub mod fs;
18pub mod handle;
19pub mod int;
20pub mod io;
21pub mod io_error;
22pub mod marshal;
23pub mod model;
24pub mod nat;
25pub mod object;
26pub mod panic;
27pub mod process;
28pub mod string;
29pub mod thunk;
30pub mod uint;
31
32pub use array::*;
33pub use closure::*;
34pub use float::*;
35pub use fs::*;
36pub use handle::*;
37pub use int::*;
38pub use io::*;
39pub use io_error::*;
40pub use marshal::*;
41pub use model::*;
42pub use nat::*;
43pub use object::*;
44pub use panic::*;
45pub use process::*;
46pub use string::*;
47pub use thunk::*;
48pub use uint::*;
49
50/// `System.Platform.getNumBits`: the word size of the target.
51pub unsafe fn lean_system_platform_nbits(_unit: Obj) -> Obj {
52    lean_box(usize::BITS as usize)
53}
54
55/// `System.Platform.getIsWindows`.
56pub unsafe fn lean_system_platform_windows(_unit: Obj) -> u8 {
57    cfg!(windows) as u8
58}
59
60/// `System.Platform.getIsOSX`.
61pub unsafe fn lean_system_platform_osx(_unit: Obj) -> u8 {
62    cfg!(target_os = "macos") as u8
63}
64
65/// `System.Platform.getIsLinux`.
66pub unsafe fn lean_system_platform_linux(_unit: Obj) -> u8 {
67    cfg!(target_os = "linux") as u8
68}
69
70/// `System.Platform.getIsEmscripten`.
71pub unsafe fn lean_system_platform_emscripten(_unit: Obj) -> u8 {
72    cfg!(target_os = "emscripten") as u8
73}
74
75/// `System.Platform.getTarget`: the target triple the runtime was compiled for.
76pub unsafe fn lean_system_platform_target(_unit: Obj) -> Obj {
77    lean_mk_string(env!("LEANFRAN_TARGET"))
78}
79
80/// `System.Platform.Internal.getHardwareConcurrency`: the number of hardware threads, or 0.
81pub unsafe fn lean_internal_get_hardware_concurrency(_unit: Obj) -> u32 {
82    std::thread::available_parallelism().map_or(0, |n| n.get() as u32)
83}
84
85/// `ptrAddrUnsafe`.
86pub unsafe fn lean_ptr_addr_unsafe(a: Obj) -> usize {
87    lean_ptr_addr(a)
88}
89
90include!(concat!(env!("OUT_DIR"), "/externs.rs"));
91
92/// The Lean source of `LeanRust/RuntimeExterns.lean`: [`EXTERNS`] as `runtimeExterns`.
93#[doc(hidden)]
94pub fn runtime_externs_lean() -> String {
95    let mut s = String::from(
96        "/-! C names of the functions the runtime `leanfran-runtime` provides. Generated by `make gen-externs`. -/\n\nnamespace LeanRust\n\ndef runtimeExterns : Array String := #[\n",
97    );
98    let items: Vec<String> = EXTERNS.iter().map(|n| format!("  \"{n}\"")).collect();
99    s.push_str(&items.join(",\n"));
100    s.push_str("\n]\n\nend LeanRust\n");
101    s
102}