Skip to content

Commit

Permalink
use native Object.{ defineProperty, getOwnPropertyDescriptor } with…
Browse files Browse the repository at this point in the history
…out fallbacks in simple cases
  • Loading branch information
zloirock committed Oct 8, 2023
1 parent 038e964 commit 7cd2b90
Show file tree
Hide file tree
Showing 41 changed files with 33 additions and 84 deletions.
3 changes: 2 additions & 1 deletion packages/core-js-pure/override/internals/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ var global = require('../internals/global');
var apply = require('../internals/function-apply');
var uncurryThis = require('../internals/function-uncurry-this-clause');
var isCallable = require('../internals/is-callable');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
var isForced = require('../internals/is-forced');
var path = require('../internals/path');
var bind = require('../internals/function-bind-context');
var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
var hasOwn = require('../internals/has-own-property');

var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

var wrapConstructor = function (NativeConstructor) {
var Wrapper = function (a, b, c) {
if (this instanceof Wrapper) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/array-buffer-non-extensible.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var fails = require('../internals/fails');
module.exports = fails(function () {
if (typeof ArrayBuffer == 'function') {
var buffer = new ArrayBuffer(8);
// eslint-disable-next-line es/no-object-isextensible, es/no-object-defineproperty -- safe
// eslint-disable-next-line es/no-object-isextensible -- safe
if (Object.isExtensible(buffer)) Object.defineProperty(buffer, 'a', { value: 8 });
}
});
2 changes: 0 additions & 2 deletions packages/core-js/internals/array-set-length.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
var $TypeError = TypeError;
// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var isArray = Array.isArray;

Expand All @@ -9,7 +8,6 @@ var SILENT_ON_NON_WRITABLE_LENGTH_SET = !function () {
// makes no sense without proper strict mode support
if (this !== undefined) return true;
try {
// eslint-disable-next-line es/no-object-defineproperty -- safe
Object.defineProperty([], 'length', { writable: false }).length = 1;
} catch (error) {
return error instanceof TypeError;
Expand Down
7 changes: 3 additions & 4 deletions packages/core-js/internals/copy-constructor-properties.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict';
var hasOwn = require('../internals/has-own-property');
var ownKeys = require('../internals/own-keys');
var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor');
var definePropertyModule = require('../internals/object-define-property');

var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var defineProperty = Object.defineProperty;

module.exports = function (target, source, exceptions) {
var keys = ownKeys(source);
var defineProperty = definePropertyModule.f;
var getOwnPropertyDescriptor = getOwnPropertyDescriptorModule.f;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!hasOwn(target, key) && !(exceptions && hasOwn(exceptions, key))) {
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/internals/define-global-property.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
var global = require('../internals/global');

// eslint-disable-next-line es/no-object-defineproperty -- safe
var defineProperty = Object.defineProperty;

module.exports = function (key, value) {
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/internals/error-stack-installable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var createPropertyDescriptor = require('../internals/create-property-descriptor'
module.exports = !fails(function () {
var error = new Error('a');
if (!('stack' in error)) return true;
// eslint-disable-next-line es/no-object-defineproperty -- safe
Object.defineProperty(error, 'stack', createPropertyDescriptor(1, 7));
return error.stack !== 7;
});
1 change: 0 additions & 1 deletion packages/core-js/internals/error-to-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var nativeErrorToString = Error.prototype.toString;

var INCORRECT_TO_STRING = fails(function () {
// Chrome 32- incorrectly call accessor
// eslint-disable-next-line es/no-object-defineproperty -- safe
var object = Object.create(Object.defineProperty({}, 'name', { get: function () {
return this === object;
} }));
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js/internals/export.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
var global = require('../internals/global');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
var defineBuiltIn = require('../internals/define-built-in');
var defineGlobalProperty = require('../internals/define-global-property');
var copyConstructorProperties = require('../internals/copy-constructor-properties');
var isForced = require('../internals/is-forced');

var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

/*
options.target - name of the target object
options.global - target is the global object
Expand Down
4 changes: 1 addition & 3 deletions packages/core-js/internals/function-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
var hasOwn = require('../internals/has-own-property');

var FunctionPrototype = Function.prototype;
// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
var getDescriptor = Object.getOwnPropertyDescriptor;

var EXISTS = hasOwn(FunctionPrototype, 'name');
// additional protection from minified / mangled / dropped function names
var PROPER = EXISTS && (function something() { /* empty */ }).name === 'something';
var CONFIGURABLE = EXISTS && getDescriptor(FunctionPrototype, 'name').configurable;
var CONFIGURABLE = EXISTS && Object.getOwnPropertyDescriptor(FunctionPrototype, 'name').configurable;

module.exports = {
EXISTS: EXISTS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var aCallable = require('../internals/a-callable');

module.exports = function (object, key, method) {
try {
// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
return uncurryThis(aCallable(Object.getOwnPropertyDescriptor(object, key)[method]));
} catch (error) { /* empty */ }
};
1 change: 0 additions & 1 deletion packages/core-js/internals/make-built-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var InternalStateModule = require('../internals/internal-state');
var enforceInternalState = InternalStateModule.enforce;
var getInternalState = InternalStateModule.get;
var $String = String;
// eslint-disable-next-line es/no-object-defineproperty -- safe
var defineProperty = Object.defineProperty;
var stringSlice = uncurryThis(''.slice);
var replace = uncurryThis(''.replace);
Expand Down
3 changes: 1 addition & 2 deletions packages/core-js/internals/microtask.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
var global = require('../internals/global');
var bind = require('../internals/function-bind');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
var macrotask = require('../internals/task').set;
var Queue = require('../internals/queue');
var IS_IOS = require('../internals/engine-is-ios');
Expand All @@ -14,7 +13,7 @@ var document = global.document;
var process = global.process;
var Promise = global.Promise;
// Node.js 11 shows ExperimentalWarning on getting `queueMicrotask`
var queueMicrotaskDescriptor = getOwnPropertyDescriptor(global, 'queueMicrotask');
var queueMicrotaskDescriptor = Object.getOwnPropertyDescriptor(global, 'queueMicrotask');
var microtask = queueMicrotaskDescriptor && queueMicrotaskDescriptor.value;
var notify, toggle, node, promise, then;

Expand Down
1 change: 0 additions & 1 deletion packages/core-js/internals/object-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var IndexedObject = require('../internals/indexed-object');

// eslint-disable-next-line es/no-object-assign -- safe
var $assign = Object.assign;
// eslint-disable-next-line es/no-object-defineproperty -- required for testing
var defineProperty = Object.defineProperty;
var objectKeys = Object.keys;
var concat = uncurryThis([].concat);
Expand Down
5 changes: 2 additions & 3 deletions packages/core-js/internals/object-define-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ var objectKeys = Object.keys;

// `Object.defineProperties` method
// https://tc39.es/ecma262/#sec-object.defineproperties
// eslint-disable-next-line es/no-object-defineproperties -- safe
exports.f = !V8_PROTOTYPE_DEFINE_BUG ? Object.defineProperties : function defineProperties(O, Properties) {
exports.f = V8_PROTOTYPE_DEFINE_BUG ? function defineProperties(O, Properties) {
anObject(O);
var props = toIndexedObject(Properties);
var keys = objectKeys(Properties);
Expand All @@ -18,4 +17,4 @@ exports.f = !V8_PROTOTYPE_DEFINE_BUG ? Object.defineProperties : function define
var key;
while (length > index) definePropertyModule.f(O, key = keys[index++], props[key]);
return O;
};
} : Object.defineProperties;
2 changes: 0 additions & 2 deletions packages/core-js/internals/object-define-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ var V8_PROTOTYPE_DEFINE_BUG = require('../internals/v8-prototype-define-bug');
var anObject = require('../internals/an-object');
var toPropertyKey = require('../internals/to-property-key');

// eslint-disable-next-line es/no-object-defineproperty -- safe
var $defineProperty = Object.defineProperty;
// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
var $getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var ENUMERABLE = 'enumerable';
var CONFIGURABLE = 'configurable';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
'use strict';
// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
var $getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

// `Object.getOwnPropertyDescriptor` method
// https://tc39.es/ecma262/#sec-object.getownpropertydescriptor
exports.f = $getOwnPropertyDescriptor;
exports.f = Object.getOwnPropertyDescriptor;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
var $propertyIsEnumerable = {}.propertyIsEnumerable;
// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

// Nashorn ~ JDK8 bug
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/internals/v8-prototype-define-bug.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var fails = require('../internals/fails');
// V8 ~ Chrome 36-
// https://bugs.chromium.org/p/v8/issues/detail?id=3334
module.exports = fails(function () {
// eslint-disable-next-line es/no-object-defineproperty -- required for testing
return Object.defineProperty(function () { /* empty */ }, 'prototype', {
value: 42,
writable: false,
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/modules/es.array.push.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var INCORRECT_TO_LENGTH = fails(function () {
// https://bugs.chromium.org/p/v8/issues/detail?id=12681
var properErrorOnNonWritableLength = function () {
try {
// eslint-disable-next-line es/no-object-defineproperty -- safe
Object.defineProperty([], 'length', { writable: false }).push();
} catch (error) {
return error instanceof TypeError;
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/modules/es.array.unshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var INCORRECT_RESULT = [].unshift(0) !== 1;
// V8 ~ Chrome < 71 and Safari <= 15.4, FF < 23 throws InternalError
var properErrorOnNonWritableLength = function () {
try {
// eslint-disable-next-line es/no-object-defineproperty -- safe
Object.defineProperty([], 'length', { writable: false }).unshift();
} catch (error) {
return error instanceof TypeError;
Expand Down
14 changes: 1 addition & 13 deletions packages/core-js/modules/es.number.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ var global = require('../internals/global');
var path = require('../internals/path');
var uncurryThis = require('../internals/function-uncurry-this');
var isForced = require('../internals/is-forced');
var hasOwn = require('../internals/has-own-property');
var inheritIfRequired = require('../internals/inherit-if-required');
var isPrototypeOf = require('../internals/object-is-prototype-of');
var isSymbol = require('../internals/is-symbol');
var toPrimitive = require('../internals/to-primitive');
var fails = require('../internals/fails');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
var defineProperty = require('../internals/object-define-property').f;
var copyConstructorProperties = require('../internals/copy-constructor-properties');
var thisNumberValue = require('../internals/this-number-value');
var trim = require('../internals/string-trim').trim;

Expand All @@ -21,7 +19,6 @@ var NativeNumber = global[NUMBER];
var PureNumberNamespace = path[NUMBER];
var NumberPrototype = NativeNumber.prototype;
var TypeError = global.TypeError;
var getOwnPropertyNames = Object.getOwnPropertyNames;
var stringSlice = uncurryThis(''.slice);
var charCodeAt = uncurryThis(''.charCodeAt);

Expand Down Expand Up @@ -94,14 +91,5 @@ $({ global: true, constructor: true, wrap: true, forced: FORCED }, {
Number: NumberWrapper,
});

// Use `internal/copy-constructor-properties` helper in `core-js@4`
var copyConstructorProperties = function (target, source) {
for (var keys = getOwnPropertyNames(source), j = 0, key; keys.length > j; j++) {
if (hasOwn(source, key = keys[j]) && !hasOwn(target, key)) {
defineProperty(target, key, getOwnPropertyDescriptor(source, key));
}
}
};

if (IS_PURE && PureNumberNamespace) copyConstructorProperties(path[NUMBER], PureNumberNamespace);
if (FORCED || IS_PURE) copyConstructorProperties(path[NUMBER], NativeNumber);
1 change: 0 additions & 1 deletion packages/core-js/modules/es.object.define-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var defineProperties = require('../internals/object-define-properties').f;

// `Object.defineProperties` method
// https://tc39.es/ecma262/#sec-object.defineproperties
// eslint-disable-next-line es/no-object-defineproperties -- safe
$({ target: 'Object', stat: true, forced: Object.defineProperties !== defineProperties }, {
defineProperties: defineProperties,
});
1 change: 0 additions & 1 deletion packages/core-js/modules/es.object.define-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var defineProperty = require('../internals/object-define-property').f;

// `Object.defineProperty` method
// https://tc39.es/ecma262/#sec-object.defineproperty
// eslint-disable-next-line es/no-object-defineproperty -- safe
$({ target: 'Object', stat: true, forced: Object.defineProperty !== defineProperty }, {
defineProperty: defineProperty,
});
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.object.lookup-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var $ = require('../internals/export');
var FORCED = require('../internals/object-prototype-accessors-forced');
var toObject = require('../internals/to-object');
var toPropertyKey = require('../internals/to-property-key');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;

var getPrototypeOf = Object.getPrototypeOf;
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

// `Object.prototype.__lookupGetter__` method
// https://tc39.es/ecma262/#sec-object.prototype.__lookupGetter__
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.object.lookup-setter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var $ = require('../internals/export');
var FORCED = require('../internals/object-prototype-accessors-forced');
var toObject = require('../internals/to-object');
var toPropertyKey = require('../internals/to-property-key');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;

var getPrototypeOf = Object.getPrototypeOf;
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

// `Object.prototype.__lookupSetter__` method
// https://tc39.es/ecma262/#sec-object.prototype.__lookupSetter__
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js/modules/es.reflect.delete-property.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var $ = require('../internals/export');
var anObject = require('../internals/an-object');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;

var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

// `Reflect.deleteProperty` method
// https://tc39.es/ecma262/#sec-reflect.deleteproperty
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/es.reflect.get.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ var call = require('../internals/function-call');
var isObject = require('../internals/is-object');
var anObject = require('../internals/an-object');
var isDataDescriptor = require('../internals/is-data-descriptor');
var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor');

var getPrototypeOf = Object.getPrototypeOf;
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

// `Reflect.get` method
// https://tc39.es/ecma262/#sec-reflect.get
function get(target, propertyKey /* , receiver */) {
var receiver = arguments.length < 3 ? target : arguments[2];
var descriptor, prototype;
if (anObject(target) === receiver) return target[propertyKey];
descriptor = getOwnPropertyDescriptorModule.f(target, propertyKey);
descriptor = getOwnPropertyDescriptor(target, propertyKey);
if (descriptor) return isDataDescriptor(descriptor)
? descriptor.value
: descriptor.get === undefined ? undefined : call(descriptor.get, receiver);
Expand Down
2 changes: 0 additions & 2 deletions packages/core-js/modules/es.regexp.flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var FORCED = fails(function () {
var expected = INDICES_SUPPORT ? 'dgimsy' : 'gimsy';

var addGetter = function (key, chr) {
// eslint-disable-next-line es/no-object-defineproperty -- safe
Object.defineProperty(O, key, { get: function () {
calls += chr;
return true;
Expand All @@ -41,7 +40,6 @@ var FORCED = fails(function () {

for (var key in pairs) addGetter(key, pairs[key]);

// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
var result = Object.getOwnPropertyDescriptor(RegExpPrototype, 'flags').get.call(O);

return result !== expected || calls !== expected;
Expand Down
3 changes: 1 addition & 2 deletions packages/core-js/modules/es.string.ends-with.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this-clause');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
var toLength = require('../internals/to-length');
var toString = require('../internals/to-string');
var notARegExp = require('../internals/not-a-regexp');
Expand All @@ -17,7 +16,7 @@ var min = Math.min;
var CORRECT_IS_REGEXP_LOGIC = correctIsRegExpLogic('endsWith');
// https://github.com/zloirock/core-js/pull/702
var MDN_POLYFILL_BUG = !IS_PURE && !CORRECT_IS_REGEXP_LOGIC && !!function () {
var descriptor = getOwnPropertyDescriptor(String.prototype, 'endsWith');
var descriptor = Object.getOwnPropertyDescriptor(String.prototype, 'endsWith');
return descriptor && !descriptor.writable;
}();

Expand Down
3 changes: 1 addition & 2 deletions packages/core-js/modules/es.string.starts-with.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this-clause');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
var toLength = require('../internals/to-length');
var toString = require('../internals/to-string');
var notARegExp = require('../internals/not-a-regexp');
Expand All @@ -17,7 +16,7 @@ var min = Math.min;
var CORRECT_IS_REGEXP_LOGIC = correctIsRegExpLogic('startsWith');
// https://github.com/zloirock/core-js/pull/702
var MDN_POLYFILL_BUG = !IS_PURE && !CORRECT_IS_REGEXP_LOGIC && !!function () {
var descriptor = getOwnPropertyDescriptor(String.prototype, 'startsWith');
var descriptor = Object.getOwnPropertyDescriptor(String.prototype, 'startsWith');
return descriptor && !descriptor.writable;
}();

Expand Down
1 change: 0 additions & 1 deletion packages/core-js/modules/esnext.function.is-callable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var $isCallable = require('../internals/is-callable');
var inspectSource = require('../internals/inspect-source');
var hasOwn = require('../internals/has-own-property');

// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var classRegExp = /^\s*class\b/;
var exec = uncurryThis(classRegExp.exec);
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/modules/esnext.json.parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var Number = global.Number;
var SyntaxError = global.SyntaxError;
var nativeParse = JSON.parse;
var enumerableOwnProperties = Object.keys;
// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var isArray = Array.isArray;
var at = uncurryThis(''.charAt);
Expand Down
Loading

0 comments on commit 7cd2b90

Please sign in to comment.