-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathlib.rs
413 lines (382 loc) · 14.2 KB
/
lib.rs
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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the "hack" directory of this source tree.
use std::ffi::CString;
use std::panic::UnwindSafe;
pub use bumpalo::Bump;
use ocamlrep::Allocator;
use ocamlrep::BlockBuilder;
pub use ocamlrep::FromOcamlRep;
pub use ocamlrep::FromOcamlRepIn;
use ocamlrep::MemoizationCache;
use ocamlrep::OpaqueValue;
use ocamlrep::ToOcamlRep;
pub use ocamlrep::Value;
extern "C" {
fn ocamlpool_enter();
fn ocamlpool_leave();
fn ocamlpool_reserve_block(tag: u8, size: usize) -> usize;
fn caml_failwith(msg: *const i8);
fn caml_initialize(addr: *mut usize, value: usize);
static ocamlpool_generation: usize;
static ocamlpool_color: usize;
}
pub struct Pool {
cache: MemoizationCache,
}
impl Pool {
/// Prepare the ocamlpool library to allocate values directly on the OCaml
/// runtime's garbage-collected heap.
///
/// # Safety
///
/// The OCaml runtime is not thread-safe, and this function will interact
/// with it. If any other thread interacts with the OCaml runtime or
/// ocamlpool library during the lifetime of the `Pool`, undefined behavior
/// will result.
#[inline(always)]
pub unsafe fn new() -> Self {
ocamlpool_enter();
Self {
cache: MemoizationCache::new(),
}
}
#[inline(always)]
pub fn add<'a, T: ToOcamlRep + ?Sized>(&'a self, value: &'a T) -> OpaqueValue<'a> {
value.to_ocamlrep(self)
}
}
impl Drop for Pool {
#[inline(always)]
fn drop(&mut self) {
unsafe { ocamlpool_leave() };
}
}
impl Allocator for Pool {
#[inline(always)]
fn generation(&self) -> usize {
unsafe { ocamlpool_generation }
}
#[inline(always)]
fn block_with_size_and_tag(&self, size: usize, tag: u8) -> BlockBuilder<'_> {
let ptr = unsafe { ocamlpool_reserve_block(tag, size) as *mut OpaqueValue<'_> };
BlockBuilder::new(ptr as usize, size)
}
#[inline(always)]
fn set_field<'a>(&self, block: &mut BlockBuilder<'a>, index: usize, value: OpaqueValue<'a>) {
assert!(index < block.size());
unsafe {
caml_initialize(
(self.block_ptr_mut(block) as *mut usize).add(index),
value.to_bits(),
)
};
}
unsafe fn block_ptr_mut<'a>(&self, block: &mut BlockBuilder<'a>) -> *mut OpaqueValue<'a> {
block.address() as *mut _
}
fn memoized<'a>(
&'a self,
ptr: usize,
size: usize,
f: impl FnOnce(&'a Self) -> OpaqueValue<'a>,
) -> OpaqueValue<'a> {
let bits = self.cache.memoized(ptr, size, || f(self).to_bits());
// SAFETY: The only memoized values in the cache are those computed in
// the closure on the previous line. Since f returns OpaqueValue<'a>, any
// cached bits must represent a valid OpaqueValue<'a>,
unsafe { OpaqueValue::from_bits(bits) }
}
fn add_root<'a, T: ToOcamlRep + ?Sized>(&'a self, value: &'a T) -> OpaqueValue<'a> {
self.cache.with_cache(|| value.to_ocamlrep(self))
}
}
/// Convert the given value to an OCaml value on the OCaml runtime's
/// garbage-collected heap.
///
/// # Safety
///
/// The OCaml runtime is not thread-safe, and this function will interact with
/// it. If any other thread interacts with the OCaml runtime or ocamlpool
/// library during the execution of `to_ocaml`, undefined behavior will result.
///
/// # Panics
///
/// Panics upon attempts to re-enter `to_ocaml`.
#[inline(always)]
pub unsafe fn to_ocaml<T: ToOcamlRep + ?Sized>(value: &T) -> usize {
let pool = Pool::new();
let result = pool.add_root(value);
result.to_bits()
}
/// Catches panics in `f` and raises a OCaml exception of type Failure
/// with the panic message (if the panic was raised with a `&str` or `String`).
pub fn catch_unwind(f: impl FnOnce() -> usize + UnwindSafe) -> usize {
catch_unwind_with_handler(f, |msg: &str| -> Result<usize, String> { Err(msg.into()) })
}
/// Catches panics in `f` and raises a OCaml exception of type Failure
/// with the panic message (if the panic was raised with a `&str` or `String`).
/// `h` handles panic msg, it may re-raise by returning Err.
pub fn catch_unwind_with_handler(
f: impl FnOnce() -> usize + UnwindSafe,
h: impl FnOnce(&str) -> Result<usize, String>,
) -> usize {
let err = match std::panic::catch_unwind(f) {
Ok(value) => return value,
Err(err) => err,
};
let msg: &str = if let Some(s) = err.downcast_ref::<&str>() {
s
} else if let Some(s) = err.downcast_ref::<String>() {
s.as_str()
} else {
// TODO: Build a smarter message in this case (using panic::set_hook?)
"Panicked with non-string object"
};
match h(msg) {
Ok(value) => return value,
Err(err) => unsafe {
let msg = CString::new(err).unwrap();
caml_failwith(msg.as_ptr().cast());
},
}
unreachable!();
}
/// Assume that some Pool exists in some parent scope. Since ocamlpool is
/// implemented with statics, we don't need a reference to that pool to write to
/// it.
///
/// Does not preserve sharing of values referred to by multiple references or
/// Rcs (but sharing is preserved for `ocamlrep::rc::RcOc`).
///
/// # Safety
///
/// The OCaml runtime is not thread-safe, and this function will interact with
/// it. If any other thread interacts with the OCaml runtime or ocamlpool
/// library during the execution of this function, undefined behavior will
/// result.
#[inline(always)]
pub unsafe fn add_to_ambient_pool<T: ToOcamlRep>(value: &T) -> usize {
let fake_pool = Pool {
cache: MemoizationCache::new(),
};
let result = value.to_ocamlrep(&fake_pool).to_bits();
std::mem::forget(fake_pool);
result
}
/// # Safety
///
/// The OCaml runtime is not thread-safe, and this function will interact with
/// it. If any other thread interacts with the OCaml runtime or ocamlpool
/// library during the execution of this function, undefined behavior will
/// result.
pub unsafe fn copy_slab_into_ocaml_heap(slab: ocamlrep::slab::SlabReader<'_>) -> usize {
// Enter an ocamlpool region. Use `Pool` instead of `ocamlpool_enter`
// directly so that `Pool` will invoke `ocamlpool_leave` in the event of a
// panic (it does so in its `Drop` implementation).
let _pool = Pool::new();
// Allocate a block large enough for the entire slab contents and copy the
// slab into it. Use `size - 1`, since we intend to overwrite the header.
let size = slab.value_size_in_words();
let block = ocamlpool_reserve_block(0, size - 1) as *mut usize;
let block = block.sub(1);
let block_words = std::slice::from_raw_parts_mut(block, size);
let value = ocamlrep::slab::copy_and_rebase_value(slab, block_words);
let value = value.to_bits();
// Write the correct GC color to every header in the slab (else values will
// be collected prematurely).
let mut idx = 0;
while idx < block_words.len() {
let size = block_words[idx] >> 10;
block_words[idx] |= ocamlpool_color;
idx += size + 1;
}
value
}
#[macro_export]
macro_rules! ocaml_ffi_fn {
(fn $name:ident($($param:ident: $ty:ty),+ $(,)?) -> $ret:ty $code:block) => {
#[no_mangle]
pub unsafe extern "C" fn $name ($($param: usize,)*) -> usize {
$crate::catch_unwind(|| {
fn inner($($param: $ty,)*) -> $ret { $code }
use $crate::FromOcamlRep;
$(let $param = <$ty>::from_ocaml($param).unwrap();)*
let result = inner($($param,)*);
$crate::to_ocaml(&result)
})
}
};
(fn $name:ident() -> $ret:ty $code:block) => {
#[no_mangle]
pub unsafe extern "C" fn $name (_unit: usize) -> usize {
$crate::catch_unwind(|| {
fn inner() -> $ret { $code }
let result = inner();
$crate::to_ocaml(&result)
})
}
};
(fn $name:ident($($param:ident: $ty:ty),* $(,)?) $code:block) => {
$crate::ocaml_ffi_fn! {
fn $name($($param: $ty),*) -> () $code
}
};
}
/// Convenience macro for declaring OCaml FFI wrappers.
///
/// Each parameter will be converted from OCaml using `ocamlrep` and the result
/// will be converted to OCaml using `ocamlrep` and allocated on the OCaml GC
/// heap using `ocamlpool`.
///
/// Panics in the function body will be caught and converted to an OCaml
/// exception of type Failure.
#[macro_export]
macro_rules! ocaml_ffi {
($(fn $name:ident($($param:ident: $ty:ty),* $(,)?) $(-> $ret:ty)? $code:block)*) => {
$($crate::ocaml_ffi_fn! {
fn $name($($param: $ty),*) $(-> $ret)* $code
})*
};
}
#[macro_export]
macro_rules! ocaml_ffi_with_arena_fn {
(fn $name:ident<$lifetime:lifetime>($arena:ident: $arena_ty:ty, $($param:ident: $ty:ty),+ $(,)?) -> $ret:ty $code:block) => {
#[no_mangle]
pub unsafe extern "C" fn $name ($($param: usize,)*) -> usize {
$crate::catch_unwind(|| {
use $crate::FromOcamlRepIn;
let arena = &$crate::Bump::new();
fn inner<$lifetime>($arena: $arena_ty, $($param: usize,)*) -> $ret {
$(let $param = unsafe {
<$ty>::from_ocamlrep_in($crate::Value::from_bits($param), $arena).unwrap()
};)*
$code
}
let result = inner(arena, $($param,)*);
$crate::to_ocaml(&result)
})
}
};
(fn $name:ident<$lifetime:lifetime>($arena:ident: $arena_ty:ty $(,)?) -> $ret:ty $code:block) => {
#[no_mangle]
pub unsafe extern "C" fn $name (_unit: usize) -> usize {
$crate::catch_unwind(|| {
fn inner<$lifetime>($arena: $arena_ty) -> $ret { $code }
let arena = &$crate::Bump::new();
let result = inner(arena);
$crate::to_ocaml(&result)
})
}
};
(fn $name:ident<$lifetime:lifetime>($($param:ident: $ty:ty),* $(,)?) $code:block) => {
$crate::ocaml_ffi_with_arena_fn! {
fn $name<$lifetime>($($param: $ty),*) -> () $code
}
};
}
/// Convenience macro for declaring OCaml FFI wrappers which use an arena to
/// allocate the arguments and return value.
///
/// FFI functions declared with this macro must declare exactly one lifetime
/// parameter. The function's first value parameter must be a reference to a
/// `bumpalo::Bump` arena with that lifetime:
///
/// ```
/// ocaml_ffi_with_arena! {
/// fn swap_str_pair<'a>(arena: &'a Bump, pair: (&'a str, &'a str)) -> (&'a str, &'a str) {
/// (pair.1, pair.0)
/// }
/// }
/// ```
///
/// An OCaml extern declaration for this function would look like this:
///
/// ```
/// external swap_str_pair : string * string -> string * string = "swap_str_pair"
/// ```
///
/// Note that no parameter for the arena appears on the OCaml side--it is
/// constructed on the Rust side and lives only for the duration of one FFI
/// call.
///
/// Each (non-arena) parameter will be converted from OCaml using
/// `ocamlrep::FromOcamlRepIn`, and allocated in the given arena (if its
/// `FromOcamlRepIn` implementation makes use of the arena).
///
/// The return value (which may be allocated in the given arena, if convenient)
/// will be converted to OCaml using `ocamlrep::ToOcamlRep`. The converted OCaml
/// value will be allocated on the OCaml heap using `ocamlpool`.
///
/// Panics in the function body will be caught and converted to an OCaml
/// exception of type `Failure`.
#[macro_export]
macro_rules! ocaml_ffi_with_arena {
($(fn $name:ident<$lifetime:lifetime>($($param:ident: $ty:ty),* $(,)?) $(-> $ret:ty)? $code:block)*) => {
$($crate::ocaml_ffi_with_arena_fn! {
fn $name<$lifetime>($($param: $ty),*) $(-> $ret)* $code
})*
};
}
#[macro_export]
macro_rules! ocaml_ffi_arena_result_fn {
(fn $name:ident<$lifetime:lifetime>($arena:ident: $arena_ty:ty, $($param:ident: $ty:ty),+ $(,)?) -> $ret:ty $code:block) => {
#[no_mangle]
pub unsafe extern "C" fn $name ($($param: usize,)*) -> usize {
$crate::catch_unwind(|| {
fn inner<$lifetime>($arena: $arena_ty, $($param: $ty,)*) -> $ret {
$code
}
use $crate::FromOcamlRep;
$(let $param = <$ty>::from_ocaml($param).unwrap();)*
let arena = &$crate::Bump::new();
let result = inner(arena, $($param,)*);
$crate::to_ocaml(&result)
})
}
};
}
/// Convenience macro for declaring OCaml FFI wrappers which use an arena to
/// just the return value.
///
/// FFI functions declared with this macro must declare exactly one lifetime
/// parameter. The function's first value parameter must be a reference to a
/// `bumpalo::Bump` arena with that lifetime:
///
/// ```
/// ocaml_ffi_arena_result! {
/// fn parse_example<'a>(arena: &'a Bump, text: String) -> ('a str, &'a str) {
/// /* copy parts of text into arena while parsing */
/// (pair.1, pair.0)
/// }
/// }
/// ```
///
/// An OCaml extern declaration for this function would look like this:
///
/// ```
/// external parse_example : string -> string * string = "parse_example"
/// ```
///
/// Note that no parameter for the arena appears on the OCaml side--it is
/// constructed on the Rust side and lives only for the duration of one FFI
/// call.
///
/// Each parameter after the arena parameter will be converted from OCaml using
/// `ocamlrep` and passed in as an owned value.
///
/// The return value (which may be allocated in the given arena, if convenient)
/// will be converted to OCaml using `ocamlrep::ToOcamlRep`. The converted OCaml
/// value will be allocated on the OCaml heap using `ocamlpool`.
///
/// Panics in the function body will be caught and converted to an OCaml
/// exception of type `Failure`.
#[macro_export]
macro_rules! ocaml_ffi_arena_result {
($(fn $name:ident<$lifetime:lifetime>($($param:ident: $ty:ty),* $(,)?) $(-> $ret:ty)? $code:block)*) => {
$($crate::ocaml_ffi_arena_result_fn! {
fn $name<$lifetime>($($param: $ty),*) $(-> $ret)* $code
})*
};
}