Skip to main content

nifty_rt/
symbols.rs

1//! Everything compiled code links to, by the name it's exported under. Each function's
2//! name comes from its own identifier, so the names can't drift from the exports;
3//! [`addresses`] pairs every name with what it names, for loading code in-process.
4
5use crate::abi::{
6    nf_enter, nf_equal, nf_fail_divide_by_zero, nf_fail_too_large, nf_float_rem, nf_leave,
7    nf_list_get, nf_list_item_mut, nf_list_len, nf_list_new, nf_list_push, nf_list_remove,
8    nf_release, nf_retain, nf_state, nf_state_mut, nf_store, nf_string_compare, nf_string_concat,
9    nf_string_len, nf_string_new, nf_struct_field_mut, nf_struct_get, nf_struct_new,
10    nf_text_finish, nf_text_new, nf_text_push, nf_text_push_str,
11};
12use crate::dialog::{nf_fail_dialog_into_variable, nf_pick};
13use crate::download::{
14    nf_download, nf_download_command, nf_fail_download_into_variable, nf_format_bytes,
15};
16use crate::nifty_rt_main;
17use crate::show::{nf_show_item, nf_show_same, nf_show_view};
18use crate::value::{TypeDesc, TypeKind};
19use crate::window::nf_window;
20
21macro_rules! exports {
22    (
23        functions { $($function:ident = $symbol:ident,)* }
24        types { $($type_name:ident = $type_symbol:literal: $type_static:ident = $kind:ident,)* }
25    ) => {
26        $(pub const $function: &str = stringify!($symbol);)*
27
28        $(
29            pub const $type_name: &str = $type_symbol;
30            #[unsafe(export_name = $type_symbol)]
31            pub static $type_static: TypeDesc = TypeDesc::scalar(TypeKind::$kind);
32        )*
33
34        /// Every export with its address.
35        #[must_use]
36        pub fn addresses() -> Vec<(&'static str, *const u8)> {
37            vec![
38                $(($function, $symbol as *const u8),)*
39                $(($type_name, (&raw const $type_static).cast::<u8>()),)*
40            ]
41        }
42    };
43}
44
45exports! {
46    functions {
47        MAIN = nifty_rt_main,
48        RETAIN = nf_retain,
49        RELEASE = nf_release,
50        EQUAL = nf_equal,
51        STORE = nf_store,
52        FLOAT_REM = nf_float_rem,
53        STRING_NEW = nf_string_new,
54        STRING_CONCAT = nf_string_concat,
55        STRING_LEN = nf_string_len,
56        STRING_COMPARE = nf_string_compare,
57        TEXT_NEW = nf_text_new,
58        TEXT_PUSH_STR = nf_text_push_str,
59        TEXT_PUSH = nf_text_push,
60        TEXT_FINISH = nf_text_finish,
61        LIST_NEW = nf_list_new,
62        LIST_LEN = nf_list_len,
63        LIST_GET = nf_list_get,
64        LIST_PUSH = nf_list_push,
65        LIST_REMOVE = nf_list_remove,
66        LIST_ITEM_MUT = nf_list_item_mut,
67        STRUCT_NEW = nf_struct_new,
68        STRUCT_GET = nf_struct_get,
69        STRUCT_FIELD_MUT = nf_struct_field_mut,
70        STATE = nf_state,
71        STATE_MUT = nf_state_mut,
72        ENTER = nf_enter,
73        LEAVE = nf_leave,
74        FAIL_DIVIDE_BY_ZERO = nf_fail_divide_by_zero,
75        FAIL_TOO_LARGE = nf_fail_too_large,
76        SHOW_SAME = nf_show_same,
77        SHOW_ITEM = nf_show_item,
78        SHOW_VIEW = nf_show_view,
79        DOWNLOAD = nf_download,
80        FAIL_DOWNLOAD_INTO_VARIABLE = nf_fail_download_into_variable,
81        FORMAT_BYTES = nf_format_bytes,
82        DOWNLOAD_COMMAND = nf_download_command,
83        PICK = nf_pick,
84        FAIL_DIALOG_INTO_VARIABLE = nf_fail_dialog_into_variable,
85        WINDOW = nf_window,
86    }
87    types {
88        TYPE_INT = "nf_type_int": INT = Int,
89        TYPE_FLOAT = "nf_type_float": FLOAT = Float,
90        TYPE_BOOL = "nf_type_bool": BOOL = Bool,
91        TYPE_STRING = "nf_type_string": STRING = String,
92    }
93}