access page from context, document call_depth usage

This commit is contained in:
Karl Seguin
2026-01-06 18:04:17 +08:00
parent 05da040ce1
commit 645ec79fce
2 changed files with 15 additions and 8 deletions

View File

@@ -115,8 +115,19 @@ pub fn tryCallWithThis(self: *const Function, comptime T: type, this: anytype, a
pub fn callWithThis(self: *const Function, comptime T: type, this: anytype, args: anytype) !T {
const context = self.context;
context.call_depth += 1;
defer context.call_depth -= 1;
// When we're calling a function from within JavaScript itself, this isn't
// necessary. We're within a Caller instantiation, which will already have
// incremented the call_depth and it won't decrement it until the Caller is
// done.
// But some JS functions are initiated from Zig code, and not v8. For
// example, Observers, some event and window callbacks. In those cases, we
// need to increase the call_depth so that the call_arena remains valid for
// the duration of the function call. If we don't do this, the call_arena
// will be reset after each statement of the function which executes Zig code.
const call_depth = context.call_depth;
context.call_depth = call_depth + 1;
defer context.call_depth = call_depth;
const js_this = try context.valueToExistingObject(this);