Browse Source

use a set to track which functions are being called

rewrite
Rich Harris 8 years ago
parent
commit
34a13a74d0
  1. 10
      src/ast/nodes/shared/callHasEffects.js

10
src/ast/nodes/shared/callHasEffects.js

@ -3,9 +3,11 @@ import isReference from '../../utils/isReference.js';
import pureFunctions from './pureFunctions.js'; import pureFunctions from './pureFunctions.js';
import { UNKNOWN } from '../../values.js'; import { UNKNOWN } from '../../values.js';
const currentlyCalling = new Set();
function fnHasEffects ( fn ) { function fnHasEffects ( fn ) {
if ( fn._calling ) return true; // prevent infinite loops... TODO there must be a better way if ( currentlyCalling.has( fn ) ) return true; // prevent infinite loops... TODO there must be a better way
fn._calling = true; currentlyCalling.add( fn );
// handle body-less arrow functions // handle body-less arrow functions
const scope = fn.body.scope || fn.scope; const scope = fn.body.scope || fn.scope;
@ -13,12 +15,12 @@ function fnHasEffects ( fn ) {
for ( const node of body ) { for ( const node of body ) {
if ( node.hasEffects( scope ) ) { if ( node.hasEffects( scope ) ) {
fn._calling = false; currentlyCalling.delete( fn );
return true; return true;
} }
} }
fn._calling = false; currentlyCalling.delete( fn );
return false; return false;
} }

Loading…
Cancel
Save