xbDebug.js
8.34 KB
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
// xbDebug.js revision: 0.003 2002-02-26
/* ***** BEGIN LICENSE BLOCK *****
* Licensed under Version: MPL 1.1/GPL 2.0/LGPL 2.1
* Full Terms at /xbProjects-srce/license/mpl-tri-license.txt
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Netscape code.
*
* The Initial Developer of the Original Code is
* Netscape Corporation.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): Bob Clary <bclary@netscape.com>
*
* ***** END LICENSE BLOCK ***** */
/*
ChangeLog:
2002-02-25: bclary - modified xbDebugTraceOject to make sure
that original versions of wrapped functions were not
rewrapped. This had caused an infinite loop in IE.
2002-02-07: bclary - modified xbDebug.prototype.close to not null
the debug window reference. This can cause problems with
Internet Explorer if the page is refreshed. These issues will
be addressed at a later date.
*/
function xbDebug()
{
this.on = false;
this.stack = new Array();
this.debugwindow = null;
this.execprofile = new Object();
}
xbDebug.prototype.push = function ()
{
this.stack[this.stack.length] = this.on;
this.on = true;
}
xbDebug.prototype.pop = function ()
{
this.on = this.stack[this.stack.length - 1];
--this.stack.length;
}
xbDebug.prototype.open = function ()
{
if (this.debugwindow && !this.debugwindow.closed)
this.close();
this.debugwindow = window.open('about:blank', 'DEBUGWINDOW', 'height=400,width=600,resizable=yes,scrollbars=yes');
this.debugwindow.title = 'xbDebug Window';
this.debugwindow.document.write('<html><head><title>xbDebug Window</title></head><body><h3>Javascript Debug Window</h3></body></html>');
this.debugwindow.focus();
}
xbDebug.prototype.close = function ()
{
if (!this.debugwindow)
return;
if (!this.debugwindow.closed)
this.debugwindow.close();
// bc 2002-02-07, other windows may still hold a reference to this: this.debugwindow = null;
}
xbDebug.prototype.dump = function (msg)
{
if (!this.on)
return;
if (!this.debugwindow || this.debugwindow.closed)
this.open();
this.debugwindow.document.write(msg + '<br>');
return;
}
var xbDEBUG = new xbDebug();
window.onunload = function () {
xbDEBUG.close();
}
function xbDebugGetFunctionName(funcref)
{
if (!funcref)
{
return '';
}
if (funcref.name)
return funcref.name;
var name = funcref + '';
name = name.substring(name.indexOf(' ') + 1, name.indexOf('('));
funcref.name = name;
if (!name) alert('name not defined');
return name;
}
// emulate functionref.apply for IE mac and IE win < 5.5
function xbDebugApplyFunction(funcname, funcref, thisref, argumentsref)
{
var rv;
if (!funcref)
{
alert('xbDebugApplyFunction: funcref is null');
}
if (typeof(funcref.apply) != 'undefined')
return funcref.apply(thisref, argumentsref);
var applyexpr = 'thisref.xbDebug_orig_' + funcname + '(';
var i;
for (i = 0; i < argumentsref.length; i++)
{
applyexpr += 'argumentsref[' + i + '],';
}
if (argumentsref.length > 0)
{
applyexpr = applyexpr.substring(0, applyexpr.length - 1);
}
applyexpr += ')';
return eval(applyexpr);
}
function xbDebugCreateFunctionWrapper(scopename, funcname, precall, postcall)
{
var wrappedfunc;
var scopeobject = eval(scopename);
var funcref = scopeobject[funcname];
scopeobject['xbDebug_orig_' + funcname] = funcref;
wrappedfunc = function ()
{
var rv;
precall(scopename, funcname, arguments);
rv = xbDebugApplyFunction(funcname, funcref, scopeobject, arguments);
postcall(scopename, funcname, arguments, rv);
return rv;
};
if (typeof(funcref.constructor) != 'undefined')
wrappedfunc.constructor = funcref.constuctor;
if (typeof(funcref.prototype) != 'undefined')
wrappedfunc.prototype = funcref.prototype;
scopeobject[funcname] = wrappedfunc;
}
function xbDebugCreateMethodWrapper(contextname, classname, methodname, precall, postcall)
{
var context = eval(contextname);
var methodref = context[classname].prototype[methodname];
context[classname].prototype['xbDebug_orig_' + methodname] = methodref;
var wrappedmethod = function ()
{
var rv;
// eval 'this' at method run time to pick up reference to the object's instance
var thisref = eval('this');
// eval 'arguments' at method run time to pick up method's arguments
var argsref = arguments;
precall(contextname + '.' + classname, methodname, argsref);
rv = xbDebugApplyFunction(methodname, methodref, thisref, argsref);
postcall(contextname + '.' + classname, methodname, argsref, rv);
return rv;
};
return wrappedmethod;
}
function xbDebugPersistToString(obj)
{
var s = '';
var p;
if (obj == null)
return 'null';
switch (typeof(obj))
{
case 'number':
return obj;
case 'string':
return '"' + obj + '"';
case 'undefined':
return 'undefined';
case 'boolean':
return obj + '';
}
if (obj.constructor)
return '[' + xbDebugGetFunctionName(obj.constructor) + ']';
return null;
}
function xbDebugTraceBefore(scopename, funcname, funcarguments)
{
var i;
var s = '';
var execprofile = xbDEBUG.execprofile[scopename + '.' + funcname];
if (!execprofile)
execprofile = xbDEBUG.execprofile[scopename + '.' + funcname] = { started: 0, time: 0, count: 0 };
for (i = 0; i < funcarguments.length; i++)
{
s += xbDebugPersistToString(funcarguments[i]);
if (i < funcarguments.length - 1)
s += ', ';
}
xbDEBUG.dump('enter ' + scopename + '.' + funcname + '(' + s + ')');
execprofile.started = (new Date()).getTime();
}
function xbDebugTraceAfter(scopename, funcname, funcarguments, rv)
{
var i;
var s = '';
var execprofile = xbDEBUG.execprofile[scopename + '.' + funcname];
if (!execprofile)
xbDEBUG.dump('xbDebugTraceAfter: execprofile not created for ' + scopename + '.' + funcname);
else if (execprofile.started == 0)
xbDEBUG.dump('xbDebugTraceAfter: execprofile.started == 0 for ' + scopename + '.' + funcname);
else
{
execprofile.time += (new Date()).getTime() - execprofile.started;
execprofile.count++;
execprofile.started = 0;
}
for (i = 0; i < funcarguments.length; i++)
{
s += xbDebugPersistToString(funcarguments[i]);
if (i < funcarguments.length - 1)
s += ', ';
}
xbDEBUG.dump('exit ' + scopename + '.' + funcname + '(' + s + ')==' + xbDebugPersistToString(rv));
}
function xbDebugTraceFunction(scopename, funcname)
{
xbDebugCreateFunctionWrapper(scopename, funcname, xbDebugTraceBefore, xbDebugTraceAfter);
}
function xbDebugTraceObject(contextname, classname)
{
var classref = eval(contextname + '.' + classname);
var p;
var sp;
if (!classref || !classref.prototype)
return;
for (p in classref.prototype)
{
sp = p + '';
if (typeof(classref.prototype[sp]) == 'function' && (sp).indexOf('xbDebug_orig') == -1)
{
classref.prototype[sp] = xbDebugCreateMethodWrapper(contextname, classname, sp, xbDebugTraceBefore, xbDebugTraceAfter);
}
}
}
function xbDebugDumpProfile()
{
var p;
var execprofile;
var avg;
for (p in xbDEBUG.execprofile)
{
execprofile = xbDEBUG.execprofile[p];
avg = Math.round(100 * execprofile.time / execprofile.count) / 100;
xbDEBUG.dump('Execution profile ' + p + ' called ' + execprofile.count + ' times. Total time=' + execprofile.time + 'ms. Avg Time=' + avg + 'ms.');
}
}