-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrace.js
369 lines (309 loc) · 15.5 KB
/
trace.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* ===========================================================================
trace.js
Copyright 2012 John Dunning.
http://johndunning.com/fireworks
trace.js is released under the MIT license. See the LICENSE file
for details.
======================================================================== */
/*
First, install the Fireworks Console extension from here:
http://johndunning.com/fireworks/about/FWConsole
Then restart Fireworks and open the console panel. Add `return trace(this);`
to the functions whose execution you want to trace:
function myBuggyFunc(count)
{
return trace(this);
var dom = fw.getDocumentDOM();
dom.selectAll();
dom.deleteSelection();
for (var i = 0; i < count; i++) {
dom.clipPaste();
dom.moveSelectionTo({ x: i * 10, y: i * 10 });
}
}
myBuggyFunc();
See the readme for more information about the parameters that can be
passed to trace().
*/
// ===========================================================================
try { (function() {
if (typeof log != "function") {
alert("Before trace() can be called, the Fireworks Console extension must be installed and the panel must be open. The extension can be downloaded from here:\n\n" +
"http://johndunning.com/fireworks/about/FWConsole");
return;
}
// =======================================================================
function forEach(obj, iterator, context) {
if (obj == null) { return; }
if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
// i in obj seems to be false when it's an array extracted
// from customData
if (iterator.call(context, obj[i], i, obj) === false) { return; }
}
} else {
for (var key in obj) {
if (iterator.call(context, obj[key], key, obj) === false) { return; }
}
}
}
// =======================================================================
function map(obj, iterator, context) {
var results = [];
if (obj == null) { return results; }
forEach(obj, function(value, index, list) {
results[results.length] = iterator.call(context, value, index, list);
});
return results;
}
// =======================================================================
function logWatched(
inExpression,
inFunctionName)
{
inFunctionName = inFunctionName.slice(0, -1);
var test = inExpression,
prefix = " ";
if (/[<>=]+/.test(test)) {
return '((' + test + ') ? "" : log("' + inFunctionName + prefix +
'ASSERTION FAILED:", ' + test.quote() + '));';
} else {
return 'log("' + inFunctionName + prefix + test + ':", ' + test + ');';
}
}
// =======================================================================
function getScopeFacade(
inCaller)
{
function Facade()
{
// this is an empty class that we use just to check whether a scope
// we encounter is actually a Facade
}
function createGetter(
inName,
inScope)
{
// get the value from the scope that contains this variable
return function() { return inScope[inName]; }
}
function createSetter(
inName,
inScope)
{
// set the value on the scope that contains this variable
return function(inValue) { inScope[inName] = inValue; }
}
function addScope(
inScope,
inFacade)
{
// add a getter and setter for every variable in this scope.
// these functions will get/set the values from/in the scope
// the var is originally from, rather than the inFacade object.
for (var name in inScope) {
inFacade.__defineGetter__(name, createGetter(name, inScope));
inFacade.__defineSetter__(name, createSetter(name, inScope));
// this crazy syntax is valid in the FW JS engine, though it
// was removed from later versions of the Mozilla engine.
// it seems to be the only way to make the g/setters
// enumerable, which is sometimes useful for debugging. but
// since this looks like a syntax error to NetBeans, leave
// it commented out when not needed.
// inFacade[name] getter = createGetter(name, inScope);
// inFacade[name] setter = createSetter(name, inScope);
}
}
function walkScopeChain(
inScope,
inFacade)
{
if (inScope instanceof Facade) {
// we've hit a scope that was created by an earlier call to
// trace(). if we tried to add that scope to our facade,
// we'd get in an endless loop, so flee!
return inFacade;
} else if (inScope.__parent__ && inScope.__parent__.__parent__) {
// add the variables from the scope's parent scope first,
// in case the scope shadows some vars that are also defined
// in the parent scope, but only if the parent isn't the
// global scope. that should always be accessible and so
// doesn't need to be on the facade.
inFacade = arguments.callee(inScope.__parent__, inFacade);
}
// add this scope to the facade
addScope(inScope, inFacade);
return inFacade;
}
// we use an empty Facade object to store the scope values so that
// we can tell we created it, using instanceof. that way, if the
// function we're tracing calls another function that is also
// traced, we won't get into an infinite loop.
var facade = new Facade();
// first add all the parent scopes from the caller's scope chain
walkScopeChain(inCaller.__parent__, facade);
// then add the local variables from the caller, so that they can
// shadow ones in the parent scopes, if needed
addScope(inCaller.__call__, facade);
return facade;
}
// =======================================================================
trace = function(
__CALLER_THIS__,
inWatched,
inFunctionName)
{
// this function is local to trace just so it can access the name
// of the function being traced
function logStatement(
inWholeLine,
inWhitespace,
inStatement)
{
// convert tabs to 2 spaces and get rid of newlines
inWhitespace = (inWhitespace || "").replace(/\n/g, "").replace(/\t/g, " ");
// display the function name and statement in the console
var logCall = 'log(' + (functionName + inWhitespace + inStatement).quote() + ');',
code;
if (/^(return|continue|break)[\s;]/.test(inStatement)) {
// since putting a log call after return, continue or break
// is pointless, put it before the statement. we don't need
// to match the whole statement, just verify that it's a
// token followed by a space or ;. toString always puts a
// space after a return, even if the source looks like
// return(foo);
code = [logCall, watchedVars, inWholeLine];
} else if (/^(for|while|if)\s*\(/.test(inStatement) || /^(\}\s+else|do)\s+\{/.test(inStatement)) {
// we want to put the log call and watched vars after the
// looping statement so that we see them each time through
// the loop
code = [inWholeLine, logCall, watchedVars];
} else {
// put the log call before the statement, so that we can see a
// function call before the code steps into the function. doing
// this would normally break a block of var definitions separated
// by commas and a new line, since the regexp would match the
// last line in the block and stick the log call before it, which
// breaks the syntax. but toString on a function puts all var
// definitions on a single line, as well as a mutli-line object
// definitions, like var foo = { bar: 42 }. we want to add the
// watchedVars after the statement, so that it shows the value
// of the watched vars after the statement is executed.
code = [logCall, inWholeLine, watchedVars];
}
return code.join("\n");
}
// adjust the optional parameters
if (typeof inWatched == "string") {
inFunctionName = inWatched;
inWatched = [];
}
// we can't call this "caller", because that creates a property
// called "caller" on our function object, which then overwrites
// the arguments.callee.caller property. ffs.
var callerFunc = arguments.callee.caller,
__CALLER_ARGS__ = callerFunc.arguments,
callerSource = arguments.callee.caller.toString(),
// ignore the calling function's name if one was passed in
functionName = inFunctionName || callerFunc.name,
// pull out the body and params of the function
codeMatch = callerSource.match(/function\s+([^(]+)?\(([^)]*)\)\s*\{([\s\S]*)\}/),
params = codeMatch[2] ? codeMatch[2].split(/\s*,\s*/) : [],
body = codeMatch[3],
paramLog,
watchedVars = "",
traceMatch;
// add a colon after the function name, if there is one
functionName = functionName ? functionName + ": " : "";
if (inWatched) {
watchedVars = map(inWatched, function(watched) {
// argh, there's some global native function called "watch"?
// and we can't shadow it with a local var? wtf? ah, it's
// actually an incredibly useful watch() function to get
// callbacks when a property changes. booyah.
return logWatched(watched, functionName);
});
watchedVars = watchedVars.join("\n");
}
// create a log statement to display the function's parameters.
// include the function name as the first string in the log call.
paramLog = [(functionName + "(").quote()].concat(map(params, function(param) {
return [(param + ":").quote(), param];
}));
paramLog.push(")".quote());
paramLog = 'log(' + paramLog.join(", ") + ');\n';
traceMatch = body.match(/return\s+trace\s*\([^)]*\)\s*;/);
if (!traceMatch) {
// the caller must have set a var to trace and then called us
// through that var. but we can't trace if we can't find the
// trace call and strip it out.
alert('A "return trace();" statement could not be found in the calling function. The trace function must be called directly, not through a reference.');
return;
}
// start tracing from the statement after the trace() call, and
// remove any other calls to trace that happen to be in the function.
// also, the array returned by match() doesn't have a lastIndex
// property on it. ffs.
body = body.slice(traceMatch.index + traceMatch[0].length);
body = body.replace(/return\s+trace\s*\([^)]*\)\s*;/g, "");
// this monster regex will:
//
// - add a log call after the opening brace of an if/else/for/while block.
// but do so only if there's a space in front of the if, to avoid
// sticking log calls in the middle of another log call, which can
// happen with a nested function definition. FW returns the body of
// the nested function all on one line when doing toString, so the
// replace call above will log the whole line. but then this `if`
// replace will see if statements inside the log string and replace
// them, breaking the code. requiring a space before the keyword
// won't match in the nested function case, since FW removes all
// the whitespace from the function body.
//
// - add a log call before each ;\n to display the statement
//
// - add a log call after each do statement
body = body.replace(/^(\s*)((?:(?:if|for|while)\s+\([^{]+\)\s*\{)|(?:[^\n]+;)|(?:do\s+\{)|(?:\}\s+else\s+\{))\s*$/mg,
logStatement);
// override the arguments var with the caller's arguments so that
// things like arguments.callee correctly point to the caller. we
// do this at the very beginning of the function, but after all the
// statements have been logged, so that this doesn't get logged, too.
body = 'arguments = __CALLER_ARGS__;\n' + body;
// wrap the body in an anonymous function so our caller can execute
// it in the caller's scope, and apply it to the caller's this value
// and arguments. we have to pass the caller's this and arguments
// into the function so that if code in the caller does something
// like `arguments.length`, it'll have the appropriate value. then
// capture that function's return value, log it, and return it from
// another anonymous function. we use slightly obscure var names for
// this and args so that hopefully, the caller doesn't have anything
// with the same names in its scope.
body = '(function(){ var returnValue = (function(){' +
paramLog + body + '}).apply(__CALLER_THIS__, __CALLER_ARGS__); log("' +
functionName + 'returns:", returnValue); return returnValue; })();';
// create a facade object that maps all of the identifiers in the
// our caller's scope chain to the owning scope, and then eval the
// caller's code within that fake scope. this is where all the
// magic happens.
with (getScopeFacade(callerFunc)) {
try {
return eval(body);
} catch (exception) {
if (exception.lineNumber) {
alert([exception, exception.lineNumber, exception.fileName].join("\n"));
} else {
// this is an internal error with a useless error number,
// so throw it to let FW handle it and show a better message
throw exception;
}
}
}
}
})(); } catch (exception) {
if (exception.lineNumber) {
alert([exception, exception.lineNumber, exception.fileName].join("\n"));
} else {
throw exception;
}
}