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
use std::ffi::CString;
use std::ptr::{null_mut, NonNull};
use failure::Error;
use foreign_types::ForeignTypeRef;
use crate::{ffi, value::FALSE, ContextRef, RuntimeRef};
pub type ModuleDef = ffi::JSModuleDef;
pub type ModuleInitFunc = ffi::JSModuleInitFunc;
pub type ModuleLoaderFunc = ffi::JSModuleLoaderFunc;
pub type ModuleNormalizeFunc = ffi::JSModuleNormalizeFunc;
impl RuntimeRef {
pub fn set_module_loader<T>(
&self,
module_normalize: ModuleNormalizeFunc,
module_loader: ModuleLoaderFunc,
opaque: Option<NonNull<T>>,
) {
unsafe {
ffi::JS_SetModuleLoaderFunc(
self.as_ptr(),
module_normalize,
module_loader,
opaque.map_or_else(null_mut, |p| p.cast().as_ptr()),
)
}
}
}
impl ContextRef {
pub fn new_c_module<T: Into<Vec<u8>>>(
&self,
name: T,
init: ModuleInitFunc,
) -> Result<NonNull<ffi::JSModuleDef>, Error> {
self.check_null(unsafe {
ffi::JS_NewCModule(
self.as_ptr(),
CString::new(name).expect("name").as_ptr(),
init,
)
})
}
pub fn detect_module<T: Into<Vec<u8>>>(&self, input: T) -> bool {
let input = input.into();
unsafe { ffi::JS_DetectModule(input.as_ptr() as *const _, input.len()) != FALSE }
}
}