-
Notifications
You must be signed in to change notification settings - Fork 55
/
runner.js
236 lines (214 loc) · 8.37 KB
/
runner.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer
* Copyright (c) 2018 Persper Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0.
*******************************************************************************/
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function (require, exports) {
const bindings = require('./bindings'),
astutil = require('./astutil'),
pessimistic = require('./pessimistic'),
semioptimistic = require('./semioptimistic'),
callbackCounter = require('./callbackCounter'),
requireJsGraph = require('./requireJsGraph'),
path = require('path'),
fs = require('fs'),
utils = require('./utils'),
JSONStream = require('JSONStream');
this.args = null;
this.files = null;
this.consoleOutput = null;
Array.prototype.remove = function () {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
let addNode = function (edge, v) {
if (v.type === 'CalleeVertex') {
let nd = v.call;
edge.label = astutil.encFuncName(nd.attr.enclosingFunction);
edge.file = nd.attr.enclosingFile;
edge.start = {row: nd.loc.start.line, column: nd.loc.start.column};
edge.end = {row: nd.loc.end.line, column: nd.loc.end.column};
edge.range = {start: nd.range[0], end: nd.range[1]};
return edge;
}
if (v.type === 'FuncVertex') {
edge.label = astutil.funcname(v.func);
edge.file = v.func.attr.enclosingFile;
edge.start = {row: v.func.loc.start.line, column: v.func.loc.start.column};
edge.end = {row: v.func.loc.end.line, column: v.func.loc.end.column};
edge.range = {start: v.func.range[0], end: v.func.range[1]};
return edge;
}
if (v.type === 'NativeVertex') {
//'Math_log' (Native)
edge.label = v.name;
edge.file = "Native";
edge.start.row = null;
edge.end.row = null;
edge.start.column = null;
edge.end.column = null;
edge.range = {start: null, end: null};
return edge;
}
throw new Error("strange vertex: " + v);
};
let buildBinding = function (call, fn) {
let edge = {
source: {
label: null,
file: null,
start: {row: null, column: null},
end: {row: null, column: null},
range: {start: null, end: null}
},
target: {
label: null,
file: null,
start: {row: null, column: null},
end: {row: null, column: null},
range: {start: null, end: null}
}
};
addNode(edge.source, call);
addNode(edge.target, fn);
return edge;
};
let pp = function (v) {
if (v.type === 'CalleeVertex')
return '\'' + astutil.encFuncName(v.call.attr.enclosingFunction) + '\' (' + astutil.ppPos(v.call) + ')';
if (v.type === 'FuncVertex')
return '\'' + astutil.funcname(v.func) + '\' (' + astutil.ppPos(v.func) + ')';
if (v.type === 'NativeVertex')
return '\'' + v.name + '\' (Native)';
throw new Error("strange vertex: " + v);
};
let build = function () {
let args = this.args;
let files = this.files;
let consoleOutput = this.consoleOutput;
let filter = this.filter;
if (filter !== undefined && filter.length > 0) {
let filteredfiles = [];
files.forEach(function (file) {
filteredfiles.push(file);
filter.forEach(function (elem) {
let trunk = elem.substr(1).trim();
let expression = new RegExp(trunk, "gm");
let result = expression.test(file);
if (result && elem.startsWith('-')) {
filteredfiles.remove(file);
}
if (result && elem.startsWith('+')) {
filteredfiles.push(file);
}
});
});
files = Array.from(new Set(filteredfiles));
}
args.strategy = args.strategy || 'ONESHOT';
if (!args.strategy.match(/^(NONE|ONESHOT|DEMAND|FULL)$/)) {
process.exit(-1);
}
if (args.strategy === 'FULL') {
console.warn('strategy FULL not implemented yet; using DEMAND instead');
args.strategy = 'DEMAND';
}
if (args.time) console.time("parsing ");
var ast = astutil.astFromFiles(files);
if (args.time) console.timeEnd("parsing ");
if (args.time) console.time("bindings ");
bindings.addBindings(ast);
if (args.time) console.timeEnd("bindings ");
if (args.time) console.time("callgraph");
var cg;
if (args.strategy === 'NONE' || args.strategy === 'ONESHOT')
cg = pessimistic.buildCallGraph(ast, args.strategy === 'NONE');
else if (args.strategy === 'DEMAND')
cg = semioptimistic.buildCallGraph(ast);
if (args.time) console.timeEnd("callgraph");
if (args.fg){
let serializedGraph = cg.fg.graph.serialize();
serializedGraph.links.forEach((link) => {
console.log(link.source, "=>", link.target);
});
}
if (args.countCB)
callbackCounter.countCallbacks(ast);
if (args.reqJs)
requireJsGraph.makeRequireJsGraph(ast).forEach(function (edge) {
console.log(edge.toString());
});
if (args.cg) {
let result = [];
cg.edges.iter(function (call, fn) {
result.push(buildBinding(call, fn));
if (consoleOutput)
console.log(pp(call) + " -> " + pp(fn));
});
if (this.args.output !== null) {
let filename = this.args.output[0];
if (!filename.endsWith(".json")) {
filename += ".json";
}
fs.writeFile(filename, JSON.stringify(result, null, 2), function (err) {
if (err) {
/*
When happened something wrong (usually out of memory when we want print
the result into a file), then we try to file with JSONStream.
*/
let transformStream = JSONStream.stringify();
let outputStream = fs.createWriteStream(filename);
transformStream.pipe(outputStream);
result.forEach(transformStream.write);
transformStream.end();
}
});
}
return result;
}
};
exports.setFiles = function (inputList) {
let filelist = [];
inputList.forEach(function (file) {
file = path.resolve(file);
if (!fs.existsSync(file)) {
console.warn('The path "' + file + '" does not exists.');
}
else if (fs.statSync(file).isDirectory()) {
filelist = utils.collectFiles(file, filelist);
}
else if (file.endsWith(".js") || file.endsWith(".ts") || file.endsWith(".vue")) {
filelist.push(file);
}
});
this.files = Array.from(new Set(filelist));
if (this.files.length === 0) {
console.warn("Input file list is empty!");
process.exit(-1);
}
};
exports.setFilter = function (filter) {
this.filter = filter;
};
exports.setArgs = function (args) {
this.args = args;
};
exports.setConsoleOutput = function (value) {
this.consoleOutput = value;
};
exports.build = build;
return exports;
});