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.

remove duplicate code

+28 -54
+5
include/internal.h
··· 247 247 return v == T_EMPTY; 248 248 } 249 249 250 + static inline bool is_callable(ant_value_t v) { 251 + uint8_t t = vtype(v); 252 + return t == T_FUNC || t == T_CFUNC; 253 + } 254 + 250 255 bool is_internal_prop(const char *key, ant_offset_t klen); 251 256 size_t uint_to_str(char *buf, size_t bufsize, uint64_t val); 252 257
-5
src/ant.c
··· 6393 6393 return js_call_toString(js, js->this_val); 6394 6394 } 6395 6395 6396 - static inline bool is_callable(ant_value_t v) { 6397 - uint8_t t = vtype(v); 6398 - return t == T_FUNC || t == T_CFUNC; 6399 - } 6400 - 6401 6396 static inline ant_value_t require_callback(ant_t *js, ant_value_t *args, int nargs, const char *name) { 6402 6397 if (nargs == 0 || !is_callable(args[0])) 6403 6398 return js_mkerr(js, "%s requires a function argument", name);
+3 -8
src/modules/async_hooks.c
··· 11 11 #include "modules/async_hooks.h" 12 12 #include "modules/symbol.h" 13 13 14 - static inline bool async_hooks_is_callable(ant_value_t v) { 15 - uint8_t t = vtype(v); 16 - return t == T_FUNC || t == T_CFUNC; 17 - } 18 - 19 14 static ant_value_t async_hooks_call_with_tail_args( 20 15 ant_t *js, ant_value_t fn, ant_value_t this_arg, ant_value_t *args, int nargs, int start_idx 21 16 ) { ··· 32 27 } 33 28 34 29 static ant_value_t async_local_storage_run(ant_t *js, ant_value_t *args, int nargs) { 35 - if (nargs < 2 || !async_hooks_is_callable(args[1])) { 30 + if (nargs < 2 || !is_callable(args[1])) { 36 31 return js_mkerr(js, "AsyncLocalStorage.run(store, callback, ...args) requires a callback"); 37 32 } 38 33 ··· 49 44 } 50 45 51 46 static ant_value_t async_local_storage_exit(ant_t *js, ant_value_t *args, int nargs) { 52 - if (nargs < 1 || !async_hooks_is_callable(args[0])) { 47 + if (nargs < 1 || !is_callable(args[0])) { 53 48 return js_mkerr(js, "AsyncLocalStorage.exit(callback, ...args) requires a callback"); 54 49 } 55 50 ··· 89 84 } 90 85 91 86 static ant_value_t async_resource_runInAsyncScope(ant_t *js, ant_value_t *args, int nargs) { 92 - if (nargs < 1 || !async_hooks_is_callable(args[0])) { 87 + if (nargs < 1 || !is_callable(args[0])) { 93 88 return js_mkerr(js, "AsyncResource.runInAsyncScope(fn[, thisArg, ...args]) requires a function"); 94 89 } 95 90 ant_value_t this_arg = nargs > 1 ? args[1] : js_mkundef();
+5 -10
src/modules/napi.c
··· 226 226 return (napi_env)napi_get_or_create_env(js); 227 227 } 228 228 229 - static inline bool napi_is_callable(ant_value_t v) { 230 - uint8_t t = vtype(v); 231 - return t == T_FUNC || t == T_CFUNC; 232 - } 233 - 234 229 static void napi_mark_pending_exception(napi_env env, napi_value exception) { 235 230 ant_napi_env_t *nenv = (ant_napi_env_t *)env; 236 231 if (!nenv) return; ··· 499 494 ant_value_t cb = tsfn->func_val; 500 495 if (tsfn->call_js_cb) { 501 496 tsfn->call_js_cb((napi_env)tsfn->env, (napi_value)cb, tsfn->context, item->data); 502 - } else if (napi_is_callable(cb)) { 497 + } else if (is_callable(cb)) { 503 498 sv_vm_call(js->vm, js, cb, js_mkundef(), NULL, 0, NULL, false); 504 499 } 505 500 ··· 629 624 ant_value_t process_obj = js_get(js, js_glob(js), "process"); 630 625 ant_value_t dlopen_fn = is_object_type(process_obj) ? js_get(js, process_obj, "dlopen") : js_mkundef(); 631 626 632 - if (napi_is_callable(dlopen_fn)) { 627 + if (is_callable(dlopen_fn)) { 633 628 ant_value_t argv[2] = {module_obj, js_mkstr(js, module_path, strlen(module_path))}; 634 629 ant_value_t dl_res = sv_vm_call(js->vm, js, dlopen_fn, process_obj, argv, 2, NULL, false); 635 630 if (is_err(dl_res) || js->thrown_exists) return js_throw(js, js->thrown_value); ··· 1973 1968 napi_value *result 1974 1969 ) { 1975 1970 ant_napi_env_t *nenv = (ant_napi_env_t *)env; 1976 - if (!nenv || !nenv->js || !napi_is_callable((ant_value_t)func)) { 1971 + if (!nenv || !nenv->js || !is_callable((ant_value_t)func)) { 1977 1972 return napi_set_last(env, napi_invalid_arg, "invalid argument"); 1978 1973 } 1979 1974 ··· 2001 1996 napi_value *result 2002 1997 ) { 2003 1998 ant_napi_env_t *nenv = (ant_napi_env_t *)env; 2004 - if (!nenv || !nenv->js || !result || !napi_is_callable((ant_value_t)constructor)) { 1999 + if (!nenv || !nenv->js || !result || !is_callable((ant_value_t)constructor)) { 2005 2000 return napi_set_last(env, napi_invalid_arg, "invalid argument"); 2006 2001 } 2007 2002 ··· 2065 2060 } 2066 2061 2067 2062 ant_value_t obj_ctor = js_get(nenv->js, js_glob(nenv->js), "Object"); 2068 - if (!napi_is_callable(obj_ctor)) return napi_set_last(env, napi_generic_failure, "Object constructor missing"); 2063 + if (!is_callable(obj_ctor)) return napi_set_last(env, napi_generic_failure, "Object constructor missing"); 2069 2064 ant_value_t arg = (ant_value_t)value; 2070 2065 ant_value_t out = sv_vm_call(nenv->js->vm, nenv->js, obj_ctor, js_mkundef(), &arg, 1, NULL, false); 2071 2066 if (is_err(out) || nenv->js->thrown_exists) return napi_check_pending_from_result(env, out);
+1 -4
src/modules/observable.c
··· 11 11 #include "modules/symbol.h" 12 12 #include "modules/observable.h" 13 13 14 - static inline bool is_callable(ant_value_t val) { 15 - uint8_t t = vtype(val); 16 - return t == T_FUNC || t == T_CFUNC; 17 - } 14 + 18 15 19 16 static bool subscription_closed(ant_t *js, ant_value_t subscription) { 20 17 ant_value_t observer = js_get_slot(subscription, SLOT_SUBSCRIPTION_OBSERVER);
+1 -4
src/modules/tty.c
··· 37 37 #include "modules/symbol.h" 38 38 #include "modules/tty.h" 39 39 40 - static inline bool is_callable(ant_value_t value) { 41 - uint8_t t = vtype(value); 42 - return t == T_FUNC || t == T_CFUNC; 43 - } 40 + 44 41 45 42 static bool parse_fd(ant_value_t value, int *fd_out) { 46 43 int fd = 0;
+2 -7
src/modules/util.c
··· 56 56 {"bgWhite", "\x1b[47m", "\x1b[49m"}, 57 57 }; 58 58 59 - static inline bool util_is_callable(ant_value_t v) { 60 - uint8_t t = vtype(v); 61 - return t == T_FUNC || t == T_CFUNC; 62 - } 63 - 64 59 static bool util_sb_reserve(util_sb_t *sb, size_t extra) { 65 60 size_t need = sb->len + extra + 1; 66 61 if (need <= sb->cap) return true; ··· 348 343 static ant_value_t util_promisified_call(ant_t *js, ant_value_t *args, int nargs) { 349 344 ant_value_t fn = js_getcurrentfunc(js); 350 345 ant_value_t original = js_get_slot(fn, SLOT_DATA); 351 - if (!util_is_callable(original)) return js_mkerr(js, "promisified target is not callable"); 346 + if (!is_callable(original)) return js_mkerr(js, "promisified target is not callable"); 352 347 353 348 ant_value_t promise = js_mkpromise(js); 354 349 ant_value_t ctx = js_mkobj(js); ··· 385 380 } 386 381 387 382 static ant_value_t util_promisify(ant_t *js, ant_value_t *args, int nargs) { 388 - if (nargs < 1 || !util_is_callable(args[0])) { 383 + if (nargs < 1 || !is_callable(args[0])) { 389 384 return js_mkerr(js, "promisify(fn) requires a function"); 390 385 } 391 386 return js_heavy_mkfun(js, util_promisified_call, args[0]);
+11 -16
src/modules/worker_threads.c
··· 61 61 62 62 static ant_worker_thread_t *active_workers_head = NULL; 63 63 64 - static inline bool wt_is_callable(ant_value_t v) { 65 - uint8_t t = vtype(v); 66 - return t == T_FUNC || t == T_CFUNC; 67 - } 68 - 69 64 static bool wt_is_worker_mode(void) { 70 65 const char *mode = getenv(WT_ENV_MODE); 71 66 return mode && strcmp(mode, "1") == 0; ··· 181 176 } 182 177 183 178 static void wt_port_call_listener(ant_t *js, ant_value_t this_obj, ant_value_t fn, ant_value_t arg) { 184 - if (!wt_is_callable(fn)) return; 179 + if (!is_callable(fn)) return; 185 180 ant_value_t argv[1] = {arg}; 186 181 187 182 sv_vm_call(js->vm, js, fn, this_obj, argv, 1, NULL, false); ··· 194 189 ant_value_t once_fn = js_get_slot(port, SLOT_WT_PORT_ONCE_MESSAGE); 195 190 ant_value_t onmessage = js_get(js, port, "onmessage"); 196 191 197 - bool has_event_listener = wt_is_callable(on_fn) || wt_is_callable(once_fn); 198 - if (wt_is_callable(onmessage)) return true; 192 + bool has_event_listener = is_callable(on_fn) || is_callable(once_fn); 193 + if (is_callable(onmessage)) return true; 199 194 return started && has_event_listener; 200 195 } 201 196 ··· 210 205 wt_port_call_listener(js, port, on_fn, msg); 211 206 212 207 ant_value_t once_fn = js_get_slot(port, SLOT_WT_PORT_ONCE_MESSAGE); 213 - if (wt_is_callable(once_fn)) { 208 + if (is_callable(once_fn)) { 214 209 wt_port_call_listener(js, port, once_fn, msg); 215 210 js_set_slot(port, SLOT_WT_PORT_ONCE_MESSAGE, js_mkundef()); 216 211 } 217 212 218 213 ant_value_t onmessage = js_get(js, port, "onmessage"); 219 - if (wt_is_callable(onmessage)) { 214 + if (is_callable(onmessage)) { 220 215 ant_value_t event_obj = js_mkobj(js); 221 216 js_set(js, event_obj, "data", msg); 222 217 wt_port_call_listener(js, port, onmessage, event_obj); ··· 233 228 } 234 229 235 230 static void wt_call_listener(ant_t *js, ant_value_t this_obj, ant_value_t fn, ant_value_t arg) { 236 - if (!wt_is_callable(fn)) return; 231 + if (!is_callable(fn)) return; 237 232 ant_value_t argv[1] = {arg}; 238 233 239 234 sv_vm_call(js->vm, js, fn, this_obj, argv, 1, NULL, false); ··· 258 253 wt_call_listener(js, this_obj, on_fn, arg); 259 254 260 255 ant_value_t once_fn = js_get_slot(this_obj, once_slot); 261 - if (wt_is_callable(once_fn)) { 256 + if (is_callable(once_fn)) { 262 257 wt_call_listener(js, this_obj, once_fn, arg); 263 258 js_set_slot(this_obj, once_slot, js_mkundef()); 264 259 } ··· 579 574 } 580 575 581 576 static ant_value_t worker_threads_worker_on(ant_t *js, ant_value_t *args, int nargs) { 582 - if (nargs < 2 || vtype(args[0]) != T_STR || !wt_is_callable(args[1])) { 577 + if (nargs < 2 || vtype(args[0]) != T_STR || !is_callable(args[1])) { 583 578 return js_mkerr(js, "Worker.on(event, listener) requires (string, function)"); 584 579 } 585 580 ··· 600 595 } 601 596 602 597 static ant_value_t worker_threads_worker_once(ant_t *js, ant_value_t *args, int nargs) { 603 - if (nargs < 2 || vtype(args[0]) != T_STR || !wt_is_callable(args[1])) { 598 + if (nargs < 2 || vtype(args[0]) != T_STR || !is_callable(args[1])) { 604 599 return js_mkerr(js, "Worker.once(event, listener) requires (string, function)"); 605 600 } 606 601 ··· 688 683 } 689 684 690 685 static ant_value_t worker_threads_message_port_on(ant_t *js, ant_value_t *args, int nargs) { 691 - if (nargs < 2 || vtype(args[0]) != T_STR || !wt_is_callable(args[1])) { 686 + if (nargs < 2 || vtype(args[0]) != T_STR || !is_callable(args[1])) { 692 687 return js_mkerr(js, "MessagePort.on(event, listener) requires (string, function)"); 693 688 } 694 689 ant_value_t this_obj = js_getthis(js); ··· 706 701 } 707 702 708 703 static ant_value_t worker_threads_message_port_once(ant_t *js, ant_value_t *args, int nargs) { 709 - if (nargs < 2 || vtype(args[0]) != T_STR || !wt_is_callable(args[1])) { 704 + if (nargs < 2 || vtype(args[0]) != T_STR || !is_callable(args[1])) { 710 705 return js_mkerr(js, "MessagePort.once(event, listener) requires (string, function)"); 711 706 } 712 707 ant_value_t this_obj = js_getthis(js);