refactor(ivy): remove reserveSlots instruction (#25533)

PR Close #25533
This commit is contained in:
Kara Erickson
2018-08-15 16:32:08 -07:00
committed by Jason Aden
parent 21d22ce4ad
commit 4708cb91ef
8 changed files with 281 additions and 295 deletions

View File

@ -6,34 +6,44 @@
* found in the LICENSE file at https://angular.io/license
*/
import {assertReservedSlotInitialized, bindingUpdated, bindingUpdated2, bindingUpdated4, checkAndUpdateBinding, consumeBinding, getCreationMode, moveBindingIndexToReservedSlot, restoreBindingIndex} from './instructions';
import {bindingUpdated, bindingUpdated2, bindingUpdated4, updateBinding, getBinding, getCreationMode, getTView, initBindings, bindingUpdated3,} from './instructions';
/**
* Bindings for pure functions are stored after regular bindings.
*
* ----------------------------------------------------------------------------
* | LNodes ... | regular bindings / interpolations | pure function bindings
* ----------------------------------------------------------------------------
* ^
* TView.bindingStartIndex
*
* Pure function instructions are given an offset from TView.bindingStartIndex.
* Adding the offset to TView.bindingStartIndex gives the first index where the bindings
* are stored.
*/
/**
* If the value hasn't been saved, calls the pure function to store and return the
* value. If it has been saved, returns the saved value.
*
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn Function that returns a value
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param thisArg Optional calling context of pureFn
* @returns value
*/
export function pureFunction0<T>(slotOffset: number, pureFn: () => T, thisArg?: any): T {
ngDevMode && assertReservedSlotInitialized(slotOffset, 1);
const index = moveBindingIndexToReservedSlot(slotOffset);
const value = getCreationMode() ?
checkAndUpdateBinding(thisArg ? pureFn.call(thisArg) : pureFn()) :
consumeBinding();
restoreBindingIndex(index);
return value;
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
return getCreationMode() ?
updateBinding(bindingIndex, thisArg ? pureFn.call(thisArg) : pureFn()) :
getBinding(bindingIndex);
}
/**
* If the value of the provided exp has changed, calls the pure function to return
* an updated value. Or if the value has not changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn Function that returns an updated value
* @param exp Updated expression value
* @param thisArg Optional calling context of pureFn
@ -41,20 +51,18 @@ export function pureFunction0<T>(slotOffset: number, pureFn: () => T, thisArg?:
*/
export function pureFunction1(
slotOffset: number, pureFn: (v: any) => any, exp: any, thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, 2);
const index = moveBindingIndexToReservedSlot(slotOffset);
const value = bindingUpdated(exp) ?
checkAndUpdateBinding(thisArg ? pureFn.call(thisArg, exp) : pureFn(exp)) :
consumeBinding();
restoreBindingIndex(index);
return value;
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
return bindingUpdated(bindingIndex, exp) ?
updateBinding(bindingIndex + 1, thisArg ? pureFn.call(thisArg, exp) : pureFn(exp)) :
getBinding(bindingIndex + 1);
}
/**
* If the value of any provided exp has changed, calls the pure function to return
* an updated value. Or if no values have changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn
* @param exp1
* @param exp2
@ -64,20 +72,19 @@ export function pureFunction1(
export function pureFunction2(
slotOffset: number, pureFn: (v1: any, v2: any) => any, exp1: any, exp2: any,
thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, 3);
const index = moveBindingIndexToReservedSlot(slotOffset);
const value = bindingUpdated2(exp1, exp2) ?
checkAndUpdateBinding(thisArg ? pureFn.call(thisArg, exp1, exp2) : pureFn(exp1, exp2)) :
consumeBinding();
restoreBindingIndex(index);
return value;
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
return bindingUpdated2(bindingIndex, exp1, exp2) ?
updateBinding(
bindingIndex + 2, thisArg ? pureFn.call(thisArg, exp1, exp2) : pureFn(exp1, exp2)) :
getBinding(bindingIndex + 2);
}
/**
* If the value of any provided exp has changed, calls the pure function to return
* an updated value. Or if no values have changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn
* @param exp1
* @param exp2
@ -88,22 +95,20 @@ export function pureFunction2(
export function pureFunction3(
slotOffset: number, pureFn: (v1: any, v2: any, v3: any) => any, exp1: any, exp2: any, exp3: any,
thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, 4);
const index = moveBindingIndexToReservedSlot(slotOffset);
const different = bindingUpdated2(exp1, exp2);
const value = bindingUpdated(exp3) || different ?
checkAndUpdateBinding(
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
return bindingUpdated3(bindingIndex, exp1, exp2, exp3) ?
updateBinding(
bindingIndex + 3,
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3) : pureFn(exp1, exp2, exp3)) :
consumeBinding();
restoreBindingIndex(index);
return value;
getBinding(bindingIndex + 3);
}
/**
* If the value of any provided exp has changed, calls the pure function to return
* an updated value. Or if no values have changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn
* @param exp1
* @param exp2
@ -115,21 +120,20 @@ export function pureFunction3(
export function pureFunction4(
slotOffset: number, pureFn: (v1: any, v2: any, v3: any, v4: any) => any, exp1: any, exp2: any,
exp3: any, exp4: any, thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, 5);
const index = moveBindingIndexToReservedSlot(slotOffset);
const value = bindingUpdated4(exp1, exp2, exp3, exp4) ?
checkAndUpdateBinding(
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
return bindingUpdated4(bindingIndex, exp1, exp2, exp3, exp4) ?
updateBinding(
bindingIndex + 4,
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4) : pureFn(exp1, exp2, exp3, exp4)) :
consumeBinding();
restoreBindingIndex(index);
return value;
getBinding(bindingIndex + 4);
}
/**
* If the value of any provided exp has changed, calls the pure function to return
* an updated value. Or if no values have changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn
* @param exp1
* @param exp2
@ -142,23 +146,21 @@ export function pureFunction4(
export function pureFunction5(
slotOffset: number, pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any) => any, exp1: any,
exp2: any, exp3: any, exp4: any, exp5: any, thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, 6);
const index = moveBindingIndexToReservedSlot(slotOffset);
const different = bindingUpdated4(exp1, exp2, exp3, exp4);
const value = bindingUpdated(exp5) || different ?
checkAndUpdateBinding(
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5) :
pureFn(exp1, exp2, exp3, exp4, exp5)) :
consumeBinding();
restoreBindingIndex(index);
return value;
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
const different = bindingUpdated4(bindingIndex, exp1, exp2, exp3, exp4);
return bindingUpdated(bindingIndex + 4, exp5) || different ?
updateBinding(
bindingIndex + 5, thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5) :
pureFn(exp1, exp2, exp3, exp4, exp5)) :
getBinding(bindingIndex + 5);
}
/**
* If the value of any provided exp has changed, calls the pure function to return
* an updated value. Or if no values have changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn
* @param exp1
* @param exp2
@ -172,23 +174,21 @@ export function pureFunction5(
export function pureFunction6(
slotOffset: number, pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any) => any,
exp1: any, exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, 7);
const index = moveBindingIndexToReservedSlot(slotOffset);
const different = bindingUpdated4(exp1, exp2, exp3, exp4);
const value = bindingUpdated2(exp5, exp6) || different ?
checkAndUpdateBinding(
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6) :
pureFn(exp1, exp2, exp3, exp4, exp5, exp6)) :
consumeBinding();
restoreBindingIndex(index);
return value;
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
const different = bindingUpdated4(bindingIndex, exp1, exp2, exp3, exp4);
return bindingUpdated2(bindingIndex + 4, exp5, exp6) || different ?
updateBinding(
bindingIndex + 6, thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6) :
pureFn(exp1, exp2, exp3, exp4, exp5, exp6)) :
getBinding(bindingIndex + 6);
}
/**
* If the value of any provided exp has changed, calls the pure function to return
* an updated value. Or if no values have changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn
* @param exp1
* @param exp2
@ -204,24 +204,22 @@ export function pureFunction7(
slotOffset: number,
pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, v7: any) => any, exp1: any,
exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, exp7: any, thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, 8);
const index = moveBindingIndexToReservedSlot(slotOffset);
let different = bindingUpdated4(exp1, exp2, exp3, exp4);
different = bindingUpdated2(exp5, exp6) || different;
const value = bindingUpdated(exp7) || different ?
checkAndUpdateBinding(
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6, exp7) :
pureFn(exp1, exp2, exp3, exp4, exp5, exp6, exp7)) :
consumeBinding();
restoreBindingIndex(index);
return value;
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
let different = bindingUpdated4(bindingIndex, exp1, exp2, exp3, exp4);
return bindingUpdated3(bindingIndex + 4, exp5, exp6, exp7) || different ?
updateBinding(
bindingIndex + 7, thisArg ?
pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6, exp7) :
pureFn(exp1, exp2, exp3, exp4, exp5, exp6, exp7)) :
getBinding(bindingIndex + 7);
}
/**
* If the value of any provided exp has changed, calls the pure function to return
* an updated value. Or if no values have changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn
* @param exp1
* @param exp2
@ -239,16 +237,15 @@ export function pureFunction8(
pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, v7: any, v8: any) => any,
exp1: any, exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, exp7: any, exp8: any,
thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, 9);
const index = moveBindingIndexToReservedSlot(slotOffset);
const different = bindingUpdated4(exp1, exp2, exp3, exp4);
const value = bindingUpdated4(exp5, exp6, exp7, exp8) || different ?
checkAndUpdateBinding(
thisArg ? pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8) :
pureFn(exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8)) :
consumeBinding();
restoreBindingIndex(index);
return value;
initBindings(); // TODO(kara): remove this check when we have pre-filled array
const bindingIndex = getTView().bindingStartIndex + slotOffset;
const different = bindingUpdated4(bindingIndex, exp1, exp2, exp3, exp4);
return bindingUpdated4(bindingIndex + 4, exp5, exp6, exp7, exp8) || different ?
updateBinding(
bindingIndex + 8, thisArg ?
pureFn.call(thisArg, exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8) :
pureFn(exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8)) :
getBinding(bindingIndex + 8);
}
/**
@ -257,7 +254,7 @@ export function pureFunction8(
* If the value of any provided exp has changed, calls the pure function to return
* an updated value. Or if no values have changed, returns cached value.
*
* @param slotOffset the offset in the reserved slot space {@link reserveSlots}
* @param slotOffset the offset from binding root to the reserved slot
* @param pureFn A pure function that takes binding values and builds an object or array
* containing those values.
* @param exps An array of binding values
@ -266,14 +263,12 @@ export function pureFunction8(
*/
export function pureFunctionV(
slotOffset: number, pureFn: (...v: any[]) => any, exps: any[], thisArg?: any): any {
ngDevMode && assertReservedSlotInitialized(slotOffset, exps.length + 1);
const index = moveBindingIndexToReservedSlot(slotOffset);
initBindings(); // TODO(kara): remove this check when we have pre-filled array
let bindingIndex = getTView().bindingStartIndex + slotOffset;
let different = false;
for (let i = 0; i < exps.length; i++) {
bindingUpdated(exps[i]) && (different = true);
bindingUpdated(bindingIndex++, exps[i]) && (different = true);
}
const value = different ? checkAndUpdateBinding(pureFn.apply(thisArg, exps)) : consumeBinding();
restoreBindingIndex(index);
return value;
return different ? updateBinding(bindingIndex, pureFn.apply(thisArg, exps)) :
getBinding(bindingIndex);
}