Permalink
Browse files

Always rewrite arguments because it is kind of keyword on javascript.

  • Loading branch information...
1 parent c0e4a36 commit a362bd2915f2e092ae55278e3c2f43f67c1d6386 @josevalim committed Aug 9, 2011
Showing with 19 additions and 13 deletions.
  1. +17 −13 lib/lua/compiler/translator.js
  2. +2 −0 spec/grammar_spec.js
@@ -1,6 +1,7 @@
var VarsScope = function() {
- this.readVars = [];
- this.shadowedVars = {};
+ this.readVars = ["arguments"];
+ this.shadowedVars = { };
+ this.shadowedGlobal = { "arguments": "__largs" };
}
var LoopScope = function() {
@@ -11,7 +12,7 @@ var ASTWalker = function () {
this.argsTranslate = false;
this.functionSpread = null;
this.loopScope = null;
- this.varScope = new VarsScope();
+ this.varsScope = new VarsScope();
this.varCounter = 0;
}
@@ -75,10 +76,10 @@ ASTWalker.prototype = {
},
"shadowVars": function(fun) {
- var old = this.varScope;
- this.varScope = new VarsScope();
+ var old = this.varsScope;
+ this.varsScope = new VarsScope();
var result = fun(this);
- this.varScope = old;
+ this.varsScope = old;
return result;
},
@@ -149,10 +150,10 @@ ASTWalker.prototype = {
"LOCALVAR": function(current) {
var original = current[1];
var result = original;
- var varScope = this.varScope;
+ var varsScope = this.varsScope;
- if(varScope.readVars.indexOf(original) !== -1) {
- var shadowed = varScope.shadowedVars;
+ if(varsScope.readVars.indexOf(original) !== -1) {
+ var shadowed = varsScope.shadowedVars;
result = shadowed[original] || this.createTempVar();
shadowed[original] = result
}
@@ -162,13 +163,16 @@ ASTWalker.prototype = {
"VAR": function(current) {
var original = current[1];
var result = original;
- var varScope = this.varScope;
- var shadowed = varScope.shadowedVars[original];
+ var varsScope = this.varsScope;
+ var shadowed = varsScope.shadowedVars[original];
+ var shadowedGlobal = varsScope.shadowedGlobal[original];
if(shadowed) {
result = shadowed;
- } else if(varScope.readVars.indexOf(original) === -1) {
- varScope.readVars.push(original);
+ } else if(shadowedGlobal) {
+ result = shadowedGlobal;
+ } else if(varsScope.readVars.indexOf(original) === -1) {
+ varsScope.readVars.push(original);
}
return result;
@@ -256,6 +256,8 @@ exports.testTDOTAssignments = function(test) {
// 1 x 1
test.strictEqual(1, Lua.evalText("function x(...)\nlocal a = ...\nreturn a\nend\nreturn x(1)"));
test.strictEqual(1, Lua.evalText("function x(...)\nlocal a = ...\nreturn a\nend\nreturn x(1,2)"));
+ test.strictEqual(1, Lua.evalText("function x(...)\nlocal arguments = nil\nlocal a = ...\nreturn a\nend\nreturn x(1,2)"));
+ test.strictEqual(10, Lua.evalText("arguments = 10\nfunction x()\nreturn arguments\nend\nreturn x()"));
// 2 x 2
test.strictEqual(1, Lua.evalText("function x(...)\nlocal a, b = 1, ...\nreturn a\nend\nreturn x(2)"));

0 comments on commit a362bd2

Please sign in to comment.