Skip to content

Commit c767492

Browse files
committed
The most basic File implementation.
Almost silly as-is, but handles this case: ``` if (input instanceof File) { throw Error('file not supported') } ``` as seen on reddit.
1 parent ccc9618 commit c767492

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/browser/env.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const WebApis = struct {
3434
@import("url/url.zig").Interfaces,
3535
@import("xhr/xhr.zig").Interfaces,
3636
@import("xhr/form_data.zig").Interfaces,
37+
@import("xhr/File.zig"),
3738
@import("xmlserializer/xmlserializer.zig").Interfaces,
3839
});
3940
};

src/browser/xhr/File.zig

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2023-2025 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+
// https://w3c.github.io/FileAPI/#file-section
22+
const File = @This();
23+
24+
// Very incomplete. The prototype for this is Blob, which we don't have.
25+
// This minimum "implementation" is added because some JavaScript code just
26+
// checks: if (x instanceof File) throw Error(...)
27+
pub fn constructor() File {
28+
return .{};
29+
}
30+
31+
const testing = @import("../../testing.zig");
32+
test "Browser.File" {
33+
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "" });
34+
defer runner.deinit();
35+
36+
try runner.testCases(&.{
37+
.{ "let f = new File()", null },
38+
.{ "f instanceof File", "true" },
39+
}, .{});
40+
}

0 commit comments

Comments
 (0)