allow direct running of embedded rust

This commit is contained in:
TudbuT 2023-08-04 23:17:35 +02:00
parent 18b8b89549
commit 62fb4d0f91
4 changed files with 65 additions and 19 deletions

2
Cargo.lock generated
View file

@ -22,7 +22,7 @@ checksum = "b03f7fbd470aa8b3ad163c85cce8bccfc11cc9c44ef12da0a4eddd98bd307352"
[[package]]
name = "spl"
version = "0.1.5"
version = "0.1.6"
dependencies = [
"multicall",
"once_cell",

View file

@ -1,6 +1,6 @@
[package]
name = "spl"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
description = "Stack Pogramming Language: A simple, concise scripting language."
license = "MIT"

View file

@ -7,20 +7,38 @@ fn main() {
let arg = &args
.next()
.unwrap_or_else(|| find_in_splpath("repl.spl").expect("no file to be run"));
if arg == "--build" {
if arg == "--build" || arg == "--run" {
let file = args.next().unwrap();
let data = fs::read_to_string(file.clone()).expect("unable to read specified file");
println!("Building SPL with specified natives file...");
let mut builder = RustAppBuilder::new();
if let Some(name) = args.next() {
builder.set_name(name);
let build_only = arg == "--build";
if build_only {
println!("Building SPL with specified natives file...");
}
let mut builder = RustAppBuilder::new();
if build_only {
if let Some(name) = args.next() {
builder.set_name(name);
}
println!("Embedding source...");
}
builder.add_source(file.to_owned(), data.to_owned());
if build_only {
println!("Preparing rust code...");
}
println!("Embedding source...");
builder.add_source(file, data.to_owned());
println!("Preparing rust code...");
builder.prepare(lex(data.to_owned()).expect("invalid SPL in natives file."));
println!("Building...");
println!("Built! Binary is {}", builder.build().unwrap().get_binary());
if build_only {
println!("Building...");
}
let app = builder.build(build_only).unwrap();
if build_only {
println!("Built! Binary is {}", app.get_binary());
} else {
let mut args: Vec<String> = args.collect();
args.insert(0, file);
app.execute(args).unwrap();
app.delete();
}
return;
}
if let Err(x) = start_file(arg) {

View file

@ -4,10 +4,12 @@
use std::{
collections::{hash_map::DefaultHasher, HashMap},
env, fs,
env,
ffi::OsStr,
fs,
hash::{Hash, Hasher},
io,
process::{Child, Command},
process::{Child, Command, Stdio},
};
use crate::{FuncImplType, Keyword, Word, Words};
@ -18,6 +20,8 @@ mod splrs;
pub struct RustApp {
/// The path to the binary
binary: String,
/// The path to the project dir
dir: String,
}
impl RustApp {
@ -27,9 +31,18 @@ impl RustApp {
}
/// Executes the binary with some args
pub fn execute(&self, args: Vec<&str>) -> Result<Child, io::Error> {
pub fn execute<I, S>(&self, args: I) -> Result<Child, io::Error>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
Command::new(self.binary.clone()).args(args).spawn()
}
/// Deletes the binary and rust project
pub fn delete(self) {
fs::remove_dir_all(self.dir).expect("unable to delete RustApp");
}
}
/// A rust function which was embedded in SPL
@ -99,7 +112,7 @@ impl RustAppBuilder {
}
/// Builds the desired app, including literally building it using cargo.
pub fn build(self) -> Result<RustApp, io::Error> {
pub fn build(self, output: bool) -> Result<RustApp, io::Error> {
// we need a temp folder!
let tmp = "."; // TODO replace?
let name = match self.name {
@ -115,16 +128,20 @@ impl RustAppBuilder {
.arg("new")
.arg(format!("spl-{name}"))
.current_dir(tmp)
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.unwrap()
.wait_with_output();
.wait();
Command::new("cargo")
.arg("add")
.arg(format!("spl@{}", env!("CARGO_PKG_VERSION")))
.current_dir(format!("{tmp}/spl-{name}"))
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.unwrap()
.wait_with_output()?;
.wait()?;
let mut runtime_init = String::new();
let mut code = String::new();
for func in self.rust_functions.into_iter().enumerate() {
@ -169,10 +186,21 @@ impl RustAppBuilder {
.arg("build")
.arg("--release")
.current_dir(format!("{tmp}/spl-{name}"))
.stdout(if output {
Stdio::inherit()
} else {
Stdio::null()
})
.stderr(if output {
Stdio::inherit()
} else {
Stdio::null()
})
.spawn()
.unwrap()
.wait_with_output()?;
.wait()?;
Ok(RustApp {
dir: format!("{tmp}/spl-{name}"),
binary: {
// insanity. will have to clean this up at some point.
let dir = format!("{tmp}/spl-{name}/target/release/");