Skip to content

Commit 3222629

Browse files
committed
Remove the generic nature of Env and most of the JS classes
Back in the zig-js-runtime days, globals were used for the state and webapi declarations. This caused problems largely because it was done across compilation units (using @import("root")...). The generic Env(S, WebApi) was used to solve these problems, while still making it work for different States and WebApis. This change removes the generics and hard-codes the *Page as the state and only supports our WebApis for the class declarations. To accommodate this change, the runtime/*tests* have been removed. I don't consider this a huge loss - whatever behavior these were testing, already exists in the browser/**/*.zig web api. As we write more complex/complete WebApis, we're seeing more and more cases that need to rely on js objects directly (JsObject, Function, Promises, etc...). The goal is to make these easier to use. Rather than using Env.JsObject, you now import "js.zig" and use js.JsObject (TODO: rename JsObject to Object). Everything is just a plain Zig struct, rather than being nested in a generic. After this change, I plan on: 1 - Renaming the js objects, JsObject -> Object. These should be referenced in the webapi as js.Object, js.This, ... 2 - Splitting the code across multiple files (Env.zig, Context.zig, Caller.zig, ...)
1 parent ab18c90 commit 3222629

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+4574
-5684
lines changed

src/app.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Allocator = std.mem.Allocator;
44

55
const log = @import("log.zig");
66
const Http = @import("http/Http.zig");
7-
const Platform = @import("runtime/js.zig").Platform;
7+
const Platform = @import("browser/js/js.zig").Platform;
88

99
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
1010
const Notification = @import("notification.zig").Notification;

src/browser/ScriptManager.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
const std = @import("std");
2020

21+
const js = @import("js/js.zig");
2122
const log = @import("../log.zig");
2223
const parser = @import("netsurf.zig");
2324

24-
const Env = @import("env.zig").Env;
2525
const Page = @import("page.zig").Page;
2626
const DataURI = @import("DataURI.zig");
2727
const Http = @import("../http/Http.zig");
@@ -627,7 +627,7 @@ const Script = struct {
627627

628628
const Callback = union(enum) {
629629
string: []const u8,
630-
function: Env.Function,
630+
function: js.Function,
631631
};
632632

633633
const Source = union(enum) {
@@ -664,7 +664,7 @@ const Script = struct {
664664
});
665665

666666
const js_context = page.main_context;
667-
var try_catch: Env.TryCatch = undefined;
667+
var try_catch: js.TryCatch = undefined;
668668
try_catch.init(js_context);
669669
defer try_catch.deinit();
670670

@@ -706,7 +706,7 @@ const Script = struct {
706706

707707
switch (callback) {
708708
.string => |str| {
709-
var try_catch: Env.TryCatch = undefined;
709+
var try_catch: js.TryCatch = undefined;
710710
try_catch.init(page.main_context);
711711
defer try_catch.deinit();
712712

@@ -728,7 +728,7 @@ const Script = struct {
728728
};
729729
defer parser.eventDestroy(loadevt);
730730

731-
var result: Env.Function.Result = undefined;
731+
var result: js.Function.Result = undefined;
732732
const iface = Event.toInterface(loadevt);
733733
f.tryCall(void, .{iface}, &result) catch {
734734
log.warn(.user_script, "script callback", .{

src/browser/State.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
// this quickly proved necessary, since different fields are needed on the same
2727
// data at different levels of the prototype chain. This isn't memory efficient.
2828

29-
const Env = @import("env.zig").Env;
29+
const js = @import("js/js.zig");
3030
const parser = @import("netsurf.zig");
3131
const DataSet = @import("html/DataSet.zig");
3232
const ShadowRoot = @import("dom/shadow_root.zig").ShadowRoot;
3333
const StyleSheet = @import("cssom/StyleSheet.zig");
3434
const CSSStyleDeclaration = @import("cssom/CSSStyleDeclaration.zig");
3535

3636
// for HTMLScript (but probably needs to be added to more)
37-
onload: ?Env.Function = null,
38-
onerror: ?Env.Function = null,
37+
onload: ?js.Function = null,
38+
onerror: ?js.Function = null,
3939

4040
// for HTMLElement
4141
style: CSSStyleDeclaration = .empty,
@@ -53,7 +53,7 @@ style_sheet: ?*StyleSheet = null,
5353

5454
// for dom/document
5555
active_element: ?*parser.Element = null,
56-
adopted_style_sheets: ?Env.JsObject = null,
56+
adopted_style_sheets: ?js.JsObject = null,
5757

5858
// for HTMLSelectElement
5959
// By default, if no option is explicitly selected, the first option should

src/browser/browser.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const std = @import("std");
2121
const Allocator = std.mem.Allocator;
2222
const ArenaAllocator = std.heap.ArenaAllocator;
2323

24+
const js = @import("js/js.zig");
2425
const State = @import("State.zig");
25-
const Env = @import("env.zig").Env;
2626
const App = @import("../app.zig").App;
2727
const Session = @import("session.zig").Session;
2828
const Notification = @import("../notification.zig").Notification;
@@ -34,7 +34,7 @@ const HttpClient = @import("../http/Client.zig");
3434
// You can create multiple browser instances.
3535
// A browser contains only one session.
3636
pub const Browser = struct {
37-
env: *Env,
37+
env: *js.Env,
3838
app: *App,
3939
session: ?Session,
4040
allocator: Allocator,
@@ -48,7 +48,7 @@ pub const Browser = struct {
4848
pub fn init(app: *App) !Browser {
4949
const allocator = app.allocator;
5050

51-
const env = try Env.init(allocator, &app.platform, .{});
51+
const env = try js.Env.init(allocator, &app.platform, .{});
5252
errdefer env.deinit();
5353

5454
const notification = try Notification.init(allocator, app.notification);

src/browser/console/console.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,47 @@ const std = @import("std");
2020
const builtin = @import("builtin");
2121
const log = @import("../../log.zig");
2222

23+
const js = @import("../js/js.zig");
2324
const Page = @import("../page.zig").Page;
24-
const JsObject = @import("../env.zig").Env.JsObject;
2525

2626
pub const Console = struct {
2727
// TODO: configurable writer
2828
timers: std.StringHashMapUnmanaged(u32) = .{},
2929
counts: std.StringHashMapUnmanaged(u32) = .{},
3030

31-
pub fn _lp(values: []JsObject, page: *Page) !void {
31+
pub fn _lp(values: []js.JsObject, page: *Page) !void {
3232
if (values.len == 0) {
3333
return;
3434
}
3535
log.fatal(.console, "lightpanda", .{ .args = try serializeValues(values, page) });
3636
}
3737

38-
pub fn _log(values: []JsObject, page: *Page) !void {
38+
pub fn _log(values: []js.JsObject, page: *Page) !void {
3939
if (values.len == 0) {
4040
return;
4141
}
4242
log.info(.console, "info", .{ .args = try serializeValues(values, page) });
4343
}
4444

45-
pub fn _info(values: []JsObject, page: *Page) !void {
45+
pub fn _info(values: []js.JsObject, page: *Page) !void {
4646
return _log(values, page);
4747
}
4848

49-
pub fn _debug(values: []JsObject, page: *Page) !void {
49+
pub fn _debug(values: []js.JsObject, page: *Page) !void {
5050
if (values.len == 0) {
5151
return;
5252
}
5353
log.debug(.console, "debug", .{ .args = try serializeValues(values, page) });
5454
}
5555

56-
pub fn _warn(values: []JsObject, page: *Page) !void {
56+
pub fn _warn(values: []js.JsObject, page: *Page) !void {
5757
if (values.len == 0) {
5858
return;
5959
}
6060
log.warn(.console, "warn", .{ .args = try serializeValues(values, page) });
6161
}
6262

63-
pub fn _error(values: []JsObject, page: *Page) !void {
63+
pub fn _error(values: []js.JsObject, page: *Page) !void {
6464
if (values.len == 0) {
6565
return;
6666
}
@@ -132,7 +132,7 @@ pub const Console = struct {
132132
log.warn(.console, "timer stop", .{ .label = label, .elapsed = elapsed - kv.value });
133133
}
134134

135-
pub fn _assert(assertion: JsObject, values: []JsObject, page: *Page) !void {
135+
pub fn _assert(assertion: js.JsObject, values: []js.JsObject, page: *Page) !void {
136136
if (assertion.isTruthy()) {
137137
return;
138138
}
@@ -143,7 +143,7 @@ pub const Console = struct {
143143
log.info(.console, "assertion failed", .{ .values = serialized_values });
144144
}
145145

146-
fn serializeValues(values: []JsObject, page: *Page) ![]const u8 {
146+
fn serializeValues(values: []js.JsObject, page: *Page) ![]const u8 {
147147
if (values.len == 0) {
148148
return "";
149149
}

src/browser/crypto/crypto.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

1919
const std = @import("std");
20-
const Env = @import("../env.zig").Env;
20+
const js = @import("../js/js.zig");
2121
const uuidv4 = @import("../../id.zig").uuidv4;
2222

2323
// https://w3c.github.io/webcrypto/#crypto-interface
2424
pub const Crypto = struct {
2525
_not_empty: bool = true,
2626

27-
pub fn _getRandomValues(_: *const Crypto, js_obj: Env.JsObject) !Env.JsObject {
27+
pub fn _getRandomValues(_: *const Crypto, js_obj: js.JsObject) !js.JsObject {
2828
var into = try js_obj.toZig(Crypto, "getRandomValues", RandomValues);
2929
const buf = into.asBuffer();
3030
if (buf.len > 65_536) {

src/browser/cssom/CSSStyleSheet.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
const std = @import("std");
2020

21-
const Env = @import("../env.zig").Env;
21+
const js = @import("../js/js.zig");
2222
const Page = @import("../page.zig").Page;
2323
const StyleSheet = @import("StyleSheet.zig");
2424
const CSSRuleList = @import("CSSRuleList.zig");
@@ -73,7 +73,7 @@ pub fn _deleteRule(self: *CSSStyleSheet, index: usize) !void {
7373
_ = self.css_rules.list.orderedRemove(index);
7474
}
7575

76-
pub fn _replace(self: *CSSStyleSheet, text: []const u8, page: *Page) !Env.Promise {
76+
pub fn _replace(self: *CSSStyleSheet, text: []const u8, page: *Page) !js.Promise {
7777
_ = self;
7878
_ = text;
7979
// TODO: clear self.css_rules

src/browser/dom/Animation.zig

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@
1818

1919
const std = @import("std");
2020

21+
const js = @import("../js/js.zig");
2122
const Page = @import("../page.zig").Page;
22-
const JsObject = @import("../env.zig").JsObject;
23-
const Promise = @import("../env.zig").Promise;
24-
const PromiseResolver = @import("../env.zig").PromiseResolver;
2523

2624
const Animation = @This();
2725

28-
effect: ?JsObject,
29-
timeline: ?JsObject,
30-
ready_resolver: ?PromiseResolver,
31-
finished_resolver: ?PromiseResolver,
26+
effect: ?js.JsObject,
27+
timeline: ?js.JsObject,
28+
ready_resolver: ?js.PromiseResolver,
29+
finished_resolver: ?js.PromiseResolver,
3230

33-
pub fn constructor(effect: ?JsObject, timeline: ?JsObject) !Animation {
31+
pub fn constructor(effect: ?js.JsObject, timeline: ?js.JsObject) !Animation {
3432
return .{
3533
.effect = if (effect) |eo| try eo.persist() else null,
3634
.timeline = if (timeline) |to| try to.persist() else null,
@@ -49,7 +47,7 @@ pub fn get_pending(self: *const Animation) bool {
4947
return false;
5048
}
5149

52-
pub fn get_finished(self: *Animation, page: *Page) !Promise {
50+
pub fn get_finished(self: *Animation, page: *Page) !js.Promise {
5351
if (self.finished_resolver == null) {
5452
const resolver = page.main_context.createPromiseResolver();
5553
try resolver.resolve(self);
@@ -58,7 +56,7 @@ pub fn get_finished(self: *Animation, page: *Page) !Promise {
5856
return self.finished_resolver.?.promise();
5957
}
6058

61-
pub fn get_ready(self: *Animation, page: *Page) !Promise {
59+
pub fn get_ready(self: *Animation, page: *Page) !js.Promise {
6260
// never resolved, because we're always "finished"
6361
if (self.ready_resolver == null) {
6462
const resolver = page.main_context.createPromiseResolver();
@@ -67,19 +65,19 @@ pub fn get_ready(self: *Animation, page: *Page) !Promise {
6765
return self.ready_resolver.?.promise();
6866
}
6967

70-
pub fn get_effect(self: *const Animation) ?JsObject {
68+
pub fn get_effect(self: *const Animation) ?js.JsObject {
7169
return self.effect;
7270
}
7371

74-
pub fn set_effect(self: *Animation, effect: JsObject) !void {
72+
pub fn set_effect(self: *Animation, effect: js.JsObject) !void {
7573
self.effect = try effect.persist();
7674
}
7775

78-
pub fn get_timeline(self: *const Animation) ?JsObject {
76+
pub fn get_timeline(self: *const Animation) ?js.JsObject {
7977
return self.timeline;
8078
}
8179

82-
pub fn set_timeline(self: *Animation, timeline: JsObject) !void {
80+
pub fn set_timeline(self: *Animation, timeline: js.JsObject) !void {
8381
self.timeline = try timeline.persist();
8482
}
8583

0 commit comments

Comments
 (0)