1use crate::{Engine, Module, SharedModule};
4
5pub(crate) mod arithmetic;
6pub(crate) mod array_basic;
7pub(crate) mod bit_field;
8pub(crate) mod blob_basic;
9pub(crate) mod debugging;
10pub(crate) mod fn_basic;
11pub(crate) mod iter_basic;
12pub(crate) mod lang_core;
13pub(crate) mod logic;
14pub(crate) mod map_basic;
15pub(crate) mod math_basic;
16pub(crate) mod pkg_core;
17pub(crate) mod pkg_std;
18pub(crate) mod string_basic;
19pub(crate) mod string_more;
20pub(crate) mod time_basic;
21
22pub use arithmetic::ArithmeticPackage;
23#[cfg(not(feature = "no_index"))]
24pub use array_basic::BasicArrayPackage;
25pub use bit_field::BitFieldPackage;
26#[cfg(not(feature = "no_index"))]
27pub use blob_basic::BasicBlobPackage;
28#[cfg(feature = "debugging")]
29pub use debugging::DebuggingPackage;
30pub use fn_basic::BasicFnPackage;
31pub use iter_basic::BasicIteratorPackage;
32pub use lang_core::LanguageCorePackage;
33pub use logic::LogicPackage;
34#[cfg(not(feature = "no_object"))]
35pub use map_basic::BasicMapPackage;
36pub use math_basic::BasicMathPackage;
37pub use pkg_core::CorePackage;
38pub use pkg_std::StandardPackage;
39pub use string_basic::BasicStringPackage;
40pub use string_more::MoreStringPackage;
41#[cfg(not(feature = "no_time"))]
42pub use time_basic::BasicTimePackage;
43
44pub trait Package {
46 #[cold]
49 fn init(module: &mut Module);
50
51 #[cold]
55 #[inline]
56 #[allow(unused_variables)]
57 fn init_engine(engine: &mut Engine) {}
58
59 #[cold]
72 #[inline]
73 fn register_into_engine(&self, engine: &mut Engine) -> &Self {
74 Self::init_engine(engine);
75 engine.register_global_module(self.as_shared_module());
76 self
77 }
78
79 #[cfg(not(feature = "no_module"))]
92 #[cold]
93 #[inline]
94 fn register_into_engine_as(&self, engine: &mut Engine, name: &str) -> &Self {
95 Self::init_engine(engine);
96 engine.register_static_module(name, self.as_shared_module());
97 self
98 }
99
100 #[must_use]
102 fn as_shared_module(&self) -> SharedModule;
103}
104
105#[macro_export]
129macro_rules! def_package {
130 ($($(#[$outer:meta])* $mod:vis $package:ident($lib:ident)
131 $( : $($(#[$base_meta:meta])* $base_pkg:ty),+ )?
132 $block:block
133 $( |> | $engine:ident | $init_engine:block )?
134 )+) => { $(
135 $(#[$outer])*
136 $mod struct $package($crate::Shared<$crate::Module>);
137
138 impl $crate::packages::Package for $package {
139 #[inline(always)]
140 fn as_shared_module(&self) -> $crate::Shared<$crate::Module> {
141 self.0.clone()
142 }
143 fn init($lib: &mut $crate::Module) {
144 $($(
145 $(#[$base_meta])* { <$base_pkg>::init($lib); }
146 )*)*
147
148 $block
149 }
150 fn init_engine(_engine: &mut $crate::Engine) {
151 $($(
152 $(#[$base_meta])* { <$base_pkg>::init_engine(_engine); }
153 )*)*
154
155 $(
156 let $engine = _engine;
157 $init_engine
158 )*
159 }
160 }
161
162 impl Default for $package {
163 #[inline(always)]
164 fn default() -> Self {
165 Self::new()
166 }
167 }
168
169 impl $package {
170 #[doc=concat!("Create a new `", stringify!($package), "`")]
171 #[inline]
172 #[must_use]
173 pub fn new() -> Self {
174 let mut module = $crate::Module::new();
175 <Self as $crate::packages::Package>::init(&mut module);
176 module.build_index();
177 Self(module.into())
178 }
179 }
180 )* };
181 ($($(#[$outer:meta])* $root:ident :: $package:ident => | $lib:ident | $block:block)+) => { $(
182 $(#[$outer])*
183 #[deprecated(since = "1.5.0", note = "this is an old syntax of `def_package!` and is deprecated; use the new syntax of `def_package!` instead")]
189 pub struct $package($root::Shared<$root::Module>);
190
191 impl $root::packages::Package for $package {
192 fn as_shared_module(&self) -> $root::Shared<$root::Module> {
193 self.0.clone()
194 }
195 fn init($lib: &mut $root::Module) {
196 $block
197 }
198 }
199
200 impl Default for $package {
201 #[inline(always)]
202 #[must_use]
203 fn default() -> Self {
204 Self::new()
205 }
206 }
207
208 impl $package {
209 #[inline]
210 #[must_use]
211 pub fn new() -> Self {
212 let mut module = $root::Module::new();
213 <Self as $root::packages::Package>::init(&mut module);
214 module.build_index();
215 Self(module.into())
216 }
217 }
218 )* };
219 ($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => {
220 #[doc=$comment]
221 #[deprecated(since = "1.4.0", note = "this is an old syntax of `def_package!` and is deprecated; use the new syntax of `def_package!` instead")]
228 pub struct $package($root::Shared<$root::Module>);
229
230 impl $root::packages::Package for $package {
231 fn as_shared_module(&self) -> $root::Shared<$root::Module> {
232 self.0.clone()
233 }
234 fn init($lib: &mut $root::Module) {
235 $block
236 }
237 }
238
239 impl Default for $package {
240 #[inline(always)]
241 #[must_use]
242 fn default() -> Self {
243 Self::new()
244 }
245 }
246
247 impl $package {
248 #[inline]
249 #[must_use]
250 pub fn new() -> Self {
251 let mut module = $root::Module::new();
252 <Self as $root::packages::Package>::init(&mut module);
253 module.build_index();
254 Self(module.into())
255 }
256 }
257 };
258}