leanfran_runtime/
handle.rs1use crate::marshal::*;
18use crate::object::*;
19use core::cell::Cell;
20use core::fmt;
21use core::marker::PhantomData;
22
23pub unsafe trait LeanRepr {
31 type Value;
33
34 #[doc(hidden)]
40 unsafe fn to_obj(value: Self::Value) -> Obj;
41
42 #[doc(hidden)]
48 unsafe fn from_obj(o: Obj) -> Self::Value;
49}
50
51pub struct List<T>(PhantomData<fn() -> T>);
53
54pub struct Array<T>(PhantomData<fn() -> T>);
56
57pub struct Fin<const N: u64>;
59
60pub struct Lean<L: LeanRepr> {
66 obj: Obj,
67 ty: PhantomData<fn() -> L>,
68}
69
70unsafe impl<L: LeanRepr> Send for Lean<L> {}
74unsafe impl<L: LeanRepr> Sync for Lean<L> {}
75
76impl<L: LeanRepr> Lean<L> {
77 pub fn new(value: L::Value) -> Self {
79 unsafe { Self::from_owned(L::to_obj(value)) }
80 }
81
82 pub fn get(&self) -> L::Value {
84 unsafe { L::from_obj(self.obj) }
85 }
86
87 #[doc(hidden)]
94 pub unsafe fn from_owned(obj: Obj) -> Self {
95 lean_mark_mt(obj);
96 Lean {
97 obj,
98 ty: PhantomData,
99 }
100 }
101
102 #[doc(hidden)]
104 pub fn as_obj(&self) -> Obj {
105 self.obj
106 }
107}
108
109impl<L: LeanRepr> Lean<L> {
110 pub fn to_local(&self) -> Local<L> {
113 unsafe {
114 lean_inc(self.obj);
115 Local::from_owned(self.obj)
116 }
117 }
118}
119
120impl<L: LeanRepr> Clone for Lean<L> {
121 fn clone(&self) -> Self {
122 unsafe { lean_inc(self.obj) };
123 Lean {
124 obj: self.obj,
125 ty: PhantomData,
126 }
127 }
128}
129
130impl<L: LeanRepr> Drop for Lean<L> {
131 fn drop(&mut self) {
132 unsafe { lean_dec(self.obj) };
133 }
134}
135
136impl<L: LeanRepr> fmt::Debug for Lean<L>
137where
138 L::Value: fmt::Debug,
139{
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 f.debug_tuple("Lean").field(&self.get()).finish()
142 }
143}
144
145pub struct Local<L: LeanRepr> {
165 obj: Obj,
166 scope: u64,
168 ty: PhantomData<fn() -> L>,
169 thread: PhantomData<*mut ()>,
170}
171
172thread_local! {
173 static SCOPE: Cell<u64> = const { Cell::new(0) };
176 static SCOPE_LIVE: Cell<usize> = const { Cell::new(0) };
177 static SCOPE_LAST: Cell<u64> = const { Cell::new(0) };
178}
179
180#[inline]
182fn enter_scope() -> u64 {
183 let scope = SCOPE.get();
184 if scope != 0 {
185 SCOPE_LIVE.set(SCOPE_LIVE.get() + 1);
186 }
187 scope
188}
189
190#[inline]
193fn leave_scope(scope: u64) {
194 if scope != 0 && scope == SCOPE.get() {
195 SCOPE_LIVE.set(SCOPE_LIVE.get() - 1);
196 }
197}
198
199impl<L: LeanRepr> Local<L> {
200 pub fn new(value: L::Value) -> Self {
202 unsafe { Self::from_owned(L::to_obj(value)) }
203 }
204
205 pub fn get(&self) -> L::Value {
207 unsafe { L::from_obj(self.obj) }
208 }
209
210 pub fn share(self) -> Lean<L> {
213 unsafe { Lean::from_owned(self.release()) }
214 }
215
216 #[inline]
220 fn settle(&self) {
221 if self.scope != SCOPE.get() {
222 unsafe { lean_mark_mt(self.obj) };
223 }
224 }
225
226 #[inline]
228 fn release(self) -> Obj {
229 self.settle();
230 leave_scope(self.scope);
231 let obj = self.obj;
232 core::mem::forget(self);
233 obj
234 }
235
236 #[doc(hidden)]
242 pub unsafe fn from_owned(obj: Obj) -> Self {
243 Local {
244 obj,
245 scope: enter_scope(),
246 ty: PhantomData,
247 thread: PhantomData,
248 }
249 }
250
251 #[doc(hidden)]
253 pub fn as_obj(&self) -> Obj {
254 self.obj
255 }
256}
257
258impl<L: LeanRepr> Clone for Local<L> {
259 fn clone(&self) -> Self {
260 self.settle();
261 unsafe {
262 lean_inc(self.obj);
263 Local::from_owned(self.obj)
264 }
265 }
266}
267
268impl<L: LeanRepr> Drop for Local<L> {
269 fn drop(&mut self) {
270 leave_scope(self.scope);
271 unsafe { lean_dec(self.obj) };
272 }
273}
274
275impl<L: LeanRepr> fmt::Debug for Local<L>
276where
277 L::Value: fmt::Debug,
278{
279 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
280 f.debug_tuple("Local").field(&self.get()).finish()
281 }
282}
283
284pub unsafe trait IntoLean<L: LeanRepr> {
292 #[doc(hidden)]
298 unsafe fn into_obj(self) -> Obj;
299}
300
301unsafe impl<L: LeanRepr> IntoLean<L> for Lean<L> {
302 unsafe fn into_obj(self) -> Obj {
303 let obj = self.obj;
304 core::mem::forget(self);
305 obj
306 }
307}
308
309unsafe impl<L: LeanRepr> IntoLean<L> for &Lean<L> {
310 unsafe fn into_obj(self) -> Obj {
311 lean_inc(self.obj);
312 self.obj
313 }
314}
315
316unsafe impl<L: LeanRepr> IntoLean<L> for Local<L> {
317 unsafe fn into_obj(self) -> Obj {
318 self.release()
319 }
320}
321
322unsafe impl<L: LeanRepr> IntoLean<L> for &Local<L> {
323 unsafe fn into_obj(self) -> Obj {
324 self.settle();
325 lean_inc(self.obj);
326 self.obj
327 }
328}
329
330pub struct Owned<L: LeanRepr> {
364 obj: Obj,
366 ty: PhantomData<fn() -> L>,
367}
368
369unsafe impl<L: LeanRepr> Send for Owned<L> {}
374
375impl<L: LeanRepr> Owned<L> {
376 pub fn new(value: L::Value) -> Self {
378 Owned {
379 obj: unsafe { L::to_obj(value) },
380 ty: PhantomData,
381 }
382 }
383
384 fn obj(&self) -> Obj {
385 assert!(
386 !self.obj.is_null(),
387 "the Owned value was lost: a closure of `with` panicked"
388 );
389 self.obj
390 }
391
392 pub fn get(&self) -> L::Value {
394 unsafe { L::from_obj(self.obj()) }
395 }
396
397 pub fn with<R>(&mut self, f: impl FnOnce(Local<L>) -> (Local<L>, R)) -> R {
405 struct Restore(u64, usize);
406 impl Drop for Restore {
407 fn drop(&mut self) {
408 SCOPE.set(self.0);
409 SCOPE_LIVE.set(self.1);
410 }
411 }
412 let obj = core::mem::replace(&mut self.obj, core::ptr::null_mut());
413 assert!(
414 !obj.is_null(),
415 "the Owned value was lost: a closure of `with` panicked"
416 );
417 let scope = SCOPE_LAST.get() + 1;
418 SCOPE_LAST.set(scope);
419 let restore = Restore(SCOPE.replace(scope), SCOPE_LIVE.replace(0));
420 let (value, result) = f(unsafe { Local::from_owned(obj) });
421 let alone = value.scope == scope && SCOPE_LIVE.get() == 1;
422 let obj = value.obj;
423 core::mem::forget(value);
424 if !alone {
425 unsafe { lean_mark_mt(obj) };
426 }
427 drop(restore);
428 self.obj = obj;
429 result
430 }
431
432 pub fn share(self) -> Lean<L> {
434 let obj = self.obj();
435 core::mem::forget(self);
436 unsafe { Lean::from_owned(obj) }
437 }
438}
439
440impl<L: LeanRepr> From<Lean<L>> for Owned<L> {
441 fn from(value: Lean<L>) -> Self {
444 let obj = value.obj;
445 core::mem::forget(value);
446 Owned {
447 obj,
448 ty: PhantomData,
449 }
450 }
451}
452
453impl<L: LeanRepr> Drop for Owned<L> {
454 fn drop(&mut self) {
455 if !self.obj.is_null() {
456 unsafe { lean_dec(self.obj) };
457 }
458 }
459}
460
461impl<L: LeanRepr> fmt::Debug for Owned<L>
462where
463 L::Value: fmt::Debug,
464{
465 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
466 f.debug_tuple("Owned").field(&self.get()).finish()
467 }
468}
469
470macro_rules! prim_repr {
471 ($($t:ty),*) => {$(
472 unsafe impl LeanRepr for $t {
473 type Value = $t;
474 unsafe fn to_obj(value: $t) -> Obj {
475 ToLean::to_lean(value)
476 }
477 unsafe fn from_obj(o: Obj) -> $t {
478 <$t as FromLean>::from_lean(o)
479 }
480 }
481 unsafe impl IntoLean<$t> for $t {
482 unsafe fn into_obj(self) -> Obj {
483 ToLean::to_lean(self)
484 }
485 }
486 )*};
487}
488
489prim_repr!(
490 Nat,
491 Int,
492 bool,
493 char,
494 String,
495 u8,
496 u16,
497 u32,
498 u64,
499 usize,
500 i8,
501 i16,
502 i32,
503 i64,
504 isize,
505 f64,
506 f32,
507 ()
508);
509
510unsafe impl IntoLean<String> for &str {
511 unsafe fn into_obj(self) -> Obj {
512 ToLean::to_lean(self)
513 }
514}
515
516unsafe impl IntoLean<String> for &String {
517 unsafe fn into_obj(self) -> Obj {
518 ToLean::to_lean(self.as_str())
519 }
520}
521
522unsafe impl<T: LeanRepr> LeanRepr for List<T> {
523 type Value = Vec<T::Value>;
524 unsafe fn to_obj(value: Self::Value) -> Obj {
525 list_to_lean(value, |v| T::to_obj(v))
526 }
527 unsafe fn from_obj(o: Obj) -> Self::Value {
528 list_from_lean(o, |v| T::from_obj(v))
529 }
530}
531
532unsafe impl<T: LeanRepr> IntoLean<List<T>> for Vec<T::Value> {
533 unsafe fn into_obj(self) -> Obj {
534 List::<T>::to_obj(self)
535 }
536}
537
538unsafe impl<T: LeanRepr> LeanRepr for Array<T> {
539 type Value = Vec<T::Value>;
540 unsafe fn to_obj(value: Self::Value) -> Obj {
541 array_to_lean(value, |v| T::to_obj(v))
542 }
543 unsafe fn from_obj(o: Obj) -> Self::Value {
544 array_from_lean(o, |v| T::from_obj(v))
545 }
546}
547
548unsafe impl<T: LeanRepr> IntoLean<Array<T>> for Vec<T::Value> {
549 unsafe fn into_obj(self) -> Obj {
550 Array::<T>::to_obj(self)
551 }
552}
553
554unsafe impl<T: LeanRepr> LeanRepr for Option<T> {
555 type Value = Option<T::Value>;
556 unsafe fn to_obj(value: Self::Value) -> Obj {
557 option_to_lean(value, |v| T::to_obj(v))
558 }
559 unsafe fn from_obj(o: Obj) -> Self::Value {
560 option_from_lean(o, |v| T::from_obj(v))
561 }
562}
563
564unsafe impl<T: LeanRepr> IntoLean<Option<T>> for Option<T::Value> {
565 unsafe fn into_obj(self) -> Obj {
566 Option::<T>::to_obj(self)
567 }
568}
569
570unsafe impl<T: LeanRepr, E: LeanRepr> LeanRepr for Result<T, E> {
571 type Value = Result<T::Value, E::Value>;
572 unsafe fn to_obj(value: Self::Value) -> Obj {
573 except_to_lean(value, |v| E::to_obj(v), |v| T::to_obj(v))
574 }
575 unsafe fn from_obj(o: Obj) -> Self::Value {
576 except_from_lean(o, |v| E::from_obj(v), |v| T::from_obj(v))
577 }
578}
579
580unsafe impl<T: LeanRepr, E: LeanRepr> IntoLean<Result<T, E>> for Result<T::Value, E::Value> {
581 unsafe fn into_obj(self) -> Obj {
582 Result::<T, E>::to_obj(self)
583 }
584}
585
586unsafe impl<A: LeanRepr, B: LeanRepr> LeanRepr for (A, B) {
587 type Value = (A::Value, B::Value);
588 unsafe fn to_obj(value: Self::Value) -> Obj {
589 prod_to_lean(value, |v| A::to_obj(v), |v| B::to_obj(v))
590 }
591 unsafe fn from_obj(o: Obj) -> Self::Value {
592 prod_from_lean(o, |v| A::from_obj(v), |v| B::from_obj(v))
593 }
594}
595
596unsafe impl<A: LeanRepr, B: LeanRepr> IntoLean<(A, B)> for (A::Value, B::Value) {
597 unsafe fn into_obj(self) -> Obj {
598 <(A, B)>::to_obj(self)
599 }
600}
601
602unsafe impl<const N: u64> LeanRepr for Fin<N> {
603 type Value = usize;
604 unsafe fn to_obj(value: usize) -> Obj {
605 fin_to_lean(value, N as usize)
606 }
607 unsafe fn from_obj(o: Obj) -> usize {
608 fin_from_lean(o)
609 }
610}
611
612unsafe impl<const N: u64> IntoLean<Fin<N>> for usize {
613 unsafe fn into_obj(self) -> Obj {
614 Fin::<N>::to_obj(self)
615 }
616}