PageRenderTime 74ms CodeModel.GetById 40ms app.highlight 19ms RepoModel.GetById 11ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/doc/example-jsunit/jsunit/app/xbDebug.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 306 lines | 211 code | 59 blank | 36 comment | 43 complexity | cc90106c67921d19e0c08e24ed41eed1 MD5 | raw file
  1// xbDebug.js revision: 0.003 2002-02-26
  2
  3/* ***** BEGIN LICENSE BLOCK *****
  4 * Licensed under Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 * Full Terms at /xbProjects-srce/license/mpl-tri-license.txt
  6 *
  7 * Software distributed under the License is distributed on an "AS IS" basis,
  8 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  9 * for the specific language governing rights and limitations under the
 10 * License.
 11 *
 12 * The Original Code is Netscape code.
 13 *
 14 * The Initial Developer of the Original Code is
 15 * Netscape Corporation.
 16 * Portions created by the Initial Developer are Copyright (C) 2001
 17 * the Initial Developer. All Rights Reserved.
 18 *
 19 * Contributor(s): Bob Clary <bclary@netscape.com>
 20 *
 21 * ***** END LICENSE BLOCK ***** */
 22
 23/*
 24ChangeLog:
 25
 262002-02-25: bclary - modified xbDebugTraceOject to make sure 
 27            that original versions of wrapped functions were not
 28            rewrapped. This had caused an infinite loop in IE.
 29
 302002-02-07: bclary - modified xbDebug.prototype.close to not null
 31            the debug window reference. This can cause problems with
 32	          Internet Explorer if the page is refreshed. These issues will
 33	          be addressed at a later date.
 34*/
 35
 36function xbDebug()
 37{
 38    this.on = false;
 39    this.stack = new Array();
 40    this.debugwindow = null;
 41    this.execprofile = new Object();
 42}
 43
 44xbDebug.prototype.push = function ()
 45{
 46    this.stack[this.stack.length] = this.on;
 47    this.on = true;
 48}
 49
 50xbDebug.prototype.pop = function ()
 51{
 52    this.on = this.stack[this.stack.length - 1];
 53    --this.stack.length;
 54}
 55
 56xbDebug.prototype.open = function ()
 57{
 58    if (this.debugwindow && !this.debugwindow.closed)
 59        this.close();
 60
 61    this.debugwindow = window.open('about:blank', 'DEBUGWINDOW', 'height=400,width=600,resizable=yes,scrollbars=yes');
 62
 63    this.debugwindow.title = 'xbDebug Window';
 64    this.debugwindow.document.write('<html><head><title>xbDebug Window</title></head><body><h3>Javascript Debug Window</h3></body></html>');
 65    this.debugwindow.focus();
 66}
 67
 68xbDebug.prototype.close = function ()
 69{
 70    if (!this.debugwindow)
 71        return;
 72
 73    if (!this.debugwindow.closed)
 74        this.debugwindow.close();
 75
 76    // bc 2002-02-07, other windows may still hold a reference to this: this.debugwindow = null;
 77}
 78
 79xbDebug.prototype.dump = function (msg)
 80{
 81    if (!this.on)
 82        return;
 83
 84    if (!this.debugwindow || this.debugwindow.closed)
 85        this.open();
 86
 87    this.debugwindow.document.write(msg + '<br>');
 88
 89    return;
 90}
 91
 92var xbDEBUG = new xbDebug();
 93
 94window.onunload = function () {
 95    xbDEBUG.close();
 96}
 97
 98function xbDebugGetFunctionName(funcref)
 99{
100
101    if (!funcref)
102    {
103        return '';
104    }
105
106    if (funcref.name)
107        return funcref.name;
108
109    var name = funcref + '';
110    name = name.substring(name.indexOf(' ') + 1, name.indexOf('('));
111    funcref.name = name;
112
113    if (!name) alert('name not defined');
114    return name;
115}
116
117// emulate functionref.apply for IE mac and IE win < 5.5
118function xbDebugApplyFunction(funcname, funcref, thisref, argumentsref)
119{
120    var rv;
121
122    if (!funcref)
123    {
124        alert('xbDebugApplyFunction: funcref is null');
125    }
126
127    if (typeof(funcref.apply) != 'undefined')
128        return funcref.apply(thisref, argumentsref);
129
130    var applyexpr = 'thisref.xbDebug_orig_' + funcname + '(';
131    var i;
132
133    for (i = 0; i < argumentsref.length; i++)
134    {
135        applyexpr += 'argumentsref[' + i + '],';
136    }
137
138    if (argumentsref.length > 0)
139    {
140        applyexpr = applyexpr.substring(0, applyexpr.length - 1);
141    }
142
143    applyexpr += ')';
144
145    return eval(applyexpr);
146}
147
148function xbDebugCreateFunctionWrapper(scopename, funcname, precall, postcall)
149{
150    var wrappedfunc;
151    var scopeobject = eval(scopename);
152    var funcref = scopeobject[funcname];
153
154    scopeobject['xbDebug_orig_' + funcname] = funcref;
155
156    wrappedfunc = function ()
157    {
158        var rv;
159
160        precall(scopename, funcname, arguments);
161        rv = xbDebugApplyFunction(funcname, funcref, scopeobject, arguments);
162        postcall(scopename, funcname, arguments, rv);
163        return rv;
164    };
165
166    if (typeof(funcref.constructor) != 'undefined')
167        wrappedfunc.constructor = funcref.constuctor;
168
169    if (typeof(funcref.prototype) != 'undefined')
170        wrappedfunc.prototype = funcref.prototype;
171
172    scopeobject[funcname] = wrappedfunc;
173}
174
175function xbDebugCreateMethodWrapper(contextname, classname, methodname, precall, postcall)
176{
177    var context = eval(contextname);
178    var methodref = context[classname].prototype[methodname];
179
180    context[classname].prototype['xbDebug_orig_' + methodname] = methodref;
181
182    var wrappedmethod = function ()
183    {
184        var rv;
185        // eval 'this' at method run time to pick up reference to the object's instance
186        var thisref = eval('this');
187        // eval 'arguments' at method run time to pick up method's arguments
188        var argsref = arguments;
189
190        precall(contextname + '.' + classname, methodname, argsref);
191        rv = xbDebugApplyFunction(methodname, methodref, thisref, argsref);
192        postcall(contextname + '.' + classname, methodname, argsref, rv);
193        return rv;
194    };
195
196    return wrappedmethod;
197}
198
199function xbDebugPersistToString(obj)
200{
201    var s = '';
202    var p;
203
204    if (obj == null)
205        return 'null';
206
207    switch (typeof(obj))
208            {
209        case 'number':
210            return obj;
211        case 'string':
212            return '"' + obj + '"';
213        case 'undefined':
214            return 'undefined';
215        case 'boolean':
216            return obj + '';
217    }
218
219    if (obj.constructor)
220        return '[' + xbDebugGetFunctionName(obj.constructor) + ']';
221
222    return null;
223}
224
225function xbDebugTraceBefore(scopename, funcname, funcarguments)
226{
227    var i;
228    var s = '';
229    var execprofile = xbDEBUG.execprofile[scopename + '.' + funcname];
230    if (!execprofile)
231        execprofile = xbDEBUG.execprofile[scopename + '.' + funcname] = { started: 0, time: 0, count: 0 };
232
233    for (i = 0; i < funcarguments.length; i++)
234    {
235        s += xbDebugPersistToString(funcarguments[i]);
236        if (i < funcarguments.length - 1)
237            s += ', ';
238    }
239
240    xbDEBUG.dump('enter ' + scopename + '.' + funcname + '(' + s + ')');
241    execprofile.started = (new Date()).getTime();
242}
243
244function xbDebugTraceAfter(scopename, funcname, funcarguments, rv)
245{
246    var i;
247    var s = '';
248    var execprofile = xbDEBUG.execprofile[scopename + '.' + funcname];
249    if (!execprofile)
250        xbDEBUG.dump('xbDebugTraceAfter: execprofile not created for ' + scopename + '.' + funcname);
251    else if (execprofile.started == 0)
252        xbDEBUG.dump('xbDebugTraceAfter: execprofile.started == 0 for ' + scopename + '.' + funcname);
253    else
254    {
255        execprofile.time += (new Date()).getTime() - execprofile.started;
256        execprofile.count++;
257        execprofile.started = 0;
258    }
259
260    for (i = 0; i < funcarguments.length; i++)
261    {
262        s += xbDebugPersistToString(funcarguments[i]);
263        if (i < funcarguments.length - 1)
264            s += ', ';
265    }
266
267    xbDEBUG.dump('exit  ' + scopename + '.' + funcname + '(' + s + ')==' + xbDebugPersistToString(rv));
268}
269
270function xbDebugTraceFunction(scopename, funcname)
271{
272    xbDebugCreateFunctionWrapper(scopename, funcname, xbDebugTraceBefore, xbDebugTraceAfter);
273}
274
275function xbDebugTraceObject(contextname, classname)
276{
277    var classref = eval(contextname + '.' + classname);
278    var p;
279    var sp;
280
281    if (!classref || !classref.prototype)
282        return;
283
284    for (p in classref.prototype)
285    {
286        sp = p + '';
287        if (typeof(classref.prototype[sp]) == 'function' && (sp).indexOf('xbDebug_orig') == -1)
288        {
289            classref.prototype[sp] = xbDebugCreateMethodWrapper(contextname, classname, sp, xbDebugTraceBefore, xbDebugTraceAfter);
290        }
291    }
292}
293
294function xbDebugDumpProfile()
295{
296    var p;
297    var execprofile;
298    var avg;
299
300    for (p in xbDEBUG.execprofile)
301    {
302        execprofile = xbDEBUG.execprofile[p];
303        avg = Math.round(100 * execprofile.time / execprofile.count) / 100;
304        xbDEBUG.dump('Execution profile ' + p + ' called ' + execprofile.count + ' times. Total time=' + execprofile.time + 'ms. Avg Time=' + avg + 'ms.');
305    }
306}