-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathmodule.rs
More file actions
782 lines (727 loc) · 25.9 KB
/
module.rs
File metadata and controls
782 lines (727 loc) · 25.9 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
use std::{convert::TryFrom, ffi::CString, mem, ptr};
use super::{ClassBuilder, FunctionBuilder};
use crate::{
PHP_DEBUG, PHP_ZTS,
class::RegisteredClass,
constant::IntoConst,
describe::DocComments,
error::Result,
ffi::{ZEND_MODULE_API_NO, ext_php_rs_php_build_id},
flags::ClassFlags,
zend::{FunctionEntry, ModuleEntry},
};
#[cfg(feature = "enum")]
use crate::{builders::enum_builder::EnumBuilder, enum_::RegisteredEnum};
/// Builds a Zend module extension to be registered with PHP. Must be called
/// from within an external function called `get_module`, returning a mutable
/// pointer to a `ModuleEntry`.
///
/// ```rust,no_run
/// use ext_php_rs::{
/// builders::ModuleBuilder,
/// zend::ModuleEntry,
/// info_table_start, info_table_end, info_table_row
/// };
///
/// #[unsafe(no_mangle)]
/// pub extern "C" fn php_module_info(_module: *mut ModuleEntry) {
/// info_table_start!();
/// info_table_row!("column 1", "column 2");
/// info_table_end!();
/// }
///
/// #[unsafe(no_mangle)]
/// pub extern "C" fn get_module() -> *mut ModuleEntry {
/// let (entry, _) = ModuleBuilder::new("ext-name", "ext-version")
/// .info_function(php_module_info)
/// .try_into()
/// .unwrap();
/// entry.into_raw()
/// }
/// ```
#[must_use]
#[derive(Debug, Default)]
pub struct ModuleBuilder<'a> {
pub(crate) name: String,
pub(crate) version: String,
pub(crate) functions: Vec<FunctionBuilder<'a>>,
pub(crate) constants: Vec<(String, Box<dyn IntoConst + Send>, DocComments)>,
pub(crate) classes: Vec<fn() -> ClassBuilder>,
pub(crate) interfaces: Vec<fn() -> ClassBuilder>,
#[cfg(feature = "enum")]
pub(crate) enums: Vec<fn() -> EnumBuilder>,
startup_func: Option<StartupShutdownFunc>,
shutdown_func: Option<StartupShutdownFunc>,
request_startup_func: Option<StartupShutdownFunc>,
request_shutdown_func: Option<StartupShutdownFunc>,
post_deactivate_func: Option<unsafe extern "C" fn() -> i32>,
info_func: Option<InfoFunc>,
}
impl ModuleBuilder<'_> {
/// Creates a new module builder with a given name and version.
///
/// # Arguments
///
/// * `name` - The name of the extension.
/// * `version` - The current version of the extension.
pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
Self {
name: name.into(),
version: version.into(),
functions: vec![],
constants: vec![],
classes: vec![],
..Default::default()
}
}
/// Overrides module name.
///
/// # Arguments
///
/// * `name` - The name of the extension.
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
/// Overrides module version.
///
/// # Arguments
///
/// * `version` - The current version of the extension.
pub fn version(mut self, version: impl Into<String>) -> Self {
self.version = version.into();
self
}
/// Sets the startup function for the extension.
///
/// # Arguments
///
/// * `func` - The function to be called on startup.
pub fn startup_function(mut self, func: StartupShutdownFunc) -> Self {
self.startup_func = Some(func);
self
}
/// Sets the shutdown function for the extension.
///
/// # Arguments
///
/// * `func` - The function to be called on shutdown.
pub fn shutdown_function(mut self, func: StartupShutdownFunc) -> Self {
self.shutdown_func = Some(func);
self
}
/// Sets the request startup function for the extension.
///
/// # Arguments
///
/// * `func` - The function to be called when startup is requested.
pub fn request_startup_function(mut self, func: StartupShutdownFunc) -> Self {
self.request_startup_func = Some(func);
self
}
/// Sets the request shutdown function for the extension.
///
/// # Arguments
///
/// * `func` - The function to be called when shutdown is requested.
pub fn request_shutdown_function(mut self, func: StartupShutdownFunc) -> Self {
self.request_shutdown_func = Some(func);
self
}
/// Sets the post request shutdown function for the extension.
///
/// This function can be useful if you need to do any final cleanup at the
/// very end of a request, after all other resources have been released. For
/// example, if your extension creates any persistent resources that last
/// beyond a single request, you could use this function to clean those up.
/// # Arguments
///
/// * `func` - The function to be called when shutdown is requested.
pub fn post_deactivate_function(mut self, func: unsafe extern "C" fn() -> i32) -> Self {
self.post_deactivate_func = Some(func);
self
}
/// Sets the extension information function for the extension.
///
/// # Arguments
///
/// * `func` - The function to be called to retrieve the information about
/// the extension.
pub fn info_function(mut self, func: InfoFunc) -> Self {
self.info_func = Some(func);
self
}
/// Registers a function call observer for profiling or tracing.
///
/// The factory function is called once globally during MINIT to create
/// a singleton observer instance shared across all requests and threads.
/// The observer must be `Send + Sync` as it may be accessed concurrently
/// in ZTS builds.
///
/// # Arguments
///
/// * `factory` - A function that creates an observer instance
///
/// # Example
///
/// ```ignore
/// use ext_php_rs::prelude::*;
/// use ext_php_rs::zend::{FcallObserver, FcallInfo, ExecuteData};
/// use ext_php_rs::types::Zval;
///
/// struct MyProfiler;
///
/// impl FcallObserver for MyProfiler {
/// fn should_observe(&self, info: &FcallInfo) -> bool {
/// !info.is_internal
/// }
/// fn begin(&self, _: &ExecuteData) {}
/// fn end(&self, _: &ExecuteData, _: Option<&Zval>) {}
/// }
///
/// #[php_module]
/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
/// module.fcall_observer(|| MyProfiler)
/// }
/// ```
///
/// # Panics
///
/// Panics if called more than once on the same module.
#[cfg(feature = "observer")]
pub fn fcall_observer<F, O>(self, factory: F) -> Self
where
F: Fn() -> O + Send + Sync + 'static,
O: crate::zend::FcallObserver + Send + Sync,
{
let boxed_factory: Box<
dyn Fn() -> Box<dyn crate::zend::FcallObserver + Send + Sync> + Send + Sync,
> = Box::new(move || Box::new(factory()));
crate::zend::observer::register_fcall_observer_factory(boxed_factory);
self
}
/// Registers an error observer for monitoring PHP errors.
///
/// The factory function is called once during MINIT to create
/// a singleton observer instance shared across all requests.
/// The observer must be `Send + Sync` for ZTS builds.
///
/// # Arguments
///
/// * `factory` - A function that creates an observer instance
///
/// # Example
///
/// ```ignore
/// use ext_php_rs::prelude::*;
///
/// struct MyErrorLogger;
///
/// impl ErrorObserver for MyErrorLogger {
/// fn should_observe(&self, error_type: ErrorType) -> bool {
/// ErrorType::FATAL.contains(error_type)
/// }
///
/// fn on_error(&self, error: &ErrorInfo) {
/// eprintln!("[{}:{}] {}",
/// error.filename.unwrap_or("<unknown>"),
/// error.lineno,
/// error.message
/// );
/// }
/// }
///
/// #[php_module]
/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
/// module.error_observer(MyErrorLogger)
/// }
/// ```
///
/// # Panics
///
/// Panics if called more than once on the same module.
#[cfg(feature = "observer")]
pub fn error_observer<F, O>(self, factory: F) -> Self
where
F: Fn() -> O + Send + Sync + 'static,
O: crate::zend::ErrorObserver + Send + Sync,
{
let boxed_factory: Box<
dyn Fn() -> Box<dyn crate::zend::ErrorObserver + Send + Sync> + Send + Sync,
> = Box::new(move || Box::new(factory()));
crate::zend::error_observer::register_error_observer_factory(boxed_factory);
self
}
/// Registers an exception observer for monitoring thrown PHP exceptions.
///
/// The factory function is called once during MINIT to create
/// a singleton observer instance shared across all requests.
/// The observer must be `Send + Sync` for ZTS builds.
///
/// The observer is called at throw time, before any catch blocks are evaluated.
///
/// # Arguments
///
/// * `factory` - A function that creates an observer instance
///
/// # Example
///
/// ```ignore
/// use ext_php_rs::prelude::*;
///
/// struct MyExceptionLogger;
///
/// impl ExceptionObserver for MyExceptionLogger {
/// fn on_exception(&self, exception: &ExceptionInfo) {
/// eprintln!("[EXCEPTION] {}: {} at {}:{}",
/// exception.class_name,
/// exception.message.as_deref().unwrap_or("<no message>"),
/// exception.file.as_deref().unwrap_or("<unknown>"),
/// exception.line
/// );
/// }
/// }
///
/// #[php_module]
/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
/// module.exception_observer(|| MyExceptionLogger)
/// }
/// ```
///
/// # Panics
///
/// Panics if called more than once on the same module.
#[cfg(feature = "observer")]
pub fn exception_observer<F, O>(self, factory: F) -> Self
where
F: Fn() -> O + Send + Sync + 'static,
O: crate::zend::ExceptionObserver + Send + Sync,
{
let boxed_factory: Box<
dyn Fn() -> Box<dyn crate::zend::ExceptionObserver + Send + Sync> + Send + Sync,
> = Box::new(move || Box::new(factory()));
crate::zend::exception_observer::register_exception_observer_factory(boxed_factory);
self
}
/// Registers a zend extension handler for low-level engine hooks.
///
/// Enables dual registration as both a regular PHP extension and a
/// `zend_extension`, giving access to hooks like `op_array_handler`
/// and `statement_handler` for building profilers and APMs.
///
/// The factory function is called once during MINIT to create
/// a singleton handler instance. The handler must be `Send + Sync`
/// for ZTS builds.
///
/// # Arguments
///
/// * `factory` - A function that creates a handler instance
///
/// # Example
///
/// ```ignore
/// use ext_php_rs::prelude::*;
/// use ext_php_rs::ffi::zend_op_array;
///
/// struct MyProfiler;
///
/// impl ZendExtensionHandler for MyProfiler {
/// fn op_array_handler(&self, _op_array: &mut zend_op_array) {}
/// fn statement_handler(&self, _execute_data: &ExecuteData) {}
/// }
///
/// #[php_module]
/// pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
/// module.zend_extension_handler(|| MyProfiler)
/// }
/// ```
///
/// # Panics
///
/// Panics if called more than once on the same module.
#[cfg(feature = "observer")]
pub fn zend_extension_handler<F, H>(self, factory: F) -> Self
where
F: Fn() -> H + Send + Sync + 'static,
H: crate::zend::ZendExtensionHandler,
{
let boxed_factory: Box<
dyn Fn() -> Box<dyn crate::zend::ZendExtensionHandler> + Send + Sync,
> = Box::new(move || Box::new(factory()));
crate::zend::zend_extension::register_zend_extension_factory(boxed_factory);
self
}
/// Adds a function to the extension.
///
/// # Arguments
///
/// * `func` - The function to be added to the extension.
pub fn function(mut self, func: FunctionBuilder<'static>) -> Self {
self.functions.push(func);
self
}
/// Adds a constant to the extension.
///
/// # Arguments
///
/// * `const` - Tuple containing the name, value and doc comments for the
/// constant. This is a tuple to support the [`wrap_constant`] macro.
///
/// [`wrap_constant`]: crate::wrap_constant
pub fn constant(
mut self,
r#const: (&str, impl IntoConst + Send + 'static, DocComments),
) -> Self {
let (name, val, docs) = r#const;
self.constants.push((
name.into(),
Box::new(val) as Box<dyn IntoConst + Send>,
docs,
));
self
}
/// Adds a interface to the extension.
///
/// # Panics
///
/// * Panics if a constant could not be registered.
pub fn interface<T: RegisteredClass>(mut self) -> Self {
self.interfaces.push(|| {
let mut builder = ClassBuilder::new(T::CLASS_NAME);
for (method, flags) in T::method_builders() {
builder = builder.method(method, flags);
}
for interface in T::IMPLEMENTS {
builder = builder.implements(*interface);
}
for (name, value, docs) in T::constants() {
builder = builder
.dyn_constant(*name, *value, docs)
.expect("Failed to register constant");
}
if let Some(modifier) = T::BUILDER_MODIFIER {
builder = modifier(builder);
}
builder = builder.flags(ClassFlags::Interface);
// Note: interfaces should NOT have object_override because they cannot be instantiated
builder
.registration(|ce| {
T::get_metadata().set_ce(ce);
})
.docs(T::DOC_COMMENTS)
});
self
}
/// Adds a class to the extension.
///
/// # Panics
///
/// * Panics if a constant could not be registered.
pub fn class<T: RegisteredClass>(mut self) -> Self {
self.classes.push(|| {
let mut builder = ClassBuilder::new(T::CLASS_NAME);
for (method, flags) in T::method_builders() {
builder = builder.method(method, flags);
}
// Methods from #[php_impl_interface] trait implementations.
// Uses the inventory crate for cross-crate method discovery.
for (method, flags) in T::interface_method_implementations() {
builder = builder.method(method, flags);
}
if let Some(parent) = T::EXTENDS {
builder = builder.extends(parent);
}
// Interfaces declared via #[php(implements(...))] attribute
for interface in T::IMPLEMENTS {
builder = builder.implements(*interface);
}
// Interfaces from #[php_impl_interface] trait implementations.
// Uses the inventory crate for cross-crate interface discovery.
for interface in T::interface_implementations() {
builder = builder.implements(interface);
}
for (name, value, docs) in T::constants() {
builder = builder
.dyn_constant(*name, *value, docs)
.expect("Failed to register constant");
}
for (name, prop_info) in T::get_properties() {
builder = builder.property(name, prop_info.flags, None, prop_info.docs);
}
for (name, flags, default, docs) in T::static_properties() {
let default_fn = default.map(|v| {
Box::new(move || v.as_zval(true))
as Box<dyn FnOnce() -> crate::error::Result<crate::types::Zval>>
});
builder = builder.property(*name, *flags, default_fn, docs);
}
if let Some(modifier) = T::BUILDER_MODIFIER {
builder = modifier(builder);
}
builder
.flags(T::FLAGS)
.object_override::<T>()
.registration(|ce| {
T::get_metadata().set_ce(ce);
})
.docs(T::DOC_COMMENTS)
});
self
}
/// Adds an enum to the extension.
#[cfg(feature = "enum")]
pub fn enumeration<T>(mut self) -> Self
where
T: RegisteredClass + RegisteredEnum,
{
self.enums.push(|| {
let mut builder = EnumBuilder::new(T::CLASS_NAME);
for case in T::CASES {
builder = builder.case(case);
}
for (method, flags) in T::method_builders() {
builder = builder.method(method, flags);
}
builder
.registration(|ce| {
T::get_metadata().set_ce(ce);
})
.docs(T::DOC_COMMENTS)
});
self
}
}
/// Artifacts from the [`ModuleBuilder`] that should be revisited inside the
/// extension startup function.
pub struct ModuleStartup {
#[cfg(feature = "observer")]
name: String,
#[cfg(feature = "observer")]
version: String,
constants: Vec<(String, Box<dyn IntoConst + Send>)>,
classes: Vec<fn() -> ClassBuilder>,
interfaces: Vec<fn() -> ClassBuilder>,
#[cfg(feature = "enum")]
enums: Vec<fn() -> EnumBuilder>,
}
impl ModuleStartup {
/// Completes startup of the module. Should only be called inside the module
/// startup function.
///
/// # Errors
///
/// * Returns an error if a constant could not be registered.
///
/// # Panics
///
/// * Panics if a class could not be registered.
pub fn startup(self, _ty: i32, mod_num: i32) -> Result<()> {
for (name, val) in self.constants {
val.register_constant(&name, mod_num)?;
}
// Interfaces must be registered before classes so that classes can implement
// them
self.interfaces.into_iter().map(|c| c()).for_each(|c| {
c.register().expect("Failed to build interface");
});
self.classes.into_iter().map(|c| c()).for_each(|c| {
c.register().expect("Failed to build class");
});
#[cfg(feature = "enum")]
self.enums
.into_iter()
.map(|builder| builder())
.for_each(|e| {
e.register().expect("Failed to build enum");
});
// Initialize observer systems if registered
#[cfg(feature = "observer")]
unsafe {
crate::zend::observer::observer_startup();
crate::zend::error_observer::error_observer_startup();
crate::zend::exception_observer::exception_observer_startup();
crate::zend::zend_extension::zend_extension_startup(&self.name, &self.version);
}
Ok(())
}
}
/// A function to be called when the extension is starting up or shutting down.
pub type StartupShutdownFunc = unsafe extern "C" fn(_type: i32, _module_number: i32) -> i32;
/// A function to be called when `phpinfo();` is called.
pub type InfoFunc = unsafe extern "C" fn(zend_module: *mut ModuleEntry);
/// Builds a [`ModuleEntry`] and [`ModuleStartup`] from a [`ModuleBuilder`].
/// This is the entry point for the module to be registered with PHP.
impl TryFrom<ModuleBuilder<'_>> for (ModuleEntry, ModuleStartup) {
type Error = crate::error::Error;
fn try_from(builder: ModuleBuilder) -> Result<Self, Self::Error> {
let mut functions = builder
.functions
.into_iter()
.map(FunctionBuilder::build)
.collect::<Result<Vec<_>>>()?;
functions.push(FunctionEntry::end());
let functions = Box::into_raw(functions.into_boxed_slice()) as *const FunctionEntry;
#[cfg(feature = "observer")]
let ext_name = builder.name.clone();
#[cfg(feature = "observer")]
let ext_version = builder.version.clone();
let name = CString::new(builder.name)?.into_raw();
let version = CString::new(builder.version)?.into_raw();
let startup = ModuleStartup {
#[cfg(feature = "observer")]
name: ext_name,
#[cfg(feature = "observer")]
version: ext_version,
constants: builder
.constants
.into_iter()
.map(|(n, v, _)| (n, v))
.collect(),
classes: builder.classes,
interfaces: builder.interfaces,
#[cfg(feature = "enum")]
enums: builder.enums,
};
#[cfg(not(php_zts))]
let module_entry = ModuleEntry {
size: mem::size_of::<ModuleEntry>().try_into()?,
zend_api: ZEND_MODULE_API_NO,
zend_debug: u8::from(PHP_DEBUG),
zts: u8::from(PHP_ZTS),
ini_entry: ptr::null(),
deps: ptr::null(),
name,
functions,
module_startup_func: builder.startup_func,
module_shutdown_func: builder.shutdown_func,
request_startup_func: builder.request_startup_func,
request_shutdown_func: builder.request_shutdown_func,
info_func: builder.info_func,
version,
globals_size: 0,
globals_ptr: ptr::null_mut(),
globals_ctor: None,
globals_dtor: None,
post_deactivate_func: builder.post_deactivate_func,
module_started: 0,
type_: 0,
handle: ptr::null_mut(),
module_number: 0,
build_id: unsafe { ext_php_rs_php_build_id() },
};
#[cfg(php_zts)]
let module_entry = ModuleEntry {
size: mem::size_of::<ModuleEntry>().try_into()?,
zend_api: ZEND_MODULE_API_NO,
zend_debug: u8::from(PHP_DEBUG),
zts: u8::from(PHP_ZTS),
ini_entry: ptr::null(),
deps: ptr::null(),
name,
functions,
module_startup_func: builder.startup_func,
module_shutdown_func: builder.shutdown_func,
request_startup_func: builder.request_startup_func,
request_shutdown_func: builder.request_shutdown_func,
info_func: builder.info_func,
version,
globals_size: 0,
globals_id_ptr: ptr::null_mut(),
globals_ctor: None,
globals_dtor: None,
post_deactivate_func: builder.post_deactivate_func,
module_started: 0,
type_: 0,
handle: ptr::null_mut(),
module_number: 0,
build_id: unsafe { ext_php_rs_php_build_id() },
};
Ok((module_entry, startup))
}
}
#[cfg(test)]
mod tests {
use crate::test::{
test_deactivate_function, test_function, test_info_function, test_startup_shutdown_function,
};
use super::*;
#[test]
fn test_new() {
let builder = ModuleBuilder::new("test", "1.0");
assert_eq!(builder.name, "test");
assert_eq!(builder.version, "1.0");
assert!(builder.functions.is_empty());
assert!(builder.constants.is_empty());
assert!(builder.classes.is_empty());
assert!(builder.interfaces.is_empty());
assert!(builder.startup_func.is_none());
assert!(builder.shutdown_func.is_none());
assert!(builder.request_startup_func.is_none());
assert!(builder.request_shutdown_func.is_none());
assert!(builder.post_deactivate_func.is_none());
assert!(builder.info_func.is_none());
#[cfg(feature = "enum")]
assert!(builder.enums.is_empty());
}
#[test]
fn test_name() {
let builder = ModuleBuilder::new("test", "1.0").name("new_test");
assert_eq!(builder.name, "new_test");
}
#[test]
fn test_version() {
let builder = ModuleBuilder::new("test", "1.0").version("2.0");
assert_eq!(builder.version, "2.0");
}
#[test]
fn test_startup_function() {
let builder =
ModuleBuilder::new("test", "1.0").startup_function(test_startup_shutdown_function);
assert!(builder.startup_func.is_some());
}
#[test]
fn test_shutdown_function() {
let builder =
ModuleBuilder::new("test", "1.0").shutdown_function(test_startup_shutdown_function);
assert!(builder.shutdown_func.is_some());
}
#[test]
fn test_request_startup_function() {
let builder = ModuleBuilder::new("test", "1.0")
.request_startup_function(test_startup_shutdown_function);
assert!(builder.request_startup_func.is_some());
}
#[test]
fn test_request_shutdown_function() {
let builder = ModuleBuilder::new("test", "1.0")
.request_shutdown_function(test_startup_shutdown_function);
assert!(builder.request_shutdown_func.is_some());
}
#[test]
fn test_set_post_deactivate_function() {
let builder =
ModuleBuilder::new("test", "1.0").post_deactivate_function(test_deactivate_function);
assert!(builder.post_deactivate_func.is_some());
}
#[test]
fn test_set_info_function() {
let builder = ModuleBuilder::new("test", "1.0").info_function(test_info_function);
assert!(builder.info_func.is_some());
}
#[test]
fn test_add_function() {
let builder =
ModuleBuilder::new("test", "1.0").function(FunctionBuilder::new("test", test_function));
assert_eq!(builder.functions.len(), 1);
}
#[test]
#[cfg(feature = "embed")]
fn test_add_constant() {
let builder =
ModuleBuilder::new("test", "1.0").constant(("TEST_CONST", 42, DocComments::default()));
assert_eq!(builder.constants.len(), 1);
assert_eq!(builder.constants[0].0, "TEST_CONST");
// TODO: Check if the value is 42
assert_eq!(builder.constants[0].2, DocComments::default());
}
}