Skip to content

Commit d32fbfd

Browse files
authored
Merge pull request #749 from lightpanda-io/functions
Poor support for functions/namespaces.
2 parents 6b0c532 + 6451065 commit d32fbfd

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/browser/console/console.zig

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,46 @@ pub const Console = struct {
3030
timers: std.StringHashMapUnmanaged(u32) = .{},
3131
counts: std.StringHashMapUnmanaged(u32) = .{},
3232

33-
pub fn _lp(_: *const Console, values: []JsObject, page: *Page) !void {
33+
pub fn static_lp(values: []JsObject, page: *Page) !void {
3434
if (values.len == 0) {
3535
return;
3636
}
3737
log.fatal(.console, "lightpanda", .{ .args = try serializeValues(values, page) });
3838
}
3939

40-
pub fn _log(_: *const Console, values: []JsObject, page: *Page) !void {
40+
pub fn static_log(values: []JsObject, page: *Page) !void {
4141
if (values.len == 0) {
4242
return;
4343
}
4444
log.info(.console, "info", .{ .args = try serializeValues(values, page) });
4545
}
4646

47-
pub fn _info(console: *const Console, values: []JsObject, page: *Page) !void {
48-
return console._log(values, page);
47+
pub fn static_info(values: []JsObject, page: *Page) !void {
48+
return static_log(values, page);
4949
}
5050

51-
pub fn _debug(_: *const Console, values: []JsObject, page: *Page) !void {
51+
pub fn static_debug(values: []JsObject, page: *Page) !void {
5252
if (values.len == 0) {
5353
return;
5454
}
5555
log.debug(.console, "debug", .{ .args = try serializeValues(values, page) });
5656
}
5757

58-
pub fn _warn(_: *const Console, values: []JsObject, page: *Page) !void {
58+
pub fn static_warn(values: []JsObject, page: *Page) !void {
5959
if (values.len == 0) {
6060
return;
6161
}
6262
log.warn(.console, "warn", .{ .args = try serializeValues(values, page) });
6363
}
6464

65-
pub fn _error(_: *const Console, values: []JsObject, page: *Page) !void {
65+
pub fn static_error(values: []JsObject, page: *Page) !void {
6666
if (values.len == 0) {
6767
return;
6868
}
6969
log.info(.console, "error", .{ .args = try serializeValues(values, page) });
7070
}
7171

72-
pub fn _clear(_: *const Console) void {}
72+
pub fn static_clear() void {}
7373

7474
pub fn _count(self: *Console, label_: ?[]const u8, page: *Page) !void {
7575
const label = label_ orelse "default";
@@ -130,7 +130,7 @@ pub const Console = struct {
130130
log.warn(.console, "timer stop", .{ .label = label, .elapsed = elapsed - kv.value });
131131
}
132132

133-
pub fn _assert(_: *Console, assertion: JsObject, values: []JsObject, page: *Page) !void {
133+
pub fn static_assert(assertion: JsObject, values: []JsObject, page: *Page) !void {
134134
if (assertion.isTruthy()) {
135135
return;
136136
}
@@ -225,7 +225,18 @@ test "Browser.Console" {
225225
try testing.expectEqual("[assertion failed] values= 1: x 2: true", captured[1]);
226226
try testing.expectEqual("[assertion failed] values= 1: x", captured[2]);
227227
}
228+
229+
{
230+
test_capture.reset();
231+
try runner.testCases(&.{
232+
.{ "[1].forEach(console.log)", null },
233+
}, .{});
234+
235+
const captured = test_capture.captured.items;
236+
try testing.expectEqual("[info] args= 1: 1 2: 0 3: [1]", captured[0]);
237+
}
228238
}
239+
229240
const TestCapture = struct {
230241
captured: std.ArrayListUnmanaged([]const u8) = .{},
231242

src/runtime/js.zig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,8 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
16801680
}
16811681
} else if (comptime std.mem.startsWith(u8, name, "get_")) {
16821682
generateProperty(Struct, name[4..], isolate, template_proto);
1683+
} else if (comptime std.mem.startsWith(u8, name, "static_")) {
1684+
generateFunction(Struct, name[7..], isolate, template_proto);
16831685
}
16841686
}
16851687

@@ -1769,6 +1771,23 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
17691771
template_proto.set(js_name, function_template, v8.PropertyAttribute.None);
17701772
}
17711773

1774+
fn generateFunction(comptime Struct: type, comptime name: []const u8, isolate: v8.Isolate, template_proto: v8.ObjectTemplate) void {
1775+
const js_name = v8.String.initUtf8(isolate, name).toName();
1776+
const function_template = v8.FunctionTemplate.initCallback(isolate, struct {
1777+
fn callback(raw_info: ?*const v8.C_FunctionCallbackInfo) callconv(.c) void {
1778+
const info = v8.FunctionCallbackInfo.initFromV8(raw_info);
1779+
var caller = Caller(Self, State).init(info);
1780+
defer caller.deinit();
1781+
1782+
const named_function = comptime NamedFunction.init(Struct, "static_" ++ name);
1783+
caller.function(Struct, named_function, info) catch |err| {
1784+
caller.handleError(Struct, named_function, err, info);
1785+
};
1786+
}
1787+
}.callback);
1788+
template_proto.set(js_name, function_template, v8.PropertyAttribute.None);
1789+
}
1790+
17721791
fn generateAttribute(comptime Struct: type, comptime name: []const u8, isolate: v8.Isolate, template: v8.FunctionTemplate, template_proto: v8.ObjectTemplate) void {
17731792
const zig_value = @field(Struct, name);
17741793
const js_value = simpleZigValueToJs(isolate, zig_value, true);
@@ -2154,6 +2173,8 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
21542173
return @ptrFromInt(@intFromPtr(toa.ptr) + @as(usize, @intCast(offset)));
21552174
}
21562175
if (prototype_index == type_index) {
2176+
// When a type has itself as the prototype, then we've
2177+
// reached the end of the chain.
21572178
return error.InvalidArgument;
21582179
}
21592180
type_index = prototype_index;
@@ -2341,6 +2362,14 @@ fn Caller(comptime E: type, comptime State: type) type {
23412362
info.getReturnValue().set(try scope.zigValueToJs(res));
23422363
}
23432364

2365+
fn function(self: *Self, comptime Struct: type, comptime named_function: NamedFunction, info: v8.FunctionCallbackInfo) !void {
2366+
const scope = self.scope;
2367+
const func = @field(Struct, named_function.name);
2368+
const args = try self.getArgs(Struct, named_function, 0, info);
2369+
const res = @call(.auto, func, args);
2370+
info.getReturnValue().set(try scope.zigValueToJs(res));
2371+
}
2372+
23442373
fn getter(self: *Self, comptime Struct: type, comptime named_function: NamedFunction, info: v8.FunctionCallbackInfo) !void {
23452374
const scope = self.scope;
23462375
const func = @field(Struct, named_function.name);

0 commit comments

Comments
 (0)