/
florian_on_js_reflection.js
179 lines (154 loc) · 5.99 KB
/
florian_on_js_reflection.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
// This is an xpcshell script written by Florian Quèze [1] for the Bug 1330464 [2].
// This script live parses and modifies JS files to detect and revise calls to Services.foo.bar() with wrong arg numbers.
// In this script, it made a good example of using the OS file APIs and SpiderMonkey Reflection Parser APIs.
// [1] Florian Quèze: florian@queze.net
// [2] https://bugzilla.mozilla.org/show_bug.cgi?id=1330464
var { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Components.utils.import("resource://gre/modules/osfile.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
const init = Components.classes["@mozilla.org/jsreflect;1"].createInstance();
init();
var done = false;
const kIgnorePaths = [
"obj-browser", "obj-browser-dbg", "objdir-artifact",
".hg",
"removeNonExistentParameters.js",
"tools/lint/eslint"
];
function processScript(file, relativePath = "", startLine = 1) {
file = file.replace(/^#/gm, "//XPCShell-preprocessor-");
try {
let removals = [];
Reflect.parse(file, {
source: relativePath,
line: startLine,
builder: {
callExpression: function(callee, args, loc) {
if (!callee ||
callee.type != "MemberExpression" ||
callee.computed ||
callee.property.type != "Identifier" ||
callee.object.type != "MemberExpression" ||
callee.object.property.type != "Identifier" ||
callee.object.object.type != "Identifier" ||
callee.object.object.name != "Services" ||
!(callee.object.property.name in Services) ||
!(callee.property.name in Services[callee.object.property.name]))
return ({type: "", loc: loc});
try {
let expected =
Services[callee.object.property.name][callee.property.name].length;
if (args.length > expected) {
let fromLoc;
if (expected)
fromLoc = args[expected - 1].loc.end;
else
fromLoc = args[0].loc.start;
let toLoc = args[args.length - 1].loc.end;
removals.push({from_line: fromLoc.line - startLine,
from_column: fromLoc.column,
to_line: toLoc.line - startLine,
to_column: toLoc.column
});
}
} catch(ex) { dump("ex = " + ex + "\n"); }
return ({type: "", loc: loc});
}
}
});
if (!removals.length)
return null;
let lines = file.split("\n");
let removal;
while ((removal = removals.pop())) {
let line = lines[removal.from_line].slice(0, removal.from_column) +
lines[removal.to_line].slice(removal.to_column);
lines[removal.from_line] = line;
lines.splice(removal.from_line + 1, removal.to_line - removal.from_line);
}
return lines.join("\n").replace(/^\/\/XPCShell-preprocessor-/gm, "#");
} catch (ex) {
dump("Error reading " + relativePath + ":" + ex.lineNumber + " " + ex + "\n");
return null;
}
}
Task.spawn(function() {
let currentDirectory = yield OS.File.getCurrentDirectory();
let ignoredPaths = kIgnorePaths.map(p => currentDirectory + "/" + p);
let paths = [currentDirectory];
let decoder = new TextDecoder();
while (paths.length) {
let iterator;
try {
iterator = new OS.File.DirectoryIterator(paths.pop());
for (let child in iterator) {
child = yield child;
let path = child.path;
if (ignoredPaths.includes(path))
continue;
if (child.isDir) {
paths.push(path);
continue;
}
if (!path.endsWith(".js") && !path.endsWith(".jsm") && !path.endsWith(".xml"))
continue;
let file = decoder.decode(yield OS.File.read(path));
if (!file.includes("Services."))
continue;
let relativePath = path.slice(currentDirectory.length + 1);
if (path.endsWith(".js") || path.endsWith(".jsm")) {
file = processScript(file, relativePath);
} else {
// Handle XBL bindings
if (!file.includes("xmlns=\"http://www.mozilla.org/xbl\""))
continue;
let lines = file.split("\n");
let startLine, endLine;
for (startLine = 0; startLine < lines.length; ++startLine) {
if (lines[startLine].includes("<![CDATA[")) {
let prefix = lines[startLine].replace(/.*<!\[CDATA\[/, "");
for (endLine = ++startLine; endLine < lines.length; ++endLine) {
if (lines[endLine].includes("]]>"))
break;
}
let suffix = lines[endLine].replace(/\]\]>.*/, "");
let f = lines.slice(startLine, endLine).join("\n");
if (!f.includes("Services."))
continue;
f = processScript("function f() {" + prefix + "\n"+f+"\n" + suffix + "}",
path, startLine);
if (!f)
continue;
let modifiedLines = f.split("\n");
modifiedLines.shift();
modifiedLines.pop();
lines.splice(startLine, endLine - startLine, ...modifiedLines);
startLine += modifiedLines.length;
}
}
file = lines.join("\n");
}
if (file)
yield OS.File.writeAtomic(path, (new TextEncoder()).encode(file));
}
} catch (ex) {
// Ignore StopIteration to prevent exiting the loop.
if (ex != StopIteration) {
throw ex;
}
}
iterator.close();
}
done = true;
});
// Spin an event loop.
(() => {
var thread = Components.classes["@mozilla.org/thread-manager;1"]
.getService().currentThread;
while (!done)
thread.processNextEvent(true);
// get rid of any pending event
while (thread.hasPendingEvents())
thread.processNextEvent(true);
})();