feat(parser): make method calls aware of ContextWithVariableBindings
This commit is contained in:
parent
8b2a5d7d17
commit
156f3d99e0
|
@ -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) {
|
||||
|
|
|
@ -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", () => {
|
||||
|
|
Loading…
Reference in New Issue