Skip to content

Commit

Permalink
Clean up Scope closing code
Browse files Browse the repository at this point in the history
  • Loading branch information
Constellation committed Jan 10, 2015
1 parent 2195fcf commit 845b947
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions src/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,37 +219,38 @@ export default class Scope {
registerScope(scopeManager, this);
}

__close(scopeManager) {
var i, iz, ref, current, implicit, info;

// Because if this is global environment, upper is null
if (!this.dynamic || scopeManager.__isOptimistic()) {
// static resolve
for (i = 0, iz = this.__left.length; i < iz; ++i) {
ref = this.__left[i];
if (!this.__resolve(ref)) {
this.__delegateToUpperScope(ref);
}
__shouldStaticallyClose(scopeManager) {
return (!this.dynamic || scopeManager.__isOptimistic());
}

__staticClose(scopeManager) {
// static resolve
for (let i = 0, iz = this.__left.length; i < iz; ++i) {
let ref = this.__left[i];
if (!this.__resolve(ref)) {
this.__delegateToUpperScope(ref);
}
}
}

__dynamicClose(scopeManager) {
// This path is for "global" and "function with eval" environment.
for (let i = 0, iz = this.__left.length; i < iz; ++i) {
// notify all names are through to global
let ref = this.__left[i];
let current = this;
do {
current.through.push(ref);
current = current.upper;
} while (current);
}
}

__close(scopeManager) {
if (this.__shouldStaticallyClose(scopeManager)) {
this.__staticClose();
} else {
// this is "global" / "with" / "function with eval" environment
if (this.type === 'with') {
for (i = 0, iz = this.__left.length; i < iz; ++i) {
ref = this.__left[i];
ref.tainted = true;
this.__delegateToUpperScope(ref);
}
} else {
for (i = 0, iz = this.__left.length; i < iz; ++i) {
// notify all names are through to global
ref = this.__left[i];
current = this;
do {
current.through.push(ref);
current = current.upper;
} while (current);
}
}
this.__dynamicClose();
}

this.__left = null;
Expand Down Expand Up @@ -484,6 +485,21 @@ export class WithScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, 'with', upperScope, block, false);
}

__close(scopeManager) {
if (this.__shouldStaticallyClose(scopeManager)) {
return super.__close(scopeManager);
}

for (let i = 0, iz = this.__left.length; i < iz; ++i) {
let ref = this.__left[i];
ref.tainted = true;
this.__delegateToUpperScope(ref);
}
this.__left = null;

return this.upper;
}
}

export class TDZScope extends Scope {
Expand Down

0 comments on commit 845b947

Please sign in to comment.