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
use crate::error::{JlrsError, JlrsResult};
use crate::global::Global;
use crate::traits::{private::Internal, Cast, Frame, TemporarySymbol};
use crate::value::symbol::Symbol;
use crate::value::{CallResult, Value};
use crate::{impl_julia_type, impl_julia_typecheck, impl_valid_layout};
use jl_sys::{
jl_base_module, jl_core_module, jl_get_global, jl_main_module, jl_module_t, jl_module_type,
jl_set_const, jl_set_global, jl_typeis,
};
use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::marker::PhantomData;
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Module<'base>(*mut jl_module_t, PhantomData<&'base ()>);
impl<'base> Module<'base> {
pub(crate) unsafe fn wrap(module: *mut jl_module_t) -> Self {
Module(module, PhantomData)
}
#[doc(hidden)]
pub unsafe fn ptr(self) -> *mut jl_module_t {
self.0
}
pub fn name(self) -> Symbol<'base> {
unsafe { Symbol::wrap((&*(self.ptr())).name) }
}
pub fn parent(self) -> Option<Self> {
unsafe {
let parent = (&*(self.ptr())).parent;
if parent.is_null() {
return None;
}
Some(Self::wrap(parent))
}
}
pub fn extend<'global>(self, _: Global<'global>) -> Module<'global> {
unsafe { Module::wrap(self.ptr()) }
}
pub fn main(_: Global<'base>) -> Self {
unsafe { Module::wrap(jl_main_module) }
}
pub fn core(_: Global<'base>) -> Self {
unsafe { Module::wrap(jl_core_module) }
}
pub fn base(_: Global<'base>) -> Self {
unsafe { Module::wrap(jl_base_module) }
}
pub fn submodule<N>(self, name: N) -> JlrsResult<Self>
where
N: TemporarySymbol,
{
unsafe {
let symbol = name.temporary_symbol(Internal);
let submodule = jl_get_global(self.ptr(), symbol.ptr());
if !submodule.is_null() && jl_typeis(submodule, jl_module_type) {
Ok(Module(submodule as *mut jl_module_t, PhantomData))
} else {
Err(JlrsError::NotAModule(symbol.into()).into())
}
}
}
pub unsafe fn set_global<'frame, N>(
self,
name: N,
value: Value<'frame, 'static>,
) -> Value<'base, 'static>
where
N: TemporarySymbol,
{
jl_set_global(
self.ptr(),
name.temporary_symbol(Internal).ptr(),
value.ptr(),
);
Value::wrap(value.ptr())
}
pub fn set_const<'frame, N>(
self,
name: N,
value: Value<'frame, 'static>,
) -> JlrsResult<Value<'base, 'static>>
where
N: TemporarySymbol,
{
unsafe {
let symbol = name.temporary_symbol(Internal);
if self.global(symbol).is_ok() {
Err(JlrsError::ConstAlreadyExists(symbol.into()))?;
}
jl_set_const(self.ptr(), symbol.ptr(), value.ptr());
Ok(Value::wrap(value.ptr()))
}
}
pub fn global<N>(self, name: N) -> JlrsResult<Value<'base, 'static>>
where
N: TemporarySymbol,
{
unsafe {
let symbol = name.temporary_symbol(Internal);
let func = jl_get_global(self.ptr(), symbol.ptr());
if func.is_null() {
return Err(JlrsError::FunctionNotFound(symbol.into()).into());
}
Ok(Value::wrap(func.cast()))
}
}
pub fn function<N>(self, name: N) -> JlrsResult<Value<'base, 'static>>
where
N: TemporarySymbol,
{
self.global(name)
}
pub fn as_value(self) -> Value<'base, 'static> {
self.into()
}
pub fn require<'frame, F, S>(
self,
frame: &mut F,
module: S,
) -> JlrsResult<CallResult<'frame, 'static, Self>>
where
F: Frame<'frame>,
S: TemporarySymbol,
{
unsafe {
let out = Module::wrap(jl_base_module)
.function("require")
.unwrap()
.call2(
frame,
self.as_value(),
module.temporary_symbol(Internal).as_value(),
)?
.map(|a| std::mem::transmute(a.cast_unchecked::<Module>()));
Ok(out)
}
}
pub fn require_or_panic<S>(self, global: Global<'base>, module: S) -> JlrsResult<Self>
where
S: TemporarySymbol,
{
unsafe {
let out = Module::base(global)
.function("require")
.unwrap()
.call2_unprotected(
global,
self.as_value(),
module.temporary_symbol(Internal).as_value(),
)
.expect(&format!(
"Could not load ${:?}",
module.temporary_symbol(Internal)
))
.cast_unchecked::<Module>();
Ok(out)
}
}
}
impl<'base> Into<Value<'base, 'static>> for Module<'base> {
fn into(self) -> Value<'base, 'static> {
unsafe { Value::wrap(self.ptr().cast()) }
}
}
unsafe impl<'frame, 'data> Cast<'frame, 'data> for Module<'frame> {
type Output = Self;
fn cast(value: Value<'frame, 'data>) -> JlrsResult<Self::Output> {
if value.is::<Self::Output>() {
return unsafe { Ok(Self::cast_unchecked(value)) };
}
Err(JlrsError::NotAModule("This".to_string()))?
}
unsafe fn cast_unchecked(value: Value<'frame, 'data>) -> Self::Output {
Self::wrap(value.ptr().cast())
}
}
impl_julia_typecheck!(Module<'frame>, jl_module_type, 'frame);
impl_julia_type!(Module<'frame>, jl_module_type, 'frame);
impl_valid_layout!(Module<'frame>, 'frame);
impl<'frame, 'data> Debug for Module<'frame> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let name: String = self.name().into();
f.debug_tuple("Module").field(&name).finish()
}
}