Skip to content

Commit 27539d9

Browse files
committed
[compiler][optim] Add shape for Array.from
(see title)
1 parent 339059a commit 27539d9

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,29 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
119119
],
120120
/*
121121
* https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.from
122-
* Array.from(arrayLike, optionalFn, optionalThis) not added because
123-
* the Effect of `arrayLike` is polymorphic i.e.
122+
* Array.from(arrayLike, optionalFn, optionalThis)
123+
* Note that the Effect of `arrayLike` is polymorphic i.e.
124124
* - Effect.read if
125125
* - it does not have an @iterator property and is array-like
126126
* (i.e. has a length property)
127127
* - it is an iterable object whose iterator does not mutate itself
128128
* - Effect.mutate if it is a self-mutative iterator (e.g. a generator
129129
* function)
130130
*/
131+
[
132+
'from',
133+
addFunction(DEFAULT_SHAPES, [], {
134+
positionalParams: [
135+
Effect.ConditionallyMutate,
136+
Effect.ConditionallyMutate,
137+
Effect.ConditionallyMutate,
138+
],
139+
restParam: Effect.Read,
140+
returnType: {kind: 'Object', shapeId: BuiltInArrayId},
141+
calleeEffect: Effect.Read,
142+
returnValueKind: ValueKind.Mutable,
143+
}),
144+
],
131145
[
132146
'of',
133147
// Array.of(element0, ..., elementN)

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-granular-iterator-semantics.expect.md

+16-8
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,29 @@ function Validate({ x, input }) {
6868
}
6969
function useFoo(input) {
7070
"use memo";
71-
const $ = _c(3);
71+
const $ = _c(5);
7272

7373
const x = Array.from([{}]);
7474
useIdentity();
75-
x.push([input]);
7675
let t0;
77-
if ($[0] !== input || $[1] !== x) {
78-
t0 = <Validate x={x} input={input} />;
76+
if ($[0] !== input) {
77+
t0 = [input];
7978
$[0] = input;
80-
$[1] = x;
81-
$[2] = t0;
79+
$[1] = t0;
80+
} else {
81+
t0 = $[1];
82+
}
83+
x.push(t0);
84+
let t1;
85+
if ($[2] !== input || $[3] !== x) {
86+
t1 = <Validate x={x} input={input} />;
87+
$[2] = input;
88+
$[3] = x;
89+
$[4] = t1;
8290
} else {
83-
t0 = $[2];
91+
t1 = $[4];
8492
}
85-
return t0;
93+
return t1;
8694
}
8795

8896
export const FIXTURE_ENTRYPOINT = {
+38-10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ function useFoo({val1, val2}) {
3737
export const FIXTURE_ENTRYPOINT = {
3838
fn: useFoo,
3939
params: [{val1: 1, val2: 2}],
40+
params: [
41+
{val1: 1, val2: 2},
42+
{val1: 1, val2: 2},
43+
{val1: 1, val2: 3},
44+
{val1: 4, val2: 2},
45+
],
4046
};
4147

4248
```
@@ -71,29 +77,51 @@ function Validate({ x, val1, val2 }) {
7177
}
7278
function useFoo(t0) {
7379
"use memo";
74-
const $ = _c(4);
80+
const $ = _c(8);
7581
const { val1, val2 } = t0;
7682

7783
const x = Array.from([]);
7884
useIdentity();
79-
x.push([val1]);
80-
x.push([val2]);
8185
let t1;
82-
if ($[0] !== val1 || $[1] !== val2 || $[2] !== x) {
83-
t1 = <Validate x={x} val1={val1} val2={val2} />;
86+
if ($[0] !== val1) {
87+
t1 = [val1];
8488
$[0] = val1;
85-
$[1] = val2;
86-
$[2] = x;
87-
$[3] = t1;
89+
$[1] = t1;
90+
} else {
91+
t1 = $[1];
92+
}
93+
x.push(t1);
94+
let t2;
95+
if ($[2] !== val2) {
96+
t2 = [val2];
97+
$[2] = val2;
98+
$[3] = t2;
99+
} else {
100+
t2 = $[3];
101+
}
102+
x.push(t2);
103+
let t3;
104+
if ($[4] !== val1 || $[5] !== val2 || $[6] !== x) {
105+
t3 = <Validate x={x} val1={val1} val2={val2} />;
106+
$[4] = val1;
107+
$[5] = val2;
108+
$[6] = x;
109+
$[7] = t3;
88110
} else {
89-
t1 = $[3];
111+
t3 = $[7];
90112
}
91-
return t1;
113+
return t3;
92114
}
93115

94116
export const FIXTURE_ENTRYPOINT = {
95117
fn: useFoo,
96118
params: [{ val1: 1, val2: 2 }],
119+
params: [
120+
{ val1: 1, val2: 2 },
121+
{ val1: 1, val2: 2 },
122+
{ val1: 1, val2: 3 },
123+
{ val1: 4, val2: 2 },
124+
],
97125
};
98126

99127
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/todo-type-inference-array-from.js compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/type-inference-array-from.js

+6
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ function useFoo({val1, val2}) {
3333
export const FIXTURE_ENTRYPOINT = {
3434
fn: useFoo,
3535
params: [{val1: 1, val2: 2}],
36+
params: [
37+
{val1: 1, val2: 2},
38+
{val1: 1, val2: 2},
39+
{val1: 1, val2: 3},
40+
{val1: 4, val2: 2},
41+
],
3642
};

0 commit comments

Comments
 (0)