window: use window as global object

This commit is contained in:
Pierre Tachoire
2024-02-26 10:57:51 +01:00
parent b8bf09c8e5
commit fec212ab94
9 changed files with 22 additions and 43 deletions

View File

@@ -240,9 +240,7 @@ pub const Page = struct {
// add global objects // add global objects
log.debug("setup global env", .{}); log.debug("setup global env", .{});
try self.session.env.addObject(self.session.window, "window"); try self.session.env.bindGlobal(self.session.window);
try self.session.env.addObject(self.session.window, "self");
try self.session.env.addObject(html_doc, "document");
// browse the DOM tree to retrieve scripts // browse the DOM tree to retrieve scripts
// TODO execute the synchronous scripts during the HTL parsing. // TODO execute the synchronous scripts during the HTL parsing.

View File

@@ -9,6 +9,7 @@ const EventTarget = @import("../dom/event_target.zig").EventTarget;
pub const Window = struct { pub const Window = struct {
pub const prototype = *EventTarget; pub const prototype = *EventTarget;
pub const mem_guarantied = true; pub const mem_guarantied = true;
pub const global_type = true;
// Extend libdom event target for pure zig struct. // Extend libdom event target for pure zig struct.
base: parser.EventTargetTBase = parser.EventTargetTBase{}, base: parser.EventTargetTBase = parser.EventTargetTBase{},

View File

@@ -4,6 +4,7 @@ const jsruntime = @import("jsruntime");
const parser = @import("netsurf.zig"); const parser = @import("netsurf.zig");
const apiweb = @import("apiweb.zig"); const apiweb = @import("apiweb.zig");
const Window = @import("html/window.zig").Window;
pub const Types = jsruntime.reflect(apiweb.Interfaces); pub const Types = jsruntime.reflect(apiweb.Interfaces);
@@ -16,17 +17,14 @@ fn execJS(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
) anyerror!void { ) anyerror!void {
// start JS env // start JS env
try js_env.start(alloc); try js_env.start(alloc);
defer js_env.stop(); defer js_env.stop();
// alias global as self and window // alias global as self and window
try js_env.attachObject(try js_env.getGlobal(), "self", null); var window = Window.create(null);
try js_env.attachObject(try js_env.getGlobal(), "window", null); window.replaceDocument(parser.documentHTMLToDocument(doc));
try js_env.bindGlobal(window);
// add document object
try js_env.addObject(doc, "document");
while (true) { while (true) {

View File

@@ -3,6 +3,7 @@ const Browser = @import("browser/browser.zig").Browser;
const jsruntime = @import("jsruntime"); const jsruntime = @import("jsruntime");
const apiweb = @import("apiweb.zig"); const apiweb = @import("apiweb.zig");
pub const Types = jsruntime.reflect(apiweb.Interfaces); pub const Types = jsruntime.reflect(apiweb.Interfaces);
pub const std_options = struct { pub const std_options = struct {

View File

@@ -4,6 +4,7 @@ const jsruntime = @import("jsruntime");
const parser = @import("netsurf.zig"); const parser = @import("netsurf.zig");
const apiweb = @import("apiweb.zig"); const apiweb = @import("apiweb.zig");
const Window = @import("html/window.zig").Window;
const html_test = @import("html_test.zig").html; const html_test = @import("html_test.zig").html;
@@ -15,17 +16,14 @@ fn execJS(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
) anyerror!void { ) anyerror!void {
// start JS env // start JS env
try js_env.start(alloc); try js_env.start(alloc);
defer js_env.stop(); defer js_env.stop();
// alias global as self and window // alias global as self and window
try js_env.attachObject(try js_env.getGlobal(), "self", null); var window = Window.create(null);
try js_env.attachObject(try js_env.getGlobal(), "window", null); window.replaceDocument(parser.documentHTMLToDocument(doc));
try js_env.bindGlobal(window);
// add document object
try js_env.addObject(doc, "document");
// launch shellExec // launch shellExec
try jsruntime.shellExec(alloc, js_env); try jsruntime.shellExec(alloc, js_env);

View File

@@ -30,6 +30,7 @@ const Out = enum {
}; };
pub const Types = jsruntime.reflect(apiweb.Interfaces); pub const Types = jsruntime.reflect(apiweb.Interfaces);
pub const GlobalType = apiweb.GlobalType;
// TODO For now the WPT tests run is specific to WPT. // TODO For now the WPT tests run is specific to WPT.
// It manually load js framwork libs, and run the first script w/ js content in // It manually load js framwork libs, and run the first script w/ js content in

View File

@@ -41,10 +41,6 @@ fn testExecFn(
try js_env.start(alloc); try js_env.start(alloc);
defer js_env.stop(); defer js_env.stop();
// alias global as self and window
try js_env.attachObject(try js_env.getGlobal(), "self", null);
try js_env.attachObject(try js_env.getGlobal(), "window", null);
// document // document
const file = try std.fs.cwd().openFile("test.html", .{}); const file = try std.fs.cwd().openFile("test.html", .{});
defer file.close(); defer file.close();
@@ -54,8 +50,10 @@ fn testExecFn(
std.debug.print("documentHTMLClose error: {s}\n", .{@errorName(err)}); std.debug.print("documentHTMLClose error: {s}\n", .{@errorName(err)});
}; };
// add document object // alias global as self and window
try js_env.addObject(doc, "document"); var window = Window.create(null);
window.replaceDocument(parser.documentHTMLToDocument(doc));
try js_env.bindGlobal(window);
// run test // run test
try execFn(alloc, js_env); try execFn(alloc, js_env);

View File

@@ -8,6 +8,7 @@ const parser = @import("../netsurf.zig");
const jsruntime = @import("jsruntime"); const jsruntime = @import("jsruntime");
const Loop = jsruntime.Loop; const Loop = jsruntime.Loop;
const Env = jsruntime.Env; const Env = jsruntime.Env;
const Window = @import("../html/window.zig").Window;
const Types = @import("../main_wpt.zig").Types; const Types = @import("../main_wpt.zig").Types;
@@ -50,31 +51,14 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
} }
// setup global env vars. // setup global env vars.
try js_env.attachObject(try js_env.getGlobal(), "self", null); var window = Window.create(null);
try js_env.attachObject(try js_env.getGlobal(), "window", null); window.replaceDocument(doc);
try js_env.addObject(html_doc, "document"); try js_env.bindGlobal(window);
// thanks to the arena, we don't need to deinit res. // thanks to the arena, we don't need to deinit res.
var res: jsruntime.JSResult = undefined; var res: jsruntime.JSResult = undefined;
const init = const init =
\\window.listeners = [];
\\window.document = document;
\\window.parent = window;
\\window.addEventListener = function (type, listener, options) {
\\ window.listeners.push({type: type, listener: listener, options: options});
\\};
\\window.dispatchEvent = function (event) {
\\ len = window.listeners.length;
\\ for (var i = 0; i < len; i++) {
\\ if (window.listeners[i].type == event.target) {
\\ window.listeners[i].listener(event);
\\ }
\\ }
\\ return true;
\\};
\\window.removeEventListener = function () {};
\\
\\console = []; \\console = [];
\\console.log = function () { \\console.log = function () {
\\ console.push(...arguments); \\ console.push(...arguments);
@@ -116,7 +100,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
} }
// Mark tests as ready to run. // Mark tests as ready to run.
res = try evalJS(js_env, alloc, "window.dispatchEvent({target: 'load'});", "ready"); res = try evalJS(js_env, alloc, "window.dispatchEvent(new Event('load'));", "ready");
if (!res.success) { if (!res.success) {
return res; return res;
} }