Use global lua compiler instead of recreating on load

This commit is contained in:
Filip Tibell 2023-08-05 17:51:56 -05:00
parent 483713e635
commit 55a2338a3e
No known key found for this signature in database
3 changed files with 20 additions and 7 deletions

View file

@ -6,7 +6,7 @@ use std::{
sync::Arc,
};
use mlua::{prelude::*, Compiler as LuaCompiler};
use mlua::prelude::*;
use tokio::fs;
use tokio::sync::Mutex as AsyncMutex;
@ -209,9 +209,8 @@ async fn load_file<'lua>(
.trim_end_matches(".lua")
.trim_end_matches(".luau");
// Load the file into a thread
let compiled_func = LuaCompiler::default().compile(&contents);
let loaded_func = lua
.load(compiled_func)
.load(contents)
.set_name(path_relative_no_extension)
.into_function()?;
let loaded_thread = lua.create_thread(loaded_func)?;

View file

@ -1,4 +1,4 @@
use mlua::prelude::*;
use mlua::{prelude::*, Compiler as LuaCompiler};
/*
- Level 0 is the call to info
@ -76,14 +76,27 @@ end
*/
pub fn create() -> LuaResult<&'static Lua> {
let lua = Lua::new().into_static();
// Enable jit and set global compiler options
lua.set_compiler(
LuaCompiler::default()
.set_coverage_level(0)
.set_debug_level(1)
.set_optimization_level(1),
);
// Extract some global tables that we will extract
// built-in functions from and store in the registry
let globals = &lua.globals();
let debug: LuaTable = globals.raw_get("debug")?;
let table: LuaTable = globals.raw_get("table")?;
let string: LuaTable = globals.raw_get("string")?;
let coroutine: LuaTable = globals.get("coroutine")?;
// Create a _G table that is separate from our built-in globals
let global_table = lua.create_table()?;
globals.set("_G", global_table)?;
// Store original lua global functions in the registry so we can use
// them later without passing them around and dealing with lifetimes
lua.set_named_registry_value("print", globals.get::<_, LuaFunction>("print")?)?;
@ -109,6 +122,7 @@ pub fn create() -> LuaResult<&'static Lua> {
"tab.setmeta",
globals.get::<_, LuaFunction>("setmetatable")?,
)?;
// Create a trace function that can be called to obtain a full stack trace from
// lua, this is not possible to do from rust when using our manual scheduler
let dbg_trace_env = lua.create_table_with_capacity(0, 1)?;
@ -122,6 +136,7 @@ pub fn create() -> LuaResult<&'static Lua> {
.set_environment(dbg_trace_env)
.into_function()?;
lua.set_named_registry_value("dbg.trace", dbg_trace_fn)?;
// Modify the _VERSION global to also contain the current version of Lune
let luau_version_full = globals
.get::<_, LuaString>("_VERSION")
@ -142,6 +157,7 @@ pub fn create() -> LuaResult<&'static Lua> {
luau = luau_version,
))?,
)?;
// All done
Ok(lua)
}

View file

@ -2,7 +2,6 @@ use std::process::ExitCode;
use lua::task::{TaskScheduler, TaskSchedulerResumeExt, TaskSchedulerScheduleExt};
use mlua::prelude::*;
use mlua::Compiler as LuaCompiler;
use tokio::task::LocalSet;
pub mod builtins;
@ -67,7 +66,6 @@ impl Lune {
) -> Result<ExitCode, LuaError> {
// Create our special lune-flavored Lua object with extra registry values
let lua = lua::create_lune_lua()?;
let script = LuaCompiler::default().compile(script_contents);
// Create our task scheduler and all globals
// NOTE: Some globals require the task scheduler to exist on startup
let sched = TaskScheduler::new(lua)?.into_static();
@ -75,7 +73,7 @@ impl Lune {
importer::create(lua, self.args.clone())?;
// Create the main thread and schedule it
let main_chunk = lua
.load(script)
.load(script_contents.as_ref())
.set_name(script_name.as_ref())
.into_function()?;
let main_thread = lua.create_thread(main_chunk)?;