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