use crate::version::LuaVersion;
use crate::vm::exec::Vm;
#[derive(Debug, Clone, Copy)]
pub struct SandboxBuilder {
version: LuaVersion,
base: bool,
math: bool,
string: bool,
table: bool,
coroutine: bool,
instr_budget: Option<i64>,
memory_cap: Option<usize>,
bytecode_loading: bool,
}
impl SandboxBuilder {
pub(crate) fn new(version: LuaVersion) -> Self {
SandboxBuilder {
version,
base: false,
math: false,
string: false,
table: false,
coroutine: false,
instr_budget: None,
memory_cap: None,
bytecode_loading: false,
}
}
pub fn open_base(mut self) -> Self {
self.base = true;
self
}
pub fn open_math(mut self) -> Self {
self.math = true;
self
}
pub fn open_string(mut self) -> Self {
self.string = true;
self
}
pub fn open_table(mut self) -> Self {
self.table = true;
self
}
pub fn open_coroutine(mut self) -> Self {
self.coroutine = true;
self
}
pub fn with_instr_budget(mut self, n: i64) -> Self {
self.instr_budget = Some(n);
self
}
pub fn with_memory_cap(mut self, n: usize) -> Self {
self.memory_cap = Some(n);
self
}
pub fn allow_bytecode_loading(mut self) -> Self {
self.bytecode_loading = true;
self
}
pub fn build(self) -> Vm {
let mut vm = Vm::new_minimal(self.version);
if self.base {
vm.open_base();
}
if self.math {
vm.open_math();
}
if self.string {
vm.open_string();
}
if self.table {
vm.open_table();
}
if self.coroutine {
vm.open_coroutine();
}
vm.set_instr_budget(self.instr_budget);
if let Some(cap) = self.memory_cap {
vm.set_memory_cap(Some(cap));
}
vm.set_bytecode_loading(self.bytecode_loading);
vm
}
}
impl Vm {
pub fn sandbox(version: LuaVersion) -> SandboxBuilder {
SandboxBuilder::new(version)
}
}