ensured `arguments` in each scope

This commit is contained in:
satyr 2010-10-02 08:21:34 +09:00
parent dafe6b09c5
commit 8cdee9c0f4
2 changed files with 47 additions and 43 deletions

View File

@ -1,12 +1,15 @@
(function() { (function() {
var Scope, extend; var Scope, _ref, extend, last;
var __hasProp = Object.prototype.hasOwnProperty; var __hasProp = Object.prototype.hasOwnProperty;
extend = require('./helpers').extend; _ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
exports.Scope = (function() { exports.Scope = (function() {
Scope = function(parent, expressions, method) { Scope = function(_arg, _arg2, _arg3) {
var _ref; this.method = _arg3;
_ref = [parent, expressions, method], this.parent = _ref[0], this.expressions = _ref[1], this.method = _ref[2]; this.expressions = _arg2;
this.variables = {}; this.parent = _arg;
this.variables = {
'arguments': 'arguments'
};
if (this.parent) { if (this.parent) {
this.garbage = this.parent.garbage; this.garbage = this.parent.garbage;
} else { } else {
@ -20,12 +23,13 @@
return this.garbage.push([]); return this.garbage.push([]);
}; };
Scope.prototype.endLevel = function() { Scope.prototype.endLevel = function() {
var _i, _len, _ref, _result, name; var _i, _len, _ref2, _result, name, vars;
_result = []; _ref = this.garbage.pop(); vars = this.variables;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { _result = []; _ref2 = this.garbage.pop();
name = _ref[_i]; for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
if (this.variables[name] === 'var') { name = _ref2[_i];
_result.push(this.variables[name] = 'reuse'); if (vars[name] === 'var') {
_result.push(vars[name] = 'reuse');
} }
} }
return _result; return _result;
@ -38,11 +42,11 @@
return false; return false;
}; };
Scope.prototype.any = function(fn) { Scope.prototype.any = function(fn) {
var _ref, k, v; var _ref2, k, v;
_ref = this.variables; _ref2 = this.variables;
for (v in _ref) { for (v in _ref2) {
if (!__hasProp.call(_ref, v)) continue; if (!__hasProp.call(_ref2, v)) continue;
k = _ref[v]; k = _ref2[v];
if (fn(v, k)) { if (fn(v, k)) {
return true; return true;
} }
@ -53,12 +57,12 @@
return (this.variables[name] = 'param'); return (this.variables[name] = 'param');
}; };
Scope.prototype.check = function(name, options) { Scope.prototype.check = function(name, options) {
var immediate; var _ref2, immediate;
immediate = Object.prototype.hasOwnProperty.call(this.variables, name); immediate = Object.prototype.hasOwnProperty.call(this.variables, name);
if (immediate || (options && options.immediate)) { if (immediate || ((options != null) ? options.immediate : null)) {
return immediate; return immediate;
} }
return !!(this.parent && this.parent.check(name)); return !!(((_ref2 = this.parent) != null) ? _ref2.check(name) : null);
}; };
Scope.prototype.temporary = function(type, index) { Scope.prototype.temporary = function(type, index) {
return type.length > 1 ? '_' + type + (index > 1 ? index : '') : '_' + (index + parseInt(type, 36)).toString(36).replace(/\d/g, 'a'); return type.length > 1 ? '_' + type + (index > 1 ? index : '') : '_' + (index + parseInt(type, 36)).toString(36).replace(/\d/g, 'a');
@ -71,7 +75,7 @@
} }
this.variables[temp] = 'var'; this.variables[temp] = 'var';
if (this.garbage.length) { if (this.garbage.length) {
this.garbage[this.garbage.length - 1].push(temp); last(this.garbage).push(temp);
} }
return temp; return temp;
}; };
@ -83,7 +87,7 @@
}; };
Scope.prototype.hasDeclarations = function(body) { Scope.prototype.hasDeclarations = function(body) {
return body === this.expressions && this.any(function(k, val) { return body === this.expressions && this.any(function(k, val) {
return val === 'var' || val === 'reuse'; return ('var' === val || 'reuse' === val);
}); });
}; };
Scope.prototype.hasAssignments = function(body) { Scope.prototype.hasAssignments = function(body) {
@ -92,13 +96,13 @@
}); });
}; };
Scope.prototype.declaredVariables = function() { Scope.prototype.declaredVariables = function() {
var _ref, _result, key, val; var _ref2, _result, key, val;
return (function() { return (function() {
_result = []; _ref = this.variables; _result = []; _ref2 = this.variables;
for (key in _ref) { for (key in _ref2) {
if (!__hasProp.call(_ref, key)) continue; if (!__hasProp.call(_ref2, key)) continue;
val = _ref[key]; val = _ref2[key];
if (val === 'var' || val === 'reuse') { if (('var' === val || 'reuse' === val)) {
_result.push(key); _result.push(key);
} }
} }
@ -106,11 +110,11 @@
}).call(this).sort(); }).call(this).sort();
}; };
Scope.prototype.assignedVariables = function() { Scope.prototype.assignedVariables = function() {
var _ref, _result, key, val; var _ref2, _result, key, val;
_result = []; _ref = this.variables; _result = []; _ref2 = this.variables;
for (key in _ref) { for (key in _ref2) {
if (!__hasProp.call(_ref, key)) continue; if (!__hasProp.call(_ref2, key)) continue;
val = _ref[key]; val = _ref2[key];
if (val.assigned) { if (val.assigned) {
_result.push("" + (key) + " = " + (val.value)); _result.push("" + (key) + " = " + (val.value));
} }

View File

@ -6,7 +6,7 @@
# with the outside. # with the outside.
# Import the helpers we plan to use. # Import the helpers we plan to use.
{extend} = require './helpers' {extend, last} = require './helpers'
exports.Scope = class Scope exports.Scope = class Scope
@ -17,9 +17,8 @@ exports.Scope = class Scope
# as well as a reference to the **Expressions** node is belongs to, which is # as well as a reference to the **Expressions** node is belongs to, which is
# where it should declare its variables, and a reference to the function that # where it should declare its variables, and a reference to the function that
# it wraps. # it wraps.
constructor: (parent, expressions, method) -> constructor: (@parent, @expressions, @method) ->
[@parent, @expressions, @method] = [parent, expressions, method] @variables = {'arguments'}
@variables = {}
if @parent if @parent
@garbage = @parent.garbage @garbage = @parent.garbage
else else
@ -33,7 +32,8 @@ exports.Scope = class Scope
# Return to the previous garbage level and erase referenced temporary # Return to the previous garbage level and erase referenced temporary
# variables in current level from scope. # variables in current level from scope.
endLevel: -> endLevel: ->
(@variables[name] = 'reuse') for name in @garbage.pop() when @variables[name] is 'var' vars = @variables
(vars[name] = 'reuse') for name in @garbage.pop() when vars[name] is 'var'
# Look up a variable name in lexical scope, and declare it if it does not # Look up a variable name in lexical scope, and declare it if it does not
# already exist. # already exist.
@ -57,8 +57,8 @@ exports.Scope = class Scope
# walks up to the root scope. # walks up to the root scope.
check: (name, options) -> check: (name, options) ->
immediate = Object::hasOwnProperty.call @variables, name immediate = Object::hasOwnProperty.call @variables, name
return immediate if immediate or (options and options.immediate) return immediate if immediate or options?.immediate
!!(@parent and @parent.check(name)) !!@parent?.check name
# Generate a temporary variable name at the given index. # Generate a temporary variable name at the given index.
temporary: (type, index) -> temporary: (type, index) ->
@ -73,7 +73,7 @@ exports.Scope = class Scope
index = 0 index = 0
index++ while @check(temp = @temporary type, index) and @variables[temp] isnt 'reuse' index++ while @check(temp = @temporary type, index) and @variables[temp] isnt 'reuse'
@variables[temp] = 'var' @variables[temp] = 'var'
@garbage[@garbage.length - 1].push temp if @garbage.length last(@garbage).push temp if @garbage.length
temp temp
# Ensure that an assignment is made at the top of this scope # Ensure that an assignment is made at the top of this scope
@ -84,7 +84,7 @@ exports.Scope = class Scope
# Does this scope reference any variables that need to be declared in the # Does this scope reference any variables that need to be declared in the
# given function body? # given function body?
hasDeclarations: (body) -> hasDeclarations: (body) ->
body is @expressions and @any (k, val) -> val is 'var' or val is 'reuse' body is @expressions and @any (k, val) -> val in ['var', 'reuse']
# Does this scope reference any assignments that need to be declared at the # Does this scope reference any assignments that need to be declared at the
# top of the given function body? # top of the given function body?
@ -93,7 +93,7 @@ exports.Scope = class Scope
# Return the list of variables first declared in this scope. # Return the list of variables first declared in this scope.
declaredVariables: -> declaredVariables: ->
(key for key, val of @variables when val is 'var' or val is 'reuse').sort() (key for key, val of @variables when val in ['var', 'reuse']).sort()
# Return the list of assignments that are supposed to be made at the top # Return the list of assignments that are supposed to be made at the top
# of this scope. # of this scope.