-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
34 lines (28 loc) · 1.12 KB
/
mod.rs
File metadata and controls
34 lines (28 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#![allow(dead_code)]
use std::{ffi::CStr, fs::File, io::Write};
pub fn mrbc_compile(fname: &'static str, code: &'static str) -> Vec<u8> {
let mut src = std::env::temp_dir();
src.push(format!("{}.{}.rb", fname, std::process::id()));
let mut f = File::create(&src).expect("cannot open src file");
f.write_all(code.as_bytes())
.expect("cannot create src file");
f.flush().unwrap();
let mut src0 = src.as_os_str().to_string_lossy().into_owned();
src0.push('\0');
let mut dest = std::env::temp_dir();
dest.push(format!("{}.{}.mrb", fname, std::process::id()));
let mut dest0 = dest.as_os_str().to_string_lossy().into_owned();
dest0.push('\0');
let args = [
CStr::from_bytes_with_nul(b"mrbc\0").unwrap().as_ptr(),
CStr::from_bytes_with_nul(b"-o\0").unwrap().as_ptr(),
CStr::from_bytes_with_nul(dest0.as_bytes())
.unwrap()
.as_ptr(),
CStr::from_bytes_with_nul(src0.as_bytes()).unwrap().as_ptr(),
];
unsafe {
mec_mrbc_sys::mrbc_main(args.len() as i32, args.as_ptr() as *mut *mut i8);
}
std::fs::read(dest).unwrap()
}