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 int;
19pub mod io;
20pub mod io_error;
21pub mod marshal;
22pub mod model;
23pub mod nat;
24pub mod object;
25pub mod panic;
26pub mod process;
27pub mod string;
28pub mod thunk;
29pub mod uint;
30
31pub use array::*;
32pub use closure::*;
33pub use float::*;
34pub use fs::*;
35pub use int::*;
36pub use io::*;
37pub use io_error::*;
38pub use marshal::*;
39pub use model::*;
40pub use nat::*;
41pub use object::*;
42pub use panic::*;
43pub use process::*;
44pub use string::*;
45pub use thunk::*;
46pub use uint::*;
47
48/// `System.Platform.getNumBits`: the word size of the target.
49pub unsafe fn lean_system_platform_nbits(_unit: Obj) -> Obj {
50    lean_box(usize::BITS as usize)
51}
52
53/// `System.Platform.getIsWindows`.
54pub unsafe fn lean_system_platform_windows(_unit: Obj) -> u8 {
55    cfg!(windows) as u8
56}
57
58/// `System.Platform.getIsOSX`.
59pub unsafe fn lean_system_platform_osx(_unit: Obj) -> u8 {
60    cfg!(target_os = "macos") as u8
61}
62
63/// `System.Platform.getIsLinux`.
64pub unsafe fn lean_system_platform_linux(_unit: Obj) -> u8 {
65    cfg!(target_os = "linux") as u8
66}
67
68/// `System.Platform.getIsEmscripten`.
69pub unsafe fn lean_system_platform_emscripten(_unit: Obj) -> u8 {
70    cfg!(target_os = "emscripten") as u8
71}
72
73/// `System.Platform.getTarget`: the target triple the runtime was compiled for.
74pub unsafe fn lean_system_platform_target(_unit: Obj) -> Obj {
75    lean_mk_string(env!("LEANFRAN_TARGET"))
76}
77
78/// `System.Platform.Internal.getHardwareConcurrency`: the number of hardware threads, or 0.
79pub unsafe fn lean_internal_get_hardware_concurrency(_unit: Obj) -> u32 {
80    std::thread::available_parallelism().map_or(0, |n| n.get() as u32)
81}
82
83/// `ptrAddrUnsafe`.
84pub unsafe fn lean_ptr_addr_unsafe(a: Obj) -> usize {
85    lean_ptr_addr(a)
86}
87
88include!(concat!(env!("OUT_DIR"), "/externs.rs"));
89
90/// The Lean source of `LeanRust/RuntimeExterns.lean`: [`EXTERNS`] as `runtimeExterns`.
91#[doc(hidden)]
92pub fn runtime_externs_lean() -> String {
93    let mut s = String::from(
94        "/-! C names of the functions the runtime `leanfran-runtime` provides. Generated by `make gen-externs`. -/\n\nnamespace LeanRust\n\ndef runtimeExterns : Array String := #[\n",
95    );
96    let items: Vec<String> = EXTERNS.iter().map(|n| format!("  \"{n}\"")).collect();
97    s.push_str(&items.join(",\n"));
98    s.push_str("\n]\n\nend LeanRust\n");
99    s
100}