Skip to content

Commit

Permalink
Fix configuring unconfigurable properties (#6098)
Browse files Browse the repository at this point in the history
## Summary

Fixes #6066. When implementing more meaningful way of shareable freezing
I overlooked the case when some properties would be unconfigurable. This
PR fixes this.

## Test plan

New button in `ShareableFreezing` example checks that it doesn't throw
anymore.
  • Loading branch information
tjzel authored Jun 10, 2024
1 parent 7697afd commit b49c0de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
27 changes: 23 additions & 4 deletions apps/common-app/src/examples/ShareableFreezingExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export default function FreezingShareables() {
onPress={tryModifyConvertedInt32Array}
/>
</View>
<View style={styles.textAndButton}>
<Text style={styles.text}>🤫</Text>
<Button
title="Modify unconfigurable object"
onPress={tryModifyUnconfigurableObject}
/>
</View>
</View>
);
}
Expand All @@ -92,7 +99,7 @@ function tryModifyConvertedHostObject() {
return;
}
makeShareableCloneRecursive(obj);
// @ts-expect-error
// @ts-expect-error It's ok
obj.prop = 2; // shouldn't warn because it's not frozen
}

Expand All @@ -107,21 +114,22 @@ function tryModifyConvertedPlainObject() {
function tryModifyConvertedRegExpLiteral() {
const obj = /a/;
makeShareableCloneRecursive(obj);
// @ts-expect-error
// @ts-expect-error It's ok
obj.prop = 2; // shouldn't warn because it's not frozen
}

function tryModifyConvertedRegExpInstance() {
// eslint-disable-next-line prefer-regex-literals
const obj = new RegExp('a');
makeShareableCloneRecursive(obj);
// @ts-expect-error
// @ts-expect-error It's ok
obj.prop = 2; // shouldn't warn because it's not frozen
}

function tryModifyConvertedArrayBuffer() {
const obj = new ArrayBuffer(8);
makeShareableCloneRecursive(obj);
// @ts-expect-error
// @ts-expect-error It's ok
obj.prop = 2; // shouldn't warn because it's not frozen
}

Expand All @@ -131,6 +139,17 @@ function tryModifyConvertedInt32Array() {
obj[1] = 2; // shouldn't warn because it's not frozen
}

function tryModifyUnconfigurableObject() {
const obj = {};
Object.defineProperty(obj, 'prop', {
value: 1,
writable: false,
enumerable: true,
configurable: false,
});
makeShareableCloneRecursive(obj);
}

const styles = StyleSheet.create({
container: {
flex: 1,
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-reanimated/src/shareables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ function freezeObjectIfDev<T extends object>(value: T) {
return;
}
Object.entries(value).forEach(([key, element]) => {
const descriptor = Object.getOwnPropertyDescriptor(value, key)!;
if (!descriptor.configurable) {
return;
}
Object.defineProperty(value, key, {
get() {
return element;
Expand Down

0 comments on commit b49c0de

Please sign in to comment.