Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

krichprollsch
Copy link
Member

No description provided.

@krichprollsch krichprollsch requested a review from karlseguin July 5, 2025 23:45
@krichprollsch krichprollsch self-assigned this Jul 5, 2025
@krichprollsch krichprollsch marked this pull request as ready for review July 5, 2025 23:45
@karlseguin
Copy link
Collaborator

Is the reason this is working is that V8 first checks to see if globalThis["fetch"] exists, which executes this callback. We say "NO, it doesn't" (missing always returns false, so we always return v8.Intercepted.No), but then there's something else (what??) v8 is checking, and at that point, fetch has been defined?

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.

@krichprollsch
Copy link
Member Author

It seems v8 first checks for native implementation and then looks at the pure JS value.
So the callback is called even if the property exists in pure JS.

return Intercepted::kYes. If the interceptor does not handle the request it must return Intercepted::kNo and it must not produce side effects.
https://v8.github.io/api/head/namespacev8.html#a35b89945995c331fb2e21d5db4e873a7

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;
Copy link
Member Author

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...

Copy link
Collaborator

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;
Copy link
Collaborator

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);
Copy link
Collaborator

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants