MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

proper console methods

+75 -1
+1 -1
meson.build
··· 74 74 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 75 75 76 76 version_conf = configuration_data() 77 - version_conf.set('ANT_VERSION', '0.1.0.24') 77 + version_conf.set('ANT_VERSION', '0.1.0.25') 78 78 version_conf.set('ANT_GIT_HASH', git_hash) 79 79 version_conf.set('ANT_BUILD_DATE', build_date) 80 80
+74
src/modules/io.c
··· 4 4 #include <stdbool.h> 5 5 #include <ctype.h> 6 6 #include <libgen.h> 7 + #include <uv.h> 7 8 8 9 #include "runtime.h" 9 10 #include "modules/io.h" 10 11 11 12 #define ANSI_RED "\x1b[31m" 12 13 #define ANSI_YELLOW "\x1b[33m" 14 + #define ANSI_CYAN "\x1b[36m" 15 + #define ANSI_MAGENTA "\x1b[35m" 13 16 #define ANSI_RESET "\x1b[0m" 14 17 15 18 #define JSON_KEY "\x1b[0m" ··· 261 264 return js_mkundef(); 262 265 } 263 266 267 + static jsval_t js_console_info(struct js *js, jsval_t *args, int nargs) { 268 + console_print(js, args, nargs, ANSI_CYAN, stdout); 269 + return js_mkundef(); 270 + } 271 + 272 + static jsval_t js_console_debug(struct js *js, jsval_t *args, int nargs) { 273 + console_print(js, args, nargs, ANSI_MAGENTA, stdout); 274 + return js_mkundef(); 275 + } 276 + 277 + static jsval_t js_console_clear(struct js *js, jsval_t *args, int nargs) { 278 + (void)args; 279 + (void)nargs; 280 + fprintf(stdout, "\033[2J\033[H"); 281 + fflush(stdout); 282 + return js_mkundef(); 283 + } 284 + 285 + static struct { char *label; double start_time; } console_timers[64]; 286 + static int console_timer_count = 0; 287 + 288 + static jsval_t js_console_time(struct js *js, jsval_t *args, int nargs) { 289 + const char *label = "default"; 290 + if (nargs > 0 && js_type(args[0]) == JS_STR) { 291 + label = js_getstr(js, args[0], NULL); 292 + } 293 + 294 + for (int i = 0; i < console_timer_count; i++) { 295 + if (strcmp(console_timers[i].label, label) == 0) { 296 + fprintf(stderr, "Timer '%s' already exists\n", label); 297 + return js_mkundef(); 298 + } 299 + } 300 + 301 + if (console_timer_count < 64) { 302 + console_timers[console_timer_count].label = strdup(label); 303 + console_timers[console_timer_count].start_time = uv_hrtime() / 1e6; 304 + console_timer_count++; 305 + } 306 + 307 + return js_mkundef(); 308 + } 309 + 310 + static jsval_t js_console_timeEnd(struct js *js, jsval_t *args, int nargs) { 311 + const char *label = "default"; 312 + if (nargs > 0 && js_type(args[0]) == JS_STR) { 313 + label = js_getstr(js, args[0], NULL); 314 + } 315 + 316 + for (int i = 0; i < console_timer_count; i++) { 317 + if (strcmp(console_timers[i].label, label) == 0) { 318 + double elapsed = (uv_hrtime() / 1e6) - console_timers[i].start_time; 319 + fprintf(stdout, "%s: %.3fms\n", label, elapsed); 320 + free(console_timers[i].label); 321 + for (int j = i; j < console_timer_count - 1; j++) { 322 + console_timers[j] = console_timers[j + 1]; 323 + } 324 + console_timer_count--; 325 + return js_mkundef(); 326 + } 327 + } 328 + 329 + fprintf(stderr, "Timer '%s' does not exist\n", label); 330 + return js_mkundef(); 331 + } 332 + 264 333 void init_console_module() { 265 334 struct js *js = rt->js; 266 335 jsval_t console_obj = js_mkobj(js); ··· 268 337 js_set(js, console_obj, "log", js_mkfun(js_console_log)); 269 338 js_set(js, console_obj, "error", js_mkfun(js_console_error)); 270 339 js_set(js, console_obj, "warn", js_mkfun(js_console_warn)); 340 + js_set(js, console_obj, "info", js_mkfun(js_console_info)); 341 + js_set(js, console_obj, "debug", js_mkfun(js_console_debug)); 271 342 js_set(js, console_obj, "assert", js_mkfun(js_console_assert)); 272 343 js_set(js, console_obj, "trace", js_mkfun(js_console_trace)); 344 + js_set(js, console_obj, "time", js_mkfun(js_console_time)); 345 + js_set(js, console_obj, "timeEnd", js_mkfun(js_console_timeEnd)); 346 + js_set(js, console_obj, "clear", js_mkfun(js_console_clear)); 273 347 274 348 js_set(js, console_obj, "@@toStringTag", js_mkstr(js, "console", 7)); 275 349 js_set(js, js_glob(js), "console", console_obj);