Skip to content

Commit c8dc424

Browse files
committed
Merge branch 'main' into requestAnimationFrame
2 parents 7d9bc5b + 54c2ded commit c8dc424

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <[email protected]>
4+
// Pierre Tachoire <[email protected]>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const std = @import("std");
20+
21+
const Env = @import("../env.zig").Env;
22+
23+
pub const Interfaces = .{
24+
TextEncoder,
25+
};
26+
27+
// https://encoding.spec.whatwg.org/#interface-textencoder
28+
pub const TextEncoder = struct {
29+
pub fn constructor() !TextEncoder {
30+
return .{};
31+
}
32+
33+
pub fn get_encoding(_: *const TextEncoder) []const u8 {
34+
return "utf-8";
35+
}
36+
37+
pub fn _encode(_: *const TextEncoder, v: []const u8) !Env.TypedArray(u8) {
38+
// Ensure the input is a valid utf-8
39+
// It seems chrome accepts invalid utf-8 sequence.
40+
//
41+
if (!std.unicode.utf8ValidateSlice(v)) {
42+
return error.InvalidUtf8;
43+
}
44+
45+
return .{ .values = v };
46+
}
47+
};
48+
49+
const testing = @import("../../testing.zig");
50+
test "Browser.Encoding.TextEncoder" {
51+
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
52+
defer runner.deinit();
53+
54+
try runner.testCases(&.{
55+
.{ "var encoder = new TextEncoder();", "undefined" },
56+
.{ "encoder.encoding;", "utf-8" },
57+
.{ "encoder.encode('€');", "226,130,172" },
58+
59+
// Invalid utf-8 sequence.
60+
// Result with chrome:
61+
// .{ "encoder.encode(new Uint8Array([0xE2,0x28,0xA1]))", "50,50,54,44,52,48,44,49,54,49" },
62+
.{ "try {encoder.encode(new Uint8Array([0xE2,0x28,0xA1])) } catch (e) { e };", "Error: InvalidUtf8" },
63+
}, .{});
64+
}

src/browser/env.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const WebApis = struct {
2828
@import("crypto/crypto.zig").Crypto,
2929
@import("console/console.zig").Console,
3030
@import("dom/dom.zig").Interfaces,
31+
@import("encoding/text_encoder.zig").Interfaces,
3132
@import("events/event.zig").Interfaces,
3233
@import("html/html.zig").Interfaces,
3334
@import("iterator/iterator.zig").Interfaces,

src/browser/html/performance.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub const Performance = struct {
3333
// else -> Resolution in non-isolated contexts: 100 microseconds
3434
const ms_resolution = 100;
3535

36-
fn limited_resolution_ms(nanoseconds: u64) f64 {
36+
fn limitedResolutionMs(nanoseconds: u64) f64 {
3737
const elapsed_at_resolution = ((nanoseconds / std.time.ns_per_us) + ms_resolution / 2) / ms_resolution * ms_resolution;
3838
const elapsed = @as(f64, @floatFromInt(elapsed_at_resolution));
3939
return elapsed / @as(f64, std.time.us_per_ms);
@@ -46,11 +46,11 @@ pub const Performance = struct {
4646
};
4747
const zero = std.time.Instant{ .timestamp = if (!is_posix) 0 else .{ .sec = 0, .nsec = 0 } };
4848
const started = self.time_origin.started.since(zero);
49-
return limited_resolution_ms(started);
49+
return limitedResolutionMs(started);
5050
}
5151

5252
pub fn _now(self: *Performance) f64 {
53-
return limited_resolution_ms(self.time_origin.read());
53+
return limitedResolutionMs(self.time_origin.read());
5454
}
5555
};
5656

@@ -71,15 +71,15 @@ test "Performance: now" {
7171
// Monotonically increasing
7272
var now = perf._now();
7373
while (now <= 0) { // Loop for now to not be 0
74-
try testing.expect(now == 0);
74+
try testing.expectEqual(0, now);
7575
now = perf._now();
7676
}
7777
// Check resolution
7878
try testing.expectDelta(@rem(now * std.time.us_per_ms, 100.0), 0.0, 0.1);
7979

8080
var after = perf._now();
8181
while (after <= now) { // Loop untill after > now
82-
try testing.expect(after == now);
82+
try testing.expectEqual(now, after);
8383
after = perf._now();
8484
}
8585
// Check resolution

0 commit comments

Comments
 (0)