Skip to content

Commit 7e125ea

Browse files
committed
feat: add methods to update stack top and set maximum stack size for QuickJS runtime
1 parent b0a597e commit 7e125ea

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

src/context/context.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,35 @@ impl Context {
205205
}
206206

207207
/// Execute the pending job in the event loop.
208+
/// Update the QuickJS runtime's stack top reference to the current native
209+
/// stack pointer.
210+
///
211+
/// This should be called before any JS execution when entering from Rust,
212+
/// so that QuickJS measures the JS stack depth from the current position
213+
/// rather than from wherever the runtime was first created.
214+
///
215+
/// This is especially important in debug builds where Rust/C frames are
216+
/// significantly larger than in release builds.
217+
pub fn update_stack_top(&self) {
218+
unsafe {
219+
q::JS_UpdateStackTop(self.runtime);
220+
}
221+
}
222+
223+
/// Set the maximum JS stack size (in bytes).
224+
///
225+
/// The default is 1MB. In debug builds the QuickJS C interpreter frames
226+
/// are unoptimized and consume more native stack per JS call, so a larger
227+
/// limit (e.g. 4MB) may be required to run the same code that works fine
228+
/// in release builds.
229+
///
230+
/// Use `0` to disable the stack size limit entirely.
231+
pub fn set_max_stack_size(&self, size: usize) {
232+
unsafe {
233+
q::JS_SetMaxStackSize(self.runtime, size);
234+
}
235+
}
236+
208237
pub fn execute_pending_job(&self) -> Result<(), ExecutionError> {
209238
let mut pctx = Box::new(std::ptr::null_mut::<JSContext>());
210239
unsafe {

0 commit comments

Comments
 (0)