Make native event type cache prototype-safe (#58203)

Summary:
The EventTarget declarative-prop cache is an ordinary object, so event names such as `constructor` resolve inherited Object properties as cached entries and silently skip their registered handler. Use a typed `Map` for exact event-name keys and add a custom native direct-event regression.

Fixes https://github.com/react/react-native/issues/58202.

## Changelog:

[GENERAL] [FIXED] - Resolve native EventTarget names that overlap Object prototype properties.

Pull Request resolved: https://github.com/react/react-native/pull/58203

Test Plan:
- Exact baseline regression expected `onConstructor` and received `undefined`; fixed test passes 1/1.
- Focused Jest suite passed.
- Fresh `yarn flow-check`: 0 errors.
- Targeted ESLint, Prettier, and `git diff --check` passed.

No UI change; screenshots are not applicable.

Reviewed By: cortinico

Differential Revision: D118266208

Pulled By: javache

fbshipit-source-id: 5ab561865383cd363e9c3f6845ed597783874119
This commit is contained in:
Oskar Eichler authored and meta-codesync[bot] committed 2026-09-01 08:40:20 -07:00
1 parent a344b4b2ce
commit 621ced78e9
2 files changed
+30 -3

No files matched your search

@@ -30,7 +30,7 @@ type EventPropNames = {
};
// Cache of already-resolved event types.
const eventTypeToProps: {[string]: EventPropNames} = {};
const eventTypeToProps = new Map<string, EventPropNames>();
/**
* Converts a topLevelType (e.g., "topPointerUp") to a DOM event type
@@ -90,13 +90,13 @@ export function getEventTypePropName(
eventType: string,
isCapture: boolean,
): string | null {
const cached = eventTypeToProps[eventType];
const cached = eventTypeToProps.get(eventType);
if (cached !== undefined) {
return isCapture ? cached.captured : cached.bubbled;
}
const entry = findEventPropNames(eventType);
if (entry != null) {
eventTypeToProps[eventType] = entry;
eventTypeToProps.set(eventType, entry);
return isCapture ? entry.captured : entry.bubbled;
}
return null;
@@ -0,0 +1,27 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import {customDirectEventTypes} from '../../../../../Libraries/Renderer/shims/ReactNativeViewConfigRegistry';
import {getEventTypePropName} from '../ReactNativeEventTypeMapping';
describe('ReactNativeEventTypeMapping', () => {
afterEach(() => {
delete customDirectEventTypes.topConstructor;
});
it('resolves event types that shadow Object prototype properties', () => {
customDirectEventTypes.topConstructor = {
registrationName: 'onConstructor',
};
expect(getEventTypePropName('constructor', false)).toBe('onConstructor');
expect(getEventTypePropName('constructor', true)).toBeNull();
});
});