Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle getters on functions and improve property deoptimization (#4493)
* Do not make Object.defineProperty/ies a side effect by default * Detect side effects for getters on functions * Less deoptimization for non-accessor assignments * Use ObjectEntity for arrow function properties * Share code between functions and arrow functions * Use new path key for defineProperty side effects * Enable custom call-effect detection per global * Improve coverage
- Loading branch information
1 parent
8c6e0f3
commit f3a1fa3
Showing
25 changed files
with
429 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import type { NormalizedTreeshakingOptions } from '../../../rollup/types'; | ||
import { type CallOptions, NO_ARGS } from '../../CallOptions'; | ||
import { DeoptimizableEntity } from '../../DeoptimizableEntity'; | ||
import { | ||
BROKEN_FLOW_NONE, | ||
type HasEffectsContext, | ||
type InclusionContext | ||
} from '../../ExecutionContext'; | ||
import { NodeEvent } from '../../NodeEvents'; | ||
import ReturnValueScope from '../../scopes/ReturnValueScope'; | ||
import { type ObjectPath, PathTracker, UNKNOWN_PATH, UnknownKey } from '../../utils/PathTracker'; | ||
import BlockStatement from '../BlockStatement'; | ||
import * as NodeType from '../NodeType'; | ||
import RestElement from '../RestElement'; | ||
import type SpreadElement from '../SpreadElement'; | ||
import { type ExpressionEntity, LiteralValueOrUnknown, UNKNOWN_EXPRESSION } from './Expression'; | ||
import { | ||
type ExpressionNode, | ||
type GenericEsTreeNode, | ||
type IncludeChildren, | ||
NodeBase | ||
} from './Node'; | ||
import { ObjectEntity } from './ObjectEntity'; | ||
import type { PatternNode } from './Pattern'; | ||
|
||
export default abstract class FunctionBase extends NodeBase { | ||
declare async: boolean; | ||
declare body: BlockStatement | ExpressionNode; | ||
declare params: readonly PatternNode[]; | ||
declare preventChildBlockScope: true; | ||
declare scope: ReturnValueScope; | ||
protected objectEntity: ObjectEntity | null = null; | ||
private deoptimizedReturn = false; | ||
|
||
deoptimizePath(path: ObjectPath): void { | ||
this.getObjectEntity().deoptimizePath(path); | ||
if (path.length === 1 && path[0] === UnknownKey) { | ||
// A reassignment of UNKNOWN_PATH is considered equivalent to having lost track | ||
// which means the return expression needs to be reassigned | ||
this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH); | ||
} | ||
} | ||
|
||
deoptimizeThisOnEventAtPath( | ||
event: NodeEvent, | ||
path: ObjectPath, | ||
thisParameter: ExpressionEntity, | ||
recursionTracker: PathTracker | ||
): void { | ||
if (path.length > 0) { | ||
this.getObjectEntity().deoptimizeThisOnEventAtPath( | ||
event, | ||
path, | ||
thisParameter, | ||
recursionTracker | ||
); | ||
} | ||
} | ||
|
||
getLiteralValueAtPath( | ||
path: ObjectPath, | ||
recursionTracker: PathTracker, | ||
origin: DeoptimizableEntity | ||
): LiteralValueOrUnknown { | ||
return this.getObjectEntity().getLiteralValueAtPath(path, recursionTracker, origin); | ||
} | ||
|
||
getReturnExpressionWhenCalledAtPath( | ||
path: ObjectPath, | ||
callOptions: CallOptions, | ||
recursionTracker: PathTracker, | ||
origin: DeoptimizableEntity | ||
): ExpressionEntity { | ||
if (path.length > 0) { | ||
return this.getObjectEntity().getReturnExpressionWhenCalledAtPath( | ||
path, | ||
callOptions, | ||
recursionTracker, | ||
origin | ||
); | ||
} | ||
if (this.async) { | ||
if (!this.deoptimizedReturn) { | ||
this.deoptimizedReturn = true; | ||
this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH); | ||
this.context.requestTreeshakingPass(); | ||
} | ||
return UNKNOWN_EXPRESSION; | ||
} | ||
return this.scope.getReturnExpression(); | ||
} | ||
|
||
hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext): boolean { | ||
return this.getObjectEntity().hasEffectsWhenAccessedAtPath(path, context); | ||
} | ||
|
||
hasEffectsWhenAssignedAtPath(path: ObjectPath, context: HasEffectsContext): boolean { | ||
return this.getObjectEntity().hasEffectsWhenAssignedAtPath(path, context); | ||
} | ||
|
||
hasEffectsWhenCalledAtPath( | ||
path: ObjectPath, | ||
callOptions: CallOptions, | ||
context: HasEffectsContext | ||
): boolean { | ||
if (path.length > 0) { | ||
return this.getObjectEntity().hasEffectsWhenCalledAtPath(path, callOptions, context); | ||
} | ||
if (this.async) { | ||
const { propertyReadSideEffects } = this.context.options | ||
.treeshake as NormalizedTreeshakingOptions; | ||
const returnExpression = this.scope.getReturnExpression(); | ||
if ( | ||
returnExpression.hasEffectsWhenCalledAtPath( | ||
['then'], | ||
{ args: NO_ARGS, thisParam: null, withNew: false }, | ||
context | ||
) || | ||
(propertyReadSideEffects && | ||
(propertyReadSideEffects === 'always' || | ||
returnExpression.hasEffectsWhenAccessedAtPath(['then'], context))) | ||
) { | ||
return true; | ||
} | ||
} | ||
for (const param of this.params) { | ||
if (param.hasEffects(context)) return true; | ||
} | ||
return false; | ||
} | ||
|
||
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void { | ||
this.included = true; | ||
const { brokenFlow } = context; | ||
context.brokenFlow = BROKEN_FLOW_NONE; | ||
this.body.include(context, includeChildrenRecursively); | ||
context.brokenFlow = brokenFlow; | ||
} | ||
|
||
includeCallArguments( | ||
context: InclusionContext, | ||
args: readonly (ExpressionNode | SpreadElement)[] | ||
): void { | ||
this.scope.includeCallArguments(context, args); | ||
} | ||
|
||
initialise(): void { | ||
this.scope.addParameterVariables( | ||
this.params.map(param => param.declare('parameter', UNKNOWN_EXPRESSION)), | ||
this.params[this.params.length - 1] instanceof RestElement | ||
); | ||
if (this.body instanceof BlockStatement) { | ||
this.body.addImplicitReturnExpressionToScope(); | ||
} else { | ||
this.scope.addReturnExpression(this.body); | ||
} | ||
} | ||
|
||
parseNode(esTreeNode: GenericEsTreeNode): void { | ||
if (esTreeNode.body.type === NodeType.BlockStatement) { | ||
this.body = new BlockStatement(esTreeNode.body, this, this.scope.hoistedBodyVarScope); | ||
} | ||
super.parseNode(esTreeNode); | ||
} | ||
|
||
protected abstract getObjectEntity(): ObjectEntity; | ||
} | ||
|
||
FunctionBase.prototype.preventChildBlockScope = true; |
Oops, something went wrong.