Skip to content

Commit

Permalink
feat: added luajit compatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmasoldi3r committed Dec 23, 2023
1 parent 3e15776 commit 72f25fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/lua/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Generates the given source as a virtual module, used by Lua target.
pub fn generate_module_chunk(name: &String, source: &String) -> String {
let tmp = format!("_G[\"__saturnus_module_{name}\"]");
format!(
"{tmp} = function()
{source}
end;
if jit then
package.loaded[\"{name}\"] = {tmp}();
else
package.preload[\"{name}\"] = {tmp};
end"
)
}
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use clap::Parser;
use code::info::InputFileInfo;
use errors::report_error;
use lua::visitor::LuaEmitter;
use lua::{helpers::generate_module_chunk, visitor::LuaEmitter};
use parser::Script;
use runtime::RuntimeError;

Expand Down Expand Up @@ -154,8 +154,9 @@ fn compile_main(
if !args.no_std {
let (std_src, _) = precompile_std(compiler)?;
src = format!(
"package.preload[\"std\"] = function()\n{}\nend;\n{}",
std_src, src
"{}\n{}",
generate_module_chunk(&"std".into(), &std_src),
src
);
}
Ok(src)
Expand Down

0 comments on commit 72f25fb

Please sign in to comment.