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