Skip to content

Commit

Permalink
ensure proper .prototype of polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed May 6, 2022
1 parent 666631a commit ef26f65
Show file tree
Hide file tree
Showing 21 changed files with 34 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Changelog
##### Unreleased
- Ensured that polyfilled constructors `.prototype` is non-writable
- Ensured that polyfilled methods `.prototype` is not defined
- Added detection and fix of a V8 ~ Chrome <103 [bug](https://bugs.chromium.org/p/v8/issues/detail?id=12542) of `struturedClone` that returns `null` if cloned object contains multiple references to one error

##### 3.22.4 - 2022.05.03
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module.exports = function (CONSTRUCTOR_NAME, wrapper, common) {
}

exported[CONSTRUCTOR_NAME] = Constructor;
$({ global: true, forced: Constructor != NativeConstructor }, exported);
$({ global: true, constructor: true, forced: Constructor != NativeConstructor }, exported);

setToStringTag(Constructor, CONSTRUCTOR_NAME);

Expand Down
11 changes: 9 additions & 2 deletions packages/core-js/internals/make-built-in.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
var fails = require('../internals/fails');
var isCallable = require('../internals/is-callable');
var hasOwn = require('../internals/has-own-property');
var defineProperty = require('../internals/object-define-property').f;
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 enforceInternalState = InternalStateModule.enforce;
var getInternalState = InternalStateModule.get;
// eslint-disable-next-line es-x/no-object-defineproperty -- safe
var defineProperty = Object.defineProperty;

var CONFIGURABLE_LENGTH = !fails(function () {
var CONFIGURABLE_LENGTH = DESCRIPTORS && !fails(function () {
return defineProperty(function () { /* empty */ }, 'length', { value: 8 }).length !== 8;
});

Expand All @@ -27,6 +29,11 @@ var makeBuiltIn = module.exports = function (value, name, options) {
if (CONFIGURABLE_LENGTH && options && hasOwn(options, 'arity') && value.length !== options.arity) {
defineProperty(value, 'length', { value: options.arity });
}
if (options && hasOwn(options, 'constructor') && options.constructor) {
if (DESCRIPTORS) try {
defineProperty(value, 'prototype', { writable: false });
} catch (error) { /* empty */ }
} else value.prototype = undefined;
var state = enforceInternalState(value);
if (!hasOwn(state, 'source')) {
state.source = TEMPLATE.join(typeof name == 'string' ? name : '');
Expand Down
6 changes: 3 additions & 3 deletions packages/core-js/internals/typed-array-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ if (DESCRIPTORS) {
createNonEnumerableProperty(TypedArrayConstructorPrototype, TYPED_ARRAY_TAG, CONSTRUCTOR_NAME);
}

var FORCED = TypedArrayConstructor != NativeTypedArrayConstructor;

exported[CONSTRUCTOR_NAME] = TypedArrayConstructor;

$({
global: true, forced: TypedArrayConstructor != NativeTypedArrayConstructor, sham: !NATIVE_ARRAY_BUFFER_VIEWS
}, exported);
$({ global: true, constructor: true, forced: FORCED, sham: !NATIVE_ARRAY_BUFFER_VIEWS }, exported);

if (!(BYTES_PER_ELEMENT in TypedArrayConstructor)) {
createNonEnumerableProperty(TypedArrayConstructor, BYTES_PER_ELEMENT, BYTES);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.aggregate-error.cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var FORCED = !fails(function () {
});

// https://github.com/tc39/proposal-error-cause
$({ global: true, arity: 2, forced: FORCED }, {
$({ global: true, constructor: true, arity: 2, forced: FORCED }, {
AggregateError: wrapErrorConstructorWithCause(AGGREGATE_ERROR, function (init) {
// eslint-disable-next-line no-unused-vars -- required for functions `.length`
return function AggregateError(errors, message) { return apply(init, this, arguments); };
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.aggregate-error.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ var AggregateErrorPrototype = $AggregateError.prototype = create(Error.prototype

// `AggregateError` constructor
// https://tc39.es/ecma262/#sec-aggregate-error-constructor
$({ global: true }, {
$({ global: true, constructor: true, arity: 2 }, {
AggregateError: $AggregateError
});
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.array-buffer.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var NativeArrayBuffer = global[ARRAY_BUFFER];

// `ArrayBuffer` constructor
// https://tc39.es/ecma262/#sec-arraybuffer-constructor
$({ global: true, forced: NativeArrayBuffer !== ArrayBuffer }, {
$({ global: true, constructor: true, forced: NativeArrayBuffer !== ArrayBuffer }, {
ArrayBuffer: ArrayBuffer
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.data-view.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-native');

// `DataView` constructor
// https://tc39.es/ecma262/#sec-dataview-constructor
$({ global: true, forced: !NATIVE_ARRAY_BUFFER }, {
$({ global: true, constructor: true, forced: !NATIVE_ARRAY_BUFFER }, {
DataView: ArrayBufferModule.DataView
});
4 changes: 2 additions & 2 deletions packages/core-js/modules/es.error.cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ var FORCED = Error('e', { cause: 7 }).cause !== 7;
var exportGlobalErrorCauseWrapper = function (ERROR_NAME, wrapper) {
var O = {};
O[ERROR_NAME] = wrapErrorConstructorWithCause(ERROR_NAME, wrapper, FORCED);
$({ global: true, arity: 1, forced: FORCED }, O);
$({ global: true, constructor: true, arity: 1, forced: FORCED }, O);
};

var exportWebAssemblyErrorCauseWrapper = function (ERROR_NAME, wrapper) {
if (WebAssembly && WebAssembly[ERROR_NAME]) {
var O = {};
O[ERROR_NAME] = wrapErrorConstructorWithCause(WEB_ASSEMBLY + '.' + ERROR_NAME, wrapper, FORCED);
$({ target: WEB_ASSEMBLY, stat: true, arity: 1, forced: FORCED }, O);
$({ target: WEB_ASSEMBLY, stat: true, constructor: true, arity: 1, forced: FORCED }, O);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.number.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ if (isForced(NUMBER, !NativeNumber(' 0o1') || !NativeNumber('0b1') || NativeNumb
}
NumberWrapper.prototype = NumberPrototype;
NumberPrototype.constructor = NumberWrapper;
defineBuiltIn(global, NUMBER, NumberWrapper);
defineBuiltIn(global, NUMBER, NumberWrapper, { constructor: true });
}
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.promise.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ if (FORCED_PROMISE_CONSTRUCTOR) {
}
}

$({ global: true, wrap: true, forced: FORCED_PROMISE_CONSTRUCTOR }, {
$({ global: true, constructor: true, wrap: true, forced: FORCED_PROMISE_CONSTRUCTOR }, {
Promise: PromiseConstructor
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.regexp.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ if (isForced('RegExp', BASE_FORCED)) {

RegExpPrototype.constructor = RegExpWrapper;
RegExpWrapper.prototype = RegExpPrototype;
defineBuiltIn(global, 'RegExp', RegExpWrapper);
defineBuiltIn(global, 'RegExp', RegExpWrapper, { constructor: true });
}

// https://tc39.es/ecma262/#sec-get-regexp-@@species
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.symbol.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ if (!NATIVE_SYMBOL) {
}
}

$({ global: true, wrap: true, forced: !NATIVE_SYMBOL, sham: !NATIVE_SYMBOL }, {
$({ global: true, constructor: true, wrap: true, forced: !NATIVE_SYMBOL, sham: !NATIVE_SYMBOL }, {
Symbol: $Symbol
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.symbol.description.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if (DESCRIPTORS && isCallable(NativeSymbol) && (!('description' in SymbolPrototy
}
});

$({ global: true, forced: true }, {
$({ global: true, constructor: true, forced: true }, {
Symbol: SymbolWrapper
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ if (IS_PURE || !hasOwn(AsyncIteratorPrototype, 'constructor') || AsyncIteratorPr
createNonEnumerableProperty(AsyncIteratorPrototype, 'constructor', AsyncIteratorConstructor);
}

$({ global: true, forced: IS_PURE }, {
$({ global: true, constructor: true, forced: IS_PURE }, {
AsyncIterator: AsyncIteratorConstructor
});
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.iterator.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ if (FORCED || !hasOwn(IteratorPrototype, 'constructor') || IteratorPrototype.con

IteratorConstructor.prototype = IteratorPrototype;

$({ global: true, forced: FORCED }, {
$({ global: true, constructor: true, forced: FORCED }, {
Iterator: IteratorConstructor
});
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.observable.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ defineBuiltIns(ObservablePrototype, {

defineBuiltIn(ObservablePrototype, $$OBSERVABLE, function () { return this; });

$({ global: true, forced: OBSERVABLE_FORCED }, {
$({ global: true, constructor: true, forced: OBSERVABLE_FORCED }, {
Observable: $Observable
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/web.dom-exception.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var FORCED_CONSTRUCTOR = IS_PURE ? INCORRECT_TO_STRING || INCORRECT_CODE || MISS

// `DOMException` constructor
// https://webidl.spec.whatwg.org/#idl-DOMException
$({ global: true, forced: FORCED_CONSTRUCTOR }, {
$({ global: true, constructor: true, forced: FORCED_CONSTRUCTOR }, {
DOMException: FORCED_CONSTRUCTOR ? $DOMException : NativeDOMException
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/web.dom-exception.stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var FORCED_CONSTRUCTOR = ERROR_HAS_STACK && !DOM_EXCEPTION_HAS_STACK;

// `DOMException` constructor patch for `.stack` where it's required
// https://webidl.spec.whatwg.org/#es-DOMException-specialness
$({ global: true, forced: IS_PURE || FORCED_CONSTRUCTOR }, { // TODO: fix export logic
$({ global: true, constructor: true, forced: IS_PURE || FORCED_CONSTRUCTOR }, { // TODO: fix export logic
DOMException: FORCED_CONSTRUCTOR ? $DOMException : NativeDOMException
});

Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/web.url-search-params.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ defineBuiltIn(URLSearchParamsPrototype, 'toString', function toString() {

setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);

$({ global: true, forced: !USE_NATIVE_URL }, {
$({ global: true, constructor: true, forced: !USE_NATIVE_URL }, {
URLSearchParams: URLSearchParamsConstructor
});

Expand Down Expand Up @@ -380,7 +380,7 @@ if (!USE_NATIVE_URL && isCallable(Headers)) {
RequestPrototype.constructor = RequestConstructor;
RequestConstructor.prototype = RequestPrototype;

$({ global: true, forced: true, noTargetGet: true }, {
$({ global: true, constructor: true, noTargetGet: true, forced: true }, {
Request: RequestConstructor
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/web.url.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,6 @@ if (NativeURL) {

setToStringTag(URLConstructor, 'URL');

$({ global: true, forced: !USE_NATIVE_URL, sham: !DESCRIPTORS }, {
$({ global: true, constructor: true, forced: !USE_NATIVE_URL, sham: !DESCRIPTORS }, {
URL: URLConstructor
});

0 comments on commit ef26f65

Please sign in to comment.