-
Notifications
You must be signed in to change notification settings - Fork 237
enable conditionnal loading for polyfill #847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Is the reason this is working is that V8 first checks to see if Put differently, i understand why this code works the 2nd time if fetch is called twice. But I don't understand why it works the first time. |
It seems v8 first checks for native implementation and then looks at the pure JS value.
When returning "no", v8 continues to search for the value, falling back to pure JS. Since we inject a polyfill, we define the property in pure JS, so it works... I know this is fragile, if the order change, it won't work anymore. But it was really easier than trying to inject the value manually after loading the polyfill. |
pub fn missing(self: *Loader, name: []const u8, js_context: *Env.JsContext) bool { | ||
if (!self.done.fetch and std.mem.eql(u8, name, "fetch")) { | ||
// load the polyfill once. | ||
self.done.fetch = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a comment, but it's important to set the polyfill done immediately.
Indeed, if your polyfill calls the property during its execution, the missing handler will be called too, resulting to an infinite recursive load...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are going to be called a lot, there could be value in optimizing the string comparison to be a single int equality check. Something like this:
switch (name.len) {
5 => switch (@as(u40, @bitCast(domain[0..5].*))) {
asUint(u40, "fetch") => if (!self.done.fetch) {
self.done.fetch = true;
return load(fetch_src);
},
else => {},
},
else => {},
};
return false;
fn asUint(comptime T: type, comptime string: []const u8) T {
return @bitCast(string[0..string.len].*);
}
pub fn missing(self: *Loader, name: []const u8, js_context: *Env.JsContext) bool { | ||
if (!self.done.fetch and std.mem.eql(u8, name, "fetch")) { | ||
// load the polyfill once. | ||
self.done.fetch = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are going to be called a lot, there could be value in optimizing the string comparison to be a single int equality check. Something like this:
switch (name.len) {
5 => switch (@as(u40, @bitCast(domain[0..5].*))) {
asUint(u40, "fetch") => if (!self.done.fetch) {
self.done.fetch = true;
return load(fetch_src);
},
else => {},
},
else => {},
};
return false;
fn asUint(comptime T: type, comptime string: []const u8) T {
return @bitCast(string[0..string.len].*);
}
@@ -569,7 +569,7 @@ const IsolatedWorld = struct { | |||
// Currently we have only 1 page/frame and thus also only 1 state in the isolate world. | |||
pub fn createContext(self: *IsolatedWorld, page: *Page) !void { | |||
if (self.executor.js_context != null) return error.Only1IsolatedContextSupported; | |||
_ = try self.executor.createJsContext(&page.window, page, {}, false); | |||
_ = try self.executor.createJsContext(&page.window, page, {}, false, null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the BrowserContext will need its own loader to track the isolate world polyfills?
No description provided.