fix(ivy): constant object literals shared across element and component instances (#33705)
Currently if a consumer does something like the following, the object literal will be shared across the two elements and any instances of the component template. The same applies to array literals: ``` <div [someDirective]="{}"></div> <div [someDirective]="{}"></div> ``` These changes make it so that we generate a pure function even if an object is constant so that each instance gets its own object. Note that the original design for this fix included moving the pure function factories into the `consts` array. In the process of doing so I realized that pure function are also used inside of directive host bindings which means that we don't have access to the `consts`. These changes also: * Fix an issue that meant that the `pureFunction0` instruction could only be run during creation mode. * Make the `getConstant` utility slightly more convenient to use. This isn't strictly required for these changes to work, but I had made it as a part of a larger refactor that I ended up reverting. PR Close #33705
This commit is contained in:
@ -1420,11 +1420,9 @@ export class ValueConverter extends AstMemoryEfficientTransformer {
|
||||
array.span, array.sourceSpan, this.visitAll(array.expressions), values => {
|
||||
// If the literal has calculated (non-literal) elements transform it into
|
||||
// calls to literal factories that compose the literal and will cache intermediate
|
||||
// values. Otherwise, just return an literal array that contains the values.
|
||||
// values.
|
||||
const literal = o.literalArr(values);
|
||||
return values.every(a => a.isConstant()) ?
|
||||
this.constantPool.getConstLiteral(literal, true) :
|
||||
getLiteralFactory(this.constantPool, literal, this.allocatePureFunctionSlots);
|
||||
return getLiteralFactory(this.constantPool, literal, this.allocatePureFunctionSlots);
|
||||
});
|
||||
}
|
||||
|
||||
@ -1432,12 +1430,10 @@ export class ValueConverter extends AstMemoryEfficientTransformer {
|
||||
return new BuiltinFunctionCall(map.span, map.sourceSpan, this.visitAll(map.values), values => {
|
||||
// If the literal has calculated (non-literal) elements transform it into
|
||||
// calls to literal factories that compose the literal and will cache intermediate
|
||||
// values. Otherwise, just return an literal array that contains the values.
|
||||
// values.
|
||||
const literal = o.literalMap(values.map(
|
||||
(value, index) => ({key: map.keys[index].key, value, quoted: map.keys[index].quoted})));
|
||||
return values.every(a => a.isConstant()) ?
|
||||
this.constantPool.getConstLiteral(literal, true) :
|
||||
getLiteralFactory(this.constantPool, literal, this.allocatePureFunctionSlots);
|
||||
return getLiteralFactory(this.constantPool, literal, this.allocatePureFunctionSlots);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1484,15 +1480,11 @@ function getLiteralFactory(
|
||||
const {literalFactory, literalFactoryArguments} = constantPool.getLiteralFactory(literal);
|
||||
// Allocate 1 slot for the result plus 1 per argument
|
||||
const startSlot = allocateSlots(1 + literalFactoryArguments.length);
|
||||
literalFactoryArguments.length > 0 || error(`Expected arguments to a literal factory function`);
|
||||
const {identifier, isVarLength} = pureFunctionCallInfo(literalFactoryArguments);
|
||||
|
||||
// Literal factories are pure functions that only need to be re-invoked when the parameters
|
||||
// change.
|
||||
const args = [
|
||||
o.literal(startSlot),
|
||||
literalFactory,
|
||||
];
|
||||
const args = [o.literal(startSlot), literalFactory];
|
||||
|
||||
if (isVarLength) {
|
||||
args.push(o.literalArr(literalFactoryArguments));
|
||||
|
Reference in New Issue
Block a user