1+ import { EVENT_CALLED , NodeEvent } from '../../NodeEvents' ;
2+ import { ObjectPath , ObjectPathKey , UNKNOWN_PATH } from '../../utils/PathTracker' ;
3+ import { ExpressionEntity , LiteralValueOrUnknown , UnknownValue } from './Expression' ;
14import {
25 METHOD_RETURNS_BOOLEAN ,
36 METHOD_RETURNS_STRING ,
47 METHOD_RETURNS_UNKNOWN
58} from './MethodTypes' ;
69import { ObjectEntity , type PropertyMap } from './ObjectEntity' ;
710
11+ const isInteger = ( prop : ObjectPathKey ) : boolean => typeof prop === 'string' && / ^ \d + $ / . test ( prop ) ;
12+
13+ // This makes sure unknown properties are not handled as "undefined" but as
14+ // "unknown" but without access side effects. An exception is done for numeric
15+ // properties as we do not expect new builtin properties to be numbers, this
16+ // will improve tree-shaking for out-of-bounds array properties
17+ const OBJECT_PROTOTYPE_FALLBACK : ExpressionEntity =
18+ new ( class ObjectPrototypeFallbackExpression extends ExpressionEntity {
19+ deoptimizeThisOnEventAtPath (
20+ event : NodeEvent ,
21+ path : ObjectPath ,
22+ thisParameter : ExpressionEntity
23+ ) : void {
24+ if ( event === EVENT_CALLED && path . length === 1 && ! isInteger ( path [ 0 ] ) ) {
25+ thisParameter . deoptimizePath ( UNKNOWN_PATH ) ;
26+ }
27+ }
28+
29+ getLiteralValueAtPath ( path : ObjectPath ) : LiteralValueOrUnknown {
30+ // We ignore number properties as we do not expect new properties to be
31+ // numbers and also want to keep handling out-of-bound array elements as
32+ // "undefined"
33+ return path . length === 1 && isInteger ( path [ 0 ] ) ? undefined : UnknownValue ;
34+ }
35+
36+ hasEffectsWhenAccessedAtPath ( path : ObjectPath ) : boolean {
37+ return path . length > 1 ;
38+ }
39+
40+ hasEffectsWhenAssignedAtPath ( path : ObjectPath ) : boolean {
41+ return path . length > 1 ;
42+ }
43+ } ) ( ) ;
44+
845export const OBJECT_PROTOTYPE = new ObjectEntity (
946 {
1047 __proto__ : null ,
@@ -15,6 +52,6 @@ export const OBJECT_PROTOTYPE = new ObjectEntity(
1552 toString : METHOD_RETURNS_STRING ,
1653 valueOf : METHOD_RETURNS_UNKNOWN
1754 } as unknown as PropertyMap ,
18- null ,
55+ OBJECT_PROTOTYPE_FALLBACK ,
1956 true
2057) ;
0 commit comments