From a636938eea024aec0e748c724b16488614173168 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 20 Aug 2023 15:22:16 -0500 Subject: [PATCH] Remove usage of unstable mlua feature --- Cargo.toml | 7 +----- src/lune/globals/require/context.rs | 4 +-- src/lune/scheduler/impl_async.rs | 6 ++--- src/lune/scheduler/impl_threads.rs | 14 +++++------ src/lune/scheduler/thread.rs | 39 ++++++++++++++++++----------- src/lune/scheduler/traits.rs | 26 ++++++++----------- tests/roblox/datatypes/CFrame.luau | 2 +- 7 files changed, 48 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 255bc92..ab74227 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,12 +78,7 @@ urlencoding = "2.1" ### RUNTIME -mlua = { version = "0.9.0", features = [ - "luau", - "luau-jit", - "serialize", - "unstable", -] } +mlua = { version = "0.9.0", features = ["luau", "luau-jit", "serialize"] } tokio = { version = "1.24", features = ["full"] } ### SERDE diff --git a/src/lune/globals/require/context.rs b/src/lune/globals/require/context.rs index 8468803..3e917e3 100644 --- a/src/lune/globals/require/context.rs +++ b/src/lune/globals/require/context.rs @@ -16,7 +16,7 @@ use tokio::{ use crate::lune::{ builtins::LuneBuiltin, - scheduler::{IntoLuaOwnedThread, Scheduler}, + scheduler::{IntoLuaThread, Scheduler}, }; const REGISTRY_KEY: &str = "RequireContext"; @@ -187,7 +187,7 @@ impl<'lua> RequireContext<'lua> { .load(file_contents) .set_name(rel_path.to_string_lossy().to_string()) .into_function()? - .into_owned_lua_thread(self.lua)?; + .into_lua_thread(self.lua)?; // Schedule the thread to run, wait for it to finish running let thread_id = sched.push_back(file_thread, ())?; diff --git a/src/lune/scheduler/impl_async.rs b/src/lune/scheduler/impl_async.rs index fb3d0a8..374ab9b 100644 --- a/src/lune/scheduler/impl_async.rs +++ b/src/lune/scheduler/impl_async.rs @@ -1,7 +1,7 @@ use futures_util::Future; use mlua::prelude::*; -use super::{IntoLuaOwnedThread, Scheduler}; +use super::{IntoLuaThread, Scheduler}; impl<'lua, 'fut> Scheduler<'lua, 'fut> where @@ -28,14 +28,14 @@ where */ pub fn schedule_future_thread( &'fut self, - thread: impl IntoLuaOwnedThread, + thread: impl IntoLuaThread<'fut>, fut: F, ) -> LuaResult<()> where FR: IntoLuaMulti<'fut>, F: Future> + 'fut, { - let thread = thread.into_owned_lua_thread(self.lua)?; + let thread = thread.into_lua_thread(self.lua)?; self.schedule_future(async move { match fut.await.and_then(|rets| rets.into_lua_multi(self.lua)) { Err(e) => { diff --git a/src/lune/scheduler/impl_threads.rs b/src/lune/scheduler/impl_threads.rs index 7545fb1..dc5a2f8 100644 --- a/src/lune/scheduler/impl_threads.rs +++ b/src/lune/scheduler/impl_threads.rs @@ -4,7 +4,7 @@ use mlua::prelude::*; use super::{ thread::{SchedulerThread, SchedulerThreadId, SchedulerThreadSender}, - IntoLuaOwnedThread, Scheduler, + IntoLuaThread, Scheduler, }; impl<'lua, 'fut> Scheduler<'lua, 'fut> @@ -43,8 +43,8 @@ where /** Schedules the `thread` to be resumed with the given [`LuaError`]. */ - pub fn push_err(&self, thread: impl IntoLuaOwnedThread, err: LuaError) -> LuaResult<()> { - let thread = thread.into_owned_lua_thread(self.lua)?; + pub fn push_err<'a>(&'a self, thread: impl IntoLuaThread<'a>, err: LuaError) -> LuaResult<()> { + let thread = thread.into_lua_thread(self.lua)?; let args = LuaMultiValue::new(); // Will be resumed with error, don't need real args let thread = SchedulerThread::new(self.lua, thread, args)?; @@ -72,10 +72,10 @@ where */ pub fn push_front<'a>( &'a self, - thread: impl IntoLuaOwnedThread, + thread: impl IntoLuaThread<'a>, args: impl IntoLuaMulti<'a>, ) -> LuaResult { - 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 thread = SchedulerThread::new(self.lua, thread, args)?; @@ -110,10 +110,10 @@ where */ pub fn push_back<'a>( &'a self, - thread: impl IntoLuaOwnedThread, + thread: impl IntoLuaThread<'a>, args: impl IntoLuaMulti<'a>, ) -> LuaResult { - 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 thread = SchedulerThread::new(self.lua, thread, args)?; diff --git a/src/lune/scheduler/thread.rs b/src/lune/scheduler/thread.rs index d6a63b3..301bd0f 100644 --- a/src/lune/scheduler/thread.rs +++ b/src/lune/scheduler/thread.rs @@ -19,14 +19,14 @@ pub type SchedulerThreadSender = Sender>>; #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] pub struct SchedulerThreadId(usize); -impl From<&LuaOwnedThread> for SchedulerThreadId { - fn from(value: &LuaOwnedThread) -> Self { - // HACK: We rely on the debug format of owned +impl From<&LuaThread<'_>> for SchedulerThreadId { + fn from(value: &LuaThread) -> Self { + // HACK: We rely on the debug format of mlua // thread refs here, but currently this is the // only way to get a proper unique id using mlua let addr_string = format!("{value:?}"); let addr = addr_string - .strip_prefix("OwnedThread(OwnedRef(0x") + .strip_prefix("Thread(Ref(0x") .expect("Invalid thread address format - unknown prefix") .split_once(')') .map(|(s, _)| s) @@ -43,8 +43,8 @@ impl From<&LuaOwnedThread> for SchedulerThreadId { #[derive(Debug)] pub(super) struct SchedulerThread { thread_id: SchedulerThreadId, - thread: LuaOwnedThread, - args: LuaRegistryKey, + key_thread: LuaRegistryKey, + key_args: LuaRegistryKey, } impl SchedulerThread { @@ -55,36 +55,45 @@ impl SchedulerThread { */ pub(super) fn new<'lua>( lua: &'lua Lua, - thread: LuaOwnedThread, + thread: LuaThread<'lua>, args: LuaMultiValue<'lua>, ) -> LuaResult { 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) .context("Failed to store value in registry")?; Ok(Self { - thread_id: SchedulerThreadId::from(&thread), - thread, - args, + thread_id, + key_thread, + key_args, }) } /** 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 - .registry_value(&self.args) + .registry_value(&self.key_args) .expect("Failed to get thread args from registry"); 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"); - (self.thread, args) + (thread, args) } /** diff --git a/src/lune/scheduler/traits.rs b/src/lune/scheduler/traits.rs index 9731608..541f35e 100644 --- a/src/lune/scheduler/traits.rs +++ b/src/lune/scheduler/traits.rs @@ -79,33 +79,27 @@ where - Lua functions ([`LuaFunction`]) - Lua chunks ([`LuaChunk`]) */ -pub trait IntoLuaOwnedThread { +pub trait IntoLuaThread<'lua> { /** Converts the value into a lua thread. */ - fn into_owned_lua_thread(self, lua: &Lua) -> LuaResult; + fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult>; } -impl IntoLuaOwnedThread for LuaOwnedThread { - fn into_owned_lua_thread(self, _lua: &Lua) -> LuaResult { +impl<'lua> IntoLuaThread<'lua> for LuaThread<'lua> { + fn into_lua_thread(self, _: &'lua Lua) -> LuaResult> { Ok(self) } } -impl<'lua> IntoLuaOwnedThread for LuaThread<'lua> { - fn into_owned_lua_thread(self, _lua: &Lua) -> LuaResult { - Ok(self.into_owned()) +impl<'lua> IntoLuaThread<'lua> for LuaFunction<'lua> { + fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult> { + lua.create_thread(self) } } -impl<'lua> IntoLuaOwnedThread for LuaFunction<'lua> { - fn into_owned_lua_thread(self, lua: &Lua) -> LuaResult { - Ok(lua.create_thread(self)?.into_owned()) - } -} - -impl<'lua, 'a> IntoLuaOwnedThread for LuaChunk<'lua, 'a> { - fn into_owned_lua_thread(self, lua: &Lua) -> LuaResult { - Ok(lua.create_thread(self.into_function()?)?.into_owned()) +impl<'lua, 'a> IntoLuaThread<'lua> for LuaChunk<'lua, 'a> { + fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult> { + lua.create_thread(self.into_function()?) } } diff --git a/tests/roblox/datatypes/CFrame.luau b/tests/roblox/datatypes/CFrame.luau index fc3e4e0..f30446e 100644 --- a/tests/roblox/datatypes/CFrame.luau +++ b/tests/roblox/datatypes/CFrame.luau @@ -99,7 +99,7 @@ assertEq( -- 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 return end