Skip to main content

opy_macro_js/
limits.rs

1//! Execution limits for JavaScript invocations.
2//!
3//! Default values mirror the pinned OverPy reference constants
4//! (`src/quickjs.ts`): `MACRO_TIMEOUT_MS = 1000`,
5//! `POST_COMPILE_HOOK_TIMEOUT_MS = 2000`, `MAX_RUNTIME_MEMORY_BYTES =
6//! 64 * 1024 * 1024`, `MAX_RUNTIME_STACK_BYTES = 512 * 1024`.
7
8use std::time::Duration;
9
10/// Resource limits enforced on each JavaScript invocation.
11///
12/// Every field is applied to the engine that executes a single macro or hook
13/// invocation (see [`crate::MacroRuntime`]).
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct Limits {
16    /// Wall-clock budget for macro invocations
17    /// ([`crate::MacroRuntime::run_macro`]).
18    ///
19    /// Enforced by a deadline-based interrupt handler: once the deadline
20    /// passes, the engine aborts the script with the QuickJS `"interrupted"`
21    /// error.
22    pub macro_time_budget: Duration,
23    /// Wall-clock budget for post-compile hook invocations
24    /// ([`crate::MacroRuntime::run_hook`]).
25    pub hook_time_budget: Duration,
26    /// Maximum engine memory in bytes (`JS_SetMemoryLimit` semantics).
27    ///
28    /// When the engine's tracked allocations exceed the limit, the script
29    /// aborts with the `"out of memory"` error.
30    pub memory_limit_bytes: usize,
31    /// Maximum JavaScript stack size in bytes (`JS_SetMaxStackSize` semantics).
32    ///
33    /// Deep recursion aborts with `"Maximum call stack size exceeded"`.
34    pub max_stack_bytes: usize,
35}
36
37impl Default for Limits {
38    fn default() -> Self {
39        Self {
40            macro_time_budget: Duration::from_millis(1000),
41            hook_time_budget: Duration::from_millis(2000),
42            memory_limit_bytes: 64 * 1024 * 1024,
43            max_stack_bytes: 512 * 1024,
44        }
45    }
46}