fix(static_reflector): resolve values of functions in the function context

This commit is contained in:
Tobias Bosch 2016-07-25 05:29:20 -07:00
parent 6f4e49ed53
commit d6b65db9a7
2 changed files with 20 additions and 12 deletions

View File

@ -279,16 +279,18 @@ export class StaticReflector implements ReflectorReader {
let callContext: {[name: string]: string}|undefined = undefined; let callContext: {[name: string]: string}|undefined = undefined;
if (expression['__symbolic'] == 'call') { if (expression['__symbolic'] == 'call') {
let target = expression['expression']; let target = expression['expression'];
let functionSymbol: StaticSymbol;
let targetFunction: any; let targetFunction: any;
if (target && target.__symbolic === 'reference') { if (target && target.__symbolic === 'reference') {
callContext = {name: target.name}; callContext = {name: target.name};
targetFunction = resolveReferenceValue(resolveReference(context, target)); functionSymbol = resolveReference(context, target);
targetFunction = resolveReferenceValue(functionSymbol);
} }
if (targetFunction && targetFunction['__symbolic'] == 'function') { if (targetFunction && targetFunction['__symbolic'] == 'function') {
if (calling.get(targetFunction)) { if (calling.get(functionSymbol)) {
throw new Error('Recursion not supported'); throw new Error('Recursion not supported');
} }
calling.set(targetFunction, true); calling.set(functionSymbol, true);
let value = targetFunction['value']; let value = targetFunction['value'];
if (value) { if (value) {
// Determine the arguments // Determine the arguments
@ -302,13 +304,13 @@ export class StaticReflector implements ReflectorReader {
let result: any; let result: any;
try { try {
scope = functionScope.done(); scope = functionScope.done();
result = simplify(value); result = simplifyInContext(functionSymbol, value, depth + 1);
} finally { } finally {
scope = oldScope; scope = oldScope;
} }
return result; return result;
} }
calling.delete(targetFunction); calling.delete(functionSymbol);
} }
} }

View File

@ -322,8 +322,8 @@ describe('StaticReflector', () => {
})).toEqual(['some-value']); })).toEqual(['some-value']);
expect(simplify(new StaticSymbol('/tmp/src/function-reference.ts', ''), { expect(simplify(new StaticSymbol('/tmp/src/function-reference.ts', ''), {
__symbolic: 'reference', __symbolic: 'reference',
name: 'two' name: 'three'
})).toEqual(2); })).toEqual(3);
}); });
it('should error on direct recursive calls', () => { it('should error on direct recursive calls', () => {
@ -332,7 +332,7 @@ describe('StaticReflector', () => {
new StaticSymbol('/tmp/src/function-reference.ts', ''), new StaticSymbol('/tmp/src/function-reference.ts', ''),
{__symbolic: 'reference', name: 'recursion'})) {__symbolic: 'reference', name: 'recursion'}))
.toThrow(new Error( .toThrow(new Error(
'Recursion not supported, resolving symbol recursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts')); 'Recursion not supported, resolving symbol recursive in /tmp/src/function-recursive.d.ts, resolving symbol recursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts'));
}); });
it('should error on indirect recursive calls', () => { it('should error on indirect recursive calls', () => {
@ -341,7 +341,7 @@ describe('StaticReflector', () => {
new StaticSymbol('/tmp/src/function-reference.ts', ''), new StaticSymbol('/tmp/src/function-reference.ts', ''),
{__symbolic: 'reference', name: 'indirectRecursion'})) {__symbolic: 'reference', name: 'indirectRecursion'}))
.toThrow(new Error( .toThrow(new Error(
'Recursion not supported, resolving symbol indirectRecursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts')); 'Recursion not supported, resolving symbol indirectRecursion2 in /tmp/src/function-recursive.d.ts, resolving symbol indirectRecursion1 in /tmp/src/function-recursive.d.ts, resolving symbol indirectRecursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts'));
}); });
it('should simplify a spread expression', () => { it('should simplify a spread expression', () => {
@ -749,9 +749,15 @@ class MockReflectorHost implements StaticReflectorHost {
__symbolic: 'binop', __symbolic: 'binop',
operator: '+', operator: '+',
left: {__symbolic: 'reference', name: 'a'}, left: {__symbolic: 'reference', name: 'a'},
right: {__symbolic: 'reference', name: 'b'} right: {
__symbolic: 'binop',
operator: '+',
left: {__symbolic: 'reference', name: 'b'},
right: {__symbolic: 'reference', name: 'oneLiteral'}
}
} }
} },
oneLiteral: 1
} }
}, },
'/tmp/src/function-reference.ts': { '/tmp/src/function-reference.ts': {
@ -767,7 +773,7 @@ class MockReflectorHost implements StaticReflectorHost {
}, },
arguments: ['some-value'] arguments: ['some-value']
}, },
two: { three: {
__symbolic: 'call', __symbolic: 'call',
expression: { expression: {
__symbolic: 'reference', __symbolic: 'reference',