intuicio_runner_simpleton/
lib.rs

1mod library;
2
3use intuicio_backend_vm::prelude::*;
4use intuicio_core::prelude::*;
5use intuicio_frontend_simpleton::{
6    library::jobs::Jobs,
7    script::{
8        SimpletonContentParser, SimpletonModule, SimpletonPackage, SimpletonScriptExpression,
9    },
10    Integer, Reference,
11};
12use std::path::Path;
13
14#[derive(Debug, Default, Clone)]
15pub struct Config {
16    pub name: Option<String>,
17    pub module_name: Option<String>,
18    pub stack_capacity: Option<usize>,
19    pub registers_capacity: Option<usize>,
20    pub heap_page_capacity: Option<usize>,
21    pub into_code: Option<String>,
22    pub into_intuicio: Option<String>,
23}
24
25pub fn execute(entry: &str, config: Config, args: impl IntoIterator<Item = String>) -> i32 {
26    let entry = if Path::new(entry).is_dir() {
27        format!("{}/main.simp", entry)
28    } else {
29        entry.to_owned()
30    };
31    let mut content_provider = ExtensionContentProvider::<SimpletonModule>::default()
32        .extension(
33            "simp",
34            FileContentProvider::new("simp", SimpletonContentParser),
35        )
36        .extension("plugin", IgnoreContentProvider)
37        .default_extension("simp");
38    let package = SimpletonPackage::new(&entry, &mut content_provider).unwrap();
39    if let Some(path) = &config.into_code {
40        std::fs::write(path, format!("{:#?}", package)).unwrap();
41    }
42    if let Some(path) = &config.into_intuicio {
43        std::fs::write(path, format!("{:#?}", package.compile())).unwrap();
44    }
45    if config.into_code.is_some() || config.into_intuicio.is_some() {
46        return 0;
47    }
48    let stack_capacity = config.stack_capacity.unwrap_or(1024);
49    let registers_capacity = config.registers_capacity.unwrap_or(1024);
50    let heap_page_capacity = config.heap_page_capacity.unwrap_or(1024);
51    let host_producer = HostProducer::new(move || {
52        let mut registry = Registry::default();
53        intuicio_frontend_simpleton::library::install(&mut registry);
54        crate::library::install(&mut registry);
55        let packages_dir = dirs::data_dir()
56            .unwrap()
57            .join(".simpleton")
58            .join("packages")
59            .to_string_lossy()
60            .to_string();
61        package.install_plugins(
62            &mut registry,
63            &[
64                #[cfg(test)]
65                "../../target/debug/",
66                "./",
67                packages_dir.as_str(),
68            ],
69        );
70        let package = package.compile();
71        package.install::<VmScope<SimpletonScriptExpression>>(&mut registry, None);
72        let context = Context::new(stack_capacity, registers_capacity, heap_page_capacity);
73        Host::new(context, registry.into())
74    });
75    let mut host = host_producer.produce();
76    host.context()
77        .set_custom(Jobs::HOST_PRODUCER_CUSTOM, host_producer);
78    let args = Reference::new_array(
79        args.into_iter()
80            .map(|arg| Reference::new_text(arg, host.registry()))
81            .collect(),
82        host.registry(),
83    );
84    host.call_function::<(Reference,), _>(
85        &config.name.unwrap_or_else(|| "main".to_owned()),
86        &config.module_name.unwrap_or_else(|| "main".to_owned()),
87        None,
88    )
89    .unwrap()
90    .run((args,))
91    .0
92    .read::<Integer>()
93    .map(|result| *result as i32)
94    .unwrap_or(0)
95}
96
97#[cfg(test)]
98mod tests {
99    use crate::*;
100
101    #[test]
102    fn test_runner() {
103        assert_eq!(
104            execute("./resources/examples", Config::default(), vec![]),
105            0
106        );
107    }
108}