use crate::closure::lean_apply_1;
use crate::object::*;
use crate::panic::lean_internal_panic;
use core::mem::size_of;
use core::ptr;
#[inline]
unsafe fn to_thunk(o: Obj) -> *mut ThunkObject {
o as *mut ThunkObject
}
#[inline]
unsafe fn to_task(o: Obj) -> *mut TaskObject {
o as *mut TaskObject
}
#[inline]
pub unsafe fn lean_mk_thunk(c: Obj) -> Obj {
let o = lean_alloc_small_object(size_of::<ThunkObject>());
lean_set_st_header(o, LEAN_THUNK as u32, 0);
(*to_thunk(o)).m_value = ptr::null_mut();
(*to_thunk(o)).m_closure = c;
o
}
#[inline]
pub unsafe fn lean_thunk_pure(v: Obj) -> Obj {
let o = lean_alloc_small_object(size_of::<ThunkObject>());
lean_set_st_header(o, LEAN_THUNK as u32, 0);
(*to_thunk(o)).m_value = v;
(*to_thunk(o)).m_closure = ptr::null_mut();
o
}
pub unsafe fn lean_thunk_get_core(t: Obj) -> Obj {
let c = (*to_thunk(t)).m_closure;
if c.is_null() {
lean_internal_panic("thunk forced while it is being evaluated");
}
(*to_thunk(t)).m_closure = ptr::null_mut();
let r = lean_apply_1(c, lean_box(0));
(*to_thunk(t)).m_value = r;
r
}
#[inline]
pub unsafe fn lean_thunk_get(t: Obj) -> Obj {
debug_check_live(t, "thunk read");
let r = (*to_thunk(t)).m_value;
if !r.is_null() {
return r;
}
lean_thunk_get_core(t)
}
#[inline]
pub unsafe fn lean_thunk_get_own(t: Obj) -> Obj {
let r = lean_thunk_get(t);
lean_inc(r);
r
}
pub unsafe fn lean_task_pure(a: Obj) -> Obj {
let o = lean_alloc_small_object(size_of::<TaskObject>());
lean_set_st_header(o, LEAN_TASK as u32, 0);
(*to_task(o)).m_value = a;
(*to_task(o)).m_imp = ptr::null_mut();
o
}
pub unsafe fn lean_task_spawn_core(c: Obj, _prio: u32, _keep_alive: bool) -> Obj {
lean_task_pure(lean_apply_1(c, lean_box(0)))
}
#[inline]
pub unsafe fn lean_task_spawn(c: Obj, prio: Obj) -> Obj {
lean_dec(prio);
lean_task_spawn_core(c, 0, false)
}
#[inline]
pub unsafe fn lean_task_get(t: Obj) -> Obj {
debug_check_live(t, "task read");
let v = (*to_task(t)).m_value;
if v.is_null() {
lean_internal_panic(
"the result of a promise that is not resolved is read (tasks run synchronously, so nothing can resolve it later)",
);
}
v
}
#[inline]
pub unsafe fn lean_task_get_own(t: Obj) -> Obj {
let r = lean_task_get(t);
lean_inc(r);
lean_dec(t);
r
}
pub unsafe fn lean_task_map_core(
f: Obj,
t: Obj,
_prio: u32,
_sync: bool,
_keep_alive: bool,
) -> Obj {
let v = lean_task_get(t);
lean_inc(v);
lean_dec(t);
lean_task_pure(lean_apply_1(f, v))
}
#[inline]
pub unsafe fn lean_task_map(f: Obj, t: Obj, prio: Obj, sync: u8) -> Obj {
lean_dec(prio);
lean_task_map_core(f, t, 0, sync != 0, false)
}
pub unsafe fn lean_task_bind_core(
x: Obj,
f: Obj,
_prio: u32,
_sync: bool,
_keep_alive: bool,
) -> Obj {
let v = lean_task_get(x);
lean_inc(v);
lean_dec(x);
lean_apply_1(f, v)
}
#[inline]
pub unsafe fn lean_task_bind(x: Obj, f: Obj, prio: Obj, sync: u8) -> Obj {
lean_dec(prio);
lean_task_bind_core(x, f, 0, sync != 0, false)
}
pub unsafe fn lean_io_check_canceled_core() -> bool {
false
}
pub unsafe fn lean_io_cancel_core(_t: Obj) {}
pub unsafe fn lean_io_get_task_state_core(t: Obj) -> u8 {
if (*to_task(t)).m_value.is_null() {
1
} else {
2
}
}
pub unsafe fn lean_io_as_task(act: Obj, prio: Obj) -> Obj {
lean_dec(prio);
lean_task_pure(lean_apply_1(act, lean_box(0)))
}
pub unsafe fn lean_io_map_task(f: Obj, t: Obj, prio: Obj, _sync: u8) -> Obj {
lean_dec(prio);
let v = lean_task_get(t);
lean_inc(v);
lean_dec(t);
lean_task_pure(crate::closure::lean_apply_2(f, v, lean_box(0)))
}
pub unsafe fn lean_io_bind_task(t: Obj, f: Obj, prio: Obj, _sync: u8) -> Obj {
lean_dec(prio);
let v = lean_task_get(t);
lean_inc(v);
lean_dec(t);
crate::closure::lean_apply_2(f, v, lean_box(0))
}
pub unsafe fn lean_io_check_canceled() -> u8 {
lean_io_check_canceled_core() as u8
}
pub unsafe fn lean_io_cancel(t: Obj) -> Obj {
lean_io_cancel_core(t);
lean_box(0)
}
pub unsafe fn lean_io_get_task_state(t: Obj) -> u8 {
lean_io_get_task_state_core(t)
}
pub unsafe fn lean_io_wait(t: Obj) -> Obj {
lean_task_get_own(t)
}
pub unsafe fn lean_io_wait_any(tasks: Obj) -> Obj {
let v = lean_task_get(lean_ctor_get(tasks, 0));
lean_inc(v);
v
}
unsafe fn promise_finalize(p: *mut core::ffi::c_void) {
let t = *Box::from_raw(p as *mut Obj);
if (*to_task(t)).m_value.is_null() {
(*to_task(t)).m_value = lean_box(0);
}
lean_dec(t);
}
unsafe fn promise_foreach(_p: *mut core::ffi::c_void, _f: Obj) {}
static PROMISE_CLASS: ExternalClass = ExternalClass {
m_finalize: promise_finalize,
m_foreach: promise_foreach,
};
unsafe fn promise_task(p: Obj) -> Obj {
*(lean_get_external_data(p) as *const Obj)
}
pub unsafe fn lean_io_promise_new() -> Obj {
let t = lean_alloc_small_object(size_of::<TaskObject>());
lean_set_st_header(t, LEAN_TASK as u32, 0);
(*to_task(t)).m_value = ptr::null_mut();
(*to_task(t)).m_imp = ptr::null_mut();
lean_alloc_external(
&PROMISE_CLASS,
Box::into_raw(Box::new(t)) as *mut core::ffi::c_void,
)
}
pub unsafe fn lean_io_promise_resolve(value: Obj, p: Obj) -> Obj {
let t = promise_task(p);
if (*to_task(t)).m_value.is_null() {
let s = lean_alloc_ctor(1, 1, 0);
lean_ctor_set(s, 0, value);
(*to_task(t)).m_value = s;
} else {
lean_dec(value);
}
lean_box(0)
}
pub unsafe fn lean_io_promise_result_opt(p: Obj) -> Obj {
let t = promise_task(p);
lean_inc(t);
t
}
pub unsafe fn lean_option_get_or_block(o: Obj) -> Obj {
if lean_is_scalar(o) {
crate::panic::lean_panic(
"PANIC: Promise.result!: promise has been dropped without ever being resolved",
true,
);
lean_internal_panic(
"Promise.result!: promise has been dropped without ever being resolved",
);
}
let v = lean_ctor_get(o, 0);
lean_inc(v);
lean_dec(o);
v
}