Skip to content

Commit cd6d86a

Browse files
committed
Split single lua module to multiple submodules under state
1 parent 8b2d067 commit cd6d86a

File tree

23 files changed

+3888
-3896
lines changed

23 files changed

+3888
-3896
lines changed

src/chunk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::string::String as StdString;
77

88
use crate::error::{Error, ErrorContext, Result};
99
use crate::function::Function;
10-
use crate::lua::{Lua, WeakLua};
10+
use crate::state::{Lua, WeakLua};
1111
use crate::table::Table;
1212
use crate::value::{FromLuaMulti, IntoLua, IntoLuaMulti};
1313

src/conversion.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use num_traits::cast;
1212

1313
use crate::error::{Error, Result};
1414
use crate::function::Function;
15-
use crate::lua::{Lua, LuaInner};
15+
use crate::state::{Lua, RawLua};
1616
use crate::string::String;
1717
use crate::table::Table;
1818
use crate::thread::Thread;
@@ -34,7 +34,7 @@ impl IntoLua for &Value {
3434
}
3535

3636
#[inline]
37-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
37+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
3838
lua.push_value(self)
3939
}
4040
}
@@ -60,7 +60,7 @@ impl IntoLua for &String {
6060
}
6161

6262
#[inline]
63-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
63+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
6464
lua.push_ref(&self.0);
6565
Ok(())
6666
}
@@ -93,7 +93,7 @@ impl IntoLua for &Table {
9393
}
9494

9595
#[inline]
96-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
96+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
9797
lua.push_ref(&self.0);
9898
Ok(())
9999
}
@@ -127,7 +127,7 @@ impl IntoLua for &Function {
127127
}
128128

129129
#[inline]
130-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
130+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
131131
lua.push_ref(&self.0);
132132
Ok(())
133133
}
@@ -161,7 +161,7 @@ impl IntoLua for &Thread {
161161
}
162162

163163
#[inline]
164-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
164+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
165165
lua.push_ref(&self.0);
166166
Ok(())
167167
}
@@ -195,7 +195,7 @@ impl IntoLua for &AnyUserData {
195195
}
196196

197197
#[inline]
198-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
198+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
199199
lua.push_ref(&self.0);
200200
Ok(())
201201
}
@@ -250,7 +250,7 @@ impl IntoLua for RegistryKey {
250250
}
251251

252252
#[inline]
253-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
253+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
254254
<&RegistryKey>::push_into_stack(&self, lua)
255255
}
256256
}
@@ -261,7 +261,7 @@ impl IntoLua for &RegistryKey {
261261
lua.registry_value(self)
262262
}
263263

264-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
264+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
265265
if !lua.owns_registry_value(self) {
266266
return Err(Error::MismatchedRegistryKey);
267267
}
@@ -290,7 +290,7 @@ impl IntoLua for bool {
290290
}
291291

292292
#[inline]
293-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
293+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
294294
ffi::lua_pushboolean(lua.state(), self as c_int);
295295
Ok(())
296296
}
@@ -307,7 +307,7 @@ impl FromLua for bool {
307307
}
308308

309309
#[inline]
310-
unsafe fn from_stack(idx: c_int, lua: &LuaInner) -> Result<Self> {
310+
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
311311
Ok(ffi::lua_toboolean(lua.state(), idx) != 0)
312312
}
313313
}
@@ -363,7 +363,7 @@ impl IntoLua for StdString {
363363
}
364364

365365
#[inline]
366-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
366+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
367367
push_bytes_into_stack(self, lua)
368368
}
369369
}
@@ -384,7 +384,7 @@ impl FromLua for StdString {
384384
}
385385

386386
#[inline]
387-
unsafe fn from_stack(idx: c_int, lua: &LuaInner) -> Result<Self> {
387+
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
388388
let state = lua.state();
389389
if ffi::lua_type(state, idx) == ffi::LUA_TSTRING {
390390
let mut size = 0;
@@ -410,7 +410,7 @@ impl IntoLua for &str {
410410
}
411411

412412
#[inline]
413-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
413+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
414414
push_bytes_into_stack(self, lua)
415415
}
416416
}
@@ -522,7 +522,7 @@ impl FromLua for BString {
522522
}
523523
}
524524

525-
unsafe fn from_stack(idx: c_int, lua: &LuaInner) -> Result<Self> {
525+
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
526526
let state = lua.state();
527527
match ffi::lua_type(state, idx) {
528528
ffi::LUA_TSTRING => {
@@ -553,7 +553,7 @@ impl IntoLua for &BStr {
553553
}
554554

555555
#[inline]
556-
unsafe fn push_bytes_into_stack<T>(this: T, lua: &LuaInner) -> Result<()>
556+
unsafe fn push_bytes_into_stack<T>(this: T, lua: &RawLua) -> Result<()>
557557
where
558558
T: IntoLua + AsRef<[u8]>,
559559
{
@@ -584,7 +584,7 @@ macro_rules! lua_convert_int {
584584
}
585585

586586
#[inline]
587-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
587+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
588588
match cast(self) {
589589
Some(i) => ffi::lua_pushinteger(lua.state(), i),
590590
None => ffi::lua_pushnumber(lua.state(), self as ffi::lua_Number),
@@ -881,7 +881,7 @@ impl<T: IntoLua> IntoLua for Option<T> {
881881
}
882882

883883
#[inline]
884-
unsafe fn push_into_stack(self, lua: &LuaInner) -> Result<()> {
884+
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
885885
match self {
886886
Some(val) => val.push_into_stack(lua)?,
887887
None => ffi::lua_pushnil(lua.state()),
@@ -900,7 +900,7 @@ impl<T: FromLua> FromLua for Option<T> {
900900
}
901901

902902
#[inline]
903-
unsafe fn from_stack(idx: c_int, lua: &LuaInner) -> Result<Self> {
903+
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
904904
if ffi::lua_isnil(lua.state(), idx) != 0 {
905905
Ok(None)
906906
} else {

src/function.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ptr;
55
use std::slice;
66

77
use crate::error::{Error, Result};
8-
use crate::lua::Lua;
8+
use crate::state::Lua;
99
use crate::table::Table;
1010
use crate::types::{Callback, MaybeSend, ValueRef};
1111
use crate::util::{
@@ -161,11 +161,13 @@ impl Function {
161161
R: FromLuaMulti,
162162
{
163163
let lua = self.0.lua.lock();
164-
let thread_res = lua.create_recycled_thread(self).map(|th| {
165-
let mut th = th.into_async(args);
166-
th.set_recyclable(true);
167-
th
168-
});
164+
let thread_res = unsafe {
165+
lua.create_recycled_thread(self).map(|th| {
166+
let mut th = th.into_async(args);
167+
th.set_recyclable(true);
168+
th
169+
})
170+
};
169171
async move { thread_res?.await }
170172
}
171173

src/hook.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::borrow::Cow;
22
use std::cell::UnsafeCell;
3-
use std::mem::ManuallyDrop;
3+
use std::ops::Deref;
44
#[cfg(not(feature = "luau"))]
55
use std::ops::{BitOr, BitOrAssign};
66
use std::os::raw::c_int;
77

88
use ffi::lua_Debug;
99
use parking_lot::ReentrantMutexGuard;
1010

11-
use crate::lua::{Lua, LuaInner};
11+
use crate::state::RawLua;
1212
use crate::util::{linenumber_to_usize, ptr_to_lossy_str, ptr_to_str};
1313

1414
/// Contains information about currently executing Lua code.
@@ -20,38 +20,46 @@ use crate::util::{linenumber_to_usize, ptr_to_lossy_str, ptr_to_str};
2020
///
2121
/// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#lua_Debug
2222
/// [`Lua::set_hook`]: crate::Lua::set_hook
23-
pub struct Debug<'lua> {
24-
lua: ManuallyDrop<ReentrantMutexGuard<'lua, LuaInner>>,
23+
pub struct Debug<'a> {
24+
lua: EitherLua<'a>,
2525
ar: ActivationRecord,
2626
#[cfg(feature = "luau")]
2727
level: c_int,
2828
}
2929

30-
impl<'lua> Drop for Debug<'lua> {
31-
fn drop(&mut self) {
32-
if let ActivationRecord::Owned(_) = self.ar {
33-
unsafe { ManuallyDrop::drop(&mut self.lua) }
30+
enum EitherLua<'a> {
31+
Owned(ReentrantMutexGuard<'a, RawLua>),
32+
Borrowed(&'a RawLua),
33+
}
34+
35+
impl Deref for EitherLua<'_> {
36+
type Target = RawLua;
37+
38+
fn deref(&self) -> &Self::Target {
39+
match self {
40+
EitherLua::Owned(guard) => &*guard,
41+
EitherLua::Borrowed(lua) => lua,
3442
}
3543
}
3644
}
3745

38-
impl<'lua> Debug<'lua> {
46+
impl<'a> Debug<'a> {
3947
// We assume the lock is held when this function is called.
4048
#[cfg(not(feature = "luau"))]
41-
pub(crate) fn new(lua: &'lua Lua, ar: *mut lua_Debug) -> Self {
49+
pub(crate) fn new(lua: &'a RawLua, ar: *mut lua_Debug) -> Self {
4250
Debug {
43-
lua: unsafe { lua.guard_unchecked() },
51+
lua: EitherLua::Borrowed(lua),
4452
ar: ActivationRecord::Borrowed(ar),
4553
}
4654
}
4755

4856
pub(crate) fn new_owned(
49-
guard: ReentrantMutexGuard<'lua, LuaInner>,
57+
guard: ReentrantMutexGuard<'a, RawLua>,
5058
_level: c_int,
5159
ar: lua_Debug,
5260
) -> Self {
5361
Debug {
54-
lua: ManuallyDrop::new(guard),
62+
lua: EitherLua::Owned(guard),
5563
ar: ActivationRecord::Owned(UnsafeCell::new(ar)),
5664
#[cfg(feature = "luau")]
5765
level: _level,

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ mod conversion;
8484
mod error;
8585
mod function;
8686
mod hook;
87-
mod lua;
8887
#[cfg(feature = "luau")]
8988
mod luau;
9089
mod memory;
9190
mod multi;
9291
// mod scope;
92+
mod state;
9393
mod stdlib;
9494
mod string;
9595
mod table;
@@ -107,7 +107,7 @@ pub use crate::chunk::{AsChunk, Chunk, ChunkMode};
107107
pub use crate::error::{Error, ErrorContext, ExternalError, ExternalResult, Result};
108108
pub use crate::function::{Function, FunctionInfo};
109109
pub use crate::hook::{Debug, DebugEvent, DebugNames, DebugSource, DebugStack};
110-
pub use crate::lua::{GCMode, Lua, LuaOptions};
110+
pub use crate::state::{GCMode, Lua, LuaOptions};
111111
pub use crate::multi::Variadic;
112112
// pub use crate::scope::Scope;
113113
pub use crate::stdlib::StdLib;

0 commit comments

Comments
 (0)