diff --git a/compiler/forget/src/HIR/ObjectShape.ts b/compiler/forget/src/HIR/ObjectShape.ts index ff83f8508f..e4db1f6614 100644 --- a/compiler/forget/src/HIR/ObjectShape.ts +++ b/compiler/forget/src/HIR/ObjectShape.ts @@ -230,6 +230,16 @@ addObject(BUILTIN_SHAPES, BuiltInArrayId, [ returnValueKind: ValueKind.Mutable, }), ], + [ + "join", + addFunction(BUILTIN_SHAPES, [], { + positionalParams: [], + restParam: Effect.ConditionallyMutate, + returnType: PRIMITIVE_TYPE, + calleeEffect: Effect.Read, + returnValueKind: ValueKind.Immutable, + }), + ], // TODO: rest of Array properties ]); diff --git a/compiler/forget/src/__tests__/fixtures/compiler/array-join.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/array-join.expect.md new file mode 100644 index 0000000000..9bc2c32d5f --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/array-join.expect.md @@ -0,0 +1,68 @@ + +## Input + +```javascript +function Component(props) { + const x = [{}, [], props.value]; + const y = x.join(() => "this closure gets stringified, not called"); + foo(y); + return [x, y]; +} + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function Component(props) { + const $ = useMemoCache(8); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = {}; + $[0] = t0; + } else { + t0 = $[0]; + } + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = []; + $[1] = t1; + } else { + t1 = $[1]; + } + const c_2 = $[2] !== props.value; + let t2; + if (c_2) { + t2 = [t0, t1, props.value]; + $[2] = props.value; + $[3] = t2; + } else { + t2 = $[3]; + } + const x = t2; + let t3; + if ($[4] === Symbol.for("react.memo_cache_sentinel")) { + t3 = () => "this closure gets stringified, not called"; + $[4] = t3; + } else { + t3 = $[4]; + } + const y = x.join(t3); + foo(y); + const c_5 = $[5] !== x; + const c_6 = $[6] !== y; + let t4; + if (c_5 || c_6) { + t4 = [x, y]; + $[5] = x; + $[6] = y; + $[7] = t4; + } else { + t4 = $[7]; + } + return t4; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/array-join.js b/compiler/forget/src/__tests__/fixtures/compiler/array-join.js new file mode 100644 index 0000000000..2637db4ec8 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/array-join.js @@ -0,0 +1,6 @@ +function Component(props) { + const x = [{}, [], props.value]; + const y = x.join(() => "this closure gets stringified, not called"); + foo(y); + return [x, y]; +}