mirror of
https://github.com/lune-org/lune.git
synced 2025-03-04 11:11:39 +00:00
Use global lua compiler instead of recreating on load
This commit is contained in:
parent
483713e635
commit
55a2338a3e
3 changed files with 20 additions and 7 deletions
|
@ -6,7 +6,7 @@ use std::{
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use mlua::{prelude::*, Compiler as LuaCompiler};
|
use mlua::prelude::*;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use tokio::sync::Mutex as AsyncMutex;
|
use tokio::sync::Mutex as AsyncMutex;
|
||||||
|
|
||||||
|
@ -209,9 +209,8 @@ async fn load_file<'lua>(
|
||||||
.trim_end_matches(".lua")
|
.trim_end_matches(".lua")
|
||||||
.trim_end_matches(".luau");
|
.trim_end_matches(".luau");
|
||||||
// Load the file into a thread
|
// Load the file into a thread
|
||||||
let compiled_func = LuaCompiler::default().compile(&contents);
|
|
||||||
let loaded_func = lua
|
let loaded_func = lua
|
||||||
.load(compiled_func)
|
.load(contents)
|
||||||
.set_name(path_relative_no_extension)
|
.set_name(path_relative_no_extension)
|
||||||
.into_function()?;
|
.into_function()?;
|
||||||
let loaded_thread = lua.create_thread(loaded_func)?;
|
let loaded_thread = lua.create_thread(loaded_func)?;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use mlua::prelude::*;
|
use mlua::{prelude::*, Compiler as LuaCompiler};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
- Level 0 is the call to info
|
- Level 0 is the call to info
|
||||||
|
@ -76,14 +76,27 @@ end
|
||||||
*/
|
*/
|
||||||
pub fn create() -> LuaResult<&'static Lua> {
|
pub fn create() -> LuaResult<&'static Lua> {
|
||||||
let lua = Lua::new().into_static();
|
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 globals = &lua.globals();
|
||||||
let debug: LuaTable = globals.raw_get("debug")?;
|
let debug: LuaTable = globals.raw_get("debug")?;
|
||||||
let table: LuaTable = globals.raw_get("table")?;
|
let table: LuaTable = globals.raw_get("table")?;
|
||||||
let string: LuaTable = globals.raw_get("string")?;
|
let string: LuaTable = globals.raw_get("string")?;
|
||||||
let coroutine: LuaTable = globals.get("coroutine")?;
|
let coroutine: LuaTable = globals.get("coroutine")?;
|
||||||
|
|
||||||
// Create a _G table that is separate from our built-in globals
|
// Create a _G table that is separate from our built-in globals
|
||||||
let global_table = lua.create_table()?;
|
let global_table = lua.create_table()?;
|
||||||
globals.set("_G", global_table)?;
|
globals.set("_G", global_table)?;
|
||||||
|
|
||||||
// Store original lua global functions in the registry so we can use
|
// Store original lua global functions in the registry so we can use
|
||||||
// them later without passing them around and dealing with lifetimes
|
// them later without passing them around and dealing with lifetimes
|
||||||
lua.set_named_registry_value("print", globals.get::<_, LuaFunction>("print")?)?;
|
lua.set_named_registry_value("print", globals.get::<_, LuaFunction>("print")?)?;
|
||||||
|
@ -109,6 +122,7 @@ pub fn create() -> LuaResult<&'static Lua> {
|
||||||
"tab.setmeta",
|
"tab.setmeta",
|
||||||
globals.get::<_, LuaFunction>("setmetatable")?,
|
globals.get::<_, LuaFunction>("setmetatable")?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Create a trace function that can be called to obtain a full stack trace from
|
// 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
|
// 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)?;
|
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)
|
.set_environment(dbg_trace_env)
|
||||||
.into_function()?;
|
.into_function()?;
|
||||||
lua.set_named_registry_value("dbg.trace", dbg_trace_fn)?;
|
lua.set_named_registry_value("dbg.trace", dbg_trace_fn)?;
|
||||||
|
|
||||||
// Modify the _VERSION global to also contain the current version of Lune
|
// Modify the _VERSION global to also contain the current version of Lune
|
||||||
let luau_version_full = globals
|
let luau_version_full = globals
|
||||||
.get::<_, LuaString>("_VERSION")
|
.get::<_, LuaString>("_VERSION")
|
||||||
|
@ -142,6 +157,7 @@ pub fn create() -> LuaResult<&'static Lua> {
|
||||||
luau = luau_version,
|
luau = luau_version,
|
||||||
))?,
|
))?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// All done
|
// All done
|
||||||
Ok(lua)
|
Ok(lua)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ use std::process::ExitCode;
|
||||||
|
|
||||||
use lua::task::{TaskScheduler, TaskSchedulerResumeExt, TaskSchedulerScheduleExt};
|
use lua::task::{TaskScheduler, TaskSchedulerResumeExt, TaskSchedulerScheduleExt};
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
use mlua::Compiler as LuaCompiler;
|
|
||||||
use tokio::task::LocalSet;
|
use tokio::task::LocalSet;
|
||||||
|
|
||||||
pub mod builtins;
|
pub mod builtins;
|
||||||
|
@ -67,7 +66,6 @@ impl Lune {
|
||||||
) -> Result<ExitCode, LuaError> {
|
) -> Result<ExitCode, LuaError> {
|
||||||
// Create our special lune-flavored Lua object with extra registry values
|
// Create our special lune-flavored Lua object with extra registry values
|
||||||
let lua = lua::create_lune_lua()?;
|
let lua = lua::create_lune_lua()?;
|
||||||
let script = LuaCompiler::default().compile(script_contents);
|
|
||||||
// Create our task scheduler and all globals
|
// Create our task scheduler and all globals
|
||||||
// NOTE: Some globals require the task scheduler to exist on startup
|
// NOTE: Some globals require the task scheduler to exist on startup
|
||||||
let sched = TaskScheduler::new(lua)?.into_static();
|
let sched = TaskScheduler::new(lua)?.into_static();
|
||||||
|
@ -75,7 +73,7 @@ impl Lune {
|
||||||
importer::create(lua, self.args.clone())?;
|
importer::create(lua, self.args.clone())?;
|
||||||
// Create the main thread and schedule it
|
// Create the main thread and schedule it
|
||||||
let main_chunk = lua
|
let main_chunk = lua
|
||||||
.load(script)
|
.load(script_contents.as_ref())
|
||||||
.set_name(script_name.as_ref())
|
.set_name(script_name.as_ref())
|
||||||
.into_function()?;
|
.into_function()?;
|
||||||
let main_thread = lua.create_thread(main_chunk)?;
|
let main_thread = lua.create_thread(main_chunk)?;
|
||||||
|
|
Loading…
Add table
Reference in a new issue