-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdebug.c
177 lines (157 loc) · 4.5 KB
/
debug.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* debug.c -- debugging functions
*
* Copyright (c) 2012-2017 Nick Reynolds
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <math.h>
#include "debug.h"
#include "props.h"
#include "cli.h"
// ----------------------------------------------------------------------------
// Value Debugging
// ----------------------------------------------------------------------------
static void
debug_obj(FILE *stream, js_val *obj, int indent, bool force_enum)
{
js_prop *x;
bool first = true;
OBJ_ITER(obj, x) {
if (!x->enumerable && !force_enum) continue;
if (first) {
if (indent) fprintf(stream, "%*s", indent, " ");
fprintf(stream, "{");
first = false;
}
else {
fprintf(stream, ",\n");
fprintf(stream, "%*s", indent + 1, " ");
}
fprintf(stream, " %s: ", x->name);
if (x->circular)
cfprintf(stream, ANSI_BLUE, "[Circular]");
else
fh_debug(stream, x->ptr, indent + 3, false);
};
fprintf(stream, first ? "{}" : " }");
}
static void
debug_arr(FILE *stream, js_val *arr, int indent)
{
if (arr->object.length == 0) {
fprintf(stream, "[]");
return;
}
fprintf(stream, "[ ");
bool first = true;
js_prop *prop;
unsigned long i;
for (i = 0; i < arr->object.length; i++) {
prop = fh_get_prop(arr, JSNUMKEY(i)->string.ptr);
if (!first)
fprintf(stream, ", ");
else
first = false;
if (!prop) continue;
if (prop->circular)
cfprintf(stream, ANSI_BLUE, "[Circular]");
else
fh_debug(stream, prop->ptr, 0, false);
}
fprintf(stream, " ]");
}
static void
debug_num(FILE *stream, js_val *num)
{
if (num->number.is_nan)
cfprintf(stream, ANSI_ORANGE, "NaN");
else if (num->number.is_inf)
cfprintf(stream, ANSI_ORANGE, "%sInfinity", num->number.is_neg ? "-" : "");
else {
char *fmt = "%f";
if (fmod(num->number.val, 1) == 0)
fmt = "%.0f";
if (fabs(num->number.val) > 1e21)
fmt = "%g";
cfprintf(stream, ANSI_ORANGE, fmt, num->number.val);
}
}
void
fh_debug(FILE *stream, js_val *val, int indent, bool newline)
{
switch (val->type) {
case T_BOOLEAN:
fprintf(stream, "%s", !val->boolean.val ? "false" : "true");
break;
case T_NUMBER:
debug_num(stream, val);
break;
case T_STRING:
if (fh->opt_interactive)
cfprintf(stream, ANSI_YELLOW, "'%s'", val->string.ptr);
else
fprintf(stream, "%s", val->string.ptr);
break;
case T_NULL:
cfprintf(stream, ANSI_GRAY, "null");
break;
case T_UNDEF:
cfprintf(stream, ANSI_GRAY, "undefined");
break;
case T_OBJECT:
if (IS_ARR(val))
debug_arr(stream, val, indent);
else if (IS_FUNC(val))
cfprintf(stream, ANSI_BLUE, "[Function]");
else if (IS_DATE(val))
fprintf(stream, "[Date %ld]", (long)val->object.primitive->number.val);
else
debug_obj(stream, val, indent, false);
break;
}
if (newline) fprintf(stream, "\n");
}
// Debug an object with extra verbosity, displaying non-enumerable properties.
void
fh_debug_verbose(FILE *stream, js_val *val, int indent)
{
switch (val->type) {
case T_BOOLEAN:
fprintf(stream, "Boolean: (%s)", !val->boolean.val ? "false" : "true");
break;
case T_NUMBER:
debug_num(stream, val);
break;
case T_STRING:
cfprintf(stream, ANSI_YELLOW, "String: '%s'", val->string.ptr);
break;
case T_NULL:
cfprintf(stream, ANSI_GRAY, "null");
break;
case T_UNDEF:
cfprintf(stream, ANSI_GRAY, "undefined");
break;
case T_OBJECT:
if (IS_ARR(val))
fprintf(stream, "Array: ");
else if (IS_FUNC(val))
cfprintf(stream, ANSI_BLUE, "Function: ");
else
fprintf(stream, "Object: ");
break;
}
if (IS_OBJ(val))
debug_obj(stream, val, indent, true);
fprintf(stream, "\n");
}