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
macro_rules! impl_root {
($type:tt, 2) => {
impl<'target, 'value, 'data> $crate::wrappers::ptr::Root<'target, 'value, 'data>
for $type<'value, 'data>
{
type Output = $type<'target, 'data>;
unsafe fn root<S>(
scope: S,
value: $crate::wrappers::ptr::Ref<'value, 'data, Self>,
) -> $crate::error::JlrsResult<Self::Output>
where
S: $crate::memory::scope::PartialScope<'target>,
{
if let Some(v) = Self::wrapper(value, Private) {
let ptr = v.unwrap_non_null(Private);
scope.value(ptr, Private)
} else {
Err($crate::error::AccessError::UndefRef)?
}
}
}
};
($type:tt, 1) => {
impl<'target, 'value> $crate::wrappers::ptr::Root<'target, 'value, 'static>
for $type<'value>
{
type Output = $type<'target>;
unsafe fn root<S>(
scope: S,
value: $crate::wrappers::ptr::Ref<'value, 'static, Self>,
) -> $crate::error::JlrsResult<Self::Output>
where
S: $crate::memory::scope::PartialScope<'target>,
{
if let Some(v) =
<Self as $crate::wrappers::ptr::private::WrapperPriv>::wrapper(value, Private)
{
let ptr = v.unwrap_non_null(Private);
scope.value(ptr, Private)
} else {
Err($crate::error::AccessError::UndefRef)?
}
}
}
};
}
macro_rules! impl_valid_layout {
($ref_type:ident, $type:ident) => {
unsafe impl $crate::layout::valid_layout::ValidLayout for $ref_type<'_> {
fn valid_layout(ty: $crate::wrappers::ptr::value::Value) -> bool {
if let Ok(dt) = ty.cast::<$crate::wrappers::ptr::datatype::DataType>() {
dt.is::<$type>()
} else {
false
}
}
const IS_REF: bool = true;
}
};
}
macro_rules! impl_ref_root {
($type:tt, $reftype:tt, 2) => {
impl<'scope, 'data> $reftype<'scope, 'data> {
pub unsafe fn root<'target, S>(
self,
scope: S,
) -> $crate::error::JlrsResult<$type<'target, 'data>>
where
S: $crate::memory::scope::PartialScope<'target>,
{
<$type as $crate::wrappers::ptr::Root>::root(scope, self)
}
}
};
($type:tt, $reftype:tt, 1) => {
impl<'scope> $reftype<'scope> {
pub unsafe fn root<'target, S>(
self,
scope: S,
) -> $crate::error::JlrsResult<$type<'target>>
where
S: $crate::memory::scope::PartialScope<'target>,
{
<$type as $crate::wrappers::ptr::Root>::root(scope, self)
}
}
};
}
#[macro_export]
macro_rules! impl_debug {
($type:ty) => {
impl ::std::fmt::Debug for $type {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match <Self as $crate::wrappers::ptr::Wrapper>::display_string(*self) {
Ok(s) => f.write_str(&s),
Err(e) => f.write_fmt(format_args!("<Cannot display value: {}>", e)),
}
}
}
};
}
pub mod array;
pub mod datatype;
pub mod function;
#[cfg(feature = "internal-types")]
pub mod internal;
pub mod module;
pub mod simple_vector;
pub mod string;
pub mod symbol;
pub mod task;
pub mod type_name;
pub mod type_var;
pub mod union;
pub mod union_all;
pub mod value;
use crate::{
call::Call,
error::{JlrsError, JlrsResult, CANNOT_DISPLAY_VALUE},
layout::valid_layout::ValidLayout,
memory::{global::Global, scope::PartialScope},
private::Private,
wrappers::ptr::{module::Module, private::WrapperPriv as _, string::JuliaString, value::Value},
};
use std::{
fmt::{Debug, Formatter, Result as FmtResult},
marker::PhantomData,
ptr::null_mut,
};
#[cfg(any(not(feature = "lts"), feature = "all-features-override"))]
use std::sync::atomic::AtomicPtr;
pub(crate) trait Root<'target, 'value, 'data>: Wrapper<'value, 'data> {
type Output;
unsafe fn root<S>(scope: S, value: Ref<'value, 'data, Self>) -> JlrsResult<Self::Output>
where
S: PartialScope<'target>;
}
pub trait WrapperRef<'scope, 'data>:
private::WrapperRef<'scope, 'data> + Copy + Debug + ValidLayout
{
type Wrapper: Wrapper<'scope, 'data>;
}
impl<'scope, 'data, T> WrapperRef<'scope, 'data> for Ref<'scope, 'data, T>
where
T: Wrapper<'scope, 'data>,
Self: ValidLayout,
{
type Wrapper = T;
}
pub trait Wrapper<'scope, 'data>: private::WrapperPriv<'scope, 'data> {
fn as_ref(self) -> Ref<'scope, 'data, Self> {
Ref::wrap(self.unwrap(Private))
}
fn as_value(self) -> Value<'scope, 'data> {
unsafe { Value::wrap_non_null(self.unwrap_non_null(Private).cast(), Private) }
}
fn display_string(self) -> JlrsResult<String> {
let s = unsafe {
let global = Global::new();
Module::main(global)
.submodule_ref("Jlrs")?
.wrapper_unchecked()
.function_ref("valuestring")?
.wrapper_unchecked()
.call1_unrooted(global, self.as_value())
.map_err(|e| e.value_unchecked().error_string_or(CANNOT_DISPLAY_VALUE))
.map_err(|e| JlrsError::exception(format!("Jlrs.valuestring failed: {}", e)))?
.value_unchecked()
.cast::<JuliaString>()?
.as_str()?
.to_string()
};
Ok(s)
}
fn error_string(self) -> JlrsResult<String> {
let s = unsafe {
let global = Global::new();
Module::main(global)
.submodule_ref("Jlrs")?
.wrapper_unchecked()
.function_ref("errorstring")?
.wrapper_unchecked()
.call1_unrooted(global, self.as_value())
.map_err(|e| e.value_unchecked().error_string_or(CANNOT_DISPLAY_VALUE))
.map_err(|e| JlrsError::exception(format!("Jlrs.errorstring failed: {}", e)))?
.value_unchecked()
.cast::<JuliaString>()?
.as_str()?
.to_string()
};
Ok(s)
}
fn display_string_or<S: Into<String>>(self, default: S) -> String {
self.display_string().unwrap_or(default.into())
}
fn error_string_or<S: Into<String>>(self, default: S) -> String {
self.error_string().unwrap_or(default.into())
}
}
impl<'scope, 'data, W> Wrapper<'scope, 'data> for W where W: private::WrapperPriv<'scope, 'data> {}
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Ref<'scope, 'data, T: Wrapper<'scope, 'data>>(
*mut T::Wraps,
PhantomData<&'scope ()>,
PhantomData<&'data ()>,
);
impl<'scope, 'data, T: Wrapper<'scope, 'data>> Debug for Ref<'scope, 'data, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "Ref<{}>", T::NAME)
}
}
impl<'scope, 'data, T: Wrapper<'scope, 'data>> Ref<'scope, 'data, T> {
pub(crate) fn wrap(ptr: *mut T::Wraps) -> Self {
Ref(ptr, PhantomData, PhantomData)
}
pub fn undefined_ref() -> Ref<'scope, 'data, T> {
Ref(null_mut(), PhantomData, PhantomData)
}
pub fn is_undefined(self) -> bool {
self.0.is_null()
}
pub unsafe fn wrapper(self) -> Option<T> {
T::wrapper(self, Private)
}
pub unsafe fn wrapper_unchecked(self) -> T {
T::wrapper_unchecked(self, Private)
}
pub unsafe fn value(self) -> Option<Value<'scope, 'data>> {
T::value(self, Private)
}
pub unsafe fn value_unchecked(self) -> Value<'scope, 'data> {
T::value_unchecked(self, Private)
}
pub(crate) fn ptr(self) -> *mut T::Wraps {
self.0
}
}
pub(crate) mod private {
use crate::private::Private;
use crate::wrappers::ptr::{value::Value, Ref};
use std::{fmt::Debug, ptr::NonNull};
pub trait WrapperPriv<'scope, 'data>: Copy + Debug {
type Wraps: Copy;
const NAME: &'static str;
unsafe fn wrap_non_null(inner: NonNull<Self::Wraps>, _: Private) -> Self;
unsafe fn wrap(ptr: *mut Self::Wraps, _: Private) -> Self {
debug_assert!(!ptr.is_null());
Self::wrap_non_null(NonNull::new_unchecked(ptr), Private)
}
fn unwrap_non_null(self, _: Private) -> NonNull<Self::Wraps>;
#[inline(always)]
fn unwrap(self, _: Private) -> *mut Self::Wraps {
self.unwrap_non_null(Private).as_ptr()
}
#[inline(always)]
unsafe fn wrapper_unchecked(value_ref: Ref<'scope, 'data, Self>, _: Private) -> Self
where
Self: Sized + super::Wrapper<'scope, 'data>,
{
Self::wrap(value_ref.ptr(), Private)
}
#[inline(always)]
unsafe fn cast(value: Value<'scope, 'data>, _: Private) -> Self {
Self::wrap_non_null(value.unwrap_non_null(Private).cast(), Private)
}
#[inline(always)]
unsafe fn wrapper(value_ref: Ref<'scope, 'data, Self>, _: Private) -> Option<Self>
where
Self: Sized + super::Wrapper<'scope, 'data>,
{
let ptr = value_ref.ptr();
if ptr.is_null() {
return None;
}
Some(Self::wrap(ptr, Private))
}
#[inline(always)]
unsafe fn value_unchecked(
value_ref: Ref<'scope, 'data, Self>,
_: Private,
) -> Value<'scope, 'data>
where
Self: Sized + super::Wrapper<'scope, 'data>,
{
Value::wrap(value_ref.ptr().cast(), Private)
}
#[inline(always)]
unsafe fn value(
value_ref: Ref<'scope, 'data, Self>,
_: Private,
) -> Option<Value<'scope, 'data>>
where
Self: Sized + super::Wrapper<'scope, 'data>,
{
let ptr = value_ref.ptr();
if ptr.is_null() {
return None;
}
Some(Value::wrap(ptr.cast(), Private))
}
}
pub trait WrapperRef<'scope, 'data> {}
impl<'scope, 'data, T> WrapperRef<'scope, 'data> for Ref<'scope, 'data, T> where
T: WrapperPriv<'scope, 'data>
{
}
}
#[cfg(any(not(feature = "lts"), feature = "all-features-override"))]
#[inline(always)]
pub(crate) unsafe fn atomic_value<'a, T>(addr: *const u64) -> &'a AtomicPtr<T> {
&*(addr as *const AtomicPtr<T>)
}