feat(parser): make method calls aware of ContextWithVariableBindings

This commit is contained in:
Victor Berchet 2015-01-19 12:58:44 +01:00
parent 8b2a5d7d17
commit 156f3d99e0
2 changed files with 30 additions and 3 deletions

View File

@ -335,8 +335,18 @@ export class MethodCall extends AST {
}
eval(context) {
var obj = this.receiver.eval(context);
return this.fn(obj, evalList(context, this.args));
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;
}
return this.fn(evaluatedContext, evaluatedArgs);
}
visit(visitor, args) {

View File

@ -211,6 +211,23 @@ export function main() {
it('should throw when no method', () => {
expectEvalError("blah()").toThrowError();
});
it('should evaluate a method from ContextWithVariableBindings', () => {
var context = new ContextWithVariableBindings(
td(0, 0, 'parent'),
MapWrapper.createFromPairs([['fn', () => 'child']])
);
expectEval("fn()", context).toEqual('child');
});
it('should fall back to the parent context when ContextWithVariableBindings does not ' +
'have the requested method', () => {
var context = new ContextWithVariableBindings(
td(0, 0, 'parent'),
MapWrapper.create()
);
expectEval("fn()", context).toEqual('parent');
});
});
describe("functional calls", () => {