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 #[must_use]
165 fn default() -> Self {
166 Self::new()
167 }
168 }
169
170 impl $package {
171 #[doc=concat!("Create a new `", stringify!($package), "`")]
172 #[inline]
173 #[must_use]
174 pub fn new() -> Self {
175 let mut module = $crate::Module::new();
176 <Self as $crate::packages::Package>::init(&mut module);
177 module.build_index();
178 Self(module.into())
179 }
180 }
181 )* };
182 ($($(#[$outer:meta])* $root:ident :: $package:ident => | $lib:ident | $block:block)+) => { $(
183 $(#[$outer])*
184 #[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")]
190 pub struct $package($root::Shared<$root::Module>);
191
192 impl $root::packages::Package for $package {
193 fn as_shared_module(&self) -> $root::Shared<$root::Module> {
194 self.0.clone()
195 }
196 fn init($lib: &mut $root::Module) {
197 $block
198 }
199 }
200
201 impl Default for $package {
202 #[inline(always)]
203 #[must_use]
204 fn default() -> Self {
205 Self::new()
206 }
207 }
208
209 impl $package {
210 #[inline]
211 #[must_use]
212 pub fn new() -> Self {
213 let mut module = $root::Module::new();
214 <Self as $root::packages::Package>::init(&mut module);
215 module.build_index();
216 Self(module.into())
217 }
218 }
219 )* };
220 ($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => {
221 #[doc=$comment]
222 #[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")]
229 pub struct $package($root::Shared<$root::Module>);
230
231 impl $root::packages::Package for $package {
232 fn as_shared_module(&self) -> $root::Shared<$root::Module> {
233 #[allow(deprecated)]
234 self.0.clone()
235 }
236 fn init($lib: &mut $root::Module) {
237 $block
238 }
239 }
240
241 impl Default for $package {
242 #[inline(always)]
243 #[must_use]
244 fn default() -> Self {
245 Self::new()
246 }
247 }
248
249 impl $package {
250 #[inline]
251 #[must_use]
252 pub fn new() -> Self {
253 let mut module = $root::Module::new();
254 <Self as $root::packages::Package>::init(&mut module);
255 module.build_index();
256 Self(module.into())
257 }
258 }
259 };
260}