Skip to content

Commit

Permalink
Split util module
Browse files Browse the repository at this point in the history
Refactor internal userdata types to switch to the new `TypeKey` trait
  • Loading branch information
khvzak committed Jul 29, 2024
1 parent 485921a commit 4d3803d
Show file tree
Hide file tree
Showing 8 changed files with 969 additions and 853 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ unstable = []
[dependencies]
mlua_derive = { version = "=0.9.3", optional = true, path = "mlua_derive" }
bstr = { version = "1.0", features = ["std"], default-features = false }
once_cell = { version = "1.0" }
num-traits = { version = "0.2.14" }
rustc-hash = "2.0"
futures-util = { version = "0.3", optional = true, default-features = false, features = ["std"] }
Expand Down
15 changes: 12 additions & 3 deletions src/state/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::error::Result;
use crate::state::RawLua;
use crate::stdlib::StdLib;
use crate::types::{AppData, ReentrantMutex, XRc, XWeak};
use crate::util::{get_gc_metatable, push_gc_userdata, WrappedFailure};
use crate::util::{get_internal_metatable, push_internal_userdata, TypeKey, WrappedFailure};

#[cfg(any(feature = "luau", doc))]
use crate::chunk::Compiler;
Expand Down Expand Up @@ -102,6 +102,15 @@ impl Drop for ExtraData {
}
}

static EXTRA_TYPE_KEY: u8 = 0;

impl TypeKey for XRc<UnsafeCell<ExtraData>> {
#[inline(always)]
fn type_key() -> *const c_void {
&EXTRA_TYPE_KEY as *const u8 as *const c_void
}
}

impl ExtraData {
// Index of `error_traceback` function in auxiliary thread stack
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
Expand All @@ -120,7 +129,7 @@ impl ExtraData {
);

let wrapped_failure_mt_ptr = {
get_gc_metatable::<WrappedFailure>(state);
get_internal_metatable::<WrappedFailure>(state);
let ptr = ffi::lua_topointer(state, -1);
ffi::lua_pop(state, 1);
ptr
Expand Down Expand Up @@ -213,7 +222,7 @@ impl ExtraData {
return Ok(());
}

push_gc_userdata(state, XRc::clone(extra), true)?;
push_internal_userdata(state, XRc::clone(extra), true)?;
protect_lua!(state, 1, 0, fn(state) {
let extra_key = &EXTRA_REGISTRY_KEY as *const u8 as *const c_void;
ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, extra_key);
Expand Down
30 changes: 15 additions & 15 deletions src/state/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use crate::types::{
};
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataRegistry, UserDataVariant};
use crate::util::{
assert_stack, check_stack, get_destructed_userdata_metatable, get_gc_userdata, get_main_state,
get_userdata, init_error_registry, init_gc_metatable, init_userdata_metatable, pop_error,
push_gc_userdata, push_string, push_table, rawset_field, safe_pcall, safe_xpcall, short_type_name,
assert_stack, check_stack, get_destructed_userdata_metatable, get_internal_userdata, get_main_state,
get_userdata, init_error_registry, init_internal_metatable, init_userdata_metatable, pop_error,
push_internal_userdata, push_string, push_table, rawset_field, safe_pcall, safe_xpcall, short_type_name,
StackGuard, WrappedFailure,
};
use crate::value::{FromLuaMulti, IntoLua, MultiValue, Nil, Value};
Expand Down Expand Up @@ -171,15 +171,15 @@ impl RawLua {
// Create the internal metatables and store them in the registry
// to prevent from being garbage collected.

init_gc_metatable::<Rc<UnsafeCell<ExtraData>>>(state, None)?;
init_gc_metatable::<Callback>(state, None)?;
init_gc_metatable::<CallbackUpvalue>(state, None)?;
init_internal_metatable::<XRc<UnsafeCell<ExtraData>>>(state, None)?;
init_internal_metatable::<Callback>(state, None)?;
init_internal_metatable::<CallbackUpvalue>(state, None)?;
#[cfg(feature = "async")]
{
init_gc_metatable::<AsyncCallback>(state, None)?;
init_gc_metatable::<AsyncCallbackUpvalue>(state, None)?;
init_gc_metatable::<AsyncPollUpvalue>(state, None)?;
init_gc_metatable::<Option<Waker>>(state, None)?;
init_internal_metatable::<AsyncCallback>(state, None)?;
init_internal_metatable::<AsyncCallbackUpvalue>(state, None)?;
init_internal_metatable::<AsyncPollUpvalue>(state, None)?;
init_internal_metatable::<Option<Waker>>(state, None)?;
}

// Init serde metatables
Expand Down Expand Up @@ -550,7 +550,7 @@ impl RawLua {
Value::UserData(ud) => self.push_ref(&ud.0),
Value::Error(err) => {
let protect = !self.unlikely_memory_error();
push_gc_userdata(state, WrappedFailure::Error(*err.clone()), protect)?;
push_internal_userdata(state, WrappedFailure::Error(*err.clone()), protect)?;
}
}
Ok(())
Expand Down Expand Up @@ -625,7 +625,7 @@ impl RawLua {
ffi::LUA_TUSERDATA => {
// If the userdata is `WrappedFailure`, process it as an error or panic.
let failure_mt_ptr = (*self.extra.get()).wrapped_failure_mt_ptr;
match get_gc_userdata::<WrappedFailure>(state, idx, failure_mt_ptr).as_mut() {
match get_internal_userdata::<WrappedFailure>(state, idx, failure_mt_ptr).as_mut() {
Some(WrappedFailure::Error(err)) => Value::Error(Box::new(err.clone())),
Some(WrappedFailure::Panic(panic)) => {
if let Some(panic) = panic.take() {
Expand Down Expand Up @@ -1076,7 +1076,7 @@ impl RawLua {
let func = mem::transmute::<Callback, Callback<'static>>(func);
let extra = XRc::clone(&self.extra);
let protect = !self.unlikely_memory_error();
push_gc_userdata(state, CallbackUpvalue { data: func, extra }, protect)?;
push_internal_userdata(state, CallbackUpvalue { data: func, extra }, protect)?;
if protect {
protect_lua!(state, 1, 1, fn(state) {
ffi::lua_pushcclosure(state, call_callback, 1);
Expand Down Expand Up @@ -1115,7 +1115,7 @@ impl RawLua {
let fut = func(rawlua, args);
let extra = XRc::clone(&(*upvalue).extra);
let protect = !rawlua.unlikely_memory_error();
push_gc_userdata(state, AsyncPollUpvalue { data: fut, extra }, protect)?;
push_internal_userdata(state, AsyncPollUpvalue { data: fut, extra }, protect)?;
if protect {
protect_lua!(state, 1, 1, fn(state) {
ffi::lua_pushcclosure(state, poll_future, 1);
Expand Down Expand Up @@ -1176,7 +1176,7 @@ impl RawLua {
let extra = XRc::clone(&self.extra);
let protect = !self.unlikely_memory_error();
let upvalue = AsyncCallbackUpvalue { data: func, extra };
push_gc_userdata(state, upvalue, protect)?;
push_internal_userdata(state, upvalue, protect)?;
if protect {
protect_lua!(state, 1, 1, fn(state) {
ffi::lua_pushcclosure(state, call_callback, 1);
Expand Down
6 changes: 3 additions & 3 deletions src/state/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use crate::error::{Error, Result};
use crate::state::{ExtraData, RawLua};
use crate::util::{self, get_gc_metatable, WrappedFailure};
use crate::util::{self, get_internal_metatable, WrappedFailure};

const WRAPPED_FAILURE_POOL_SIZE: usize = 64;
// const MULTIVALUE_POOL_SIZE: usize = 64;
Expand Down Expand Up @@ -137,15 +137,15 @@ where
wrapped_error,
WrappedFailure::Error(Error::CallbackError { traceback, cause }),
);
get_gc_metatable::<WrappedFailure>(state);
get_internal_metatable::<WrappedFailure>(state);
ffi::lua_setmetatable(state, -2);

ffi::lua_error(state)
}
Err(p) => {
let wrapped_panic = prealloc_failure.r#use(state, extra);
ptr::write(wrapped_panic, WrappedFailure::Panic(Some(p)));
get_gc_metatable::<WrappedFailure>(state);
get_internal_metatable::<WrappedFailure>(state);
ffi::lua_setmetatable(state, -2);
ffi::lua_error(state)
}
Expand Down
Loading

0 comments on commit 4d3803d

Please sign in to comment.