Skip to content

Commit

Permalink
feat: janus now packs std inlined in main file
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmasoldi3r committed Dec 18, 2023
1 parent 278fc2a commit d6c4cb6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
11 changes: 4 additions & 7 deletions janus/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ impl CompilationHost {
.arg(file_path.to_str().unwrap())
.arg("-o")
.arg(&out);
if &info.main != file_path {
// If main, omit no STD
cmd.arg("--no-std");
}
// Handle the final execution of the command.
match cmd.spawn() {
Ok(mut proc) => match proc.wait() {
Expand Down Expand Up @@ -147,13 +151,6 @@ impl CompilationHost {
fs::copy(entry, target).unwrap();
pb.inc(1);
}
fs::write(
target_base_path.join("std.lua"),
fs::read_to_string(objects_base_path.join("std.lua"))
.unwrap()
.as_bytes(),
)
.unwrap();
pb.finish_with_message("Done");
}
OutputFormat::FlatDirectory => todo!("Directory flattening"),
Expand Down
12 changes: 0 additions & 12 deletions janus/src/compilation/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ impl FilePipeline {
pb.inc(1);
}
if let Some(entry) = main {
pb.set_message("Linking standard library...");
file_out
.write(b"\npackage.preload[\"std\"] = function()\n")
.unwrap();
file_out
.write_all(
fs::read_to_string(objects_base_path.join("std.lua"))
.unwrap()
.as_bytes(),
)
.unwrap();
file_out.write(b"\nend;").unwrap();
pb.set_message("Collecting main file...");
let src = fs::read_to_string(entry).unwrap();
file_out.write(b"\n").unwrap();
Expand Down
12 changes: 8 additions & 4 deletions janus/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ fn resolve_dep_options(name: &String, options: &DependencyObject, bar: &Progress
}
}
bar.set_message(format!("Compiling {name}..."));
let status = Command::new("janus")
let output = Command::new("janus")
.arg("build")
.current_dir(format!("dist/dependencies/{name}"))
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.output()
.expect("Failed to compile the dependency!");
if !status.success() {
panic!("Failed to compile the dependency!");
if !output.status.success() {
panic!(
"Failed to compile '{}' dependency!\nReason:\n{}",
name,
String::from_utf8_lossy(&output.stderr),
);
}
bar.set_message(format!("Copying {name} artifacts..."));
if !Path::new(&format!("dist/cache/objects/{name}")).exists() {
Expand Down
22 changes: 17 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ struct Args {
help = "Outputs the saturnus code to stdout preprocessed but without compiling"
)]
dump_saturnus: bool,
// May be useful in the future
// #[arg(
// long,
// help = "Dumps the std library compiled"
// )]
// dump_std: bool,
#[arg(
short = 'm',
long = "mod",
Expand Down Expand Up @@ -136,7 +142,11 @@ fn precompile_std(compiler: &dyn Visitor) -> Result<(String, md5::Digest), Runti
Ok((std_src, crc))
}

fn runtime_eval(script: &Script, compiler: &dyn Visitor, args: &Args) -> Result<(), RuntimeError> {
fn compile_main(
script: &Script,
compiler: &dyn Visitor,
args: &Args,
) -> Result<String, RuntimeError> {
let mut src = compiler
.visit_script(Builder::new(" "), &script)
.map_err(|err| RuntimeError::CompilationError(err))?
Expand All @@ -148,6 +158,11 @@ fn runtime_eval(script: &Script, compiler: &dyn Visitor, args: &Args) -> Result<
std_src, src
);
}
Ok(src)
}

fn runtime_eval(script: &Script, compiler: &dyn Visitor, args: &Args) -> Result<(), RuntimeError> {
let src = compile_main(script, compiler, args)?;
let lua = rlua::Lua::new();
lua.context(move |ctx| -> rlua::Result<()> {
ctx.load(&src).eval()?;
Expand Down Expand Up @@ -193,10 +208,7 @@ fn try_run(options: CompilationOptions, input: String, indent: String) -> Result
if args.verbose {
println!("Compiling {:?}...", in_path);
}
let output = compiler
.visit_script(Builder::new(indent), &script)
.map_err(|err| RuntimeError::CompilationError(err))?
.collect();
let output = compile_main(&script, &compiler, &args)?;
if args.print {
println!("\n------\n\n");
std::io::stdout().write_all(output.as_bytes()).unwrap();
Expand Down

0 comments on commit d6c4cb6

Please sign in to comment.