mirror of https://github.com/boa-dev/boa.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.5 KiB
85 lines
2.5 KiB
2 years ago
|
use boa_engine::{Context, JsValue, Source};
|
||
2 years ago
|
|
||
|
fn main() {
|
||
|
// Create the JavaScript context.
|
||
|
let mut context = Context::default();
|
||
|
|
||
2 years ago
|
// -----------------------------------------
|
||
|
// Loop Iteration Limit
|
||
|
// -----------------------------------------
|
||
|
|
||
2 years ago
|
// Set the context's runtime limit on loops to 10 iterations.
|
||
|
context.runtime_limits_mut().set_loop_iteration_limit(10);
|
||
|
|
||
|
// The code below iterates 5 times, so no error is thrown.
|
||
2 years ago
|
let result = context.eval(Source::from_bytes(
|
||
2 years ago
|
r"
|
||
|
for (let i = 0; i < 5; ++i) { }
|
||
|
",
|
||
|
));
|
||
2 years ago
|
assert!(result.is_ok());
|
||
2 years ago
|
|
||
|
// Here we exceed the limit by 1 iteration and a `RuntimeLimit` error is thrown.
|
||
|
//
|
||
1 year ago
|
// This error cannot be caught in JavaScript, it can only be caught in Rust code.
|
||
2 years ago
|
let result = context.eval(Source::from_bytes(
|
||
2 years ago
|
r"
|
||
|
try {
|
||
1 year ago
|
for (let i = 0; i < 12; ++i) { }
|
||
2 years ago
|
} catch (e) {
|
||
|
|
||
|
}
|
||
|
",
|
||
|
));
|
||
2 years ago
|
assert!(result.is_err());
|
||
2 years ago
|
|
||
1 year ago
|
// Preventing an infinite loop
|
||
2 years ago
|
let result = context.eval(Source::from_bytes(
|
||
2 years ago
|
r"
|
||
|
while (true) { }
|
||
|
",
|
||
|
));
|
||
2 years ago
|
assert!(result.is_err());
|
||
2 years ago
|
|
||
|
// The limit applies to all types of loops.
|
||
2 years ago
|
let result = context.eval(Source::from_bytes(
|
||
2 years ago
|
r"
|
||
1 year ago
|
for (let e of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) { }
|
||
2 years ago
|
",
|
||
|
));
|
||
2 years ago
|
assert!(result.is_err());
|
||
|
|
||
|
// -----------------------------------------
|
||
|
// Recursion Limit
|
||
|
// -----------------------------------------
|
||
|
|
||
|
// Create and register `factorial` function.
|
||
2 years ago
|
let result = context.eval(Source::from_bytes(
|
||
2 years ago
|
r"
|
||
|
function factorial(n) {
|
||
|
if (n == 0) {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
return n * factorial(n - 1);
|
||
|
}
|
||
|
",
|
||
|
));
|
||
|
assert!(result.is_ok());
|
||
|
|
||
|
// Run function before setting the limit and assert that it works.
|
||
2 years ago
|
let result = context.eval(Source::from_bytes("factorial(11)"));
|
||
2 years ago
|
assert_eq!(result, Ok(JsValue::new(39_916_800)));
|
||
|
|
||
|
// Setting runtime limit for recustion to 10.
|
||
|
context.runtime_limits_mut().set_recursion_limit(10);
|
||
|
|
||
|
// Run without exceeding recursion limit and assert that it works.
|
||
2 years ago
|
let result = context.eval(Source::from_bytes("factorial(8)"));
|
||
2 years ago
|
assert_eq!(result, Ok(JsValue::new(40_320)));
|
||
|
|
||
|
// Run exceeding limit by 1 and assert that it fails.
|
||
2 years ago
|
let result = context.eval(Source::from_bytes("factorial(11)"));
|
||
2 years ago
|
assert!(result.is_err());
|
||
2 years ago
|
}
|