From ad8e297ca557e6bc6698ad42fccca964e8797660 Mon Sep 17 00:00:00 2001 From: olafbuitelaar Date: Wed, 21 May 2025 12:01:33 +0200 Subject: [PATCH 1/2] check if toString is writable in cases the `Function.prototype` is sealed/frozen, this will throw an error causing any remaining code to abort. --- packages/core-js/internals/make-built-in.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/core-js/internals/make-built-in.js b/packages/core-js/internals/make-built-in.js index 574309851270..a1fbc42cff09 100644 --- a/packages/core-js/internals/make-built-in.js +++ b/packages/core-js/internals/make-built-in.js @@ -7,6 +7,7 @@ var DESCRIPTORS = require('../internals/descriptors'); var CONFIGURABLE_FUNCTION_NAME = require('../internals/function-name').CONFIGURABLE; var inspectSource = require('../internals/inspect-source'); var InternalStateModule = require('../internals/internal-state'); +var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f; var enforceInternalState = InternalStateModule.enforce; var getInternalState = InternalStateModule.get; @@ -16,6 +17,7 @@ var defineProperty = Object.defineProperty; var stringSlice = uncurryThis(''.slice); var replace = uncurryThis(''.replace); var join = uncurryThis([].join); +var functionToStringDescriptor = getOwnPropertyDescriptor(Function.prototype, 'toString'); var CONFIGURABLE_LENGTH = DESCRIPTORS && !fails(function () { return defineProperty(function () { /* empty */ }, 'length', { value: 8 }).length !== 8; @@ -49,7 +51,9 @@ var makeBuiltIn = module.exports = function (value, name, options) { }; // add fake Function#toString for correct work wrapped methods / constructors with methods like LoDash isNative -// eslint-disable-next-line no-extend-native -- required -Function.prototype.toString = makeBuiltIn(function toString() { - return isCallable(this) && getInternalState(this).source || inspectSource(this); -}, 'toString'); +if ((functionToStringDescriptor.writable || functionToStringDescriptor.set) && functionToStringDescriptor.configurable) { + // eslint-disable-next-line no-extend-native -- required + Function.prototype.toString = makeBuiltIn(function toString() { + return isCallable(this) && getInternalState(this).source || inspectSource(this); + }, 'toString'); +} From a6d454cedf52e640c44bf9001d53f58a049941f2 Mon Sep 17 00:00:00 2001 From: olafbuitelaar Date: Wed, 21 May 2025 14:18:23 +0200 Subject: [PATCH 2/2] use try...catch block instead, in case the assignment fails --- packages/core-js/internals/make-built-in.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core-js/internals/make-built-in.js b/packages/core-js/internals/make-built-in.js index a1fbc42cff09..b784ef1c505c 100644 --- a/packages/core-js/internals/make-built-in.js +++ b/packages/core-js/internals/make-built-in.js @@ -7,7 +7,6 @@ var DESCRIPTORS = require('../internals/descriptors'); var CONFIGURABLE_FUNCTION_NAME = require('../internals/function-name').CONFIGURABLE; var inspectSource = require('../internals/inspect-source'); var InternalStateModule = require('../internals/internal-state'); -var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f; var enforceInternalState = InternalStateModule.enforce; var getInternalState = InternalStateModule.get; @@ -17,7 +16,6 @@ var defineProperty = Object.defineProperty; var stringSlice = uncurryThis(''.slice); var replace = uncurryThis(''.replace); var join = uncurryThis([].join); -var functionToStringDescriptor = getOwnPropertyDescriptor(Function.prototype, 'toString'); var CONFIGURABLE_LENGTH = DESCRIPTORS && !fails(function () { return defineProperty(function () { /* empty */ }, 'length', { value: 8 }).length !== 8; @@ -51,9 +49,11 @@ var makeBuiltIn = module.exports = function (value, name, options) { }; // add fake Function#toString for correct work wrapped methods / constructors with methods like LoDash isNative -if ((functionToStringDescriptor.writable || functionToStringDescriptor.set) && functionToStringDescriptor.configurable) { +try { // eslint-disable-next-line no-extend-native -- required Function.prototype.toString = makeBuiltIn(function toString() { return isCallable(this) && getInternalState(this).source || inspectSource(this); }, 'toString'); +} catch { + /* silently ignore assignment error */ }