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
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

View file

@ -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, ())?;

View file

@ -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<F, FR>(
&'fut self,
thread: impl IntoLuaOwnedThread,
thread: impl IntoLuaThread<'fut>,
fut: F,
) -> LuaResult<()>
where
FR: IntoLuaMulti<'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 {
match fut.await.and_then(|rets| rets.into_lua_multi(self.lua)) {
Err(e) => {

View file

@ -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<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 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<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 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)]
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<Self> {
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)
}
/**

View file

@ -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<LuaOwnedThread>;
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>>;
}
impl IntoLuaOwnedThread for LuaOwnedThread {
fn into_owned_lua_thread(self, _lua: &Lua) -> LuaResult<LuaOwnedThread> {
impl<'lua> IntoLuaThread<'lua> for LuaThread<'lua> {
fn into_lua_thread(self, _: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
Ok(self)
}
}
impl<'lua> IntoLuaOwnedThread for LuaThread<'lua> {
fn into_owned_lua_thread(self, _lua: &Lua) -> LuaResult<LuaOwnedThread> {
Ok(self.into_owned())
impl<'lua> IntoLuaThread<'lua> for LuaFunction<'lua> {
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
lua.create_thread(self)
}
}
impl<'lua> IntoLuaOwnedThread for LuaFunction<'lua> {
fn into_owned_lua_thread(self, lua: &Lua) -> LuaResult<LuaOwnedThread> {
Ok(lua.create_thread(self)?.into_owned())
}
}
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())
impl<'lua, 'a> IntoLuaThread<'lua> for LuaChunk<'lua, 'a> {
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
lua.create_thread(self.into_function()?)
}
}

View file

@ -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