From 55a2338a3e4eb21dff79f3f3ded71fe3dd2c6a41 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 5 Aug 2023 17:51:56 -0500 Subject: [PATCH] Use global lua compiler instead of recreating on load --- src/lune/importer/require.rs | 5 ++--- src/lune/lua/create.rs | 18 +++++++++++++++++- src/lune/mod.rs | 4 +--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lune/importer/require.rs b/src/lune/importer/require.rs index 51588e4..113c593 100644 --- a/src/lune/importer/require.rs +++ b/src/lune/importer/require.rs @@ -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)?; diff --git a/src/lune/lua/create.rs b/src/lune/lua/create.rs index 4ab8dde..bc5067c 100644 --- a/src/lune/lua/create.rs +++ b/src/lune/lua/create.rs @@ -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) } diff --git a/src/lune/mod.rs b/src/lune/mod.rs index 09b3842..e0eeec6 100644 --- a/src/lune/mod.rs +++ b/src/lune/mod.rs @@ -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 { // 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)?;