mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Add run_with_limit
This commit is contained in:
@@ -84,7 +84,18 @@ impl ValTrait for ErrorBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
fn to_error(_: ThisWrapper, params: Vec<Val>) -> Result<Val, Val> {
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
($fmt:expr $(, $($arg:expr),*)?) => {{
|
||||
let formatted_string = format!($fmt $(, $($arg),*)?);
|
||||
Err(to_error(
|
||||
ThisWrapper::new(true, &mut Val::Undefined),
|
||||
vec![Val::String(Rc::new(formatted_string))],
|
||||
).unwrap())
|
||||
}};
|
||||
}
|
||||
|
||||
pub fn to_error(_: ThisWrapper, params: Vec<Val>) -> Result<Val, Val> {
|
||||
Ok(Val::Object(Rc::new(VsObject {
|
||||
string_map: BTreeMap::from([(
|
||||
"message".to_string(),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
mod array_builtin;
|
||||
mod boolean_builtin;
|
||||
mod debug_builtin;
|
||||
mod error_builtin;
|
||||
pub mod error_builtin;
|
||||
mod math_builtin;
|
||||
mod number_builtin;
|
||||
pub mod range_error_builtin;
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::builtins::error_builtin::to_error;
|
||||
use crate::bytecode_decoder::BytecodeDecoder;
|
||||
use crate::error;
|
||||
use crate::first_stack_frame::FirstStackFrame;
|
||||
use crate::native_function::ThisWrapper;
|
||||
use crate::stack_frame::FrameStepOk;
|
||||
use crate::stack_frame::StackFrame;
|
||||
use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
|
||||
@@ -38,6 +41,44 @@ impl VirtualMachine {
|
||||
return Ok(self.frame.get_call_result().return_);
|
||||
}
|
||||
|
||||
pub fn run_with_limit(
|
||||
&mut self,
|
||||
bytecode: &Rc<Vec<u8>>,
|
||||
params: &[String],
|
||||
step_limit: usize,
|
||||
) -> Result<Val, Val> {
|
||||
let mut bd = BytecodeDecoder {
|
||||
data: bytecode.clone(),
|
||||
pos: 0,
|
||||
};
|
||||
|
||||
let main_fn = bd.decode_val(&Vec::new());
|
||||
|
||||
let mut frame = match main_fn.load_function() {
|
||||
LoadFunctionResult::StackFrame(f) => f,
|
||||
_ => panic!("bytecode does start with function"),
|
||||
};
|
||||
|
||||
for p in params {
|
||||
frame.write_param(Val::String(Rc::new(p.clone())));
|
||||
}
|
||||
|
||||
self.push(frame);
|
||||
|
||||
let mut step_count = 0;
|
||||
|
||||
while step_count < step_limit {
|
||||
self.step()?;
|
||||
step_count += 1;
|
||||
|
||||
if self.stack.len() == 0 {
|
||||
return Ok(self.frame.get_call_result().return_);
|
||||
}
|
||||
}
|
||||
|
||||
error!("step limit reached")
|
||||
}
|
||||
|
||||
pub fn new() -> VirtualMachine {
|
||||
let mut registers: Vec<Val> = Vec::with_capacity(2);
|
||||
registers.push(Val::Undefined);
|
||||
|
||||
Reference in New Issue
Block a user