Migrate lune-std to use async-fs and friends

This commit is contained in:
Filip Tibell 2025-04-24 21:52:07 +02:00
parent 0a3b57697d
commit 88494fedcb
No known key found for this signature in database
4 changed files with 19 additions and 19 deletions

4
Cargo.lock generated
View file

@ -1735,6 +1735,9 @@ dependencies = [
name = "lune-std" name = "lune-std"
version = "0.1.5" version = "0.1.5"
dependencies = [ dependencies = [
"async-channel",
"async-fs",
"async-lock",
"lune-std-datetime", "lune-std-datetime",
"lune-std-fs", "lune-std-fs",
"lune-std-luau", "lune-std-luau",
@ -1750,7 +1753,6 @@ dependencies = [
"mlua-luau-scheduler", "mlua-luau-scheduler",
"serde", "serde",
"serde_json", "serde_json",
"tokio",
] ]
[[package]] [[package]]

View file

@ -41,9 +41,12 @@ task = ["dep:lune-std-task"]
mlua = { version = "0.10.3", features = ["luau"] } mlua = { version = "0.10.3", features = ["luau"] }
mlua-luau-scheduler = { version = "0.0.2", path = "../mlua-luau-scheduler" } mlua-luau-scheduler = { version = "0.0.2", path = "../mlua-luau-scheduler" }
async-channel = "2.3"
async-fs = "2.1"
async-lock = "3.4"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
tokio = { version = "1", default-features = false, features = ["fs", "sync"] }
lune-utils = { version = "0.1.3", path = "../lune-utils" } lune-utils = { version = "0.1.3", path = "../lune-utils" }

View file

@ -7,13 +7,9 @@ use std::{
use mlua::prelude::*; use mlua::prelude::*;
use mlua_luau_scheduler::LuaSchedulerExt; use mlua_luau_scheduler::LuaSchedulerExt;
use tokio::{ use async_channel::{unbounded, Receiver};
fs::read, use async_fs::read;
sync::{ use async_lock::Mutex as AsyncMutex;
broadcast::{self, Sender},
Mutex as AsyncMutex,
},
};
use lune_utils::path::{clean_path, clean_path_and_make_absolute}; use lune_utils::path::{clean_path, clean_path_and_make_absolute};
@ -29,7 +25,7 @@ use crate::library::LuneStandardLibrary;
pub(super) struct RequireContext { pub(super) struct RequireContext {
libraries: Arc<AsyncMutex<HashMap<LuneStandardLibrary, LuaResult<LuaRegistryKey>>>>, libraries: Arc<AsyncMutex<HashMap<LuneStandardLibrary, LuaResult<LuaRegistryKey>>>>,
results: Arc<AsyncMutex<HashMap<PathBuf, LuaResult<LuaRegistryKey>>>>, results: Arc<AsyncMutex<HashMap<PathBuf, LuaResult<LuaRegistryKey>>>>,
pending: Arc<AsyncMutex<HashMap<PathBuf, Sender<()>>>>, pending: Arc<AsyncMutex<HashMap<PathBuf, Receiver<()>>>>,
} }
impl RequireContext { impl RequireContext {
@ -160,15 +156,15 @@ impl RequireContext {
lua: Lua, lua: Lua,
abs_path: impl AsRef<Path>, abs_path: impl AsRef<Path>,
) -> LuaResult<LuaMultiValue> { ) -> LuaResult<LuaMultiValue> {
let mut thread_recv = { let thread_recv = {
let pending = self let pending = self
.pending .pending
.try_lock() .try_lock()
.expect("RequireContext may not be used from multiple threads"); .expect("RequireContext may not be used from multiple threads");
let thread_id = pending let thread_recv = pending
.get(abs_path.as_ref()) .get(abs_path.as_ref())
.expect("Path is not currently pending require"); .expect("Path is not currently pending require");
thread_id.subscribe() thread_recv.clone()
}; };
thread_recv.recv().await.into_lua_err()?; thread_recv.recv().await.into_lua_err()?;
@ -224,11 +220,11 @@ impl RequireContext {
let rel_path = rel_path.as_ref(); let rel_path = rel_path.as_ref();
// Set this abs path as currently pending // Set this abs path as currently pending
let (broadcast_tx, _) = broadcast::channel(1); let (broadcast_tx, broadcast_rx) = unbounded();
self.pending self.pending
.try_lock() .try_lock()
.expect("RequireContext may not be used from multiple threads") .expect("RequireContext may not be used from multiple threads")
.insert(abs_path.to_path_buf(), broadcast_tx); .insert(abs_path.to_path_buf(), broadcast_rx);
// Try to load at this abs path // Try to load at this abs path
let load_res = self.load(lua.clone(), abs_path, rel_path).await; let load_res = self.load(lua.clone(), abs_path, rel_path).await;
@ -253,13 +249,12 @@ impl RequireContext {
// Remove the pending thread id from the require context, // Remove the pending thread id from the require context,
// broadcast a message to let any listeners know that this // broadcast a message to let any listeners know that this
// path has now finished the require process and is cached // path has now finished the require process and is cached
let broadcast_tx = self self.pending
.pending
.try_lock() .try_lock()
.expect("RequireContext may not be used from multiple threads") .expect("RequireContext may not be used from multiple threads")
.remove(abs_path) .remove(abs_path)
.expect("Pending require broadcaster was unexpectedly removed"); .expect("Pending require broadcaster was unexpectedly removed");
broadcast_tx.send(()).ok(); broadcast_tx.send(()).await.ok();
load_val load_val
} }

View file

@ -4,9 +4,9 @@ use std::{
sync::Arc, sync::Arc,
}; };
use async_fs::read;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use tokio::fs::read;
use lune_utils::path::{clean_path, clean_path_and_make_absolute}; use lune_utils::path::{clean_path, clean_path_and_make_absolute};