Skip to content

check if toString is writable #1436

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 2 commits into
base: master
Choose a base branch
from

Conversation

olafbuitelaar
Copy link

in cases the Function.prototype is sealed/frozen, this will throw an error causing any remaining code to abort.

in cases the `Function.prototype` is sealed/frozen, this will throw an error causing any remaining code to abort.
@zloirock
Copy link
Owner

Why and when could it be frozen?

@olafbuitelaar
Copy link
Author

If some external code call's; Object.freeze(Function.prototype); Object.seal(Function.prototype); prior. Alternativly we could build using core-js-pure, but ideally we can remain building using just core-js, and handle this case specifically.

@zloirock
Copy link
Owner

It's a case of "broken environment". You should load core-js before such manipulations with built-ins.

@olafbuitelaar
Copy link
Author

unfortunate that's not always an option. I would prefer the build bundle with core-js remainings functioning rather the erroring out.

@zloirock
Copy link
Owner

zloirock commented May 21, 2025

Maybe it's rather an issue for the place where this external code comes from? If I accept your PR in the proposed form, it is tens, if not hundreds, of extra bytes to millions of other core-js users that do not have dependencies, which breaks the environment.

@olafbuitelaar
Copy link
Author

It's definitely an issue from the external code, unfortunate there is not always a control over this, which is indeed specific for our case. If you have suggestions to minimise the impact on file size. Or otherwise maybe the case could be handle by adding a try catch block arround the assignment?

@zloirock
Copy link
Owner

Can you tell me which project this code comes from?

@olafbuitelaar
Copy link
Author

we're trying to load our library on a website (https://diep.io), which as first executes this code;

<!doctype html><html><head><script>function freeze_object(x)
      {
        Object.freeze(x);
        Object.seal(x);
        Object.freeze(x.prototype);
        Object.seal(x.prototype);
      }
      freeze_object(WebSocket);
      freeze_object(WebAssembly);
      freeze_object(CanvasRenderingContext2D);
      freeze_object(HTMLCanvasElement);
      freeze_object(Function);
      // freeze_object(Object);
      freeze_object(Path2D);
      freeze_object(ImageData);
      freeze_object(ImageBitmap);
      window.frozenHasFocus = Object.freeze({ hasFocus: document.hasFocus });</script></head></html>
      ```
      
The code of the website is not under our control 

@zloirock
Copy link
Owner

Function#toString is not the only core-js polyfill that will not work here. freeze_object(Object) is commented, but freeze_object(Function) will break some other core-js polyfills. Even if not to say about the early stage proposals, it will break Function#name and Function#[@@hasInstance] in old engines, Function#[@@metadata] cap that's on stage 3, etc..

If you distribute a library, core-js-pure is your option. It is precisely intended to prevent such conflicts.

@olafbuitelaar
Copy link
Author

olafbuitelaar commented May 21, 2025

i agree with that, only core-js-pure has some drawbacks, as it increases the overall bundle size, and some Objects aren't native or extandable, like Promise as this is implemented as a function rather than a class.
would there be an option within core-js to not throw any exceptions (and break remaining code) on such overwrites, and accept a degraded polyfill?

@zloirock
Copy link
Owner

I'll think about it. You could change your PR to try-catch, and if I decide that "accept a degraded polyfill" is fine, I'll accept it.

@olafbuitelaar
Copy link
Author

thank you for your consideration, and general hard work on core-js

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