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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::ffi::CString;
use std::ptr::NonNull;

use failure::Error;
use foreign_types::ForeignTypeRef;

use crate::{ffi, ContextRef, EvalBinary, ModuleDef, RuntimeRef};

impl ContextRef {
    pub fn init_module_std(&self) -> Result<NonNull<ModuleDef>, Error> {
        debug!("init `std` module");

        self.check_null(unsafe { ffi::js_init_module_std(self.as_ptr(), cstr!(std).as_ptr()) })
    }

    pub fn init_module_os(&self) -> Result<NonNull<ModuleDef>, Error> {
        debug!("init `os` module");

        self.check_null(unsafe { ffi::js_init_module_os(self.as_ptr(), cstr!(os).as_ptr()) })
    }

    pub fn std_add_helpers<I: IntoIterator<Item = S>, S: Into<Vec<u8>>>(
        &self,
        args: I,
    ) -> Result<(), Error> {
        let args = args
            .into_iter()
            .map(CString::new)
            .collect::<Result<Vec<_>, _>>()?;

        debug!("add global helpers with script args: {:?}", args);

        let args = args.iter().map(|s| s.as_ptr()).collect::<Vec<_>>();

        unsafe {
            ffi::js_std_add_helpers(self.as_ptr(), args.len() as i32, args.as_ptr() as *mut _);
        }

        Ok(())
    }

    pub fn std_loop(&self) {
        unsafe { ffi::js_std_loop(self.as_ptr()) }
    }

    pub fn std_dump_error(&self) {
        unsafe { ffi::js_std_dump_error(self.as_ptr()) }
    }

    pub fn std_eval_binary(&self, buf: &[u8], flags: EvalBinary) {
        unsafe {
            ffi::js_std_eval_binary(self.as_ptr(), buf.as_ptr(), buf.len(), flags.bits() as i32)
        }
    }
}

impl RuntimeRef {
    pub fn std_free_handlers(&self) {
        unsafe { ffi::js_std_free_handlers(self.as_ptr()) }
    }
}