Skip to content
This repository has been archived by the owner on Dec 9, 2017. It is now read-only.

Commit

Permalink
fixed issue #43
Browse files Browse the repository at this point in the history
  • Loading branch information
ksen007 committed Feb 6, 2014
1 parent bde88a9 commit e08bc60
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
38 changes: 37 additions & 1 deletion src/js/instrument/esnstrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,13 @@
}
};

Scope.prototype.hasOwnVar = function (name) {
var s = this;
if (s && HOP(s.vars, name))
return s.vars[name];
return null;
};

Scope.prototype.hasVar = function (name) {
var s = this;
while (s !== null) {
Expand Down Expand Up @@ -1210,20 +1217,30 @@

var currentScope = null;

// rename arguments to J$_arguments
var fromName = 'arguments';
var toName = astUtil.JALANGI_VAR+"_arguments";

function handleFun(node) {
var oldScope = currentScope;
currentScope = new Scope(currentScope);
node.scope = currentScope;
if (node.type === 'FunctionDeclaration') {
oldScope.addVar(node.id.name, "defun", node.loc);
MAP(node.params, function (param) {
if (param.name === fromName) { // rename arguments to J$_arguments
param.name = toName;
}
currentScope.addVar(param.name, "arg");
});
} else if (node.type === 'FunctionExpression') {
if (node.id !== null) {
currentScope.addVar(node.id.name, "lambda");
}
MAP(node.params, function (param) {
if (param.name === fromName) { // rename arguments to J$_arguments
param.name = toName;
}
currentScope.addVar(param.name, "arg");
});
}
Expand Down Expand Up @@ -1253,7 +1270,26 @@
var visitorPost = {
'Program':popScope,
'FunctionDeclaration':popScope,
'FunctionExpression':popScope
'FunctionExpression':popScope,
'Identifier':function (node, context) { // rename arguments to J$_arguments
if (context === astUtil.CONTEXT.RHS && node.name === fromName && currentScope.hasOwnVar(toName)) {
node.name = toName;
}
return node;
},
"UpdateExpression":function (node) { // rename arguments to J$_arguments
if (node.argument.type === 'Identifier' && node.argument.name === fromName && currentScope.hasOwnVar(toName)) {
node.argument.name = toName;
}
return node;
},
"AssignmentExpression":function (node) { // rename arguments to J$_arguments
if (node.left.type === 'Identifier' && node.left.name === fromName && currentScope.hasOwnVar(toName)) {
node.left.name = toName;
}
return node;
}

};
astUtil.transformAst(ast, visitorPost, visitorPre);
}
Expand Down
23 changes: 17 additions & 6 deletions tests/unit/shadow-arguments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
var a = function a(b, arguments) {
};
var D = function () {
a.call(null, false, null);
};
new D();
function a(b, arguments) {
var x = arguments;
arguments++;
arguments += 4;
arguments = arguments + 4;
arguments[0] = 2;
arguments.callee = 'x';
f(arguments);
}

function f(x) {}

function b() {
var x = arguments;
var y = arguments.callee;
var z = arguments[0];
}

0 comments on commit e08bc60

Please sign in to comment.