refactor(change_detect): Abstract name logic into NameRegistry

Create `NameRegistry`, responsible for understanding how names are
generated for change detector fields and variables.

Use `NameRegistry` for both JS Jit & Dart pre-generated detectors.
Making progress on #3248
This commit is contained in:
Tim Blasi
2015-07-23 16:08:55 -07:00
parent 23cd385f20
commit 0906ee8a4e
4 changed files with 233 additions and 250 deletions

View File

@ -6,6 +6,7 @@ import {ChangeDetectionUtil} from './change_detection_util';
import {DirectiveIndex, DirectiveRecord} from './directive_record';
import {ProtoRecord, RecordType} from './proto_record';
import {CodegenNameUtil, sanitizeName} from './codegen_name_util';
/**
@ -23,7 +24,6 @@ var DISPATCHER_ACCESSOR = "this.dispatcher";
var PIPES_ACCESSOR = "this.pipes";
var PROTOS_ACCESSOR = "this.protos";
var DIRECTIVES_ACCESSOR = "this.directiveRecords";
var CONTEXT_ACCESSOR = "this.context";
var IS_CHANGED_LOCAL = "isChanged";
var CHANGES_LOCAL = "changes";
var LOCALS_ACCESSOR = "this.locals";
@ -31,41 +31,16 @@ var MODE_ACCESSOR = "this.mode";
var CURRENT_PROTO = "this.currentProto";
var ALREADY_CHECKED_ACCESSOR = "this.alreadyChecked";
export class ChangeDetectorJITGenerator {
_localNames: List<string>;
_changeNames: List<string>;
_fieldNames: List<string>;
_pipeNames: List<string>;
_names: CodegenNameUtil;
constructor(public id: string, public changeDetectionStrategy: string,
public records: List<ProtoRecord>, public directiveRecords: List<any>) {
this._localNames = this._getLocalNames(records);
this._changeNames = this._getChangeNames(this._localNames);
this._fieldNames = this._getFieldNames(this._localNames);
this._pipeNames = this._getPipeNames(this._localNames);
}
_getLocalNames(records: List<ProtoRecord>): List<string> {
var index = 0;
var names = records.map((r) => { return _sanitizeName(`${r.name}${index++}`); });
return ["context"].concat(names);
}
_getChangeNames(_localNames: List<string>): List<string> {
return _localNames.map((n) => `change_${n}`);
}
_getFieldNames(_localNames: List<string>): List<string> {
return _localNames.map((n) => `this.${n}`);
}
_getPipeNames(_localNames: List<string>): List<string> {
return _localNames.map((n) => `this.${n}_pipe`);
this._names = new CodegenNameUtil(this.records, this.directiveRecords, 'this._', UTIL);
}
generate(): Function {
var typeName = _sanitizeName(`ChangeDetector_${this.id}`);
var typeName = sanitizeName(`ChangeDetector_${this.id}`);
var classDefinition = `
var ${typeName} = function ${typeName}(dispatcher, protos, directiveRecords) {
${ABSTRACT_CHANGE_DETECTOR}.call(this, ${JSON.stringify(this.id)}, dispatcher);
@ -75,7 +50,7 @@ export class ChangeDetectorJITGenerator {
${CURRENT_PROTO} = null;
${PIPES_ACCESSOR} = null;
${ALREADY_CHECKED_ACCESSOR} = false;
${this._genFieldDefinitions()}
${this._names.genDehydrateFields()}
}
${typeName}.prototype = Object.create(${ABSTRACT_CHANGE_DETECTOR}.prototype);
@ -94,12 +69,11 @@ export class ChangeDetectorJITGenerator {
${typeName}.prototype.__detectChangesInRecords = function(throwOnChange) {
${CURRENT_PROTO} = null;
${this._genLocalDefinitions()}
${this._genChangeDefinitions()}
${this._names.genInitLocals()}
var ${IS_CHANGED_LOCAL} = false;
var ${CHANGES_LOCAL} = null;
context = ${CONTEXT_ACCESSOR};
context = ${this._names.getContextName()};
${this.records.map((r) => this._genRecord(r)).join("\n")}
@ -112,7 +86,7 @@ export class ChangeDetectorJITGenerator {
${typeName}.prototype.hydrate = function(context, locals, directives, pipes) {
${MODE_ACCESSOR} = "${ChangeDetectionUtil.changeDetectionMode(this.changeDetectionStrategy)}";
${CONTEXT_ACCESSOR} = context;
${this._names.getContextName()} = context;
${LOCALS_ACCESSOR} = locals;
${this._genHydrateDirectives()}
${this._genHydrateDetectors()}
@ -121,14 +95,14 @@ export class ChangeDetectorJITGenerator {
}
${typeName}.prototype.dehydrate = function() {
${this._genPipeOnDestroy()}
${this._genFieldDefinitions()}
${this._names.genPipeOnDestroy()}
${this._names.genDehydrateFields()}
${LOCALS_ACCESSOR} = null;
${PIPES_ACCESSOR} = null;
}
${typeName}.prototype.hydrated = function() {
return ${CONTEXT_ACCESSOR} !== null;
return ${this._names.getContextName()} !== null;
}
return function(dispatcher) {
@ -140,44 +114,8 @@ export class ChangeDetectorJITGenerator {
AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords);
}
_genGetDirectiveFieldNames(): List<string> {
return this.directiveRecords.map(d => this._genGetDirective(d.directiveIndex));
}
_genGetDetectorFieldNames(): List<string> {
return this.directiveRecords.filter(r => r.isOnPushChangeDetection())
.map((d) => this._genGetDetector(d.directiveIndex));
}
_genGetDirective(d: DirectiveIndex): string { return `this.directive_${d.name}`; }
_genGetDetector(d: DirectiveIndex): string { return `this.detector_${d.name}`; }
_getNonNullPipeNames(): List<string> {
var pipes = [];
this.records.forEach((r) => {
if (r.isPipeRecord()) {
pipes.push(this._pipeNames[r.selfIndex]);
}
});
return pipes;
}
_genFieldDefinitions(): string {
var fields = [];
fields = fields.concat(this._fieldNames);
fields = fields.concat(this._getNonNullPipeNames());
fields = fields.concat(this._genGetDirectiveFieldNames());
fields = fields.concat(this._genGetDetectorFieldNames());
return fields.map((n) => {
return n == CONTEXT_ACCESSOR ? `${n} = null;` :
`${n} = ${UTIL}.uninitialized();`;
})
.join("\n");
}
_genHydrateDirectives(): string {
var directiveFieldNames = this._genGetDirectiveFieldNames();
var directiveFieldNames = this._names.getAllDirectiveNames();
var lines = ListWrapper.createFixedSize(directiveFieldNames.length);
for (var i = 0, iLen = directiveFieldNames.length; i < iLen; ++i) {
lines[i] =
@ -187,7 +125,7 @@ export class ChangeDetectorJITGenerator {
}
_genHydrateDetectors(): string {
var detectorFieldNames = this._genGetDetectorFieldNames();
var detectorFieldNames = this._names.getAllDetectorNames();
var lines = ListWrapper.createFixedSize(detectorFieldNames.length);
for (var i = 0, iLen = detectorFieldNames.length; i < iLen; ++i) {
lines[i] = `${detectorFieldNames[i]} =
@ -196,10 +134,6 @@ export class ChangeDetectorJITGenerator {
return lines.join('\n');
}
_genPipeOnDestroy(): string {
return this._getNonNullPipeNames().map((p) => `${p}.onDestroy();`).join("\n");
}
_genCallOnAllChangesDoneBody(): string {
var notifications = [];
var dirs = this.directiveRecords;
@ -207,7 +141,8 @@ export class ChangeDetectorJITGenerator {
for (var i = dirs.length - 1; i >= 0; --i) {
var dir = dirs[i];
if (dir.callOnAllChangesDone) {
notifications.push(`${this._genGetDirective(dir.directiveIndex)}.onAllChangesDone();`);
notifications.push(
`${this._names.getDirectiveName(dir.directiveIndex)}.onAllChangesDone();`);
}
}
@ -219,12 +154,6 @@ export class ChangeDetectorJITGenerator {
`;
}
_genLocalDefinitions(): string { return this._localNames.map((n) => `var ${n};`).join("\n"); }
_genChangeDefinitions(): string {
return this._changeNames.map((n) => `var ${n} = false;`).join("\n");
}
_genRecord(r: ProtoRecord): string {
var rec;
if (r.isLifeCycleRecord()) {
@ -250,14 +179,14 @@ export class ChangeDetectorJITGenerator {
}
_genPipeCheck(r: ProtoRecord): string {
var context = this._localNames[r.contextIndex];
var argString = r.args.map((arg) => this._localNames[arg]).join(", ");
var context = this._names.getLocalName(r.contextIndex);
var argString = r.args.map((arg) => this._names.getLocalName(arg)).join(", ");
var oldValue = this._fieldNames[r.selfIndex];
var newValue = this._localNames[r.selfIndex];
var change = this._changeNames[r.selfIndex];
var oldValue = this._names.getFieldName(r.selfIndex);
var newValue = this._names.getLocalName(r.selfIndex);
var change = this._names.getChangeName(r.selfIndex);
var pipe = this._pipeNames[r.selfIndex];
var pipe = this._names.getPipeName(r.selfIndex);
var cdRef = "this.ref";
var protoIndex = r.selfIndex - 1;
@ -284,15 +213,15 @@ export class ChangeDetectorJITGenerator {
}
_genReferenceCheck(r: ProtoRecord): string {
var oldValue = this._fieldNames[r.selfIndex];
var newValue = this._localNames[r.selfIndex];
var oldValue = this._names.getFieldName(r.selfIndex);
var newValue = this._names.getLocalName(r.selfIndex);
var protoIndex = r.selfIndex - 1;
var check = `
${CURRENT_PROTO} = ${PROTOS_ACCESSOR}[${protoIndex}];
${this._genUpdateCurrentValue(r)}
if (${newValue} !== ${oldValue}) {
${this._changeNames[r.selfIndex]} = true;
${this._names.getChangeName(r.selfIndex)} = true;
${this._genUpdateDirectiveOrElement(r)}
${this._genAddToChanges(r)}
${oldValue} = ${newValue};
@ -300,7 +229,7 @@ export class ChangeDetectorJITGenerator {
`;
if (r.isPureFunction()) {
var condition = r.args.map((a) => this._changeNames[a]).join(" || ");
var condition = r.args.map((a) => this._names.getChangeName(a)).join(" || ");
return `if (${condition}) { ${check} } else { ${newValue} = ${oldValue}; }`;
} else {
return check;
@ -308,10 +237,10 @@ export class ChangeDetectorJITGenerator {
}
_genUpdateCurrentValue(r: ProtoRecord): string {
var context = (r.contextIndex == -1) ? this._genGetDirective(r.directiveIndex) :
this._localNames[r.contextIndex];
var newValue = this._localNames[r.selfIndex];
var argString = r.args.map((arg) => this._localNames[arg]).join(", ");
var context = (r.contextIndex == -1) ? this._names.getDirectiveName(r.directiveIndex) :
this._names.getLocalName(r.contextIndex);
var newValue = this._names.getLocalName(r.selfIndex);
var argString = r.args.map((arg) => this._names.getLocalName(arg)).join(", ");
var rhs;
switch (r.mode) {
@ -356,7 +285,7 @@ export class ChangeDetectorJITGenerator {
break;
case RecordType.KEYED_ACCESS:
rhs = `${context}[${this._localNames[r.args[0]]}]`;
rhs = `${context}[${this._names.getLocalName(r.args[0])}]`;
break;
default:
@ -370,7 +299,7 @@ export class ChangeDetectorJITGenerator {
for (var i = 0; i < r.args.length; ++i) {
res += JSON.stringify(r.fixedArgs[i]);
res += " + ";
res += `${UTIL}.s(${this._localNames[r.args[i]]})`;
res += `${UTIL}.s(${this._names.getLocalName(r.args[i])})`;
res += " + ";
}
res += JSON.stringify(r.fixedArgs[r.args.length]);
@ -380,13 +309,13 @@ export class ChangeDetectorJITGenerator {
_genUpdateDirectiveOrElement(r: ProtoRecord): string {
if (!r.lastInBinding) return "";
var newValue = this._localNames[r.selfIndex];
var oldValue = this._fieldNames[r.selfIndex];
var newValue = this._names.getLocalName(r.selfIndex);
var oldValue = this._names.getFieldName(r.selfIndex);
var br = r.bindingRecord;
if (br.isDirective()) {
var directiveProperty =
`${this._genGetDirective(br.directiveRecord.directiveIndex)}.${br.propertyName}`;
`${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.${br.propertyName}`;
return `
${this._genThrowOnChangeCheck(oldValue, newValue)}
${directiveProperty} = ${newValue};
@ -409,8 +338,8 @@ export class ChangeDetectorJITGenerator {
}
_genAddToChanges(r: ProtoRecord): string {
var newValue = this._localNames[r.selfIndex];
var oldValue = this._fieldNames[r.selfIndex];
var newValue = this._names.getLocalName(r.selfIndex);
var oldValue = this._names.getFieldName(r.selfIndex);
if (!r.bindingRecord.callOnChange()) return "";
return `
${CHANGES_LOCAL} = ${UTIL}.addChange(
@ -430,17 +359,17 @@ export class ChangeDetectorJITGenerator {
_genOnCheck(r: ProtoRecord): string {
var br = r.bindingRecord;
return `if (!throwOnChange) ${this._genGetDirective(br.directiveRecord.directiveIndex)}.onCheck();`;
return `if (!throwOnChange) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.onCheck();`;
}
_genOnInit(r: ProtoRecord): string {
var br = r.bindingRecord;
return `if (!throwOnChange && !${ALREADY_CHECKED_ACCESSOR}) ${this._genGetDirective(br.directiveRecord.directiveIndex)}.onInit();`;
return `if (!throwOnChange && !${ALREADY_CHECKED_ACCESSOR}) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.onInit();`;
}
_genOnChange(r: ProtoRecord): string {
var br = r.bindingRecord;
return `if (!throwOnChange && ${CHANGES_LOCAL}) ${this._genGetDirective(br.directiveRecord.directiveIndex)}.onChange(${CHANGES_LOCAL});`;
return `if (!throwOnChange && ${CHANGES_LOCAL}) ${this._names.getDirectiveName(br.directiveRecord.directiveIndex)}.onChange(${CHANGES_LOCAL});`;
}
_genNotifyOnPushDetectors(r: ProtoRecord): string {
@ -448,13 +377,9 @@ export class ChangeDetectorJITGenerator {
if (!r.lastInDirective || !br.isOnPushChangeDetection()) return "";
var retVal = `
if(${IS_CHANGED_LOCAL}) {
${this._genGetDetector(br.directiveRecord.directiveIndex)}.markAsCheckOnce();
${this._names.getDetectorName(br.directiveRecord.directiveIndex)}.markAsCheckOnce();
}
`;
return retVal;
}
}
function _sanitizeName(s: string): string {
return s.replace(new RegExp("\\W", "g"), '');
}

View File

@ -0,0 +1,134 @@
import {RegExpWrapper, StringWrapper} from 'angular2/src/facade/lang';
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {DirectiveIndex} from './directive_record';
import {ProtoRecord} from './proto_record';
// `context` is always the first field.
var _CONTEXT_IDX = 0;
var _whiteSpaceRegExp = RegExpWrapper.create("\\W", "g");
/**
* Returns `s` with all non-identifier characters removed.
*/
export function sanitizeName(s: string): string {
return StringWrapper.replaceAll(s, _whiteSpaceRegExp, '');
}
/**
* Class responsible for providing field and local variable names for change detector classes.
* Also provides some convenience functions, for example, declaring variables, destroying pipes,
* and dehydrating the detector.
*/
export class CodegenNameUtil {
/**
* Record names sanitized for use as fields.
* See [sanitizeName] for details.
*/
_sanitizedNames: List<string>;
constructor(public records: List<ProtoRecord>, public directiveRecords: List<any>,
public fieldPrefix: string, public utilName: string) {
this._sanitizedNames = ListWrapper.createFixedSize(this.records.length + 1);
this._sanitizedNames[_CONTEXT_IDX] = 'context';
for (var i = 0, iLen = this.records.length; i < iLen; ++i) {
this._sanitizedNames[i + 1] = sanitizeName(`${this.records[i].name}${i}`);
}
}
getContextName(): string { return this.getFieldName(_CONTEXT_IDX); }
getLocalName(idx: int): string { return this._sanitizedNames[idx]; }
getChangeName(idx: int): string { return `c_${this._sanitizedNames[idx]}`; }
/**
* Generate a statement initializing local variables used when detecting changes.
*/
genInitLocals(): string {
var declarations = [];
var assignments = [];
for (var i = 0, iLen = this.getFieldCount(); i < iLen; ++i) {
var changeName = this.getChangeName(i);
declarations.push(`${this.getLocalName(i)},${changeName}`);
assignments.push(changeName);
}
return `var ${ListWrapper.join(declarations, ',')};` +
`${ListWrapper.join(assignments, '=')} = false;`;
}
getFieldCount(): int { return this._sanitizedNames.length; }
getFieldName(idx: int): string { return `${this.fieldPrefix}${this._sanitizedNames[idx]}`; }
getAllFieldNames(): List<string> {
var fieldList = [];
for (var k = 0, kLen = this.getFieldCount(); k < kLen; ++k) {
fieldList.push(this.getFieldName(k));
}
for (var i = 0, iLen = this.records.length; i < iLen; ++i) {
var rec = this.records[i];
if (rec.isPipeRecord()) {
fieldList.push(this.getPipeName(rec.selfIndex));
}
}
for (var j = 0, jLen = this.directiveRecords.length; j < jLen; ++j) {
var dRec = this.directiveRecords[j];
fieldList.push(this.getDirectiveName(dRec.directiveIndex));
if (dRec.isOnPushChangeDetection()) {
fieldList.push(this.getDetectorName(dRec.directiveIndex));
}
}
return fieldList;
}
/**
* Generates a statement which declares all fields.
* This is only necessary for Dart change detectors.
*/
genDeclareFields(): string {
var fields = this.getAllFieldNames();
ListWrapper.removeAt(fields, _CONTEXT_IDX);
return ListWrapper.isEmpty(fields) ? '' : `var ${ListWrapper.join(fields, ', ')};`;
}
/**
* Generates statements which clear all fields so that the change detector is dehydrated.
*/
genDehydrateFields(): string {
var fields = this.getAllFieldNames();
ListWrapper.removeAt(fields, _CONTEXT_IDX);
if (!ListWrapper.isEmpty(fields)) {
// At least one assignment.
fields.push(`${this.utilName}.uninitialized();`);
}
return `${this.getContextName()} = null; ${ListWrapper.join(fields, ' = ')}`;
}
/**
* Generates statements destroying all pipe variables.
*/
genPipeOnDestroy(): string {
return ListWrapper.join(ListWrapper.map(ListWrapper.filter(this.records, (r) => {
return r.isPipeRecord();
}), (r) => { return `${this.getPipeName(r.selfIndex)}.onDestroy();`; }), '\n');
}
getPipeName(idx: int): string { return `${this.fieldPrefix}${this._sanitizedNames[idx]}_pipe`; }
getAllDirectiveNames(): List<string> {
return ListWrapper.map(this.directiveRecords, d => this.getDirectiveName(d.directiveIndex));
}
getDirectiveName(d: DirectiveIndex): string { return `${this.fieldPrefix}directive_${d.name}`; }
getAllDetectorNames(): List<string> {
return ListWrapper.map(
ListWrapper.filter(this.directiveRecords, r => r.isOnPushChangeDetection()),
(d) => this.getDetectorName(d.directiveIndex));
}
getDetectorName(d: DirectiveIndex): string { return `${this.fieldPrefix}detector_${d.name}`; }
}