refacto a bit the missing callback into polyfill

Add a debug global unknown property
This commit is contained in:
Pierre Tachoire
2025-07-07 14:29:04 -07:00
parent 941dace7f9
commit bdfceec520

View File

@@ -24,6 +24,8 @@ const Allocator = std.mem.Allocator;
const Env = @import("../env.zig").Env; const Env = @import("../env.zig").Env;
pub const Loader = struct { pub const Loader = struct {
state: enum { empty, loading } = .empty,
done: struct { done: struct {
fetch: bool = false, fetch: bool = false,
} = .{}, } = .{},
@@ -42,9 +44,14 @@ pub const Loader = struct {
} }
pub fn missing(self: *Loader, name: []const u8, js_context: *Env.JsContext) bool { pub fn missing(self: *Loader, name: []const u8, js_context: *Env.JsContext) bool {
if (!self.done.fetch and std.mem.eql(u8, name, "fetch")) { // Avoid recursive calls during polyfill loading.
// load the polyfill once. if (self.state == .loading) {
self.done.fetch = true; return false;
}
if (!self.done.fetch and isFetch(name)) {
self.state = .loading;
defer self.state = .empty;
const _name = "fetch"; const _name = "fetch";
const source = @import("fetch.zig").source; const source = @import("fetch.zig").source;
@@ -52,7 +59,33 @@ pub const Loader = struct {
load(_name, source, js_context) catch |err| { load(_name, source, js_context) catch |err| {
log.fatal(.app, "polyfill load", .{ .name = name, .err = err }); log.fatal(.app, "polyfill load", .{ .name = name, .err = err });
}; };
// load the polyfill once.
self.done.fetch = true;
// We return false here: We want v8 to continue the calling chain
// to finally find the polyfill we just inserted. If we want to
// return false and stops the call chain, we have to use
// `info.GetReturnValue.Set()` function, or `undefined` will be
// returned immediately.
return false;
} }
if (comptime builtin.mode == .Debug) {
log.debug(.unknown_prop, "unkown global property", .{
.info = "but the property can exist in pure JS",
.property = name,
});
}
return false;
}
fn isFetch(name: []const u8) bool {
if (std.mem.eql(u8, name, "fetch")) return true;
if (std.mem.eql(u8, name, "Request")) return true;
if (std.mem.eql(u8, name, "Response")) return true;
if (std.mem.eql(u8, name, "Headers")) return true;
return false; return false;
} }
}; };