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
use crate::error::{CamlError, Error};
use crate::sys;
use crate::tag::Tag;

/// Size is an alias for the platform specific integer type used to store size values
pub type Size = sys::Size;

/// Value wraps the native OCaml `value` type transparently, this means it has the
/// same representation as an `ocaml_sys::Value`
#[derive(Debug, Copy, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Value(pub sys::Value);

impl Clone for Value {
    fn clone(&self) -> Value {
        Value(self.0)
    }
}

/// `ToValue` is used to convert from Rust types to OCaml values
pub unsafe trait ToValue {
    /// Convert to OCaml value
    fn to_value(self) -> Value;
}

/// `FromValue` is used to convert from OCaml values to Rust types
pub unsafe trait FromValue {
    /// Convert from OCaml value
    fn from_value(v: Value) -> Self;
}

unsafe impl ToValue for Value {
    fn to_value(self) -> Value {
        Value(self.0)
    }
}

unsafe impl FromValue for Value {
    #[inline]
    fn from_value(v: Value) -> Value {
        v
    }
}

const NONE: Value = Value(sys::val_int(0));
const UNIT: Value = Value(sys::UNIT);

impl Value {
    /// Returns a named value registered by OCaml
    pub fn named<T: FromValue>(name: &str) -> Option<T> {
        unsafe {
            let s = match crate::util::CString::new(name) {
                Ok(s) => s,
                Err(_) => return None,
            };
            let named = sys::caml_named_value(s.as_ptr());
            if named.is_null() {
                return None;
            }

            Some(FromValue::from_value(Value(*named)))
        }
    }

    /// Allocate a new value with the given size and tag.
    pub fn alloc(n: usize, tag: Tag) -> Value {
        crate::frame!((x) {
            x = Value(unsafe { sys::caml_alloc(n, tag.into()) });
            x
        })
    }

    /// Allocate a new tuple value
    pub fn alloc_tuple(n: usize) -> Value {
        crate::frame!((x) {
            x = Value(unsafe { sys::caml_alloc_tuple(n) });
            x
        })
    }

    /// Allocate a new small value with the given size and tag
    pub fn alloc_small(n: usize, tag: Tag) -> Value {
        crate::frame!((x) {
            x = Value(unsafe { sys::caml_alloc_small(n, tag.into()) });
            x
        })
    }

    /// Allocate a new value with a finalizer
    ///
    /// This calls `caml_alloc_final` under-the-hood, which can has less than ideal performance
    /// behavior. In most cases you should prefer `Pointer::alloc_custom` when possible.
    pub fn alloc_final<T>(
        finalizer: unsafe extern "C" fn(Value),
        cfg: Option<(usize, usize)>,
    ) -> Value {
        let (used, max) = cfg.unwrap_or_else(|| (0, 1));
        crate::frame!((x) {
            unsafe {
                Value(sys::caml_alloc_final(
                    core::mem::size_of::<T>(),
                    core::mem::transmute(finalizer),
                    used,
                    max
                ))
            }
        })
    }

    /// Allocate custom value
    pub fn alloc_custom<T: crate::Custom>() -> Value {
        let size = core::mem::size_of::<T>();
        crate::frame!((x) {
            unsafe {
                x = Value(sys::caml_alloc_custom(T::ops() as *const _ as *const sys::custom_operations, size, T::USED, T::MAX));
                x
            }
        })
    }

    /// Allocate an abstract pointer value, it is best to ensure the value is
    /// on the heap using `Box::into_raw(Box::from(...))` to create the pointer
    /// and `Box::from_raw` to free it
    pub fn alloc_abstract_ptr<T>(ptr: *mut T) -> Value {
        crate::frame!((x) {
            x = Self::alloc(1, Tag::ABSTRACT);
            let dest = x.0 as *mut *mut T;
            unsafe {
                *dest = ptr;
            }
            x
        })
    }

    /// Create a new Value from an existing OCaml `value`
    #[inline]
    pub const fn new(v: sys::Value) -> Value {
        Value(v)
    }

    /// Get array length
    pub fn array_length(self) -> usize {
        unsafe { sys::caml_array_length(self.0) }
    }

    /// See caml_register_global_root
    pub fn register_global_root(&mut self) {
        crate::frame!((x) {
            x = Value(self.0);
            unsafe { sys::caml_register_global_root(&mut x.0) }
        })
    }

    /// Set caml_remove_global_root
    pub fn remove_global_root(&mut self) {
        unsafe { sys::caml_remove_global_root(&mut self.0) }
    }

    /// Get the tag for the underlying OCaml `value`
    pub fn tag(self) -> Tag {
        unsafe { sys::tag_val(self.0).into() }
    }

    /// Convert a boolean to OCaml value
    pub const fn bool(b: bool) -> Value {
        Value::int(b as crate::Int)
    }

    /// Allocate and copy a string value
    pub fn string<S: AsRef<str>>(s: S) -> Value {
        unsafe {
            let len = s.as_ref().len();
            let value = crate::sys::caml_alloc_string(len);
            let ptr = crate::sys::string_val(value);
            core::ptr::copy_nonoverlapping(s.as_ref().as_ptr(), ptr, len);
            Value(value)
        }
    }

    /// Convert from a pointer to an OCaml string back to an OCaml value
    ///
    /// # Safety
    /// This function assumes that the `str` argument has been allocated by OCaml
    pub unsafe fn of_str(s: &str) -> Value {
        Value(s.as_ptr() as isize)
    }

    /// Convert from a pointer to an OCaml string back to an OCaml value
    ///
    /// # Safety
    /// This function assumes that the `&[u8]` argument has been allocated by OCaml
    pub unsafe fn of_bytes(s: &[u8]) -> Value {
        Value(s.as_ptr() as isize)
    }

    /// OCaml Some value
    pub fn some<V: ToValue>(v: V) -> Value {
        crate::frame!((x) {
            unsafe {
                x = Value(sys::caml_alloc(1, 0));
                x.store_field(0, v.to_value());
            }
            x
        })
    }

    /// OCaml None value
    #[inline(always)]
    pub const fn none() -> Value {
        NONE
    }

    /// OCaml Unit value
    #[inline(always)]
    pub const fn unit() -> Value {
        UNIT
    }

    /// Create a variant value
    pub fn variant(tag: u8, value: Option<Value>) -> Value {
        crate::frame!((x) {
            match value {
                Some(v) => unsafe {
                    x = Value(sys::caml_alloc(1, tag));
                    x.store_field(0, v)
                },
                None => x = Value(unsafe { sys::caml_alloc(0, tag) }),
            }
            x
        })
    }

    /// Result.Ok value
    pub fn result_ok(value: impl Into<Value>) -> Value {
        Self::variant(0, Some(value.into()))
    }

    /// Result.Error value
    pub fn result_error(value: impl Into<Value>) -> Value {
        Self::variant(1, Some(value.into()))
    }

    /// Create an OCaml `int`
    pub const fn int(i: crate::Int) -> Value {
        Value(sys::val_int(i))
    }

    /// Create an OCaml `int`
    pub const fn uint(i: crate::Uint) -> Value {
        Value(sys::val_int(i as crate::Int))
    }

    /// Create an OCaml `Int64` from `i64`
    pub fn int64(i: i64) -> Value {
        crate::frame!((x) {
            unsafe { x = Value(sys::caml_copy_int64(i)) };
            x
        })
    }

    /// Create an OCaml `Int32` from `i32`
    pub fn int32(i: i32) -> Value {
        crate::frame!((x) {
            unsafe { x = Value(sys::caml_copy_int32(i)) };
            x
        })
    }

    /// Create an OCaml `Nativeint` from `isize`
    pub fn nativeint(i: isize) -> Value {
        frame!((x) {
            unsafe { x = Value(sys::caml_copy_nativeint(i)) };
            x
        })
    }

    /// Create an OCaml `Float` from `f64`
    pub fn float(d: f64) -> Value {
        frame!((x) {
            unsafe { x = Value(sys::caml_copy_double(d)) }
            x
        })
    }

    /// Check if a Value is an integer or block, returning true if
    /// the underlying value is a block
    pub fn is_block(self) -> bool {
        sys::is_block(self.0)
    }

    /// Check if a Value is an integer or block, returning true if
    /// the underlying value is an integer
    pub fn is_long(self) -> bool {
        sys::is_long(self.0)
    }

    /// Get index of underlying OCaml block value
    pub fn field<T: FromValue>(self, i: Size) -> T {
        unsafe { T::from_value(Value(*sys::field(self.0, i))) }
    }

    /// Set index of underlying OCaml block value
    pub fn store_field<V: ToValue>(&mut self, i: Size, val: V) {
        unsafe { sys::store_field(self.0, i, val.to_value().0) }
    }

    /// Convert an OCaml `int` to `isize`
    pub const fn int_val(self) -> isize {
        sys::int_val(self.0)
    }

    /// Convert an OCaml `Float` to `f64`
    pub fn float_val(self) -> f64 {
        unsafe { *(self.0 as *const f64) }
    }

    /// Convert an OCaml `Int32` to `i32`
    pub fn int32_val(self) -> i32 {
        unsafe { *self.custom_ptr_val::<i32>() }
    }

    /// Convert an OCaml `Int64` to `i64`
    pub fn int64_val(self) -> i64 {
        unsafe { *self.custom_ptr_val::<i64>() }
    }

    /// Convert an OCaml `Nativeint` to `isize`
    pub fn nativeint_val(self) -> isize {
        unsafe { *self.custom_ptr_val::<isize>() }
    }

    /// Get pointer to data stored in an OCaml custom value
    pub fn custom_ptr_val<T>(self) -> *const T {
        unsafe { sys::field(self.0, 1) as *const T }
    }

    /// Get mutable pointer to data stored in an OCaml custom value
    pub fn custom_ptr_val_mut<T>(self) -> *mut T {
        unsafe { sys::field(self.0, 1) as *mut T }
    }

    /// Get pointer to the pointer contained by Value
    pub fn abstract_ptr_val<T>(self) -> *const T {
        unsafe { *(self.0 as *const *const T) }
    }

    /// Get mutable pointer to the pointer contained by Value
    pub fn abstract_ptr_val_mut<T>(self) -> *mut T {
        unsafe { *(self.0 as *mut *mut T) }
    }

    /// Extract OCaml exception
    pub fn exception<A: FromValue>(self) -> Option<A> {
        if !self.is_exception_result() {
            return None;
        }

        Some(A::from_value(Value(crate::sys::extract_exception(self.0))))
    }

    /// Call a closure with a single argument, returning an exception value
    pub fn call<A: ToValue>(self, arg: A) -> Result<Value, Error> {
        if self.tag() != Tag::CLOSURE {
            return Err(Error::NotCallable);
        }

        let mut v = crate::frame!((res) {
            res = unsafe { Value(sys::caml_callback_exn(self.0, arg.to_value().0)) };
            res
        });

        if v.is_exception_result() {
            v = v.exception().unwrap();
            Err(CamlError::Exception(v).into())
        } else {
            Ok(v)
        }
    }

    /// Call a closure with two arguments, returning an exception value
    pub fn call2<A: ToValue, B: ToValue>(self, arg1: A, arg2: B) -> Result<Value, Error> {
        if self.tag() != Tag::CLOSURE {
            return Err(Error::NotCallable);
        }

        let mut v = crate::frame!((res) {
            res = unsafe {
                Value(sys::caml_callback2_exn(self.0, arg1.to_value().0, arg2.to_value().0))
            };
            res
        });

        if v.is_exception_result() {
            v = v.exception().unwrap();
            Err(CamlError::Exception(v).into())
        } else {
            Ok(v)
        }
    }

    /// Call a closure with three arguments, returning an exception value
    pub fn call3<A: ToValue, B: ToValue, C: ToValue>(
        self,
        arg1: A,
        arg2: B,
        arg3: C,
    ) -> Result<Value, Error> {
        if self.tag() != Tag::CLOSURE {
            return Err(Error::NotCallable);
        }

        let mut v = crate::frame!((res) {
            res = unsafe {
                Value(sys::caml_callback3_exn(
                    self.0,
                    arg1.to_value().0,
                    arg2.to_value().0,
                    arg3.to_value().0,
                ))
            };
            res
        });

        if v.is_exception_result() {
            v = v.exception().unwrap();
            Err(CamlError::Exception(v).into())
        } else {
            Ok(v)
        }
    }

    /// Call a closure with `n` arguments, returning an exception value
    pub fn call_n<A: AsRef<[Value]>>(self, args: A) -> Result<Value, Error> {
        if self.tag() != Tag::CLOSURE {
            return Err(Error::NotCallable);
        }

        let n = args.as_ref().len();
        let x = args.as_ref();

        let mut v = crate::frame!((res) {
            res = unsafe {
                Value(sys::caml_callbackN_exn(
                    self.0,
                    n,
                    x.as_ptr() as *mut sys::Value,
                ))
            };
            res
        });

        if v.is_exception_result() {
            v = v.exception().unwrap();
            Err(CamlError::Exception(v).into())
        } else {
            Ok(v)
        }
    }

    /// Modify an OCaml value in place
    pub fn modify<V: ToValue>(&mut self, v: V) {
        unsafe { sys::caml_modify(&mut self.0, v.to_value().0) }
    }

    /// Determines if the current value is an exception
    pub fn is_exception_result(self) -> bool {
        crate::sys::is_exception_result(self.0)
    }

    /// Get hash variant as OCaml value
    pub fn hash_variant<S: AsRef<str>>(name: S, a: Option<Value>) -> Value {
        let s = crate::util::CString::new(name.as_ref()).expect("Invalid C string");
        let hash = unsafe { Value(sys::caml_hash_variant(s.as_ptr() as *const u8)) };
        match a {
            Some(x) => {
                let mut output = Value::alloc_small(2, Tag(0));
                output.store_field(0, hash);
                output.store_field(1, x);
                output
            }
            None => hash,
        }
    }

    /// Get object method
    pub fn method<S: AsRef<str>>(self, name: S) -> Option<Value> {
        if self.tag() != Tag::OBJECT {
            return None;
        }

        let v = unsafe { sys::caml_get_public_method(self.0, Self::hash_variant(name, None).0) };

        if v == 0 {
            return None;
        }

        Some(Value(v))
    }

    /// Initialize OCaml value using `caml_initialize`
    pub fn initialize(&mut self, value: Value) {
        unsafe { sys::caml_initialize(&mut self.0, value.0) }
    }

    /// This will recursively clone any OCaml value
    /// The new value is allocated inside the OCaml heap,
    /// and may end up being moved or garbage collected.
    pub fn deep_clone_to_ocaml(self) -> Self {
        if self.is_long() {
            return self;
        }
        unsafe {
            let wosize = sys::wosize_val(self.0);
            let val1 = Self::alloc(wosize, self.tag());
            let ptr0 = self.0 as *const sys::Value;
            let ptr1 = val1.0 as *mut sys::Value;
            if self.tag() >= Tag::NO_SCAN {
                ptr0.copy_to_nonoverlapping(ptr1, wosize);
                return val1;
            }
            for i in 0..(wosize as isize) {
                sys::caml_initialize(
                    ptr1.offset(i),
                    Value(ptr0.offset(i).read()).deep_clone_to_ocaml().0,
                );
            }
            val1
        }
    }

    /// This will recursively clone any OCaml value
    /// The new value is allocated outside of the OCaml heap, and should
    /// only be used for storage inside Rust structures.
    #[cfg(not(feature = "no-std"))]
    pub fn deep_clone_to_rust(self) -> Self {
        if self.is_long() {
            return self;
        }
        unsafe {
            if self.tag() >= Tag::NO_SCAN {
                let slice0 = slice(self);
                let vec1 = slice0.to_vec();
                let ptr1 = vec1.as_ptr();
                core::mem::forget(vec1);
                return Value(ptr1.offset(1) as isize);
            }
            let slice0 = slice(self);
            let vec1: Vec<Value> = slice0
                .iter()
                .enumerate()
                .map(|(i, v)| if i == 0 { *v } else { v.deep_clone_to_rust() })
                .collect();
            let ptr1 = vec1.as_ptr();
            core::mem::forget(vec1);
            Value(ptr1.offset(1) as isize)
        }
    }
}

#[cfg(not(feature = "no-std"))]
unsafe fn slice<'a>(value: Value) -> &'a [Value] {
    ::core::slice::from_raw_parts(
        (value.0 as *const Value).offset(-1),
        sys::wosize_val(value.0) + 1,
    )
}