Skip to content

Commit 9893635

Browse files
Keith Brownhramos
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 Track results of e2e tests
1 parent a52d84d commit 9893635

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

scripts/run-ci-e2e-tests.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,15 @@ try {
199199
exec('sleep 10s');
200200
if (argv.tvos) {
201201
return exec(
202-
'xcodebuild -destination "platform=tvOS Simulator,name=Apple TV 1080p,OS=10.0" -scheme EndToEndTest-tvOS -sdk appletvsimulator test | xcpretty && exit ${PIPESTATUS[0]}',
202+
'xcodebuild -destination "platform=tvOS Simulator,name=Apple TV 1080p,OS=10.0" -scheme EndToEndTest-tvOS -sdk appletvsimulator test | xcpretty --report junit --output ~/react-native/reports/junit/' +
203+
iosTestType +
204+
'-e2e-xcodebuild-results.xml && exit ${PIPESTATUS[0]}',
203205
).code;
204206
} else {
205207
return exec(
206-
'xcodebuild -destination "platform=iOS Simulator,name=iPhone 5s,OS=10.3.1" -scheme EndToEndTest -sdk iphonesimulator test | xcpretty && exit ${PIPESTATUS[0]}',
208+
'xcodebuild -destination "platform=iOS Simulator,name=iPhone 5s,OS=10.0" -scheme EndToEndTest -sdk iphonesimulator test | xcpretty --report junit --output ~/react-native/reports/junit/' +
209+
iosTestType +
210+
'-e2e-xcodebuild-results.xml && exit ${PIPESTATUS[0]}',
207211
).code;
208212
}
209213
}, numberOfRetries)

0 commit comments

Comments
 (0)