From 2ddae698bb003ffccfe2a132d60d148585a11d96 Mon Sep 17 00:00:00 2001 From: Timothy Jones Date: Mon, 25 Oct 2010 14:42:37 +1300 Subject: [PATCH] Using a typeof check in scope. --- lib/scope.js | 15 ++++----------- src/scope.coffee | 10 +++++----- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index a6bce387..6be6a0e1 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -13,6 +13,7 @@ type: 'arguments' } ]; + this.positions = {}; if (this.parent) { this.garbage = this.parent.garbage; } else { @@ -25,19 +26,11 @@ })(); Scope.root = null; Scope.prototype.add = function(name, type) { - var _len, _ref2, i, variable; - _ref2 = this.variables; - for (i = 0, _len = _ref2.length; i < _len; i++) { - variable = _ref2[i]; - if (variable.name === name) { - this.variables[i].type = type; - return; - } - } - return this.variables.push({ + var pos; + return typeof (pos = this.positions[name]) === 'number' ? this.variables[pos].type = type : this.positions[name] = this.variables.push({ name: name, type: type - }); + }) - 1; }; Scope.prototype.startLevel = function() { this.garbage.push([]); diff --git a/src/scope.coffee b/src/scope.coffee index b0e63e3d..1ae0ac6d 100644 --- a/src/scope.coffee +++ b/src/scope.coffee @@ -19,6 +19,7 @@ exports.Scope = class Scope # it wraps. constructor: (@parent, @expressions, @method) -> @variables = [{name: 'arguments', type: 'arguments'}] + @positions = {} if @parent @garbage = @parent.garbage else @@ -27,11 +28,10 @@ exports.Scope = class Scope # Adds a new variable or overrides an existing one. add: (name, type) -> - for variable, i in @variables - if variable.name is name - @variables[i].type = type - return - @variables.push {name, type} + if typeof (pos = @positions[name]) is 'number' + @variables[pos].type = type + else + @positions[name] = @variables.push({name, type}) - 1 # Create a new garbage level startLevel: ->