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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! contains the QuickJsRuntimeBuilder which may be used to instantiate a new QuickjsRuntimeFacade

use crate::facades::QuickJsRuntimeFacade;
use crate::quickjsrealmadapter::QuickJsRealmAdapter;
use crate::quickjsruntimeadapter::QuickJsRuntimeAdapter;

use crate::jsutils::modules::{CompiledModuleLoader, NativeModuleLoader, ScriptModuleLoader};
use crate::jsutils::{JsError, ScriptPreProcessor};
use std::time::Duration;

pub type EsRuntimeInitHooks =
    Vec<Box<dyn FnOnce(&QuickJsRuntimeFacade) -> Result<(), JsError> + Send + 'static>>;

/// the EsRuntimeBuilder is used to init an EsRuntime
/// # Example
/// ```rust
/// use quickjs_runtime::builder::QuickJsRuntimeBuilder;
/// // init a rt which may use 16MB of memory
/// let rt = QuickJsRuntimeBuilder::new()
/// .memory_limit(1024*1024*16)
/// .build();
/// ```
pub struct QuickJsRuntimeBuilder {
    pub(crate) script_module_loaders: Vec<Box<dyn ScriptModuleLoader + Send>>,
    pub(crate) native_module_loaders: Vec<Box<dyn NativeModuleLoader + Send>>,
    pub(crate) compiled_module_loaders: Vec<Box<dyn CompiledModuleLoader + Send>>,
    pub(crate) opt_memory_limit_bytes: Option<u64>,
    pub(crate) opt_gc_threshold: Option<u64>,
    pub(crate) opt_max_stack_size: Option<u64>,
    pub(crate) opt_gc_interval: Option<Duration>,
    pub(crate) runtime_init_hooks: EsRuntimeInitHooks,
    pub(crate) script_pre_processors: Vec<Box<dyn ScriptPreProcessor + Send>>,
    #[allow(clippy::type_complexity)]
    pub(crate) interrupt_handler: Option<Box<dyn Fn(&QuickJsRuntimeAdapter) -> bool + Send>>,
}

impl QuickJsRuntimeBuilder {
    /// build an EsRuntime
    pub fn build(self) -> QuickJsRuntimeFacade {
        log::debug!("QuickJsRuntimeBuilder.build");
        QuickJsRuntimeFacade::new(self)
    }

    /// init a new EsRuntimeBuilder
    pub fn new() -> Self {
        Self {
            script_module_loaders: vec![],
            native_module_loaders: vec![],
            compiled_module_loaders: vec![],
            opt_memory_limit_bytes: None,
            opt_gc_threshold: None,
            opt_max_stack_size: None,
            opt_gc_interval: None,
            runtime_init_hooks: vec![],
            script_pre_processors: vec![],
            interrupt_handler: None,
        }
    }

    /// add a script loaders which will be used to load modules when they are imported from script
    /// # Example
    /// ```rust
    /// use quickjs_runtime::builder::QuickJsRuntimeBuilder;
    /// use quickjs_runtime::jsutils::modules::ScriptModuleLoader;
    /// use quickjs_runtime::quickjsrealmadapter::QuickJsRealmAdapter;
    /// use quickjs_runtime::jsutils::Script;
    /// struct MyModuleLoader {}
    /// impl ScriptModuleLoader for MyModuleLoader {
    ///     fn normalize_path(&self, realm: &QuickJsRealmAdapter ,ref_path: &str,path: &str) -> Option<String> {
    ///         Some(path.to_string())
    ///     }
    ///
    ///     fn load_module(&self, realm: &QuickJsRealmAdapter, absolute_path: &str) -> String {
    ///         "export const foo = 12;".to_string()
    ///     }
    /// }
    ///
    /// let rt = QuickJsRuntimeBuilder::new()
    ///     .script_module_loader(MyModuleLoader{})
    ///     .build();
    /// rt.eval_module_sync(None, Script::new("test_module.es", "import {foo} from 'some_module.mes';\nconsole.log('foo = %s', foo);")).ok().unwrap();
    /// ```
    pub fn script_module_loader<M: ScriptModuleLoader + Send + 'static>(
        mut self,
        loader: M,
    ) -> Self {
        self.script_module_loaders.push(Box::new(loader));
        self
    }

    /// add a ScriptPreProcessor which will be called for all scripts which are evaluated and compiled
    pub fn script_pre_processor<S: ScriptPreProcessor + Send + 'static>(
        mut self,
        processor: S,
    ) -> Self {
        self.script_pre_processors.push(Box::new(processor));
        self
    }

    /// add a module loader which can load native functions and proxy classes
    /// # Example
    /// ```rust
    /// use quickjs_runtime::builder::QuickJsRuntimeBuilder;
    /// use quickjs_runtime::jsutils::modules::NativeModuleLoader;
    /// use quickjs_runtime::jsutils::Script;
    /// use quickjs_runtime::quickjsvalueadapter::QuickJsValueAdapter;
    /// use quickjs_runtime::quickjsrealmadapter::QuickJsRealmAdapter;
    /// use quickjs_runtime::quickjs_utils::functions;
    /// use quickjs_runtime::quickjs_utils::primitives::{from_bool, from_i32};
    /// use quickjs_runtime::reflection::Proxy;
    ///
    /// struct MyModuleLoader{}
    /// impl NativeModuleLoader for MyModuleLoader {
    ///     fn has_module(&self, _q_ctx: &QuickJsRealmAdapter,module_name: &str) -> bool {
    ///         module_name.eq("my_module")
    ///     }
    ///
    ///     fn get_module_export_names(&self, _q_ctx: &QuickJsRealmAdapter, _module_name: &str) -> Vec<&str> {
    ///         vec!["someVal", "someFunc", "SomeClass"]
    ///     }
    ///
    ///     fn get_module_exports(&self, q_ctx: &QuickJsRealmAdapter, _module_name: &str) -> Vec<(&str, QuickJsValueAdapter)> {
    ///         
    ///         let js_val = from_i32(1470);
    ///         let js_func = functions::new_function_q(
    ///             q_ctx,
    ///             "someFunc", |_q_ctx, _this, _args| {
    ///                 return Ok(from_i32(432));
    ///             }, 0)
    ///             .ok().unwrap();
    ///         let js_class = Proxy::new()
    ///             .name("SomeClass")
    ///             .static_method("doIt", |_rt, _q_ctx, _args|{
    ///                 return Ok(from_i32(185));
    ///             })
    ///             .install(q_ctx, false)
    ///             .ok().unwrap();
    ///
    ///         vec![("someVal", js_val), ("someFunc", js_func), ("SomeClass", js_class)]
    ///     }
    /// }
    ///
    /// let rt = QuickJsRuntimeBuilder::new()
    /// .native_module_loader(MyModuleLoader{})
    /// .build();
    ///
    /// rt.eval_module_sync(None, Script::new("test_native_mod.es", "import {someVal, someFunc, SomeClass} from 'my_module';\nlet i = (someVal + someFunc() + SomeClass.doIt());\nif (i !== 2087){throw Error('i was not 2087');}")).ok().expect("script failed");
    /// ```
    pub fn native_module_loader<S: NativeModuleLoader + Send + 'static>(
        mut self,
        module_loader: S,
    ) -> Self
    where
        Self: Sized,
    {
        self.native_module_loaders.push(Box::new(module_loader));
        self
    }

    /// set max memory the runtime may use
    pub fn memory_limit(mut self, bytes: u64) -> Self {
        self.opt_memory_limit_bytes = Some(bytes);
        self
    }

    /// number of allocations before gc is run
    pub fn gc_threshold(mut self, size: u64) -> Self {
        self.opt_gc_threshold = Some(size);
        self
    }

    /// set a max stack size
    pub fn max_stack_size(mut self, size: u64) -> Self {
        self.opt_max_stack_size = Some(size);
        self
    }

    /// set a Garbage Collection interval, this will start a timer thread which will trigger a full GC every set interval
    pub fn gc_interval(mut self, interval: Duration) -> Self {
        self.opt_gc_interval = Some(interval);
        self
    }

    /// add an interrupt handler, this will be called several times during script execution and may be used to cancel a running script
    pub fn set_interrupt_handler<I: Fn(&QuickJsRuntimeAdapter) -> bool + Send + 'static>(
        mut self,
        interrupt_handler: I,
    ) -> Self {
        self.interrupt_handler = Some(Box::new(interrupt_handler));
        self
    }
}

impl Default for QuickJsRuntimeBuilder {
    fn default() -> Self {
        QuickJsRuntimeBuilder::new()
    }
}

impl QuickJsRuntimeBuilder {
    pub fn runtime_facade_init_hook<
        H: FnOnce(&QuickJsRuntimeFacade) -> Result<(), JsError> + Send + 'static,
    >(
        mut self,
        hook: H,
    ) -> Self {
        self.runtime_init_hooks.push(Box::new(hook));
        self
    }

    pub fn realm_adapter_init_hook<
        H: Fn(&QuickJsRuntimeAdapter, &QuickJsRealmAdapter) -> Result<(), JsError> + Send + 'static,
    >(
        self,
        hook: H,
    ) -> Self {
        self.runtime_adapter_init_hook(move |rt| {
            rt.add_context_init_hook(hook)?;
            Ok(())
        })
    }

    pub fn runtime_adapter_init_hook<
        H: FnOnce(&QuickJsRuntimeAdapter) -> Result<(), JsError> + Send + 'static,
    >(
        self,
        hook: H,
    ) -> Self {
        self.runtime_facade_init_hook(|rt| {
            rt.exe_rt_task_in_event_loop(|rt| {
                let _ = hook(rt);
            });
            Ok(())
        })
    }

    pub fn compiled_module_loader<S: CompiledModuleLoader + Send + 'static>(
        mut self,
        module_loader: S,
    ) -> Self {
        self.compiled_module_loaders.push(Box::new(module_loader));
        self
    }
}

#[cfg(test)]
pub mod tests {
    use crate::builder::QuickJsRuntimeBuilder;
    use crate::jsutils::modules::ScriptModuleLoader;
    use crate::jsutils::Script;
    use crate::quickjsrealmadapter::QuickJsRealmAdapter;

    #[test]
    fn test_module_loader() {
        crate::facades::tests::init_logging();

        struct MyModuleLoader {}
        impl ScriptModuleLoader for MyModuleLoader {
            fn normalize_path(
                &self,
                _realm: &QuickJsRealmAdapter,
                _ref_path: &str,
                path: &str,
            ) -> Option<String> {
                Some(path.to_string())
            }

            fn load_module(&self, _realm: &QuickJsRealmAdapter, _absolute_path: &str) -> String {
                "export const foo = 12;".to_string()
            }
        }

        let rt = QuickJsRuntimeBuilder::new()
            .script_module_loader(MyModuleLoader {})
            .build();
        match rt.eval_module_sync(
            None,
            Script::new(
                "test_module.es",
                "import {foo} from 'some_module.mes';\nconsole.log('foo = %s', foo);",
            ),
        ) {
            Ok(_) => {}
            Err(e) => panic!("script failed {}", e),
        }
    }
}