Remove usage of unstable mlua feature

This commit is contained in:
Filip Tibell 2023-08-20 15:22:16 -05:00
parent dc6903cfae
commit 577f8d7928
7 changed files with 48 additions and 50 deletions

View file

@ -78,12 +78,7 @@ urlencoding = "2.1"
### RUNTIME ### RUNTIME
mlua = { version = "0.9.0", features = [ mlua = { version = "0.9.0", features = ["luau", "luau-jit", "serialize"] }
"luau",
"luau-jit",
"serialize",
"unstable",
] }
tokio = { version = "1.24", features = ["full"] } tokio = { version = "1.24", features = ["full"] }
### SERDE ### SERDE

View file

@ -16,7 +16,7 @@ use tokio::{
use crate::lune::{ use crate::lune::{
builtins::LuneBuiltin, builtins::LuneBuiltin,
scheduler::{IntoLuaOwnedThread, Scheduler}, scheduler::{IntoLuaThread, Scheduler},
}; };
const REGISTRY_KEY: &str = "RequireContext"; const REGISTRY_KEY: &str = "RequireContext";
@ -187,7 +187,7 @@ impl<'lua> RequireContext<'lua> {
.load(file_contents) .load(file_contents)
.set_name(rel_path.to_string_lossy().to_string()) .set_name(rel_path.to_string_lossy().to_string())
.into_function()? .into_function()?
.into_owned_lua_thread(self.lua)?; .into_lua_thread(self.lua)?;
// Schedule the thread to run, wait for it to finish running // Schedule the thread to run, wait for it to finish running
let thread_id = sched.push_back(file_thread, ())?; let thread_id = sched.push_back(file_thread, ())?;

View file

@ -1,7 +1,7 @@
use futures_util::Future; use futures_util::Future;
use mlua::prelude::*; use mlua::prelude::*;
use super::{IntoLuaOwnedThread, Scheduler}; use super::{IntoLuaThread, Scheduler};
impl<'lua, 'fut> Scheduler<'lua, 'fut> impl<'lua, 'fut> Scheduler<'lua, 'fut>
where where
@ -28,14 +28,14 @@ where
*/ */
pub fn schedule_future_thread<F, FR>( pub fn schedule_future_thread<F, FR>(
&'fut self, &'fut self,
thread: impl IntoLuaOwnedThread, thread: impl IntoLuaThread<'fut>,
fut: F, fut: F,
) -> LuaResult<()> ) -> LuaResult<()>
where where
FR: IntoLuaMulti<'fut>, FR: IntoLuaMulti<'fut>,
F: Future<Output = LuaResult<FR>> + 'fut, F: Future<Output = LuaResult<FR>> + 'fut,
{ {
let thread = thread.into_owned_lua_thread(self.lua)?; let thread = thread.into_lua_thread(self.lua)?;
self.schedule_future(async move { self.schedule_future(async move {
match fut.await.and_then(|rets| rets.into_lua_multi(self.lua)) { match fut.await.and_then(|rets| rets.into_lua_multi(self.lua)) {
Err(e) => { Err(e) => {

View file

@ -4,7 +4,7 @@ use mlua::prelude::*;
use super::{ use super::{
thread::{SchedulerThread, SchedulerThreadId, SchedulerThreadSender}, thread::{SchedulerThread, SchedulerThreadId, SchedulerThreadSender},
IntoLuaOwnedThread, Scheduler, IntoLuaThread, Scheduler,
}; };
impl<'lua, 'fut> Scheduler<'lua, 'fut> impl<'lua, 'fut> Scheduler<'lua, 'fut>
@ -43,8 +43,8 @@ where
/** /**
Schedules the `thread` to be resumed with the given [`LuaError`]. Schedules the `thread` to be resumed with the given [`LuaError`].
*/ */
pub fn push_err(&self, thread: impl IntoLuaOwnedThread, err: LuaError) -> LuaResult<()> { pub fn push_err<'a>(&'a self, thread: impl IntoLuaThread<'a>, err: LuaError) -> LuaResult<()> {
let thread = thread.into_owned_lua_thread(self.lua)?; let thread = thread.into_lua_thread(self.lua)?;
let args = LuaMultiValue::new(); // Will be resumed with error, don't need real args let args = LuaMultiValue::new(); // Will be resumed with error, don't need real args
let thread = SchedulerThread::new(self.lua, thread, args)?; let thread = SchedulerThread::new(self.lua, thread, args)?;
@ -72,10 +72,10 @@ where
*/ */
pub fn push_front<'a>( pub fn push_front<'a>(
&'a self, &'a self,
thread: impl IntoLuaOwnedThread, thread: impl IntoLuaThread<'a>,
args: impl IntoLuaMulti<'a>, args: impl IntoLuaMulti<'a>,
) -> LuaResult<SchedulerThreadId> { ) -> LuaResult<SchedulerThreadId> {
let thread = thread.into_owned_lua_thread(self.lua)?; let thread = thread.into_lua_thread(self.lua)?;
let args = args.into_lua_multi(self.lua)?; let args = args.into_lua_multi(self.lua)?;
let thread = SchedulerThread::new(self.lua, thread, args)?; let thread = SchedulerThread::new(self.lua, thread, args)?;
@ -110,10 +110,10 @@ where
*/ */
pub fn push_back<'a>( pub fn push_back<'a>(
&'a self, &'a self,
thread: impl IntoLuaOwnedThread, thread: impl IntoLuaThread<'a>,
args: impl IntoLuaMulti<'a>, args: impl IntoLuaMulti<'a>,
) -> LuaResult<SchedulerThreadId> { ) -> LuaResult<SchedulerThreadId> {
let thread = thread.into_owned_lua_thread(self.lua)?; let thread = thread.into_lua_thread(self.lua)?;
let args = args.into_lua_multi(self.lua)?; let args = args.into_lua_multi(self.lua)?;
let thread = SchedulerThread::new(self.lua, thread, args)?; let thread = SchedulerThread::new(self.lua, thread, args)?;

View file

@ -19,14 +19,14 @@ pub type SchedulerThreadSender = Sender<LuaResult<Arc<LuaRegistryKey>>>;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct SchedulerThreadId(usize); pub struct SchedulerThreadId(usize);
impl From<&LuaOwnedThread> for SchedulerThreadId { impl From<&LuaThread<'_>> for SchedulerThreadId {
fn from(value: &LuaOwnedThread) -> Self { fn from(value: &LuaThread) -> Self {
// HACK: We rely on the debug format of owned // HACK: We rely on the debug format of mlua
// thread refs here, but currently this is the // thread refs here, but currently this is the
// only way to get a proper unique id using mlua // only way to get a proper unique id using mlua
let addr_string = format!("{value:?}"); let addr_string = format!("{value:?}");
let addr = addr_string let addr = addr_string
.strip_prefix("OwnedThread(OwnedRef(0x") .strip_prefix("Thread(Ref(0x")
.expect("Invalid thread address format - unknown prefix") .expect("Invalid thread address format - unknown prefix")
.split_once(')') .split_once(')')
.map(|(s, _)| s) .map(|(s, _)| s)
@ -43,8 +43,8 @@ impl From<&LuaOwnedThread> for SchedulerThreadId {
#[derive(Debug)] #[derive(Debug)]
pub(super) struct SchedulerThread { pub(super) struct SchedulerThread {
thread_id: SchedulerThreadId, thread_id: SchedulerThreadId,
thread: LuaOwnedThread, key_thread: LuaRegistryKey,
args: LuaRegistryKey, key_args: LuaRegistryKey,
} }
impl SchedulerThread { impl SchedulerThread {
@ -55,36 +55,45 @@ impl SchedulerThread {
*/ */
pub(super) fn new<'lua>( pub(super) fn new<'lua>(
lua: &'lua Lua, lua: &'lua Lua,
thread: LuaOwnedThread, thread: LuaThread<'lua>,
args: LuaMultiValue<'lua>, args: LuaMultiValue<'lua>,
) -> LuaResult<Self> { ) -> LuaResult<Self> {
let args_vec = args.into_vec(); let args_vec = args.into_vec();
let thread_id = SchedulerThreadId::from(&thread);
let args = lua let key_thread = lua
.create_registry_value(thread)
.context("Failed to store value in registry")?;
let key_args = lua
.create_registry_value(args_vec) .create_registry_value(args_vec)
.context("Failed to store value in registry")?; .context("Failed to store value in registry")?;
Ok(Self { Ok(Self {
thread_id: SchedulerThreadId::from(&thread), thread_id,
thread, key_thread,
args, key_args,
}) })
} }
/** /**
Extracts the inner thread and args from the container. Extracts the inner thread and args from the container.
*/ */
pub(super) fn into_inner(self, lua: &Lua) -> (LuaOwnedThread, LuaMultiValue<'_>) { pub(super) fn into_inner(self, lua: &Lua) -> (LuaThread<'_>, LuaMultiValue<'_>) {
let thread = lua
.registry_value(&self.key_thread)
.expect("Failed to get thread from registry");
let args_vec = lua let args_vec = lua
.registry_value(&self.args) .registry_value(&self.key_args)
.expect("Failed to get thread args from registry"); .expect("Failed to get thread args from registry");
let args = LuaMultiValue::from_vec(args_vec); let args = LuaMultiValue::from_vec(args_vec);
lua.remove_registry_value(self.args) lua.remove_registry_value(self.key_thread)
.expect("Failed to remove thread from registry");
lua.remove_registry_value(self.key_args)
.expect("Failed to remove thread args from registry"); .expect("Failed to remove thread args from registry");
(self.thread, args) (thread, args)
} }
/** /**

View file

@ -79,33 +79,27 @@ where
- Lua functions ([`LuaFunction`]) - Lua functions ([`LuaFunction`])
- Lua chunks ([`LuaChunk`]) - Lua chunks ([`LuaChunk`])
*/ */
pub trait IntoLuaOwnedThread { pub trait IntoLuaThread<'lua> {
/** /**
Converts the value into a lua thread. Converts the value into a lua thread.
*/ */
fn into_owned_lua_thread(self, lua: &Lua) -> LuaResult<LuaOwnedThread>; fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>>;
} }
impl IntoLuaOwnedThread for LuaOwnedThread { impl<'lua> IntoLuaThread<'lua> for LuaThread<'lua> {
fn into_owned_lua_thread(self, _lua: &Lua) -> LuaResult<LuaOwnedThread> { fn into_lua_thread(self, _: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
Ok(self) Ok(self)
} }
} }
impl<'lua> IntoLuaOwnedThread for LuaThread<'lua> { impl<'lua> IntoLuaThread<'lua> for LuaFunction<'lua> {
fn into_owned_lua_thread(self, _lua: &Lua) -> LuaResult<LuaOwnedThread> { fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
Ok(self.into_owned()) lua.create_thread(self)
} }
} }
impl<'lua> IntoLuaOwnedThread for LuaFunction<'lua> { impl<'lua, 'a> IntoLuaThread<'lua> for LuaChunk<'lua, 'a> {
fn into_owned_lua_thread(self, lua: &Lua) -> LuaResult<LuaOwnedThread> { fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
Ok(lua.create_thread(self)?.into_owned()) lua.create_thread(self.into_function()?)
}
}
impl<'lua, 'a> IntoLuaOwnedThread for LuaChunk<'lua, 'a> {
fn into_owned_lua_thread(self, lua: &Lua) -> LuaResult<LuaOwnedThread> {
Ok(lua.create_thread(self.into_function()?)?.into_owned())
} }
} }

View file

@ -99,7 +99,7 @@ assertEq(
-- World & object space conversions -- World & object space conversions
-- FIXME: ToWorldSpace and/or ToObjectSpace are causing SIGTRAP? What the heck? -- FIXME: ToWorldSpace and/or ToObjectSpace are causing SIGSEGV? Invalid memory reference? What the heck?
if true then if true then
return return
end end