quickjs_rusty/context/
builder.rs1use super::Context;
2use crate::{console, ContextError};
3
4#[derive(Default)]
8pub struct ContextBuilder {
9 memory_limit: Option<usize>,
10 console_backend: Option<Box<dyn console::ConsoleBackend>>,
11}
12
13impl ContextBuilder {
14 pub fn new() -> Self {
15 Self {
16 memory_limit: None,
17 console_backend: None,
18 }
19 }
20
21 pub fn memory_limit(self, max_bytes: usize) -> Self {
27 let mut s = self;
28 s.memory_limit = Some(max_bytes);
29 s
30 }
31
32 pub fn console<B>(mut self, backend: B) -> Self
39 where
40 B: console::ConsoleBackend,
41 {
42 self.console_backend = Some(Box::new(backend));
43 self
44 }
45
46 pub fn build(self) -> Result<Context, ContextError> {
48 let context = Context::new(self.memory_limit)?;
49 if let Some(be) = self.console_backend {
50 context.set_console(be).map_err(ContextError::Execution)?;
51 }
52 Ok(context)
53 }
54}