Skip to content

Commit 9262761

Browse files
committed
[compiler][ez] Add more Array.prototype methods
Adds Array.prototype methods that return primitives or other arrays -- naive type inference can be really helpful in reducing mutable ranges -> achieving higher quality memoization. Also copies Array.prototype methods to our mixed read-only JSON-like object shape. (Inspired after going through some suboptimal internal compilation outputs.) ghstack-source-id: 0bfad11 Pull Request resolved: #30075
1 parent 9c2c7c6 commit 9262761

File tree

1 file changed

+128
-1
lines changed

1 file changed

+128
-1
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,36 @@ addObject(BUILTIN_SHAPES, BuiltInPropsId, [
218218

219219
/* Built-in array shape */
220220
addObject(BUILTIN_SHAPES, BuiltInArrayId, [
221+
[
222+
"indexOf",
223+
addFunction(BUILTIN_SHAPES, [], {
224+
positionalParams: [],
225+
restParam: Effect.Read,
226+
returnType: { kind: "Primitive" },
227+
calleeEffect: Effect.Read,
228+
returnValueKind: ValueKind.Primitive,
229+
}),
230+
],
231+
[
232+
"includes",
233+
addFunction(BUILTIN_SHAPES, [], {
234+
positionalParams: [],
235+
restParam: Effect.Read,
236+
returnType: { kind: "Primitive" },
237+
calleeEffect: Effect.Read,
238+
returnValueKind: ValueKind.Primitive,
239+
}),
240+
],
241+
[
242+
"pop",
243+
addFunction(BUILTIN_SHAPES, [], {
244+
positionalParams: [],
245+
restParam: null,
246+
returnType: { kind: "Poly" },
247+
calleeEffect: Effect.Store,
248+
returnValueKind: ValueKind.Mutable,
249+
}),
250+
],
221251
[
222252
"at",
223253
addFunction(BUILTIN_SHAPES, [], {
@@ -252,6 +282,19 @@ addObject(BUILTIN_SHAPES, BuiltInArrayId, [
252282
returnValueKind: ValueKind.Primitive,
253283
}),
254284
],
285+
[
286+
"slice",
287+
addFunction(BUILTIN_SHAPES, [], {
288+
positionalParams: [],
289+
restParam: Effect.Read,
290+
returnType: {
291+
kind: "Object",
292+
shapeId: BuiltInArrayId,
293+
},
294+
calleeEffect: Effect.Capture,
295+
returnValueKind: ValueKind.Mutable,
296+
}),
297+
],
255298
[
256299
"map",
257300
addFunction(BUILTIN_SHAPES, [], {
@@ -353,7 +396,7 @@ addObject(BUILTIN_SHAPES, BuiltInArrayId, [
353396
"join",
354397
addFunction(BUILTIN_SHAPES, [], {
355398
positionalParams: [],
356-
restParam: Effect.ConditionallyMutate,
399+
restParam: Effect.Read,
357400
returnType: PRIMITIVE_TYPE,
358401
calleeEffect: Effect.Read,
359402
returnValueKind: ValueKind.Primitive,
@@ -478,6 +521,90 @@ addObject(BUILTIN_SHAPES, BuiltInMixedReadonlyId, [
478521
noAlias: true,
479522
}),
480523
],
524+
[
525+
"concat",
526+
addFunction(BUILTIN_SHAPES, [], {
527+
positionalParams: [],
528+
restParam: Effect.Capture,
529+
returnType: {
530+
kind: "Object",
531+
shapeId: BuiltInArrayId,
532+
},
533+
calleeEffect: Effect.Capture,
534+
returnValueKind: ValueKind.Mutable,
535+
}),
536+
],
537+
[
538+
"slice",
539+
addFunction(BUILTIN_SHAPES, [], {
540+
positionalParams: [],
541+
restParam: Effect.Read,
542+
returnType: {
543+
kind: "Object",
544+
shapeId: BuiltInArrayId,
545+
},
546+
calleeEffect: Effect.Capture,
547+
returnValueKind: ValueKind.Mutable,
548+
}),
549+
],
550+
[
551+
"every",
552+
addFunction(BUILTIN_SHAPES, [], {
553+
positionalParams: [],
554+
restParam: Effect.ConditionallyMutate,
555+
returnType: { kind: "Primitive" },
556+
calleeEffect: Effect.ConditionallyMutate,
557+
returnValueKind: ValueKind.Primitive,
558+
noAlias: true,
559+
mutableOnlyIfOperandsAreMutable: true,
560+
}),
561+
],
562+
[
563+
"some",
564+
addFunction(BUILTIN_SHAPES, [], {
565+
positionalParams: [],
566+
restParam: Effect.ConditionallyMutate,
567+
returnType: { kind: "Primitive" },
568+
calleeEffect: Effect.ConditionallyMutate,
569+
returnValueKind: ValueKind.Primitive,
570+
noAlias: true,
571+
mutableOnlyIfOperandsAreMutable: true,
572+
}),
573+
],
574+
[
575+
"find",
576+
addFunction(BUILTIN_SHAPES, [], {
577+
positionalParams: [],
578+
restParam: Effect.ConditionallyMutate,
579+
returnType: { kind: "Poly" },
580+
calleeEffect: Effect.ConditionallyMutate,
581+
returnValueKind: ValueKind.Mutable,
582+
noAlias: true,
583+
mutableOnlyIfOperandsAreMutable: true,
584+
}),
585+
],
586+
[
587+
"findIndex",
588+
addFunction(BUILTIN_SHAPES, [], {
589+
positionalParams: [],
590+
restParam: Effect.ConditionallyMutate,
591+
returnType: { kind: "Primitive" },
592+
calleeEffect: Effect.ConditionallyMutate,
593+
returnValueKind: ValueKind.Primitive,
594+
noAlias: true,
595+
mutableOnlyIfOperandsAreMutable: true,
596+
}),
597+
],
598+
[
599+
"join",
600+
addFunction(BUILTIN_SHAPES, [], {
601+
positionalParams: [],
602+
restParam: Effect.Read,
603+
returnType: PRIMITIVE_TYPE,
604+
calleeEffect: Effect.Read,
605+
returnValueKind: ValueKind.Primitive,
606+
}),
607+
],
481608
["*", { kind: "Object", shapeId: BuiltInMixedReadonlyId }],
482609
]);
483610

0 commit comments

Comments
 (0)