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

@@ -88,7 +88,7 @@ pub fn createContext(self: *ExecutionWorld, page: *Page, enter: bool) !*Context
global_template.setNamedProperty(v8.NamedPropertyHandlerConfiguration{
.getter = unknownPropertyCallback,
.flags = v8.PropertyHandlerFlags.NonMasking | v8.PropertyHandlerFlags.OnlyInterceptStrings,
}, v8.External.init(isolate, page));
}, null);
const context_local = v8.Context.init(isolate, global_template, null);
const v8_context = v8.Persistent(v8.Context).init(isolate, context_local).castToContext();
@@ -157,10 +157,6 @@ pub fn resumeExecution(self: *const ExecutionWorld) void {
pub fn unknownPropertyCallback(c_name: ?*const v8.C_Name, raw_info: ?*const v8.C_PropertyCallbackInfo) callconv(.c) u8 {
const info = v8.PropertyCallbackInfo.initFromV8(raw_info);
const data_value = info.getData();
const external = data_value.castTo(v8.External);
const page: *Page = @ptrCast(@alignCast(external.get()));
const context = Context.fromIsolate(info.getIsolate());
const maybe_property: ?[]u8 = context.valueToString(.{ .handle = c_name.? }, .{}) catch null;
@@ -188,7 +184,7 @@ pub fn unknownPropertyCallback(c_name: ?*const v8.C_Name, raw_info: ?*const v8.C
if (maybe_property) |prop| {
if (!ignored.has(prop)) {
const document = page.document;
const document = context.page.document;
if (document.getElementById(prop)) |el| {
const js_value = context.zigValueToJs(el, .{}) catch {

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);