Ensure that existing properties are configurable before redefining

Summary:
`Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js` attempts to setup global variables typical in most JavaScript environments. It finds the previous property value using `Object.getOwnPropertyDescriptor` and preserves it as `original[PropertyName]` (if it existed), it then redefines the property using `Object.defineProperty`.

Properties may only be redefined if the property descriptor specifies that it is configurable ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor)). Attempting to redefine an non-configurable property will result in an error: `TypeError: Cannot redefine property: [PropertyName]`.

Not all properties being setup in `InitializeJavaScriptAppEngine.js` are necessarily configurable in the target environment.
Closes https://github.com/facebook/react-native/pull/9244

Differential Revision: D3679683

fbshipit-source-id: cd3398ef2cdf38e58c58862e64b159951c2b22c2
This commit is contained in:
Cameron Hunter 2016-08-05 21:44:32 -07:00 committed by Facebook Github Bot 6
parent 12fb313e54
commit bdf5adc40b
1 changed files with 19 additions and 12 deletions

View File

@ -77,7 +77,9 @@ function defineProperty(object: Object, name: string, newValue: mixed): void {
value: object[name], value: object[name],
}); });
} }
const {enumerable, writable} = descriptor || {};
const {enumerable, writable, configurable} = descriptor || {};
if (!descriptor || configurable) {
Object.defineProperty(object, name, { Object.defineProperty(object, name, {
configurable: true, configurable: true,
enumerable: enumerable !== false, enumerable: enumerable !== false,
@ -85,6 +87,7 @@ function defineProperty(object: Object, name: string, newValue: mixed): void {
value: newValue, value: newValue,
}); });
} }
}
function defineLazyProperty<T>( function defineLazyProperty<T>(
object: Object, object: Object,
@ -98,12 +101,16 @@ function defineLazyProperty<T>(
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`; const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
Object.defineProperty(object, backupName, descriptor); Object.defineProperty(object, backupName, descriptor);
} }
const {configurable} = descriptor || {};
if (!descriptor || configurable) {
defineLazyObjectProperty(object, name, { defineLazyObjectProperty(object, name, {
get: getNewValue, get: getNewValue,
enumerable: descriptor ? descriptor.enumerable !== false : true, enumerable: descriptor ? descriptor.enumerable !== false : true,
writable: descriptor ? descriptor.writable !== false : true, writable: descriptor ? descriptor.writable !== false : true,
}); });
} }
}
function setUpErrorHandler(): void { function setUpErrorHandler(): void {
if (global.__fbDisableExceptionsManager) { if (global.__fbDisableExceptionsManager) {