Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/browser/browser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub const Page = struct {

// TODO set the referrer to the document.

self.session.window.replaceDocument(doc);
self.session.window.replaceDocument(html_doc);

// https://html.spec.whatwg.org/#read-html

Expand All @@ -240,9 +240,7 @@ pub const Page = struct {

// add global objects
log.debug("setup global env", .{});
try self.session.env.addObject(self.session.window, "window");
try self.session.env.addObject(self.session.window, "self");
try self.session.env.addObject(html_doc, "document");
try self.session.env.bindGlobal(self.session.window);

// browse the DOM tree to retrieve scripts
// TODO execute the synchronous scripts during the HTL parsing.
Expand Down
7 changes: 4 additions & 3 deletions src/html/window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const EventTarget = @import("../dom/event_target.zig").EventTarget;
pub const Window = struct {
pub const prototype = *EventTarget;
pub const mem_guarantied = true;
pub const global_type = true;

// Extend libdom event target for pure zig struct.
base: parser.EventTargetTBase = parser.EventTargetTBase{},

document: ?*parser.Document = null,
document: ?*parser.DocumentHTML = null,
target: []const u8,

pub fn create(target: ?[]const u8) Window {
Expand All @@ -22,7 +23,7 @@ pub const Window = struct {
};
}

pub fn replaceDocument(self: *Window, doc: *parser.Document) void {
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) void {
self.document = doc;
}

Expand All @@ -38,7 +39,7 @@ pub const Window = struct {
return self;
}

pub fn get_document(self: *Window) ?*parser.Document {
pub fn get_document(self: *Window) ?*parser.DocumentHTML {
return self.document;
}

Expand Down
10 changes: 4 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jsruntime = @import("jsruntime");

const parser = @import("netsurf.zig");
const apiweb = @import("apiweb.zig");
const Window = @import("html/window.zig").Window;

pub const Types = jsruntime.reflect(apiweb.Interfaces);

Expand All @@ -16,17 +17,14 @@ fn execJS(
alloc: std.mem.Allocator,
js_env: *jsruntime.Env,
) anyerror!void {

// start JS env
try js_env.start(alloc);
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);

// add document object
try js_env.addObject(doc, "document");
var window = Window.create(null);
window.replaceDocument(doc);
try js_env.bindGlobal(window);

while (true) {

Expand Down
1 change: 1 addition & 0 deletions src/main_get.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Browser = @import("browser/browser.zig").Browser;

const jsruntime = @import("jsruntime");
const apiweb = @import("apiweb.zig");

pub const Types = jsruntime.reflect(apiweb.Interfaces);

pub const std_options = struct {
Expand Down
10 changes: 4 additions & 6 deletions src/main_shell.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jsruntime = @import("jsruntime");

const parser = @import("netsurf.zig");
const apiweb = @import("apiweb.zig");
const Window = @import("html/window.zig").Window;

const html_test = @import("html_test.zig").html;

Expand All @@ -15,17 +16,14 @@ fn execJS(
alloc: std.mem.Allocator,
js_env: *jsruntime.Env,
) anyerror!void {

// start JS env
try js_env.start(alloc);
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);

// add document object
try js_env.addObject(doc, "document");
var window = Window.create(null);
window.replaceDocument(doc);
try js_env.bindGlobal(window);

// launch shellExec
try jsruntime.shellExec(alloc, js_env);
Expand Down
1 change: 1 addition & 0 deletions src/main_wpt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const Out = enum {
};

pub const Types = jsruntime.reflect(apiweb.Interfaces);
pub const GlobalType = apiweb.GlobalType;

// 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
Expand Down
15 changes: 7 additions & 8 deletions src/run_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ fn testExecFn(
js_env: *jsruntime.Env,
comptime execFn: jsruntime.ContextExecFn,
) anyerror!void {

// start JS env
try js_env.start(alloc);
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
const file = try std.fs.cwd().openFile("test.html", .{});
defer file.close();
Expand All @@ -54,8 +49,10 @@ fn testExecFn(
std.debug.print("documentHTMLClose error: {s}\n", .{@errorName(err)});
};

// add document object
try js_env.addObject(doc, "document");
// alias global as self and window
var window = Window.create(null);
window.replaceDocument(doc);
try js_env.bindGlobal(window);

// run test
try execFn(alloc, js_env);
Expand Down Expand Up @@ -91,6 +88,8 @@ fn testsAllExecFn(
}

pub fn main() !void {
try testJSRuntime();

std.debug.print("\n", .{});
for (builtin.test_functions) |test_fn| {
try test_fn.func();
Expand All @@ -106,7 +105,7 @@ test {
std.testing.refAllDecls(DumpTest);
}

test "jsruntime" {
fn testJSRuntime() !void {
// generate tests
try generate.tests();

Expand Down
28 changes: 6 additions & 22 deletions src/wpt/run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const parser = @import("../netsurf.zig");
const jsruntime = @import("jsruntime");
const Loop = jsruntime.Loop;
const Env = jsruntime.Env;
const Window = @import("../html/window.zig").Window;

const Types = @import("../main_wpt.zig").Types;

Expand All @@ -22,7 +23,6 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
defer file.close();

const html_doc = try parser.documentHTMLParse(file.reader(), "UTF-8");
const doc = parser.documentHTMLToDocument(html_doc);

const dirname = fspath.dirname(f[dir.len..]) orelse unreachable;

Expand Down Expand Up @@ -50,31 +50,14 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
}

// setup global env vars.
try js_env.attachObject(try js_env.getGlobal(), "self", null);
try js_env.attachObject(try js_env.getGlobal(), "window", null);
try js_env.addObject(html_doc, "document");
var window = Window.create(null);
window.replaceDocument(html_doc);
try js_env.bindGlobal(window);

// thanks to the arena, we don't need to deinit res.
var res: jsruntime.JSResult = undefined;

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.log = function () {
\\ console.push(...arguments);
Expand All @@ -86,6 +69,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
}

// loop hover the scripts.
const doc = parser.documentHTMLToDocument(html_doc);
const scripts = try parser.documentGetElementsByTagName(doc, "script");
const slen = try parser.nodeListLength(scripts);
for (0..slen) |i| {
Expand Down Expand Up @@ -116,7 +100,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
}

// 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) {
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/jsruntime-lib