Fix additional functions refactor

Summary:
Fix for bug NTillmann encountered with the additional functions refactor.

Every function references itself through its `arguments`, so additional functions were recursively trying to visit themselves while visiting their arguments. It doesn't make sense to visit a function from its own scope, so we add a check to the visitor preventing this.
Closes https://github.com/facebook/prepack/pull/1508

Differential Revision: D7130078

Pulled By: cblappert

fbshipit-source-id: ecb0497456169f3ff41c89f2fcf17e3ae9670d96
This commit is contained in:
Chris Blappert 2018-03-01 15:47:59 -08:00 committed by Facebook Github Bot
parent 69ebd09e8d
commit 2b600a3d56
2 changed files with 21 additions and 4 deletions

View File

@ -786,10 +786,12 @@ export class ResidualHeapVisitor {
} else if (val instanceof FunctionValue) {
// Function declarations should get hoisted in common scope so that instances only get allocated once
let parentScope = this.scope;
this._withScope(this.commonScope, () => {
invariant(val instanceof FunctionValue);
if (this.preProcessValue(val)) this.visitValueFunction(val, parentScope);
});
// Every function references itself through arguments, prevent the recursive double-visit
if (this.scope !== val && this.commonScope !== val)
this._withScope(this.commonScope, () => {
invariant(val instanceof FunctionValue);
if (this.preProcessValue(val)) this.visitValueFunction(val, parentScope);
});
} else if (val instanceof SymbolValue) {
if (this.preProcessValue(val)) this.visitValueSymbol(val);
} else {

View File

@ -0,0 +1,15 @@
// does not contain:x = 5;
function func1() {
let x = 5;
let z = [ func1 ];
return z;
}
if (global.__registerAdditionalFunctionToPrepack) {
__registerAdditionalFunctionToPrepack(func1);
}
inspect = function() {
return func1()[0] === func1()[0];
}