-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_latest.js
214 lines (186 loc) · 6.82 KB
/
index_latest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Created by Pankajan on 08/12/2015.
*/
var fs = require('fs-extra');
var esprima = require('esprima');
var estraverse = require('estraverse');
var escodegen = require('escodegen');
var escope = require('escope');
var util = require('./lib/util');
var path = require('path');
var PERSONAL_HOME = '/Users/Pankajan/';
var OFFICE_HOME = '/afs/inf.ed.ac.uk/user/p/pchanthi/';
var PERSONAL_LAP = PERSONAL_HOME + 'Edinburgh/Research_Source/';
var OFFICE = OFFICE_HOME + 'edinburgh/research_source/';
var ROOT = PERSONAL_LAP;
var HOME = PERSONAL_HOME;
var PROJECT = 'd3';
var filename = ROOT + PROJECT; //process.argv[2];
var outFilename = ROOT + "instrumented-" + PROJECT; //process.argv[2];
var LOG_FILE = HOME + 'log_' + PROJECT + '.txt';
process(filename, outFilename);
var count=0;
function process(filename, outFilename) {
var stat = fs.lstatSync(filename);
if(stat.isDirectory()) {
if(filename.indexOf('node_modules')==-1 && filename.indexOf('/test/')==-1) {
fs.mkdirSync(outFilename);
fs.readdir(filename, function (err, files) {
if (err) {
fs.copy(filename, outFilename, {});
return console.error(err);
}
files.forEach(function (file) {
process(filename + "/" + file, outFilename + "/" + file);
});
});
} else {
fs.copy(filename, outFilename, {});
}
} else {
//console.log("Processing File : " + filename);
if(stat.isFile() && path.extname(filename)==='.js') {
try {
var newCode = instrument(filename);
fs.writeFile(outFilename, newCode, function (err) {
if (err)fs.copy(filename, outFilename, {});
});
} catch (err) {
console.log(err);
fs.copy(filename, outFilename, {});
}
} else {
fs.copy(filename, outFilename, {});
}
}
}
var scopeManager;
var scopeChain;
var currentScope;
function instrument(filename) {
var srcCode = fs.readFileSync(filename, 'utf-8');
if (srcCode.charAt(0) === '#') { //shebang, 'comment' it out, won't affect syntax tree locations for things we care about
srcCode = '//' + srcCode;
}
var error = 'ESPRIMA PARSE';
var ast;
try{
ast = esprima.parse(srcCode, {
loc: true,
range: true,
tokens: true,
comment: true
});
} catch (notAScript) {
ast = esprima.parse(srcCode, {
loc: true,
range: true,
tokens: true,
comment: true,
sourceType: 'module'
});
}
error = 'ESCOPE ANALYSIS';
scopeManager = escope.analyze(ast);
error = 'ESPRIMA ACQUIRE';
currentScope = scopeManager.acquire(ast); // global scope
//console.log(JSON.stringify(ast));
scopeChain = [];
var currentScopeVariables = [];
error = 'ESTRAVERSE TRAVERSE';
estraverse.traverse(ast, {
enter: enter,
leave: leave
});
error = 'ESCODEGEN GENERATE';
var newCode = escodegen.generate(ast);
return newCode;
}
var modifiedVariables=[];
var calledMethods=[];
function enter(node, parent){
//currentScope = scopeManager.acquire(node);
//if(currentScope!=null) {
// currentScopeVariables = currentScope.variables;
//}
if (createsNewScope(node)){
currentScope = scopeManager.acquire(node);
scopeChain.push([]);
} else if (node.type === util.astNodes.VARIABLE_DECLARATOR){
scopeChain[scopeChain.length - 1].push(node.id.name);
} else if (node.type === util.astNodes.ASSIGNMENT_EXPRESSION){
var name = node.left.name;
modifiedVariables.push(name);
return node;
} else if (node.type === util.astNodes.UPDATE_EXPRESSION) {
//modifiedVariables.push(node.argument.name);
} else if (node.type === util.astNodes.CALL_EXPRESSION) {
if(node.callee.name!= undefined)
calledMethods.push(node.callee.name);
else {
//calledMethods.push(node.callee.property.name);
}
} else if (node.type === util.astNodes.EXPRESSION_STATEMENT) {
modifiedVariables=[];
calledMethods=[];
}
}
function leave(node, parent){
if (createsNewScope(node)){
} else if (node.type === util.astNodes.EXPRESSION_STATEMENT) {
if(parent.body!=undefined && parent.body!=null) {
var index=0;
for(x=0; x<parent.body.length; x++) {
if(parent.body[x].loc===node.loc) {
index=x;
break;
}
}
var xx="";
for (x = 0; x < modifiedVariables.length; x++) {
if(modifiedVariables[x]!== undefined) {
if(modifiedVariables[x]==='touch') {
console.log('t');
}
var ii = 1;
while(ii<=scopeChain.length) {
if(scopeChain[scopeChain.length - ii].indexOf(modifiedVariables[x])>-1) break;
ii++;
}
if(ii<=scopeChain.length) {
xx = "if("+modifiedVariables[x]+") { require('fs').appendFile('"+LOG_FILE+"', '" + modifiedVariables[x] + "->'+" + modifiedVariables[x] + ");}";
if (parent.body instanceof Array) {
parent.body.splice(index + 1, 0, esprima.parse(xx));
}
/*if(currentScope!=null) {
var currentScopeVariables = currentScope.variables;
var contains = false;
for(i=0; i<currentScopeVariables.length; i++) {
if(currentScopeVariables[i].name===modifiedVariables[x]){
contains = true;
break;
}
}
if(true) {
}
}*/
}
}
}
}
for (x = 0; x < calledMethods.length; x++) {
var xx = "console.log('Method called [" + calledMethods[x] + "]');";
xx="";
if(parent.body instanceof Array) {
parent.body.splice(index + 1, 0, esprima.parse(xx));
}
}
modifiedVariables=[];
calledMethods=[];
}
}
function createsNewScope(node){
return node.type === util.astNodes.FUNCTION_DECLARATION ||
node.type === util.astNodes.FUNCTION_EXPRESSION ||
node.type === util.astNodes.PROGRAM;
}