use std::any::Any;
use std::cell::RefCell;
use crate::export::class_registry;
use super::NativeClass;
thread_local! {
static CELL: RefCell<Option<Box<dyn Any>>> = RefCell::default();
}
pub fn place<T: NativeClass>(script: T) {
CELL.with(|f| {
if f.replace(Some(Box::new(script))).is_some() {
panic!(
"there is already a value in the emplacement cell (this is a bug in the bindings)"
);
}
});
}
pub fn take<T: NativeClass>() -> Option<T> {
CELL.with(|f| f.borrow_mut().take())
.map(|script| match script.downcast() {
Ok(script) => *script,
Err(any) => panic!(
"expecting {} in the emplacement cell, got {:?} (this is a bug in the bindings)",
class_registry::class_name_or_default::<T>(),
any.type_id(),
),
})
}