-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathOutlineFunctions.ts
More file actions
58 lines (53 loc) · 1.76 KB
/
OutlineFunctions.ts
File metadata and controls
58 lines (53 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 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.
*/
import {HIRFunction, IdentifierId} from '../HIR';
export function outlineFunctions(
fn: HIRFunction,
fbtOperands: Set<IdentifierId>,
): void {
for (const [, block] of fn.body.blocks) {
for (const instr of block.instructions) {
const {value, lvalue} = instr;
if (
value.kind === 'FunctionExpression' ||
value.kind === 'ObjectMethod'
) {
// Don't outline inner functions from parents with 'worklet' directive as it might break multi-threading assumptions.
const canOutlineInnerFunctions = value.loweredFunc.func.directives
? !value.loweredFunc.func.directives.includes('worklet')
: true;
if (canOutlineInnerFunctions) {
{
// Recurse in case there are inner functions which can be outlined
outlineFunctions(value.loweredFunc.func, fbtOperands);
}
}
if (
value.kind === 'FunctionExpression' &&
value.loweredFunc.func.context.length === 0 &&
// TODO: handle outlining named functions
value.loweredFunc.func.id === null &&
!fbtOperands.has(lvalue.identifier.id)
) {
const loweredFunc = value.loweredFunc.func;
const id = fn.env.generateGloballyUniqueIdentifierName(
loweredFunc.id ?? loweredFunc.nameHint,
);
loweredFunc.id = id.value;
fn.env.outlineFunction(loweredFunc, null);
instr.value = {
kind: 'LoadGlobal',
binding: {
kind: 'Global',
name: id.value,
},
loc: value.loc,
};
}
}
}
}