refactor(view): separate context and locals
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import {isPresent, isBlank, BaseException, Type} from 'angular2/src/facade/lang';
|
||||
import {List, ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
import {ContextWithVariableBindings} from './parser/context_with_variable_bindings';
|
||||
import {AbstractChangeDetector} from './abstract_change_detector';
|
||||
import {ChangeDetectionUtil} from './change_detection_util';
|
||||
|
||||
@ -9,6 +8,7 @@ import {
|
||||
ProtoRecord,
|
||||
RECORD_TYPE_SELF,
|
||||
RECORD_TYPE_PROPERTY,
|
||||
RECORD_TYPE_LOCAL,
|
||||
RECORD_TYPE_INVOKE_METHOD,
|
||||
RECORD_TYPE_CONST,
|
||||
RECORD_TYPE_INVOKE_CLOSURE,
|
||||
@ -44,13 +44,7 @@ import {
|
||||
* var temp;
|
||||
* var context = this.context;
|
||||
*
|
||||
* temp = ChangeDetectionUtil.findContext("address", context);
|
||||
* if (temp instanceof ContextWithVariableBindings) {
|
||||
* address0 = temp.get('address');
|
||||
* } else {
|
||||
* address0 = temp.address;
|
||||
* }
|
||||
*
|
||||
* address0 = context.address;
|
||||
* if (address0 !== this.address0) {
|
||||
* this.address0 = address0;
|
||||
* }
|
||||
@ -70,14 +64,16 @@ import {
|
||||
* }
|
||||
*
|
||||
*
|
||||
* ChangeDetector0.prototype.hydrate = function(context) {
|
||||
* ChangeDetector0.prototype.hydrate = function(context, locals) {
|
||||
* this.context = context;
|
||||
* this.locals = locals;
|
||||
* }
|
||||
*
|
||||
* ChangeDetector0.prototype.dehydrate = function(context) {
|
||||
* this.context = ChangeDetectionUtil.unitialized();
|
||||
* this.address0 = ChangeDetectionUtil.unitialized();
|
||||
* this.city1 = ChangeDetectionUtil.unitialized();
|
||||
* this.locals = null;
|
||||
* }
|
||||
*
|
||||
* ChangeDetector0.prototype.hydrated = function() {
|
||||
@ -99,8 +95,10 @@ var UTIL = "ChangeDetectionUtil";
|
||||
var DISPATCHER_ACCESSOR = "this.dispatcher";
|
||||
var PIPE_REGISTRY_ACCESSOR = "this.pipeRegistry";
|
||||
var PROTOS_ACCESSOR = "this.protos";
|
||||
var CONTEXT_ACCESSOR = "this.context";
|
||||
var CHANGE_LOCAL = "change";
|
||||
var CHANGES_LOCAL = "changes";
|
||||
var LOCALS_ACCESSOR = "this.locals";
|
||||
var TEMP_LOCAL = "temp";
|
||||
|
||||
function typeTemplate(type:string, cons:string, detectChanges:string, setContext:string):string {
|
||||
@ -135,15 +133,17 @@ function pipeOnDestroyTemplate(pipeNames:List) {
|
||||
|
||||
function hydrateTemplate(type:string, fieldsDefinitions:string, pipeOnDestroy:string):string {
|
||||
return `
|
||||
${type}.prototype.hydrate = function(context) {
|
||||
this.context = context;
|
||||
${type}.prototype.hydrate = function(context, locals) {
|
||||
${CONTEXT_ACCESSOR} = context;
|
||||
${LOCALS_ACCESSOR} = locals;
|
||||
}
|
||||
${type}.prototype.dehydrate = function() {
|
||||
${pipeOnDestroy}
|
||||
${fieldsDefinitions}
|
||||
${LOCALS_ACCESSOR} = null;
|
||||
}
|
||||
${type}.prototype.hydrated = function() {
|
||||
return this.context !== ${UTIL}.unitialized();
|
||||
return ${CONTEXT_ACCESSOR} !== ${UTIL}.unitialized();
|
||||
}
|
||||
`;
|
||||
}
|
||||
@ -165,7 +165,7 @@ var ${TEMP_LOCAL};
|
||||
var ${CHANGE_LOCAL};
|
||||
var ${CHANGES_LOCAL} = null;
|
||||
|
||||
context = this.context;
|
||||
context = ${CONTEXT_ACCESSOR};
|
||||
${records}
|
||||
`;
|
||||
}
|
||||
@ -216,17 +216,6 @@ function assignmentTemplate(field:string, value:string) {
|
||||
return `${field} = ${value};`;
|
||||
}
|
||||
|
||||
function propertyReadTemplate(name:string, context:string, newValue:string) {
|
||||
return `
|
||||
${TEMP_LOCAL} = ${UTIL}.findContext("${name}", ${context});
|
||||
if (${TEMP_LOCAL} instanceof ContextWithVariableBindings) {
|
||||
${newValue} = ${TEMP_LOCAL}.get('${name}');
|
||||
} else {
|
||||
${newValue} = ${TEMP_LOCAL}.${name};
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
function invokeMethodTemplate(name:string, args:string, context:string, newValue:string) {
|
||||
return `
|
||||
${TEMP_LOCAL} = ${UTIL}.findContext("${name}", ${context});
|
||||
@ -306,7 +295,7 @@ export class ChangeDetectorJITGenerator {
|
||||
|
||||
generate():Function {
|
||||
var text = typeTemplate(this.typeName, this.genConstructor(), this.genDetectChanges(), this.genHydrate());
|
||||
return new Function('AbstractChangeDetector', 'ChangeDetectionUtil', 'ContextWithVariableBindings', 'protos', text)(AbstractChangeDetector, ChangeDetectionUtil, ContextWithVariableBindings, this.records);
|
||||
return new Function('AbstractChangeDetector', 'ChangeDetectionUtil', 'protos', text)(AbstractChangeDetector, ChangeDetectionUtil, this.records);
|
||||
}
|
||||
|
||||
genConstructor():string {
|
||||
@ -403,18 +392,13 @@ export class ChangeDetectorJITGenerator {
|
||||
return `${newValue} = ${this.genLiteral(r.funcOrValue)}`;
|
||||
|
||||
case RECORD_TYPE_PROPERTY:
|
||||
if (r.contextIndex == 0) { // only the first property read can be a local
|
||||
return propertyReadTemplate(r.name, context, newValue);
|
||||
} else {
|
||||
return assignmentTemplate(newValue, `${context}.${r.name}`);
|
||||
}
|
||||
return assignmentTemplate(newValue, `${context}.${r.name}`);
|
||||
|
||||
case RECORD_TYPE_LOCAL:
|
||||
return assignmentTemplate(newValue, `${LOCALS_ACCESSOR}.get('${r.name}')`);
|
||||
|
||||
case RECORD_TYPE_INVOKE_METHOD:
|
||||
if (r.contextIndex == 0) { // only the first property read can be a local
|
||||
return invokeMethodTemplate(r.name, args, context, newValue);
|
||||
} else {
|
||||
return assignmentTemplate(newValue, `${context}.${r.name}(${args})`);
|
||||
}
|
||||
return assignmentTemplate(newValue, `${context}.${r.name}(${args})`);
|
||||
|
||||
case RECORD_TYPE_INVOKE_CLOSURE:
|
||||
return assignmentTemplate(newValue, `${context}(${args})`);
|
||||
|
@ -1,6 +1,5 @@
|
||||
import {isPresent, isBlank, BaseException, Type} from 'angular2/src/facade/lang';
|
||||
import {List, ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
import {ContextWithVariableBindings} from './parser/context_with_variable_bindings';
|
||||
import {ProtoRecord} from './proto_record';
|
||||
import {ExpressionChangedAfterItHasBeenChecked} from './exceptions';
|
||||
import {NO_CHANGE} from './pipes/pipe';
|
||||
@ -144,16 +143,6 @@ export class ChangeDetectionUtil {
|
||||
return obj[args[0]];
|
||||
}
|
||||
|
||||
static findContext(name:string, c){
|
||||
while (c instanceof ContextWithVariableBindings) {
|
||||
if (c.hasBinding(name)) {
|
||||
return c;
|
||||
}
|
||||
c = c.parent;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
static noChangeMarker(value):boolean {
|
||||
return value === NO_CHANGE;
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
import {isPresent, isBlank, BaseException, FunctionWrapper} from 'angular2/src/facade/lang';
|
||||
import {List, ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
import {ContextWithVariableBindings} from './parser/context_with_variable_bindings';
|
||||
|
||||
import {AbstractChangeDetector} from './abstract_change_detector';
|
||||
import {PipeRegistry} from './pipes/pipe_registry';
|
||||
@ -11,6 +10,7 @@ import {
|
||||
ProtoRecord,
|
||||
RECORD_TYPE_SELF,
|
||||
RECORD_TYPE_PROPERTY,
|
||||
RECORD_TYPE_LOCAL,
|
||||
RECORD_TYPE_INVOKE_METHOD,
|
||||
RECORD_TYPE_CONST,
|
||||
RECORD_TYPE_INVOKE_CLOSURE,
|
||||
@ -26,6 +26,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
|
||||
dispatcher:any;
|
||||
pipeRegistry;
|
||||
|
||||
locals:any;
|
||||
values:List;
|
||||
changes:List;
|
||||
pipes:List;
|
||||
@ -47,12 +48,14 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
|
||||
ListWrapper.fill(this.pipes, null);
|
||||
ListWrapper.fill(this.prevContexts, uninitialized);
|
||||
ListWrapper.fill(this.changes, false);
|
||||
this.locals = null;
|
||||
|
||||
this.protos = protoRecords;
|
||||
}
|
||||
|
||||
hydrate(context:any) {
|
||||
hydrate(context:any, locals:any) {
|
||||
this.values[0] = context;
|
||||
this.locals = locals;
|
||||
}
|
||||
|
||||
dehydrate() {
|
||||
@ -61,6 +64,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
|
||||
ListWrapper.fill(this.changes, false);
|
||||
ListWrapper.fill(this.pipes, null);
|
||||
ListWrapper.fill(this.prevContexts, uninitialized);
|
||||
this.locals = null;
|
||||
}
|
||||
|
||||
_destroyPipes() {
|
||||
@ -143,26 +147,15 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
|
||||
|
||||
case RECORD_TYPE_PROPERTY:
|
||||
var context = this._readContext(proto);
|
||||
var c = ChangeDetectionUtil.findContext(proto.name, context);
|
||||
if (c instanceof ContextWithVariableBindings) {
|
||||
return c.get(proto.name);
|
||||
} else {
|
||||
var propertyGetter:Function = proto.funcOrValue;
|
||||
return propertyGetter(c);
|
||||
}
|
||||
break;
|
||||
return proto.funcOrValue(context);
|
||||
|
||||
case RECORD_TYPE_LOCAL:
|
||||
return this.locals.get(proto.name);
|
||||
|
||||
case RECORD_TYPE_INVOKE_METHOD:
|
||||
var context = this._readContext(proto);
|
||||
var args = this._readArgs(proto);
|
||||
var c = ChangeDetectionUtil.findContext(proto.name, context);
|
||||
if (c instanceof ContextWithVariableBindings) {
|
||||
return FunctionWrapper.apply(c.get(proto.name), args);
|
||||
} else {
|
||||
var methodInvoker:Function = proto.funcOrValue;
|
||||
return methodInvoker(c, args);
|
||||
}
|
||||
break;
|
||||
return proto.funcOrValue(context, args);
|
||||
|
||||
case RECORD_TYPE_KEYED_ACCESS:
|
||||
var arg = this._readArgs(proto)[0];
|
||||
|
@ -1,4 +1,5 @@
|
||||
import {List} from 'angular2/src/facade/collection';
|
||||
import {Locals} from './parser/locals';
|
||||
|
||||
export class ChangeRecord {
|
||||
bindingMemento:any;
|
||||
@ -55,7 +56,7 @@ export class ChangeDetector {
|
||||
addChild(cd:ChangeDetector) {}
|
||||
removeChild(cd:ChangeDetector) {}
|
||||
remove() {}
|
||||
hydrate(context:any) {}
|
||||
hydrate(context:any, locals:Locals) {}
|
||||
dehydrate() {}
|
||||
markPathToRootAsCheckOnce() {}
|
||||
|
||||
|
144
modules/angular2/src/change_detection/parser/ast.js
vendored
144
modules/angular2/src/change_detection/parser/ast.js
vendored
@ -1,9 +1,8 @@
|
||||
import {autoConvertAdd, isBlank, isPresent, FunctionWrapper, BaseException} from "angular2/src/facade/lang";
|
||||
import {List, Map, ListWrapper, StringMapWrapper} from "angular2/src/facade/collection";
|
||||
import {ContextWithVariableBindings} from "./context_with_variable_bindings";
|
||||
|
||||
export class AST {
|
||||
eval(context) {
|
||||
eval(context, locals) {
|
||||
throw new BaseException("Not supported");
|
||||
}
|
||||
|
||||
@ -11,7 +10,7 @@ export class AST {
|
||||
return false;
|
||||
}
|
||||
|
||||
assign(context, value) {
|
||||
assign(context, locals, value) {
|
||||
throw new BaseException("Not supported");
|
||||
}
|
||||
|
||||
@ -24,7 +23,7 @@ export class AST {
|
||||
}
|
||||
|
||||
export class EmptyExpr extends AST {
|
||||
eval(context) {
|
||||
eval(context, locals) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -34,7 +33,7 @@ export class EmptyExpr extends AST {
|
||||
}
|
||||
|
||||
export class ImplicitReceiver extends AST {
|
||||
eval(context) {
|
||||
eval(context, locals) {
|
||||
return context;
|
||||
}
|
||||
|
||||
@ -53,10 +52,10 @@ export class Chain extends AST {
|
||||
this.expressions = expressions;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
eval(context, locals) {
|
||||
var result;
|
||||
for (var i = 0; i < this.expressions.length; i++) {
|
||||
var last = this.expressions[i].eval(context);
|
||||
var last = this.expressions[i].eval(context, locals);
|
||||
if (isPresent(last)) result = last;
|
||||
}
|
||||
return result;
|
||||
@ -78,11 +77,11 @@ export class Conditional extends AST {
|
||||
this.falseExp = falseExp;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
if(this.condition.eval(context)) {
|
||||
return this.trueExp.eval(context);
|
||||
eval(context, locals) {
|
||||
if(this.condition.eval(context, locals)) {
|
||||
return this.trueExp.eval(context, locals);
|
||||
} else {
|
||||
return this.falseExp.eval(context);
|
||||
return this.falseExp.eval(context, locals);
|
||||
}
|
||||
}
|
||||
|
||||
@ -104,34 +103,29 @@ export class AccessMember extends AST {
|
||||
this.setter = setter;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
var evaluatedContext = this.receiver.eval(context);
|
||||
|
||||
while (evaluatedContext instanceof ContextWithVariableBindings) {
|
||||
if (evaluatedContext.hasBinding(this.name)) {
|
||||
return evaluatedContext.get(this.name);
|
||||
}
|
||||
evaluatedContext = evaluatedContext.parent;
|
||||
eval(context, locals) {
|
||||
if (this.receiver instanceof ImplicitReceiver &&
|
||||
isPresent(locals) && locals.contains(this.name)) {
|
||||
return locals.get(this.name);
|
||||
} else {
|
||||
var evaluatedReceiver = this.receiver.eval(context, locals);
|
||||
return this.getter(evaluatedReceiver);
|
||||
}
|
||||
|
||||
return this.getter(evaluatedContext);
|
||||
}
|
||||
|
||||
get isAssignable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
assign(context, value) {
|
||||
var evaluatedContext = this.receiver.eval(context);
|
||||
assign(context, locals, value) {
|
||||
var evaluatedContext = this.receiver.eval(context, locals);
|
||||
|
||||
while (evaluatedContext instanceof ContextWithVariableBindings) {
|
||||
if (evaluatedContext.hasBinding(this.name)) {
|
||||
throw new BaseException(`Cannot reassign a variable binding ${this.name}`)
|
||||
}
|
||||
evaluatedContext = evaluatedContext.parent;
|
||||
if (this.receiver instanceof ImplicitReceiver &&
|
||||
isPresent(locals) && locals.contains(this.name)) {
|
||||
throw new BaseException(`Cannot reassign a variable binding ${this.name}`);
|
||||
} else {
|
||||
return this.setter(evaluatedContext, value);
|
||||
}
|
||||
|
||||
return this.setter(evaluatedContext, value);
|
||||
}
|
||||
|
||||
visit(visitor) {
|
||||
@ -148,9 +142,9 @@ export class KeyedAccess extends AST {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
var obj = this.obj.eval(context);
|
||||
var key = this.key.eval(context);
|
||||
eval(context, locals) {
|
||||
var obj = this.obj.eval(context, locals);
|
||||
var key = this.key.eval(context, locals);
|
||||
return obj[key];
|
||||
}
|
||||
|
||||
@ -158,9 +152,9 @@ export class KeyedAccess extends AST {
|
||||
return true;
|
||||
}
|
||||
|
||||
assign(context, value) {
|
||||
var obj = this.obj.eval(context);
|
||||
var key = this.key.eval(context);
|
||||
assign(context, locals, value) {
|
||||
var obj = this.obj.eval(context, locals);
|
||||
var key = this.key.eval(context, locals);
|
||||
obj[key] = value;
|
||||
return value;
|
||||
}
|
||||
@ -193,7 +187,7 @@ export class LiteralPrimitive extends AST {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
eval(context, locals) {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@ -209,8 +203,8 @@ export class LiteralArray extends AST {
|
||||
this.expressions = expressions;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
return ListWrapper.map(this.expressions, (e) => e.eval(context));
|
||||
eval(context, locals) {
|
||||
return ListWrapper.map(this.expressions, (e) => e.eval(context, locals));
|
||||
}
|
||||
|
||||
visit(visitor) {
|
||||
@ -227,10 +221,10 @@ export class LiteralMap extends AST {
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
eval(context, locals) {
|
||||
var res = StringMapWrapper.create();
|
||||
for(var i = 0; i < this.keys.length; ++i) {
|
||||
StringMapWrapper.set(res, this.keys[i], this.values[i].eval(context));
|
||||
StringMapWrapper.set(res, this.keys[i], this.values[i].eval(context, locals));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@ -249,7 +243,7 @@ export class Interpolation extends AST {
|
||||
this.expressions = expressions;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
eval(context, locals) {
|
||||
throw new BaseException("evaluating an Interpolation is not supported");
|
||||
}
|
||||
|
||||
@ -269,13 +263,13 @@ export class Binary extends AST {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
var left = this.left.eval(context);
|
||||
eval(context, locals) {
|
||||
var left = this.left.eval(context, locals);
|
||||
switch (this.operation) {
|
||||
case '&&': return left && this.right.eval(context);
|
||||
case '||': return left || this.right.eval(context);
|
||||
case '&&': return left && this.right.eval(context, locals);
|
||||
case '||': return left || this.right.eval(context, locals);
|
||||
}
|
||||
var right = this.right.eval(context);
|
||||
var right = this.right.eval(context, locals);
|
||||
|
||||
switch (this.operation) {
|
||||
case '+' : return left + right;
|
||||
@ -307,8 +301,8 @@ export class PrefixNot extends AST {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
return !this.expression.eval(context);
|
||||
eval(context, locals) {
|
||||
return !this.expression.eval(context, locals);
|
||||
}
|
||||
|
||||
visit(visitor) {
|
||||
@ -325,8 +319,8 @@ export class Assignment extends AST {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
return this.target.assign(context, this.value.eval(context));
|
||||
eval(context, locals) {
|
||||
return this.target.assign(context, locals, this.value.eval(context, locals));
|
||||
}
|
||||
|
||||
visit(visitor) {
|
||||
@ -347,19 +341,16 @@ export class MethodCall extends AST {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
var evaluatedContext = this.receiver.eval(context);
|
||||
var evaluatedArgs = evalList(context, this.args);
|
||||
|
||||
while (evaluatedContext instanceof ContextWithVariableBindings) {
|
||||
if (evaluatedContext.hasBinding(this.name)) {
|
||||
var fn = evaluatedContext.get(this.name);
|
||||
return FunctionWrapper.apply(fn, evaluatedArgs);
|
||||
}
|
||||
evaluatedContext = evaluatedContext.parent;
|
||||
eval(context, locals) {
|
||||
var evaluatedArgs = evalList(context, locals, this.args);
|
||||
if (this.receiver instanceof ImplicitReceiver &&
|
||||
isPresent(locals) && locals.contains(this.name)) {
|
||||
var fn = locals.get(this.name);
|
||||
return FunctionWrapper.apply(fn, evaluatedArgs);
|
||||
} else {
|
||||
var evaluatedReceiver = this.receiver.eval(context, locals);
|
||||
return this.fn(evaluatedReceiver, evaluatedArgs);
|
||||
}
|
||||
|
||||
return this.fn(evaluatedContext, evaluatedArgs);
|
||||
}
|
||||
|
||||
visit(visitor) {
|
||||
@ -376,12 +367,12 @@ export class FunctionCall extends AST {
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
var obj = this.target.eval(context);
|
||||
eval(context, locals) {
|
||||
var obj = this.target.eval(context, locals);
|
||||
if (! (obj instanceof Function)) {
|
||||
throw new BaseException(`${obj} is not a function`);
|
||||
}
|
||||
return FunctionWrapper.apply(obj, evalList(context, this.args));
|
||||
return FunctionWrapper.apply(obj, evalList(context, locals, this.args));
|
||||
}
|
||||
|
||||
visit(visitor) {
|
||||
@ -400,16 +391,16 @@ export class ASTWithSource extends AST {
|
||||
this.ast = ast;
|
||||
}
|
||||
|
||||
eval(context) {
|
||||
return this.ast.eval(context);
|
||||
eval(context, locals) {
|
||||
return this.ast.eval(context, locals);
|
||||
}
|
||||
|
||||
get isAssignable() {
|
||||
return this.ast.isAssignable;
|
||||
}
|
||||
|
||||
assign(context, value) {
|
||||
return this.ast.assign(context, value);
|
||||
assign(context, locals, value) {
|
||||
return this.ast.assign(context, locals, value);
|
||||
}
|
||||
|
||||
visit(visitor) {
|
||||
@ -454,12 +445,19 @@ export class AstVisitor {
|
||||
visitPrefixNot(ast:PrefixNot) {}
|
||||
}
|
||||
|
||||
var _evalListCache = [[],[0],[0,0],[0,0,0],[0,0,0,0],[0,0,0,0,0]];
|
||||
function evalList(context, exps:List){
|
||||
var _evalListCache = [[],[0],[0,0],[0,0,0],[0,0,0,0],[0,0,0,0,0],
|
||||
[0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0]];
|
||||
|
||||
function evalList(context, locals, exps:List){
|
||||
var length = exps.length;
|
||||
if (length > 10) {
|
||||
throw new BaseException("Cannot have more than 10 argument");
|
||||
}
|
||||
|
||||
var result = _evalListCache[length];
|
||||
for (var i = 0; i < length; i++) {
|
||||
result[i] = exps[i].eval(context);
|
||||
result[i] = exps[i].eval(context, locals);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -1,36 +0,0 @@
|
||||
import {MapWrapper} from 'angular2/src/facade/collection';
|
||||
import {BaseException} from 'angular2/src/facade/lang';
|
||||
|
||||
export class ContextWithVariableBindings {
|
||||
parent:any;
|
||||
/// varBindings' keys are read-only. adding/removing keys is not supported.
|
||||
varBindings:Map;
|
||||
|
||||
constructor(parent:any, varBindings:Map) {
|
||||
this.parent = parent;
|
||||
this.varBindings = varBindings;
|
||||
}
|
||||
|
||||
hasBinding(name:string):boolean {
|
||||
return MapWrapper.contains(this.varBindings, name);
|
||||
}
|
||||
|
||||
get(name:string) {
|
||||
return MapWrapper.get(this.varBindings, name);
|
||||
}
|
||||
|
||||
set(name:string, value) {
|
||||
// TODO(rado): consider removing this check if we can guarantee this is not
|
||||
// exposed to the public API.
|
||||
if (this.hasBinding(name)) {
|
||||
MapWrapper.set(this.varBindings, name, value);
|
||||
} else {
|
||||
throw new BaseException(
|
||||
'VariableBindings do not support setting of new keys post-construction.');
|
||||
}
|
||||
}
|
||||
|
||||
clearValues() {
|
||||
MapWrapper.clearValues(this.varBindings);
|
||||
}
|
||||
}
|
51
modules/angular2/src/change_detection/parser/locals.js
vendored
Normal file
51
modules/angular2/src/change_detection/parser/locals.js
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
import {isPresent, BaseException} from 'angular2/src/facade/lang';
|
||||
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
export class Locals {
|
||||
parent:Locals;
|
||||
current:Map;
|
||||
|
||||
constructor(parent:Locals, current:Map) {
|
||||
this.parent = parent;
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
contains(name:string):boolean {
|
||||
if (MapWrapper.contains(this.current, name)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isPresent(this.parent)) {
|
||||
return this.parent.contains(name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
get(name:string) {
|
||||
if (MapWrapper.contains(this.current, name)) {
|
||||
return MapWrapper.get(this.current, name);
|
||||
}
|
||||
|
||||
if (isPresent(this.parent)) {
|
||||
return this.parent.get(name);
|
||||
}
|
||||
|
||||
throw new BaseException(`Cannot find '${name}'`);
|
||||
}
|
||||
|
||||
set(name:string, value) {
|
||||
// TODO(rado): consider removing this check if we can guarantee this is not
|
||||
// exposed to the public API.
|
||||
// TODO: vsavkin maybe it should check only the local map
|
||||
if (MapWrapper.contains(this.current, name)) {
|
||||
MapWrapper.set(this.current, name, value);
|
||||
} else {
|
||||
throw new BaseException('Setting of new keys post-construction is not supported.');
|
||||
}
|
||||
}
|
||||
|
||||
clearValues() {
|
||||
MapWrapper.clearValues(this.current);
|
||||
}
|
||||
}
|
@ -34,6 +34,7 @@ import {
|
||||
ProtoRecord,
|
||||
RECORD_TYPE_SELF,
|
||||
RECORD_TYPE_PROPERTY,
|
||||
RECORD_TYPE_LOCAL,
|
||||
RECORD_TYPE_INVOKE_METHOD,
|
||||
RECORD_TYPE_CONST,
|
||||
RECORD_TYPE_INVOKE_CLOSURE,
|
||||
@ -45,7 +46,7 @@ import {
|
||||
|
||||
export class ProtoChangeDetector {
|
||||
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null){}
|
||||
instantiate(dispatcher:any, bindingRecords:List):ChangeDetector{
|
||||
instantiate(dispatcher:any, bindingRecords:List, variableBindings:List):ChangeDetector{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -71,16 +72,16 @@ export class DynamicProtoChangeDetector extends ProtoChangeDetector {
|
||||
this._pipeRegistry = pipeRegistry;
|
||||
}
|
||||
|
||||
instantiate(dispatcher:any, bindingRecords:List) {
|
||||
this._createRecordsIfNecessary(bindingRecords);
|
||||
instantiate(dispatcher:any, bindingRecords:List, variableBindings:List) {
|
||||
this._createRecordsIfNecessary(bindingRecords, variableBindings);
|
||||
return new DynamicChangeDetector(dispatcher, this._pipeRegistry, this._records);
|
||||
}
|
||||
|
||||
_createRecordsIfNecessary(bindingRecords:List) {
|
||||
_createRecordsIfNecessary(bindingRecords:List, variableBindings:List) {
|
||||
if (isBlank(this._records)) {
|
||||
var recordBuilder = new ProtoRecordBuilder();
|
||||
ListWrapper.forEach(bindingRecords, (r) => {
|
||||
recordBuilder.addAst(r.ast, r.bindingMemento, r.directiveMemento);
|
||||
recordBuilder.addAst(r.ast, r.bindingMemento, r.directiveMemento, variableBindings);
|
||||
});
|
||||
this._records = coalesce(recordBuilder.records);
|
||||
}
|
||||
@ -98,16 +99,16 @@ export class JitProtoChangeDetector extends ProtoChangeDetector {
|
||||
this._factory = null;
|
||||
}
|
||||
|
||||
instantiate(dispatcher:any, bindingRecords:List) {
|
||||
this._createFactoryIfNecessary(bindingRecords);
|
||||
instantiate(dispatcher:any, bindingRecords:List, variableBindings:List) {
|
||||
this._createFactoryIfNecessary(bindingRecords, variableBindings);
|
||||
return this._factory(dispatcher, this._pipeRegistry);
|
||||
}
|
||||
|
||||
_createFactoryIfNecessary(bindingRecords:List) {
|
||||
_createFactoryIfNecessary(bindingRecords:List, variableBindings:List) {
|
||||
if (isBlank(this._factory)) {
|
||||
var recordBuilder = new ProtoRecordBuilder();
|
||||
ListWrapper.forEach(bindingRecords, (r) => {
|
||||
recordBuilder.addAst(r.ast, r.bindingMemento, r.directiveMemento);
|
||||
recordBuilder.addAst(r.ast, r.bindingMemento, r.directiveMemento, variableBindings);
|
||||
});
|
||||
var c = _jitProtoChangeDetectorClassCounter++;
|
||||
var records = coalesce(recordBuilder.records);
|
||||
@ -124,13 +125,13 @@ class ProtoRecordBuilder {
|
||||
this.records = [];
|
||||
}
|
||||
|
||||
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null) {
|
||||
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, variableBindings:List = null) {
|
||||
var last = ListWrapper.last(this.records);
|
||||
if (isPresent(last) && last.directiveMemento == directiveMemento) {
|
||||
last.lastInDirective = false;
|
||||
}
|
||||
|
||||
var pr = _ConvertAstIntoProtoRecords.convert(ast, bindingMemento, directiveMemento, this.records.length);
|
||||
var pr = _ConvertAstIntoProtoRecords.convert(ast, bindingMemento, directiveMemento, this.records.length, variableBindings);
|
||||
if (! ListWrapper.isEmpty(pr)) {
|
||||
var last = ListWrapper.last(pr);
|
||||
last.lastInBinding = true;
|
||||
@ -145,19 +146,21 @@ class _ConvertAstIntoProtoRecords {
|
||||
protoRecords:List;
|
||||
bindingMemento:any;
|
||||
directiveMemento:any;
|
||||
variableBindings:List;
|
||||
contextIndex:number;
|
||||
expressionAsString:string;
|
||||
|
||||
constructor(bindingMemento:any, directiveMemento:any, contextIndex:number, expressionAsString:string) {
|
||||
constructor(bindingMemento:any, directiveMemento:any, contextIndex:number, expressionAsString:string, variableBindings:List) {
|
||||
this.protoRecords = [];
|
||||
this.bindingMemento = bindingMemento;
|
||||
this.directiveMemento = directiveMemento;
|
||||
this.contextIndex = contextIndex;
|
||||
this.expressionAsString = expressionAsString;
|
||||
this.variableBindings = variableBindings;
|
||||
}
|
||||
|
||||
static convert(ast:AST, bindingMemento:any, directiveMemento:any, contextIndex:number) {
|
||||
var c = new _ConvertAstIntoProtoRecords(bindingMemento, directiveMemento, contextIndex, ast.toString());
|
||||
static convert(ast:AST, bindingMemento:any, directiveMemento:any, contextIndex:number, variableBindings:List) {
|
||||
var c = new _ConvertAstIntoProtoRecords(bindingMemento, directiveMemento, contextIndex, ast.toString(), variableBindings);
|
||||
ast.visit(c);
|
||||
return c.protoRecords;
|
||||
}
|
||||
@ -178,13 +181,22 @@ class _ConvertAstIntoProtoRecords {
|
||||
|
||||
visitAccessMember(ast:AccessMember) {
|
||||
var receiver = ast.receiver.visit(this);
|
||||
return this._addRecord(RECORD_TYPE_PROPERTY, ast.name, ast.getter, [], null, receiver);
|
||||
if (isPresent(this.variableBindings) && ListWrapper.contains(this.variableBindings, ast.name)) {
|
||||
return this._addRecord(RECORD_TYPE_LOCAL, ast.name, ast.name, [], null, receiver);
|
||||
} else {
|
||||
return this._addRecord(RECORD_TYPE_PROPERTY, ast.name, ast.getter, [], null, receiver);
|
||||
}
|
||||
}
|
||||
|
||||
visitMethodCall(ast:MethodCall) {
|
||||
visitMethodCall(ast:MethodCall) {;
|
||||
var receiver = ast.receiver.visit(this);
|
||||
var args = this._visitAll(ast.args);
|
||||
return this._addRecord(RECORD_TYPE_INVOKE_METHOD, ast.name, ast.fn, args, null, receiver);
|
||||
if (isPresent(this.variableBindings) && ListWrapper.contains(this.variableBindings, ast.name)) {
|
||||
var target = this._addRecord(RECORD_TYPE_LOCAL, ast.name, ast.name, [], null, receiver);
|
||||
return this._addRecord(RECORD_TYPE_INVOKE_CLOSURE, "closure", null, args, null, target);
|
||||
} else {
|
||||
return this._addRecord(RECORD_TYPE_INVOKE_METHOD, ast.name, ast.fn, args, null, receiver);
|
||||
}
|
||||
}
|
||||
|
||||
visitFunctionCall(ast:FunctionCall) {
|
||||
|
@ -4,9 +4,10 @@ export const RECORD_TYPE_SELF = 0;
|
||||
export const RECORD_TYPE_CONST = 1;
|
||||
export const RECORD_TYPE_PRIMITIVE_OP = 2;
|
||||
export const RECORD_TYPE_PROPERTY = 3;
|
||||
export const RECORD_TYPE_INVOKE_METHOD = 4;
|
||||
export const RECORD_TYPE_INVOKE_CLOSURE = 5;
|
||||
export const RECORD_TYPE_KEYED_ACCESS = 6;
|
||||
export const RECORD_TYPE_LOCAL = 4;
|
||||
export const RECORD_TYPE_INVOKE_METHOD = 5;
|
||||
export const RECORD_TYPE_INVOKE_CLOSURE = 6;
|
||||
export const RECORD_TYPE_KEYED_ACCESS = 7;
|
||||
export const RECORD_TYPE_PIPE = 8;
|
||||
export const RECORD_TYPE_INTERPOLATE = 9;
|
||||
|
||||
|
Reference in New Issue
Block a user