Skip to content

Commit b383b79

Browse files
Keith Brownmacdoum1
Keith Brown
authored andcommitted
Modified deepFreezeAndThrowOnMutationInDev to use Object.prototype.ha… (facebook#19598)
Summary: This PR fixes a bug in `deepFreezeAndThrowOnMutationInDev` which did not take into account that objects passed to it may have been created with `Object.create(null)` and thus may not have a prototype. Such objects don't have the methods `hasOwnProperty`, `__defineGetter__`, or `__defineSetter__` on the instance. I ran into an unrecoverable error in React Native when passing this type of object across the bridge because `deepFreezeAndThrowOnMutationInDev` attempts to call `object.hasOwnProperty(key)`, `object.__defineGetter__` and `object__defineSetter__` on objects passed to it. But my object instance does not have these prototype methods. Changes: * Defined `Object.prototype.hasOwnProperty` as a `const` (pattern used elsewhere in React Native) * Modified calls to `object.hasOwnProperty(key)` to use `hasOwnProperty.call(object, key)` (Per ESLint rule [here](https://eslint.org/docs/rules/no-prototype-builtins)) * Modified calls to deprecated methods `object.__defineGetter__` and `object.__defineSetter__` to instead use `Object.defineProperty` to define get and set methods on the object. (Per guidance on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__)) * Added a new test to `deepFreezeAndThrowOnMutationInDev-test` to verify the fix. I tried to create a reproducible example to post to Snack by passing prototype-less objects to a `Text` component, in various ways, but they appear to be converted to plain objects before crossing the bridge and therefore they do not throw an error. However, I was able to create a new test to reproduce the issue. I added the following test to `deepFreezeAndThrowOnMutationInDev-test`: ```JavaScript it('should not throw on object without prototype', () => { __DEV__ = true; var o = Object.create(null); o.key = 'Value'; expect(() => deepFreezeAndThrowOnMutationInDev(o)).not.toThrow(); }); ``` The changes in this PR include this new test. ESLint test produced no change in Error count (3) or Warnings (671) N/A Other areas with _possibly_ the same issue: https://github.com/facebook/react-native/blob/c6b96c0df789717d53ec520ad28ba0ae00db6ec2/Libraries/vendor/core/mergeInto.js#L50 https://github.com/facebook/react-native/blob/8dc3ba0444c94d9bbb66295b5af885bff9b9cd34/Libraries/ReactNative/requireNativeComponent.js#L134 [GENERAL] [BUGFIX] [Libraries/Utilities/deepFreezeAndThrowOnMutationInDev] -Fix for compatibility with objects without a prototype. Closes facebook#19598 Differential Revision: D8310845 Pulled By: TheSavior fbshipit-source-id: 020c414a1062a637e97f9ee99bf8e5ba2d1fcf4f
1 parent 0f50ff9 commit b383b79

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Libraries/Utilities/__tests__/deepFreezeAndThrowOnMutationInDev-test.js

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ describe('deepFreezeAndThrowOnMutationInDev', function() {
2626
expect(() => deepFreezeAndThrowOnMutationInDev()).not.toThrow();
2727
});
2828

29+
it('should not throw on object without prototype', () => {
30+
__DEV__ = true;
31+
var o = Object.create(null);
32+
o.key = 'Value';
33+
expect(() => deepFreezeAndThrowOnMutationInDev(o)).not.toThrow();
34+
});
35+
2936
it('should throw on mutation in dev with strict', () => {
3037
'use strict';
3138
__DEV__ = true;

Libraries/Utilities/deepFreezeAndThrowOnMutationInDev.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ function deepFreezeAndThrowOnMutationInDev<T: Object>(object: T): T {
3939
}
4040

4141
const keys = Object.keys(object);
42+
const hasOwnProperty = Object.prototype.hasOwnProperty;
4243

4344
for (var i = 0; i < keys.length; i++) {
4445
var key = keys[i];
45-
if (object.hasOwnProperty(key)) {
46-
object.__defineGetter__(key, identity.bind(null, object[key]));
47-
object.__defineSetter__(key, throwOnImmutableMutation.bind(null, key));
46+
if (hasOwnProperty.call(object, key)) {
47+
Object.defineProperty(object, key, {
48+
get: identity.bind(null, object[key]),
49+
});
50+
Object.defineProperty(object, key, {
51+
set: throwOnImmutableMutation.bind(null, key),
52+
});
4853
}
4954
}
5055

@@ -53,7 +58,7 @@ function deepFreezeAndThrowOnMutationInDev<T: Object>(object: T): T {
5358

5459
for (var i = 0; i < keys.length; i++) {
5560
var key = keys[i];
56-
if (object.hasOwnProperty(key)) {
61+
if (hasOwnProperty.call(object, key)) {
5762
deepFreezeAndThrowOnMutationInDev(object[key]);
5863
}
5964
}

0 commit comments

Comments
 (0)