1use std::cell::UnsafeCell;
2use std::os::raw::{c_int, c_void};
3use std::rc::Rc;
4
5use crate::error::Result;
6#[cfg(not(feature = "luau"))]
7use crate::hook::Debug;
8use crate::state::{ExtraData, Lua, RawLua};
9
10pub(crate) use sync::{ArcReentrantMutexGuard, ReentrantMutex, ReentrantMutexGuard, XRc, XWeak};
12
13#[cfg(all(feature = "async", feature = "send"))]
14pub(crate) type BoxFuture<'a, T> = futures_util::future::BoxFuture<'a, T>;
15
16#[cfg(all(feature = "async", not(feature = "send")))]
17pub(crate) type BoxFuture<'a, T> = futures_util::future::LocalBoxFuture<'a, T>;
18
19pub use app_data::{AppData, AppDataRef, AppDataRefMut};
20pub use either::Either;
21pub use registry_key::RegistryKey;
22pub(crate) use value_ref::ValueRef;
23
24pub type Integer = ffi::lua_Integer;
26pub type Number = ffi::lua_Number;
28
29#[derive(Debug, Copy, Clone, Eq, PartialEq)]
31pub struct LightUserData(pub *mut c_void);
32
33#[cfg(feature = "send")]
34unsafe impl Send for LightUserData {}
35#[cfg(feature = "send")]
36unsafe impl Sync for LightUserData {}
37
38#[cfg(feature = "send")]
39pub(crate) type Callback = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + Send + 'static>;
40
41#[cfg(not(feature = "send"))]
42pub(crate) type Callback = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + 'static>;
43
44pub(crate) type ScopedCallback<'s> = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + 's>;
45
46pub(crate) struct Upvalue<T> {
47 pub(crate) data: T,
48 pub(crate) extra: XRc<UnsafeCell<ExtraData>>,
49}
50
51pub(crate) type CallbackUpvalue = Upvalue<Option<Callback>>;
52
53#[cfg(all(feature = "async", feature = "send"))]
54pub(crate) type AsyncCallback =
55 Box<dyn for<'a> Fn(&'a RawLua, c_int) -> BoxFuture<'a, Result<c_int>> + Send + 'static>;
56
57#[cfg(all(feature = "async", not(feature = "send")))]
58pub(crate) type AsyncCallback =
59 Box<dyn for<'a> Fn(&'a RawLua, c_int) -> BoxFuture<'a, Result<c_int>> + 'static>;
60
61#[cfg(feature = "async")]
62pub(crate) type AsyncCallbackUpvalue = Upvalue<AsyncCallback>;
63
64#[cfg(feature = "async")]
65pub(crate) type AsyncPollUpvalue = Upvalue<BoxFuture<'static, Result<c_int>>>;
66
67pub enum VmState {
69 Continue,
70 Yield,
74}
75
76#[cfg(all(feature = "send", not(feature = "luau")))]
77pub(crate) type HookCallback = Rc<dyn Fn(&Lua, Debug) -> Result<VmState> + Send>;
78
79#[cfg(all(not(feature = "send"), not(feature = "luau")))]
80pub(crate) type HookCallback = Rc<dyn Fn(&Lua, Debug) -> Result<VmState>>;
81
82#[cfg(all(feature = "send", feature = "luau"))]
83pub(crate) type InterruptCallback = Rc<dyn Fn(&Lua) -> Result<VmState> + Send>;
84
85#[cfg(all(not(feature = "send"), feature = "luau"))]
86pub(crate) type InterruptCallback = Rc<dyn Fn(&Lua) -> Result<VmState>>;
87
88#[cfg(all(feature = "send", feature = "lua54"))]
89pub(crate) type WarnCallback = XRc<dyn Fn(&Lua, &str, bool) -> Result<()> + Send>;
90
91#[cfg(all(not(feature = "send"), feature = "lua54"))]
92pub(crate) type WarnCallback = XRc<dyn Fn(&Lua, &str, bool) -> Result<()>>;
93
94#[cfg(feature = "send")]
96pub trait MaybeSend: Send {}
97#[cfg(feature = "send")]
98impl<T: Send> MaybeSend for T {}
99
100#[cfg(not(feature = "send"))]
101pub trait MaybeSend {}
102#[cfg(not(feature = "send"))]
103impl<T> MaybeSend for T {}
104
105pub(crate) struct DestructedUserdata;
106
107pub(crate) trait LuaType {
108 const TYPE_ID: c_int;
109}
110
111impl LuaType for bool {
112 const TYPE_ID: c_int = ffi::LUA_TBOOLEAN;
113}
114
115impl LuaType for Number {
116 const TYPE_ID: c_int = ffi::LUA_TNUMBER;
117}
118
119impl LuaType for LightUserData {
120 const TYPE_ID: c_int = ffi::LUA_TLIGHTUSERDATA;
121}
122
123mod app_data;
124mod registry_key;
125mod sync;
126mod value_ref;
127
128#[cfg(test)]
129mod assertions {
130 use super::*;
131
132 #[cfg(not(feature = "send"))]
133 static_assertions::assert_not_impl_any!(ValueRef: Send);
134 #[cfg(feature = "send")]
135 static_assertions::assert_impl_all!(ValueRef: Send, Sync);
136}