1
Fork 0
hare-playground/cmd/httpd/sandbox+darwin.ha

46 lines
990 B
Hare
Raw Normal View History

2024-06-01 16:21:33 +00:00
use io::{mode};
use io;
use os::exec;
use os;
use strings;
use temp;
use fs;
use fmt;
export fn run_code(code: str) (str, str) = {
let tmp_dir = temp::dir();
defer os::rmdirall(tmp_dir)!;
let path = fmt::asprintf("{}/main.ha", tmp_dir);
defer free(path);
let fp = os::create(path, 0o644)!;
io::writeall(fp, strings::toutf8(code))!;
io::close(fp)!;
let cmd = exec::cmd("hare", "run", path)!;
let stdout_pipe = exec::pipe();
exec::addfile(&cmd, os::stdout_file, stdout_pipe.1);
let stderr_pipe = exec::pipe();
exec::addfile(&cmd, os::stderr_file, stderr_pipe.1);
let proc = exec::start(&cmd)!;
io::close(stdout_pipe.1)!;
io::close(stderr_pipe.1)!;
let status = exec::wait(&proc)!;
let stdout_data = io::drain(stdout_pipe.0)!;
io::close(stdout_pipe.0)!;
let stderr_data = io::drain(stderr_pipe.0)!;
io::close(stderr_pipe.0)!;
let stdout = strings::fromutf8(stdout_data) as str;
let stderr = strings::fromutf8(stderr_data) as str;
return (stdout, stderr);
};