Skip to content

Commit 4d3803d

Browse files
committed
Split util module
Refactor internal userdata types to switch to the new `TypeKey` trait
1 parent 485921a commit 4d3803d

File tree

8 files changed

+969
-853
lines changed

8 files changed

+969
-853
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ unstable = []
4646
[dependencies]
4747
mlua_derive = { version = "=0.9.3", optional = true, path = "mlua_derive" }
4848
bstr = { version = "1.0", features = ["std"], default-features = false }
49-
once_cell = { version = "1.0" }
5049
num-traits = { version = "0.2.14" }
5150
rustc-hash = "2.0"
5251
futures-util = { version = "0.3", optional = true, default-features = false, features = ["std"] }

src/state/extra.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::error::Result;
1414
use crate::state::RawLua;
1515
use crate::stdlib::StdLib;
1616
use crate::types::{AppData, ReentrantMutex, XRc, XWeak};
17-
use crate::util::{get_gc_metatable, push_gc_userdata, WrappedFailure};
17+
use crate::util::{get_internal_metatable, push_internal_userdata, TypeKey, WrappedFailure};
1818

1919
#[cfg(any(feature = "luau", doc))]
2020
use crate::chunk::Compiler;
@@ -102,6 +102,15 @@ impl Drop for ExtraData {
102102
}
103103
}
104104

105+
static EXTRA_TYPE_KEY: u8 = 0;
106+
107+
impl TypeKey for XRc<UnsafeCell<ExtraData>> {
108+
#[inline(always)]
109+
fn type_key() -> *const c_void {
110+
&EXTRA_TYPE_KEY as *const u8 as *const c_void
111+
}
112+
}
113+
105114
impl ExtraData {
106115
// Index of `error_traceback` function in auxiliary thread stack
107116
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
@@ -120,7 +129,7 @@ impl ExtraData {
120129
);
121130

122131
let wrapped_failure_mt_ptr = {
123-
get_gc_metatable::<WrappedFailure>(state);
132+
get_internal_metatable::<WrappedFailure>(state);
124133
let ptr = ffi::lua_topointer(state, -1);
125134
ffi::lua_pop(state, 1);
126135
ptr
@@ -213,7 +222,7 @@ impl ExtraData {
213222
return Ok(());
214223
}
215224

216-
push_gc_userdata(state, XRc::clone(extra), true)?;
225+
push_internal_userdata(state, XRc::clone(extra), true)?;
217226
protect_lua!(state, 1, 0, fn(state) {
218227
let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void;
219228
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key);

src/state/raw.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use crate::types::{
2323
};
2424
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataRegistry, UserDataVariant};
2525
use crate::util::{
26-
assert_stack, check_stack, get_destructed_userdata_metatable, get_gc_userdata, get_main_state,
27-
get_userdata, init_error_registry, init_gc_metatable, init_userdata_metatable, pop_error,
28-
push_gc_userdata, push_string, push_table, rawset_field, safe_pcall, safe_xpcall, short_type_name,
26+
assert_stack, check_stack, get_destructed_userdata_metatable, get_internal_userdata, get_main_state,
27+
get_userdata, init_error_registry, init_internal_metatable, init_userdata_metatable, pop_error,
28+
push_internal_userdata, push_string, push_table, rawset_field, safe_pcall, safe_xpcall, short_type_name,
2929
StackGuard, WrappedFailure,
3030
};
3131
use crate::value::{FromLuaMulti, IntoLua, MultiValue, Nil, Value};
@@ -171,15 +171,15 @@ impl RawLua {
171171
// Create the internal metatables and store them in the registry
172172
// to prevent from being garbage collected.
173173

174-
init_gc_metatable::<Rc<UnsafeCell<ExtraData>>>(state, None)?;
175-
init_gc_metatable::<Callback>(state, None)?;
176-
init_gc_metatable::<CallbackUpvalue>(state, None)?;
174+
init_internal_metatable::<XRc<UnsafeCell<ExtraData>>>(state, None)?;
175+
init_internal_metatable::<Callback>(state, None)?;
176+
init_internal_metatable::<CallbackUpvalue>(state, None)?;
177177
#[cfg(feature = "async")]
178178
{
179-
init_gc_metatable::<AsyncCallback>(state, None)?;
180-
init_gc_metatable::<AsyncCallbackUpvalue>(state, None)?;
181-
init_gc_metatable::<AsyncPollUpvalue>(state, None)?;
182-
init_gc_metatable::<Option<Waker>>(state, None)?;
179+
init_internal_metatable::<AsyncCallback>(state, None)?;
180+
init_internal_metatable::<AsyncCallbackUpvalue>(state, None)?;
181+
init_internal_metatable::<AsyncPollUpvalue>(state, None)?;
182+
init_internal_metatable::<Option<Waker>>(state, None)?;
183183
}
184184

185185
// Init serde metatables
@@ -550,7 +550,7 @@ impl RawLua {
550550
Value::UserData(ud) => self.push_ref(&ud.0),
551551
Value::Error(err) => {
552552
let protect = !self.unlikely_memory_error();
553-
push_gc_userdata(state, WrappedFailure::Error(*err.clone()), protect)?;
553+
push_internal_userdata(state, WrappedFailure::Error(*err.clone()), protect)?;
554554
}
555555
}
556556
Ok(())
@@ -625,7 +625,7 @@ impl RawLua {
625625
ffi::LUA_TUSERDATA => {
626626
// If the userdata is `WrappedFailure`, process it as an error or panic.
627627
let failure_mt_ptr = (*self.extra.get()).wrapped_failure_mt_ptr;
628-
match get_gc_userdata::<WrappedFailure>(state, idx, failure_mt_ptr).as_mut() {
628+
match get_internal_userdata::<WrappedFailure>(state, idx, failure_mt_ptr).as_mut() {
629629
Some(WrappedFailure::Error(err)) => Value::Error(Box::new(err.clone())),
630630
Some(WrappedFailure::Panic(panic)) => {
631631
if let Some(panic) = panic.take() {
@@ -1076,7 +1076,7 @@ impl RawLua {
10761076
let func = mem::transmute::<Callback, Callback<'static>>(func);
10771077
let extra = XRc::clone(&self.extra);
10781078
let protect = !self.unlikely_memory_error();
1079-
push_gc_userdata(state, CallbackUpvalue { data: func, extra }, protect)?;
1079+
push_internal_userdata(state, CallbackUpvalue { data: func, extra }, protect)?;
10801080
if protect {
10811081
protect_lua!(state, 1, 1, fn(state) {
10821082
ffi::lua_pushcclosure(state, call_callback, 1);
@@ -1115,7 +1115,7 @@ impl RawLua {
11151115
let fut = func(rawlua, args);
11161116
let extra = XRc::clone(&(*upvalue).extra);
11171117
let protect = !rawlua.unlikely_memory_error();
1118-
push_gc_userdata(state, AsyncPollUpvalue { data: fut, extra }, protect)?;
1118+
push_internal_userdata(state, AsyncPollUpvalue { data: fut, extra }, protect)?;
11191119
if protect {
11201120
protect_lua!(state, 1, 1, fn(state) {
11211121
ffi::lua_pushcclosure(state, poll_future, 1);
@@ -1176,7 +1176,7 @@ impl RawLua {
11761176
let extra = XRc::clone(&self.extra);
11771177
let protect = !self.unlikely_memory_error();
11781178
let upvalue = AsyncCallbackUpvalue { data: func, extra };
1179-
push_gc_userdata(state, upvalue, protect)?;
1179+
push_internal_userdata(state, upvalue, protect)?;
11801180
if protect {
11811181
protect_lua!(state, 1, 1, fn(state) {
11821182
ffi::lua_pushcclosure(state, call_callback, 1);

src/state/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55

66
use crate::error::{Error, Result};
77
use crate::state::{ExtraData, RawLua};
8-
use crate::util::{self, get_gc_metatable, WrappedFailure};
8+
use crate::util::{self, get_internal_metatable, WrappedFailure};
99

1010
const WRAPPED_FAILURE_POOL_SIZE: usize = 64;
1111
// const MULTIVALUE_POOL_SIZE: usize = 64;
@@ -137,15 +137,15 @@ where
137137
wrapped_error,
138138
WrappedFailure::Error(Error::CallbackError { traceback, cause }),
139139
);
140-
get_gc_metatable::<WrappedFailure>(state);
140+
get_internal_metatable::<WrappedFailure>(state);
141141
ffi::lua_setmetatable(state, -2);
142142

143143
ffi::lua_error(state)
144144
}
145145
Err(p) => {
146146
let wrapped_panic = prealloc_failure.r#use(state, extra);
147147
ptr::write(wrapped_panic, WrappedFailure::Panic(Some(p)));
148-
get_gc_metatable::<WrappedFailure>(state);
148+
get_internal_metatable::<WrappedFailure>(state);
149149
ffi::lua_setmetatable(state, -2);
150150
ffi::lua_error(state)
151151
}

0 commit comments

Comments
 (0)