intuicio_core/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
pub mod context;
pub mod function;
pub mod host;
pub mod meta;
pub mod object;
pub mod registry;
pub mod script;
pub mod transformer;
pub mod types;
pub mod utils;

pub use memoffset::offset_of as __internal__offset_of__;

/// Assumes `repr(u8)` enums only.
#[macro_export]
macro_rules! __internal__offset_of_enum__ {
    ($type:tt :: $variant:ident [ $( $field:ident ),* ] => $used_field:ident => $discriminant:literal) => {{
        let mut data = std::mem::MaybeUninit::<$type>::uninit();
        let ptr = data.as_mut_ptr().cast::<u8>();
        #[allow(clippy::macro_metavars_in_unsafe)]
        unsafe {
            ptr.write($discriminant);
            #[allow(unused_variables)]
            match data.assume_init_ref() {
                $type::$variant( $( $field ),* ) => {
                    ($used_field as *const _ as *const u8).offset_from(ptr) as usize
                }
                _ => unreachable!(),
            }
        }
    }};
    ($type:tt :: $variant:ident ( $index:tt ) => $discriminant:literal) => {{
        let mut data = std::mem::MaybeUninit::<$type>::uninit();
        let ptr = data.as_mut_ptr().cast::<u8>();
        #[allow(clippy::macro_metavars_in_unsafe)]
        unsafe {
            ptr.write($discriminant);
            #[allow(unused_variables)]
            match data.assume_init_ref() {
                $type::$variant {
                    $index: __value__, ..
                } => (__value__ as *const _ as *const u8).offset_from(ptr) as usize,
                _ => unreachable!(),
            }
        }
    }};
    ($type:tt :: $variant:ident { $field:ident } => $discriminant:literal) => {{
        let mut data = std::mem::MaybeUninit::<$type>::uninit();
        let ptr = data.as_mut_ptr().cast::<u8>();
        #[allow(clippy::macro_metavars_in_unsafe)]
        unsafe {
            ptr.write($discriminant);
            #[allow(unused_variables)]
            match data.assume_init_ref() {
                $type::$variant { $field, .. } => {
                    ($field as *const _ as *const u8).offset_from(ptr) as usize
                }
                _ => unreachable!(),
            }
        }
    }};
}

pub mod prelude {
    pub use crate::{
        context::*,
        function::*,
        host::*,
        object::*,
        registry::*,
        script::*,
        transformer::*,
        types::{enum_type::*, struct_type::*, *},
        IntuicioEnum, IntuicioStruct, IntuicioVersion, Visibility,
    };
    pub use crate::{
        define_function, define_native_enum, define_native_struct, define_runtime_enum,
        define_runtime_struct, function_signature,
    };
}

use crate::{
    registry::Registry,
    types::{enum_type::Enum, struct_type::Struct},
};
use serde::{Deserialize, Serialize};

#[derive(
    Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub enum Visibility {
    Private,
    Module,
    #[default]
    Public,
}

impl Visibility {
    pub fn is_visible(self, scope: Self) -> bool {
        self >= scope
    }

    pub fn is_public(&self) -> bool {
        *self == Visibility::Public
    }

    pub fn is_module(&self) -> bool {
        *self == Visibility::Module
    }

    pub fn is_private(&self) -> bool {
        *self == Visibility::Private
    }
}

pub trait IntuicioStruct {
    fn define_struct(registry: &Registry) -> Struct;
}

pub trait IntuicioEnum {
    fn define_enum(registry: &Registry) -> Enum;
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[repr(C)]
pub struct IntuicioVersion {
    major: usize,
    minor: usize,
    patch: usize,
}

impl IntuicioVersion {
    pub fn new(major: usize, minor: usize, patch: usize) -> Self {
        Self {
            major,
            minor,
            patch,
        }
    }

    pub fn major(&self) -> usize {
        self.major
    }

    pub fn minor(&self) -> usize {
        self.minor
    }

    pub fn patch(&self) -> usize {
        self.patch
    }

    pub fn is_compatible(&self, other: &Self) -> bool {
        self.major == other.major && self.minor == other.minor
    }
}

impl std::fmt::Display for IntuicioVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
    }
}

impl std::fmt::Debug for IntuicioVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("IntuicioVersion")
            .field("major", &self.major)
            .field("minor", &self.minor)
            .field("patch", &self.patch)
            .finish()
    }
}

#[macro_export]
macro_rules! crate_version {
    () => {{
        let major = env!("CARGO_PKG_VERSION_MAJOR", "0")
            .parse::<usize>()
            .unwrap();
        let minor = env!("CARGO_PKG_VERSION_MINOR", "0")
            .parse::<usize>()
            .unwrap();
        let patch = env!("CARGO_PKG_VERSION_PATCH", "0")
            .parse::<usize>()
            .unwrap();
        $crate::IntuicioVersion::new(major, minor, patch)
    }};
}

pub fn core_version() -> IntuicioVersion {
    crate_version!()
}

#[cfg(test)]
mod tests {
    use crate::Visibility;

    #[test]
    fn test_visibility() {
        assert!(Visibility::Private.is_visible(Visibility::Private));
        assert!(!Visibility::Private.is_visible(Visibility::Module));
        assert!(!Visibility::Private.is_visible(Visibility::Public));
        assert!(Visibility::Module.is_visible(Visibility::Private));
        assert!(Visibility::Module.is_visible(Visibility::Module));
        assert!(!Visibility::Module.is_visible(Visibility::Public));
        assert!(Visibility::Public.is_visible(Visibility::Private));
        assert!(Visibility::Public.is_visible(Visibility::Module));
        assert!(Visibility::Public.is_visible(Visibility::Public));
    }

    #[test]
    fn test_offset_of_enum() {
        #[allow(dead_code)]
        #[repr(u8)]
        enum Foo {
            A,
            B(usize),
            C(u8, u16),
            D { a: u32, b: u64 },
        }

        assert_eq!(__internal__offset_of_enum__!(Foo::B[v] => v => 1), 8);
        assert_eq!(__internal__offset_of_enum__!(Foo::B(0) => 1), 8);
        assert_eq!(__internal__offset_of_enum__!(Foo::C[a, b] => a => 2), 1);
        assert_eq!(__internal__offset_of_enum__!(Foo::C[a, b] => b => 2), 2);
        assert_eq!(__internal__offset_of_enum__!(Foo::C(0) => 2), 1);
        assert_eq!(__internal__offset_of_enum__!(Foo::C(1) => 2), 2);
        assert_eq!(__internal__offset_of_enum__!(Foo::D { a } => 3), 4);
        assert_eq!(__internal__offset_of_enum__!(Foo::D { b } => 3), 8);
    }
}