Properly store process args and env as part of runtime initialization instead of in std-process

This commit is contained in:
Filip Tibell 2025-05-02 12:18:53 +02:00
parent 120048ae95
commit 6645631c46
No known key found for this signature in database
3 changed files with 61 additions and 33 deletions

View file

@ -57,19 +57,15 @@ pub fn module(lua: Lua) -> LuaResult<LuaTable> {
"little" "little"
})?; })?;
// Find the readonly args array // Extract stored userdatas for args + env, the runtime struct should always provide this
let args_vec = lua let process_args = lua
.app_data_ref::<Vec<String>>() .app_data_ref::<ProcessArgs>()
.ok_or_else(|| LuaError::runtime("Missing args vec in Lua app data"))? .ok_or_else(|| LuaError::runtime("Missing process args in Lua app data"))?
.clone();
let process_env = lua
.app_data_ref::<ProcessEnv>()
.ok_or_else(|| LuaError::runtime("Missing process env in Lua app data"))?
.clone(); .clone();
// Create userdatas for args + env
// TODO: Move this up into the runtime creation instead,
// and set it as app data there to later fetch here
let process_args = ProcessArgs::from_iter(args_vec);
let process_env = ProcessEnv::current();
lua.set_app_data(process_args.clone());
lua.set_app_data(process_env.clone());
// Create our process exit function, the scheduler crate provides this // Create our process exit function, the scheduler crate provides this
let fns = Functions::new(lua.clone())?; let fns = Functions::new(lua.clone())?;

View file

@ -1,11 +1,14 @@
#![allow(clippy::missing_panics_doc)] #![allow(clippy::missing_panics_doc)]
use std::sync::{ use std::{
ffi::OsString,
sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
Arc, Arc,
},
}; };
use lune_utils::jit::JitEnablement; use lune_utils::process::{ProcessArgs, ProcessEnv, ProcessJitEnablement};
use mlua::prelude::*; use mlua::prelude::*;
use mlua_luau_scheduler::{Functions, Scheduler}; use mlua_luau_scheduler::{Functions, Scheduler};
@ -58,7 +61,9 @@ impl RuntimeReturnValues {
pub struct Runtime { pub struct Runtime {
lua: Lua, lua: Lua,
sched: Scheduler, sched: Scheduler,
jit: JitEnablement, args: ProcessArgs,
env: ProcessEnv,
jit: ProcessJitEnablement,
} }
impl Runtime { impl Runtime {
@ -126,21 +131,47 @@ impl Runtime {
.set(g_table.name(), g_table.create(lua.clone())?)?; .set(g_table.name(), g_table.create(lua.clone())?)?;
} }
let jit = JitEnablement::default(); let args = ProcessArgs::current();
Ok(Self { lua, sched, jit }) let env = ProcessEnv::current();
let jit = ProcessJitEnablement::default();
Ok(Self {
lua,
sched,
args,
env,
jit,
})
} }
/** /**
Sets arguments to give in `process.args` for Lune scripts. Sets arguments to give in `process.args` for Lune scripts.
By default, `std::env::args_os()` is used.
*/ */
#[must_use] #[must_use]
pub fn with_args<A, S>(self, args: A) -> Self pub fn with_args<A, S>(mut self, args: A) -> Self
where where
A: IntoIterator<Item = S>, A: IntoIterator<Item = S>,
S: Into<String>, S: Into<OsString>,
{ {
let args = args.into_iter().map(Into::into).collect::<Vec<_>>(); self.args = args.into_iter().map(Into::into).collect();
self.lua.set_app_data(args); self
}
/**
Sets environment values to give in `process.env` for Lune scripts.
By default, `std::env::vars_os()` is used.
*/
#[must_use]
pub fn with_env<E, K, V>(mut self, env: E) -> Self
where
E: IntoIterator<Item = (K, V)>,
K: Into<OsString>,
V: Into<OsString>,
{
self.env = env.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
self self
} }
@ -148,7 +179,10 @@ impl Runtime {
Enables or disables JIT compilation. Enables or disables JIT compilation.
*/ */
#[must_use] #[must_use]
pub fn with_jit(mut self, jit_status: impl Into<JitEnablement>) -> Self { pub fn with_jit<J>(mut self, jit_status: J) -> Self
where
J: Into<ProcessJitEnablement>,
{
self.jit = jit_status.into(); self.jit = jit_status.into();
self self
} }
@ -175,8 +209,12 @@ impl Runtime {
eprintln!("{}", RuntimeError::from(e)); eprintln!("{}", RuntimeError::from(e));
}); });
// Enable / disable the JIT as requested and store the current status as AppData // Store the provided args, environment variables, and jit enablement as AppData
self.lua.set_app_data(self.args.clone());
self.lua.set_app_data(self.env.clone());
self.lua.set_app_data(self.jit); self.lua.set_app_data(self.jit);
// Enable / disable the JIT as requested, before loading anything
self.lua.enable_jit(self.jit.enabled()); self.lua.enable_jit(self.jit.enabled());
// Load our "main" thread // Load our "main" thread

View file

@ -33,14 +33,8 @@ macro_rules! create_tests {
let full_name = format!("{}/tests/{}.luau", workspace_dir.display(), $value); let full_name = format!("{}/tests/{}.luau", workspace_dir.display(), $value);
let script = read_to_string(&full_name).await?; let script = read_to_string(&full_name).await?;
let mut lune = Runtime::new()? let mut lune = Runtime::new()?
.with_jit(true) .with_args(ARGS.iter().cloned())
.with_args( .with_jit(true);
ARGS
.clone()
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
);
let script_name = full_name let script_name = full_name
.trim_end_matches(".luau") .trim_end_matches(".luau")
.trim_end_matches(".lua") .trim_end_matches(".lua")