c598eedcc79b83dbc4c1ea50d24ac713beb77ae9
[euphorik.git] / js / debug.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18
19 /**
20 * Affiche un objet quelconque sur la sortie du navigateur.
21 */
22 var dumpObj = function(obj, name)
23 {
24 if (typeof(dump) == "undefined")
25 return
26
27 dump("---" + (name == undefined ? "" : " : " + name) + "\n")
28 dump(obj2text(obj))
29 dump("\n---\n")
30 }
31
32 var obj2text = function(obj, curDepth)
33 {
34 if (curDepth == undefined)
35 curDepth = 0;
36
37 var acc = ""
38
39 if (obj == undefined)
40 {
41 acc += "<undefined>"
42 }
43 else if (typeof(obj) == "string")
44 {
45 acc += "\"" + obj + "\""
46 }
47 else if (obj.length != undefined) // array
48 {
49 acc += "["
50
51 var i = 0
52 for (; i < obj.length; i++)
53 {
54 if (i != 0) acc += ","
55 acc += "\n" + indent(curDepth + 1, obj2text(obj[i], curDepth + 1))
56 }
57
58 acc += (i == 0 ? "]" : "\n" + indent(curDepth, "]"))
59 }
60 else if (typeof(obj) == "object")
61 {
62 acc += "{"
63 var i = 0
64 for (prop in obj)
65 {
66 if (i != 0) acc += ","
67 acc += "\n" + indent(curDepth + 1, prop + " : " + obj2text(obj[prop], curDepth + 1))
68 i += 1
69 }
70 acc += "\n" + indent(curDepth, "}")
71 }
72 else if (typeof(obj) == "function")
73 {
74 acc += "<function>"
75 }
76 else // value
77 {
78 acc += obj
79 }
80
81 return acc
82 }
83
84 var indent = function(depth, text)
85 {
86 var indentText = ""
87 for (var i = 0; i < depth * 3; i++)
88 indentText += " "
89 return indentText + text
90 }