diff --git a/src/browser/page.zig b/src/browser/page.zig index 0b9a0439..f13b76a2 100644 --- a/src/browser/page.zig +++ b/src/browser/page.zig @@ -95,6 +95,8 @@ pub const Page = struct { state_pool: *std.heap.MemoryPool(State), + polyfill_loader: polyfill.Loader = .{}, + pub fn init(self: *Page, arena: Allocator, session: *Session) !void { const browser = session.browser; self.* = .{ @@ -117,10 +119,7 @@ pub const Page = struct { }), .main_context = undefined, }; - self.main_context = try session.executor.createJsContext(&self.window, self, self, true); - - // load polyfills - try polyfill.load(self.arena, self.main_context); + self.main_context = try session.executor.createJsContext(&self.window, self, self, true, Env.GlobalMissingCallback.init(&self.polyfill_loader)); _ = try session.browser.app.loop.timeout(1 * std.time.ns_per_ms, &self.microtask_node); // message loop must run only non-test env diff --git a/src/browser/polyfill/fetch.zig b/src/browser/polyfill/fetch.zig index 1438022a..04bfa65c 100644 --- a/src/browser/polyfill/fetch.zig +++ b/src/browser/polyfill/fetch.zig @@ -16,7 +16,7 @@ test "Browser.fetch" { var runner = try testing.jsRunner(testing.tracking_allocator, .{}); defer runner.deinit(); - try @import("polyfill.zig").load(testing.allocator, runner.page.main_context); + try @import("polyfill.zig").Loader.load("fetch", source, runner.page.main_context); try runner.testCases(&.{ .{ diff --git a/src/browser/polyfill/polyfill.zig b/src/browser/polyfill/polyfill.zig index f7baddc9..e7e69600 100644 --- a/src/browser/polyfill/polyfill.zig +++ b/src/browser/polyfill/polyfill.zig @@ -23,25 +23,36 @@ const log = @import("../../log.zig"); const Allocator = std.mem.Allocator; const Env = @import("../env.zig").Env; -const modules = [_]struct { - name: []const u8, - source: []const u8, -}{ - .{ .name = "polyfill-fetch", .source = @import("fetch.zig").source }, -}; +pub const Loader = struct { + done: struct { + fetch: bool = false, + } = .{}, -pub fn load(allocator: Allocator, js_context: *Env.JsContext) !void { - var try_catch: Env.TryCatch = undefined; - try_catch.init(js_context); - defer try_catch.deinit(); + pub fn load(name: []const u8, source: []const u8, js_context: *Env.JsContext) !void { + var try_catch: Env.TryCatch = undefined; + try_catch.init(js_context); + defer try_catch.deinit(); - for (modules) |m| { - _ = js_context.exec(m.source, m.name) catch |err| { - if (try try_catch.err(allocator)) |msg| { - defer allocator.free(msg); - log.fatal(.app, "polyfill error", .{ .name = m.name, .err = msg }); + _ = js_context.exec(source, name) catch |err| { + if (try try_catch.err(js_context.call_arena)) |msg| { + log.fatal(.app, "polyfill error", .{ .name = name, .err = msg }); } return err; }; } -} + + pub fn missing(self: *Loader, name: []const u8, js_context: *Env.JsContext) bool { + if (!self.done.fetch and std.mem.eql(u8, name, "fetch")) { + // load the polyfill once. + self.done.fetch = true; + + const _name = "fetch"; + const source = @import("fetch.zig").source; + log.debug(.polyfill, "dynamic load", .{ .property = name }); + load(_name, source, js_context) catch |err| { + log.fatal(.app, "polyfill load", .{ .name = name, .err = err }); + }; + } + return false; + } +}; diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 27544063..a1d25e42 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -569,7 +569,7 @@ const IsolatedWorld = struct { // Currently we have only 1 page/frame and thus also only 1 state in the isolate world. pub fn createContext(self: *IsolatedWorld, page: *Page) !void { if (self.executor.js_context != null) return error.Only1IsolatedContextSupported; - _ = try self.executor.createJsContext(&page.window, page, {}, false); + _ = try self.executor.createJsContext(&page.window, page, {}, false, null); } }; diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zig index 36150619..f2c7d9d0 100644 --- a/src/cdp/domains/page.zig +++ b/src/cdp/domains/page.zig @@ -284,9 +284,6 @@ pub fn pageCreated(bc: anytype, page: *Page) !void { if (bc.isolated_world) |*isolated_world| { // We need to recreate the isolated world context try isolated_world.createContext(page); - - const polyfill = @import("../../browser/polyfill/polyfill.zig"); - try polyfill.load(bc.arena, &isolated_world.executor.js_context.?); } } diff --git a/src/log.zig b/src/log.zig index e00621ee..b50cfc3c 100644 --- a/src/log.zig +++ b/src/log.zig @@ -39,6 +39,7 @@ pub const Scope = enum { unknown_prop, web_api, xhr, + polyfill, }; const Opts = struct { diff --git a/src/main_wpt.zig b/src/main_wpt.zig index 3f2396ee..94a51132 100644 --- a/src/main_wpt.zig +++ b/src/main_wpt.zig @@ -126,8 +126,6 @@ fn run( }); defer runner.deinit(); - try polyfill.load(arena, runner.page.main_context); - // loop over the scripts. const doc = parser.documentHTMLToDocument(runner.page.window.document); const scripts = try parser.documentGetElementsByTagName(doc, "script"); diff --git a/src/runtime/js.zig b/src/runtime/js.zig index 80585970..d62a1714 100644 --- a/src/runtime/js.zig +++ b/src/runtime/js.zig @@ -346,7 +346,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { // when the handle_scope is freed. // We also maintain our own "context_arena" which allows us to have // all page related memory easily managed. - pub fn createJsContext(self: *ExecutionWorld, global: anytype, state: State, module_loader: anytype, enter: bool) !*JsContext { + pub fn createJsContext(self: *ExecutionWorld, global: anytype, state: State, module_loader: anytype, enter: bool, global_callback: ?GlobalMissingCallback) !*JsContext { std.debug.assert(self.js_context == null); const ModuleLoader = switch (@typeInfo(@TypeOf(module_loader))) { @@ -375,6 +375,30 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { const global_template = js_global.getInstanceTemplate(); global_template.setInternalFieldCount(1); + // Configure the missing property callback on the global + // object. + if (global_callback != null) { + const configuration = v8.NamedPropertyHandlerConfiguration{ + .getter = struct { + fn callback(c_name: ?*const v8.C_Name, raw_info: ?*const v8.C_PropertyCallbackInfo) callconv(.c) u8 { + const info = v8.PropertyCallbackInfo.initFromV8(raw_info); + const _isolate = info.getIsolate(); + const v8_context = _isolate.getCurrentContext(); + + const js_context: *JsContext = @ptrFromInt(v8_context.getEmbedderData(1).castTo(v8.BigInt).getUint64()); + + const property = valueToString(js_context.call_arena, .{ .handle = c_name.? }, _isolate, v8_context) catch "???"; + if (js_context.global_callback.?.missing(property, js_context)) { + return v8.Intercepted.Yes; + } + return v8.Intercepted.No; + } + }.callback, + .flags = v8.PropertyHandlerFlags.NonMasking | v8.PropertyHandlerFlags.OnlyInterceptStrings, + }; + global_template.setNamedProperty(configuration, null); + } + // All the FunctionTemplates that we created and setup in Env.init // are now going to get associated with our global instance. inline for (Types, 0..) |s, i| { @@ -456,6 +480,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { .ptr = safe_module_loader, .func = ModuleLoader.fetchModuleSource, }, + .global_callback = global_callback, }; var js_context = &self.js_context.?; @@ -608,6 +633,9 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { // necessary to lookup/store the dependent module in the module_cache. module_identifier: std.AutoHashMapUnmanaged(u32, []const u8) = .empty, + // Global callback is called on missing property. + global_callback: ?GlobalMissingCallback = null, + const ModuleLoader = struct { ptr: *anyopaque, func: *const fn (ptr: *anyopaque, specifier: []const u8) anyerror!?[]const u8, @@ -2546,6 +2574,35 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { self.callScopeEndFn(self.ptr); } }; + + // Callback called on global's property mssing. + // Return true to intercept the exectution or false to let the call + // continue the chain. + pub const GlobalMissingCallback = struct { + ptr: *anyopaque, + missingFn: *const fn (ptr: *anyopaque, name: []const u8, ctx: *JsContext) bool, + + pub fn init(ptr: anytype) GlobalMissingCallback { + const T = @TypeOf(ptr); + const ptr_info = @typeInfo(T); + + const gen = struct { + pub fn missing(pointer: *anyopaque, name: []const u8, ctx: *JsContext) bool { + const self: T = @ptrCast(@alignCast(pointer)); + return ptr_info.pointer.child.missing(self, name, ctx); + } + }; + + return .{ + .ptr = ptr, + .missingFn = gen.missing, + }; + } + + pub fn missing(self: GlobalMissingCallback, name: []const u8, ctx: *JsContext) bool { + return self.missingFn(self.ptr, name, ctx); + } + }; }; } @@ -3431,7 +3488,7 @@ fn valueToDetailString(arena: Allocator, value: v8.Value, isolate: v8.Isolate, v if (debugValueToString(arena, value.castTo(v8.Object), isolate, v8_context)) |ds| { return ds; } else |err| { - log.err(.js, "debug serialize value", .{.err = err}); + log.err(.js, "debug serialize value", .{ .err = err }); } } } diff --git a/src/runtime/testing.zig b/src/runtime/testing.zig index 25695232..87246d8e 100644 --- a/src/runtime/testing.zig +++ b/src/runtime/testing.zig @@ -53,6 +53,7 @@ pub fn Runner(comptime State: type, comptime Global: type, comptime types: anyty state, {}, true, + null, ); return self; }