-
Notifications
You must be signed in to change notification settings - Fork 5
/
Debug.js
92 lines (76 loc) · 2.48 KB
/
Debug.js
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
function debug_determineName( obj )
{
if( obj.name )
return obj.name;
if( obj.prototype && obj.prototype.name )
return obj.prototype.name;
if( obj.constructor && obj.constructor.name )
return obj.constructor.name;
return "UNKNOWN";
}
function log_write(filename, text)
{
var path = Host.Url("local://$USERCONTENT/debug/" + filename + ".txt");
let textFile = Host.IO.createTextFile(path);
if (textFile)
{
textFile.writeLine(text);
textFile.close();
}
}
function log_param_names(func)
{
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if(result === null)
result = "No Params";
else
result = result.join(" | ");
Host.Console.writeLine(result);
}
function list_methods(obj)
{
for( let method in Object.getOwnPropertyNames(obj) )
Host.Console.writeLine("method: "+method);
}
function log_obj( obj, skipValues )
{
if( obj == null )
{
Host.Console.writeLine ("DEBUG OBJ: Undefined");
return;
}
if( typeof obj == 'string' || typeof obj == 'number')
return Host.Console.writeLine( "DEBUG: ("+ typeof obj+") " + String(obj) );
Host.Console.writeLine( "DEBUG: " + debug_determineName(obj) + " (" + typeof obj + ") {" );
if( skipValues )
Host.Console.writeLine( String(obj.toSource()) );
do {
let prop = Object.getOwnPropertyNames(obj);
for( let key in prop )
{
if( ! obj.hasOwnProperty(prop[key]) || skipValues )
{
Host.Console.writeLine( " " + prop[key] );
continue;
}
let descriptor = Object.getOwnPropertyDescriptor(obj, prop[key]);
let value;
if( descriptor.value == null ) {
value = "null";
} else if( typeof descriptor.value == "function" ) {
value = descriptor.value.toSource();
} else {
value = "["+typeof descriptor.value+"] " + descriptor.value.toString().replace((/ |\r\n|\n|\r/gm),"");
}
Host.Console.writeLine( " " + prop[key] + ": " + value );
}
} while (obj = Object.getPrototypeOf(obj));
Host.Console.writeLine("}");
}
function log( obj, skipValues )
{
log_obj(obj, skipValues);
}