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