From c7674926c3bb6d150051210c3112dbd23778d07a Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Tue, 26 Aug 2025 13:25:30 +0800 Subject: [PATCH] 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. --- src/browser/env.zig | 1 + src/browser/xhr/File.zig | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/browser/xhr/File.zig diff --git a/src/browser/env.zig b/src/browser/env.zig index 024c5d5d..c6f0469c 100644 --- a/src/browser/env.zig +++ b/src/browser/env.zig @@ -34,6 +34,7 @@ const WebApis = struct { @import("url/url.zig").Interfaces, @import("xhr/xhr.zig").Interfaces, @import("xhr/form_data.zig").Interfaces, + @import("xhr/File.zig"), @import("xmlserializer/xmlserializer.zig").Interfaces, }); }; diff --git a/src/browser/xhr/File.zig b/src/browser/xhr/File.zig new file mode 100644 index 00000000..c0a97234 --- /dev/null +++ b/src/browser/xhr/File.zig @@ -0,0 +1,40 @@ +// Copyright (C) 2023-2025 Lightpanda (Selecy SAS) +// +// Francis Bouvier +// Pierre Tachoire +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +const std = @import("std"); + +// https://w3c.github.io/FileAPI/#file-section +const File = @This(); + +// Very incomplete. The prototype for this is Blob, which we don't have. +// This minimum "implementation" is added because some JavaScript code just +// checks: if (x instanceof File) throw Error(...) +pub fn constructor() File { + return .{}; +} + +const testing = @import("../../testing.zig"); +test "Browser.File" { + var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "" }); + defer runner.deinit(); + + try runner.testCases(&.{ + .{ "let f = new File()", null }, + .{ "f instanceof File", "true" }, + }, .{}); +}