duk_console.c

filter: Statementstmt 89.7% 79 / 88 Branchbran 54.1% 13 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.1% 92 / 112 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« duk.xs ↑ Summary
1 /*
2 * Minimal 'console' binding.
3 *
4 * https://github.com/DeveloperToolsWG/console-object/blob/master/api.md
5 * https://developers.google.com/web/tools/chrome-devtools/debug/console/console-reference
6 * https://developer.mozilla.org/en/docs/Web/API/console
7 */
8  
9 #include <stdio.h>
10 #include "util.h"
11 #include "duktape.h"
12 #include "duk_console.h"
13  
14 /* XXX: Add some form of log level filtering. */
15  
16 /* XXX: Should all output be written via e.g. console.write(formattedMsg)?
17 * This would make it easier for user code to redirect all console output
18 * to a custom backend.
19 */
20  
21 /* XXX: Init console object using duk_def_prop() when that call is available. */
22  
23 typedef struct ConsoleConfig {
24 ConsoleHandler* handler;
25 void* data;
26 } ConsoleConfig;
27  
28 static ConsoleConfig console_config;
29  
30 31 int duk_console_log(duk_uint_t flags, const char* fmt, ...)
31 {
32 31 int ret = 0;
33 31 ▶ if (console_config.handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (console_config.handler) { 31 0 31
34 va_list ap;
35 31 va_start(ap, fmt);
36 31 ret = console_config.handler(flags, console_config.data, fmt, ap);
37 31 va_end(ap);
38 }
39 31 return ret;
40 }
41  
42 2015 void duk_console_register_handler(ConsoleHandler* handler, void* data)
43 {
44 2015 console_config.handler = handler;
45 2015 console_config.data = data;
46 2015 }
47  
48 20 static duk_ret_t duk__console_log_helper(duk_context *ctx, const char *error_name) {
49 20 duk_uint_t flags = (duk_uint_t) duk_get_current_magic(ctx);
50 20 duk_idx_t n = duk_get_top(ctx);
51 duk_idx_t i;
52  
53 20 duk_get_global_string(ctx, "console");
54 20 duk_get_prop_string(ctx, -1, "format");
55  
56 61 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 41 20 61
57 41 ▶ if (duk_check_type_mask(ctx, i, DUK_TYPE_MASK_OBJECT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (duk_check_type_mask(ctx, i, DUK_TYPE_MASK_OBJECT)) { 14 27 41
58 /* Slow path formatting. */
59 14 duk_dup(ctx, -1); /* console.format */
60 14 duk_dup(ctx, i);
61 14 duk_call(ctx, 1);
62 14 duk_replace(ctx, i); /* arg[i] = console.format(arg[i]); */
63 }
64 }
65  
66 20 duk_pop_2(ctx);
67  
68 20 duk_push_string(ctx, " ");
69 20 duk_insert(ctx, 0);
70 20 duk_join(ctx, n);
71  
72 20 ▶ if (error_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (error_name) { 6 14 20
73 6 duk_push_error_object(ctx, DUK_ERR_ERROR, "%s", duk_require_string(ctx, -1));
74 6 duk_push_string(ctx, "name");
75 6 duk_push_string(ctx, error_name);
76 6 duk_def_prop(ctx, -3, DUK_DEFPROP_FORCE | DUK_DEFPROP_HAVE_VALUE); /* to get e.g. 'Trace: 1 2 3' */
77 6 duk_get_prop_string(ctx, -1, "stack");
78 }
79  
80 20 duk_console_log(flags, "%s\n", duk_to_string(ctx, -1));
81 20 return 0;
82 }
83  
84 0 static duk_ret_t duk__console_assert(duk_context *ctx) {
85 0 ▶ if (duk_to_boolean(ctx, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (duk_to_boolean(ctx, 0)) { 0 0 0
86 0 return 0;
87 }
88 0 duk_remove(ctx, 0);
89  
90 0 return duk__console_log_helper(ctx, "AssertionError");
91 }
92  
93 10 static duk_ret_t duk__console_log(duk_context *ctx) {
94 10 return duk__console_log_helper(ctx, NULL);
95 }
96  
97 2 static duk_ret_t duk__console_trace(duk_context *ctx) {
98 2 return duk__console_log_helper(ctx, "Trace");
99 }
100  
101 2 static duk_ret_t duk__console_info(duk_context *ctx) {
102 2 return duk__console_log_helper(ctx, NULL);
103 }
104  
105 2 static duk_ret_t duk__console_warn(duk_context *ctx) {
106 2 return duk__console_log_helper(ctx, NULL);
107 }
108  
109 4 static duk_ret_t duk__console_error(duk_context *ctx) {
110 4 return duk__console_log_helper(ctx, "Error");
111 }
112  
113 0 static duk_ret_t duk__console_dir(duk_context *ctx) {
114 /* For now, just share the formatting of .log() */
115 0 return duk__console_log_helper(ctx, 0);
116 }
117  
118 18135 static void duk__console_reg_vararg_func(duk_context *ctx, duk_c_function func, const char *name, duk_uint_t flags) {
119 18135 duk_push_c_function(ctx, func, DUK_VARARGS);
120 18135 duk_push_string(ctx, "name");
121 18135 duk_push_string(ctx, name);
122 18135 duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE); /* Improve stacktraces by displaying function name */
123 18135 duk_set_magic(ctx, -1, (duk_int_t) flags);
124 18135 duk_put_prop_string(ctx, -2, name);
125 18135 }
126  
127 2015 void duk_console_init(duk_context *ctx, duk_uint_t flags) {
128 duk_uint_t flags_orig;
129  
130 /* If both DUK_CONSOLE_TO_STDOUT and DUK_CONSOLE_TO_STDERR where specified,
131 * just turn off DUK_CONSOLE_TO_STDOUT and keep DUK_CONSOLE_TO_STDERR. */
132 2015 ▶ if ((flags & DUK_CONSOLE_TO_STDOUT) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & DUK_CONSOLE_TO_STDOUT) && 0 2015 2015
133 0 ▶ (flags & DUK_CONSOLE_TO_STDERR)) {
Branch: 1 branch
BranchTrueFalseTotal
(flags & DUK_CONSOLE_TO_STDERR)) { 0 0 0
134 0 flags &= ~DUK_CONSOLE_TO_STDOUT;
135 }
136 /* Remember the (possibly corrected) flags we received. */
137 2015 flags_orig = flags;
138  
139 2015 duk_push_object(ctx);
140  
141 /* Custom function to format objects; user can replace.
142 * For now, try JX-formatting and if that fails, fall back
143 * to ToString(v).
144 */
145 2015 duk_eval_string(ctx,
146 "(function (E) {"
147 "return function format(v){"
148 "try{"
149 "return E('jx',v);"
150 "}catch(e){"
151 "return String(v);" /* String() allows symbols, ToString() internal algorithm doesn't. */
152 "}"
153 "};"
154 "})(Duktape.enc)");
155 2015 duk_put_prop_string(ctx, -2, "format");
156  
157 2015 flags = flags_orig;
158 2015 ▶ if (!(flags & DUK_CONSOLE_TO_STDOUT) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & DUK_CONSOLE_TO_STDOUT) && 2015 0 2015
159 2015 ▶ !(flags & DUK_CONSOLE_TO_STDERR)) {
Branch: 1 branch
BranchTrueFalseTotal
!(flags & DUK_CONSOLE_TO_STDERR)) { 2015 0 2015
160 /* No output indicators were specified; these levels go to stdout. */
161 2015 flags |= DUK_CONSOLE_TO_STDOUT;
162 }
163 2015 duk__console_reg_vararg_func(ctx, duk__console_assert, "assert", flags);
164 2015 duk__console_reg_vararg_func(ctx, duk__console_log, "log", flags);
165 2015 duk__console_reg_vararg_func(ctx, duk__console_log, "debug", flags); /* alias to console.log */
166 2015 duk__console_reg_vararg_func(ctx, duk__console_trace, "trace", flags);
167 2015 duk__console_reg_vararg_func(ctx, duk__console_info, "info", flags);
168  
169 2015 flags = flags_orig;
170 2015 ▶ if (!(flags & DUK_CONSOLE_TO_STDOUT) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & DUK_CONSOLE_TO_STDOUT) && 2015 0 2015
171 2015 ▶ !(flags & DUK_CONSOLE_TO_STDERR)) {
Branch: 1 branch
BranchTrueFalseTotal
!(flags & DUK_CONSOLE_TO_STDERR)) { 2015 0 2015
172 /* No output indicators were specified; these levels go to stderr. */
173 2015 flags |= DUK_CONSOLE_TO_STDERR;
174 }
175 2015 duk__console_reg_vararg_func(ctx, duk__console_warn, "warn", flags);
176 2015 duk__console_reg_vararg_func(ctx, duk__console_error, "error", flags);
177 2015 duk__console_reg_vararg_func(ctx, duk__console_error, "exception", flags); /* alias to console.error */
178 2015 duk__console_reg_vararg_func(ctx, duk__console_dir, "dir", flags);
179  
180 2015 duk_put_global_string(ctx, "console");
181  
182 /* Proxy wrapping: ensures any undefined console method calls are
183 * ignored silently. This is required specifically by the
184 * DeveloperToolsWG proposal (and is implemented also by Firefox:
185 * https://bugzilla.mozilla.org/show_bug.cgi?id=629607).
186 */
187  
188 2015 ▶ if (flags & DUK_CONSOLE_PROXY_WRAPPER) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & DUK_CONSOLE_PROXY_WRAPPER) { 2015 0 2015
189 /* Tolerate errors: Proxy may be disabled. */
190 2015 duk_peval_string_noresult(ctx,
191 "(function(){"
192 "var D=function(){};"
193 "console=new Proxy(console,{"
194 "get:function(t,k){"
195 "var v=t[k];"
196 "return typeof v==='function'?v:D;"
197 "}"
198 "});"
199 "})();"
200 );
201 }
202 2015 }
« duk.xs ↑ Summary