interpreter/package/
mod.rs

1use crate::{types::MethodRes, Package};
2
3#[derive(Default)]
4/// ImplPackage is meant to create a package out of Box<dyn Package>
5pub(crate) struct ImplPackage {
6  pub(crate) name: &'static [u8],
7
8  pub(crate) methods: MethodRes,
9}
10
11impl Package for ImplPackage {
12  fn name(&self) -> &'static [u8] {
13    self.name
14  }
15
16  fn methods(&self) -> MethodRes {
17    self.methods
18  }
19}
20
21#[macro_export]
22/// Exports a leadlang package
23///
24/// ```rust
25/// use interpreter::exports;
26///
27/// exports! {
28///   packages = MyPkg1,MyPkg2;
29///   runtimes = {
30///     "rt1" = MyRuntime1
31///   }
32/// }
33/// ```
34macro_rules! exports {
35  (
36    packages = $($x:ident),*;
37    runtimes = {
38      $($key:literal = $val:ident),*
39    }
40  ) => {
41    #[no_mangle]
42    pub fn ver() -> u16 {
43      interpreter::VERSION_INT
44    }
45
46    static MODULES: &[&dyn interpreter::Package] = &[
47      $(interpreter::generate!(-> $x)),+
48    ];
49
50    interpreter::paste! {
51      $(
52        static [<$val _STATIC>]: &'static dyn interpreter::runtime::RuntimeValue = &$val::new_const();
53      )*
54
55      static RUNTIMES: interpreter::phf::Map<&'static str, &'static dyn interpreter::runtime::RuntimeValue> = interpreter::phf::phf_map! {
56        $($key => [<$val _STATIC>]),*
57      };
58    }
59
60    #[no_mangle]
61    pub fn modules() -> &'static [&'static dyn interpreter::Package] {
62      MODULES
63    }
64
65    #[no_mangle]
66    pub fn runtimes() -> interpreter::phf::map::Entries<'static, &'static str, &'static dyn interpreter::runtime::RuntimeValue> {
67      RUNTIMES.entries()
68    }
69
70    #[no_mangle]
71    pub fn runtime(id: &str) -> Option<&'static dyn interpreter::runtime::RuntimeValue> {
72      let Some(rt) = RUNTIMES.get(id) else {
73        return None;
74      };
75
76      Some(*rt)
77    }
78  };
79}
80
81#[macro_export]
82/// Exports a leadlang package (module-only)
83///
84/// ```rust
85/// use interpreter::generate;
86///
87/// generate! { Module1, Module2 }
88/// ```
89macro_rules! generate {
90  ($($x:ident),*) => {
91    interpreter::exports!(
92      packages = $($x),*;
93      runtimes = {}
94    );
95  };
96
97  (-> $x:ident) => {
98    &$x
99  };
100}