mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
compile script_contents
into lua bytecode before calling lua.load
This commit is contained in:
parent
9e28822e8f
commit
3e84e8336d
3 changed files with 5 additions and 21 deletions
|
@ -12,7 +12,7 @@ use tokio::{
|
||||||
use crate::{
|
use crate::{
|
||||||
setup::run_setup,
|
setup::run_setup,
|
||||||
utils::{
|
utils::{
|
||||||
files::{discover_script_file_path_including_lune_dirs, strip_shebang},
|
files::discover_script_file_path_including_lune_dirs,
|
||||||
listing::{find_lune_scripts, sort_lune_scripts, write_lune_scripts_list},
|
listing::{find_lune_scripts, sort_lune_scripts, write_lune_scripts_list},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -172,7 +172,7 @@ impl Cli {
|
||||||
// Create a new lune object with all globals & run the script
|
// Create a new lune object with all globals & run the script
|
||||||
let result = Lune::new()
|
let result = Lune::new()
|
||||||
.with_args(self.script_args)
|
.with_args(self.script_args)
|
||||||
.run(&script_display_name, strip_shebang(script_contents))
|
.run(&script_display_name, script_contents)
|
||||||
.await;
|
.await;
|
||||||
Ok(match result {
|
Ok(match result {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|
|
@ -185,21 +185,3 @@ pub fn parse_lune_description_from_file(contents: &str) -> Option<String> {
|
||||||
Some(unindented_lines)
|
Some(unindented_lines)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn strip_shebang(mut contents: Vec<u8>) -> Vec<u8> {
|
|
||||||
if contents.starts_with(b"#!") {
|
|
||||||
if let Some(first_newline_idx) =
|
|
||||||
contents
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.find_map(|(idx, c)| if *c == b'\n' { Some(idx) } else { None })
|
|
||||||
{
|
|
||||||
// NOTE: We keep the newline here on purpose to preserve
|
|
||||||
// correct line numbers in stack traces, the only reason
|
|
||||||
// we strip the shebang is to get the lua script to parse
|
|
||||||
// and the extra newline is not really a problem for that
|
|
||||||
contents.drain(..first_newline_idx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
contents
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ 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(crate) mod builtins;
|
pub(crate) mod builtins;
|
||||||
|
@ -68,6 +69,7 @@ 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_bytecode = 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 +77,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_contents.as_ref())
|
.load(script_bytecode)
|
||||||
.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