Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix configuring unconfigurable properties #6098

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading