make Object.assign polyfill conditional and spec compliant

Summary: Replace the non-compliant `Object.assign` with a spec compliant version from Mozilla. Since its spec compliant we can apply conditionally only when a native version is not available.

Reviewed By: yungsters

Differential Revision: D8896359

fbshipit-source-id: 4999fe3094eba2206293bbe28760a4e46cbee6aa
This commit is contained in:
Simon Jensen 2018-08-23 13:51:55 -07:00 committed by Facebook Github Bot
parent 1329d18c6a
commit 966bb63a80

View File

@ -6,60 +6,34 @@
* *
* @format * @format
* @polyfill * @polyfill
* @nolint
*/ */
// WARNING: This is an optimized version that fails on hasOwnProperty checks // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
// and non objects. It's not spec-compliant. It's a perf optimization.
// This is only needed for iOS 8 and current Android JSC.
Object.assign = function(target, sources) { if (typeof Object.assign !== 'function') {
if (__DEV__) { Object.defineProperty(Object, 'assign', {
if (target == null) { value: function assign(target, varArgs) {
throw new TypeError('Object.assign target cannot be null or undefined'); 'use strict';
} if (target == null) {
if (typeof target !== 'object' && typeof target !== 'function') { throw new TypeError('Cannot convert undefined or null to object');
throw new TypeError(
'In this environment the target of assign MUST be an object. ' +
'This error is a performance optimization and not spec compliant.',
);
}
}
for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
var nextSource = arguments[nextIndex];
if (nextSource == null) {
continue;
}
if (__DEV__) {
if (typeof nextSource !== 'object' && typeof nextSource !== 'function') {
throw new TypeError(
'In this environment the sources for assign MUST be an object. ' +
'This error is a performance optimization and not spec compliant.',
);
} }
}
// We don't currently support accessors nor proxies. Therefore this let to = Object(target);
// copy cannot throw. If we ever supported this then we must handle
// exceptions and side-effects.
for (var key in nextSource) { for (let index = 1; index < arguments.length; index++) {
if (__DEV__) { let nextSource = arguments[index];
var hasOwnProperty = Object.prototype.hasOwnProperty;
if (!hasOwnProperty.call(nextSource, key)) { if (nextSource != null) {
throw new TypeError( for (let nextKey in nextSource) {
'One of the sources for assign has an enumerable key on the ' + if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
'prototype chain. Are you trying to assign a prototype property? ' + to[nextKey] = nextSource[nextKey];
"We don't allow it, as this is an edge case that we do not support. " + }
'This error is a performance optimization and not spec compliant.', }
);
} }
} }
target[key] = nextSource[key]; return to;
} },
} writable: true,
configurable: true,
return target; });
}; }