diff --git a/include/internal.h b/include/internal.h index 2ecf986..a865d02 100644 --- a/include/internal.h +++ b/include/internal.h @@ -247,6 +247,11 @@ static inline bool is_empty_slot(ant_value_t v) { return v == T_EMPTY; } +static inline bool is_callable(ant_value_t v) { + uint8_t t = vtype(v); + return t == T_FUNC || t == T_CFUNC; +} + bool is_internal_prop(const char *key, ant_offset_t klen); size_t uint_to_str(char *buf, size_t bufsize, uint64_t val); diff --git a/src/ant.c b/src/ant.c index 7e7b141..46bfcb4 100644 --- a/src/ant.c +++ b/src/ant.c @@ -6393,11 +6393,6 @@ static ant_value_t builtin_object_toLocaleString(ant_t *js, ant_value_t *args, i return js_call_toString(js, js->this_val); } -static inline bool is_callable(ant_value_t v) { - uint8_t t = vtype(v); - return t == T_FUNC || t == T_CFUNC; -} - static inline ant_value_t require_callback(ant_t *js, ant_value_t *args, int nargs, const char *name) { if (nargs == 0 || !is_callable(args[0])) return js_mkerr(js, "%s requires a function argument", name); diff --git a/src/modules/async_hooks.c b/src/modules/async_hooks.c index 12a0c06..fadb14b 100644 --- a/src/modules/async_hooks.c +++ b/src/modules/async_hooks.c @@ -11,11 +11,6 @@ #include "modules/async_hooks.h" #include "modules/symbol.h" -static inline bool async_hooks_is_callable(ant_value_t v) { - uint8_t t = vtype(v); - return t == T_FUNC || t == T_CFUNC; -} - static ant_value_t async_hooks_call_with_tail_args( ant_t *js, ant_value_t fn, ant_value_t this_arg, ant_value_t *args, int nargs, int start_idx ) { @@ -32,7 +27,7 @@ static ant_value_t async_hooks_call_with_tail_args( } static ant_value_t async_local_storage_run(ant_t *js, ant_value_t *args, int nargs) { - if (nargs < 2 || !async_hooks_is_callable(args[1])) { + if (nargs < 2 || !is_callable(args[1])) { return js_mkerr(js, "AsyncLocalStorage.run(store, callback, ...args) requires a callback"); } @@ -49,7 +44,7 @@ static ant_value_t async_local_storage_run(ant_t *js, ant_value_t *args, int nar } static ant_value_t async_local_storage_exit(ant_t *js, ant_value_t *args, int nargs) { - if (nargs < 1 || !async_hooks_is_callable(args[0])) { + if (nargs < 1 || !is_callable(args[0])) { return js_mkerr(js, "AsyncLocalStorage.exit(callback, ...args) requires a callback"); } @@ -89,7 +84,7 @@ static ant_value_t async_local_storage_disable(ant_t *js, ant_value_t *args, int } static ant_value_t async_resource_runInAsyncScope(ant_t *js, ant_value_t *args, int nargs) { - if (nargs < 1 || !async_hooks_is_callable(args[0])) { + if (nargs < 1 || !is_callable(args[0])) { return js_mkerr(js, "AsyncResource.runInAsyncScope(fn[, thisArg, ...args]) requires a function"); } ant_value_t this_arg = nargs > 1 ? args[1] : js_mkundef(); diff --git a/src/modules/napi.c b/src/modules/napi.c index a7ec86f..e337501 100644 --- a/src/modules/napi.c +++ b/src/modules/napi.c @@ -226,11 +226,6 @@ napi_env ant_napi_get_env(ant_t *js) { return (napi_env)napi_get_or_create_env(js); } -static inline bool napi_is_callable(ant_value_t v) { - uint8_t t = vtype(v); - return t == T_FUNC || t == T_CFUNC; -} - static void napi_mark_pending_exception(napi_env env, napi_value exception) { ant_napi_env_t *nenv = (ant_napi_env_t *)env; if (!nenv) return; @@ -499,7 +494,7 @@ static void napi_tsfn_async_cb(uv_async_t *handle) { ant_value_t cb = tsfn->func_val; if (tsfn->call_js_cb) { tsfn->call_js_cb((napi_env)tsfn->env, (napi_value)cb, tsfn->context, item->data); - } else if (napi_is_callable(cb)) { + } else if (is_callable(cb)) { sv_vm_call(js->vm, js, cb, js_mkundef(), NULL, 0, NULL, false); } @@ -629,7 +624,7 @@ ant_value_t napi_load_native_module(ant_t *js, const char *module_path, ant_valu ant_value_t process_obj = js_get(js, js_glob(js), "process"); ant_value_t dlopen_fn = is_object_type(process_obj) ? js_get(js, process_obj, "dlopen") : js_mkundef(); - if (napi_is_callable(dlopen_fn)) { + if (is_callable(dlopen_fn)) { ant_value_t argv[2] = {module_obj, js_mkstr(js, module_path, strlen(module_path))}; ant_value_t dl_res = sv_vm_call(js->vm, js, dlopen_fn, process_obj, argv, 2, NULL, false); if (is_err(dl_res) || js->thrown_exists) return js_throw(js, js->thrown_value); @@ -1973,7 +1968,7 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_call_function( napi_value *result ) { ant_napi_env_t *nenv = (ant_napi_env_t *)env; - if (!nenv || !nenv->js || !napi_is_callable((ant_value_t)func)) { + if (!nenv || !nenv->js || !is_callable((ant_value_t)func)) { return napi_set_last(env, napi_invalid_arg, "invalid argument"); } @@ -2001,7 +1996,7 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_new_instance( napi_value *result ) { ant_napi_env_t *nenv = (ant_napi_env_t *)env; - if (!nenv || !nenv->js || !result || !napi_is_callable((ant_value_t)constructor)) { + if (!nenv || !nenv->js || !result || !is_callable((ant_value_t)constructor)) { return napi_set_last(env, napi_invalid_arg, "invalid argument"); } @@ -2065,7 +2060,7 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_coerce_to_object( } ant_value_t obj_ctor = js_get(nenv->js, js_glob(nenv->js), "Object"); - if (!napi_is_callable(obj_ctor)) return napi_set_last(env, napi_generic_failure, "Object constructor missing"); + if (!is_callable(obj_ctor)) return napi_set_last(env, napi_generic_failure, "Object constructor missing"); ant_value_t arg = (ant_value_t)value; ant_value_t out = sv_vm_call(nenv->js->vm, nenv->js, obj_ctor, js_mkundef(), &arg, 1, NULL, false); if (is_err(out) || nenv->js->thrown_exists) return napi_check_pending_from_result(env, out); diff --git a/src/modules/observable.c b/src/modules/observable.c index 07e79e9..1d535cf 100644 --- a/src/modules/observable.c +++ b/src/modules/observable.c @@ -11,10 +11,7 @@ #include "modules/symbol.h" #include "modules/observable.h" -static inline bool is_callable(ant_value_t val) { - uint8_t t = vtype(val); - return t == T_FUNC || t == T_CFUNC; -} + static bool subscription_closed(ant_t *js, ant_value_t subscription) { ant_value_t observer = js_get_slot(subscription, SLOT_SUBSCRIPTION_OBSERVER); diff --git a/src/modules/tty.c b/src/modules/tty.c index 49fcbcc..2f20335 100644 --- a/src/modules/tty.c +++ b/src/modules/tty.c @@ -37,10 +37,7 @@ #include "modules/symbol.h" #include "modules/tty.h" -static inline bool is_callable(ant_value_t value) { - uint8_t t = vtype(value); - return t == T_FUNC || t == T_CFUNC; -} + static bool parse_fd(ant_value_t value, int *fd_out) { int fd = 0; diff --git a/src/modules/util.c b/src/modules/util.c index 64e11ea..0702f51 100644 --- a/src/modules/util.c +++ b/src/modules/util.c @@ -56,11 +56,6 @@ static const util_style_entry_t util_styles[] = { {"bgWhite", "\x1b[47m", "\x1b[49m"}, }; -static inline bool util_is_callable(ant_value_t v) { - uint8_t t = vtype(v); - return t == T_FUNC || t == T_CFUNC; -} - static bool util_sb_reserve(util_sb_t *sb, size_t extra) { size_t need = sb->len + extra + 1; if (need <= sb->cap) return true; @@ -348,7 +343,7 @@ static ant_value_t util_promisify_callback(ant_t *js, ant_value_t *args, int nar static ant_value_t util_promisified_call(ant_t *js, ant_value_t *args, int nargs) { ant_value_t fn = js_getcurrentfunc(js); ant_value_t original = js_get_slot(fn, SLOT_DATA); - if (!util_is_callable(original)) return js_mkerr(js, "promisified target is not callable"); + if (!is_callable(original)) return js_mkerr(js, "promisified target is not callable"); ant_value_t promise = js_mkpromise(js); ant_value_t ctx = js_mkobj(js); @@ -385,7 +380,7 @@ static ant_value_t util_promisified_call(ant_t *js, ant_value_t *args, int nargs } static ant_value_t util_promisify(ant_t *js, ant_value_t *args, int nargs) { - if (nargs < 1 || !util_is_callable(args[0])) { + if (nargs < 1 || !is_callable(args[0])) { return js_mkerr(js, "promisify(fn) requires a function"); } return js_heavy_mkfun(js, util_promisified_call, args[0]); diff --git a/src/modules/worker_threads.c b/src/modules/worker_threads.c index 29abc8a..ae2044a 100644 --- a/src/modules/worker_threads.c +++ b/src/modules/worker_threads.c @@ -61,11 +61,6 @@ typedef struct ant_worker_thread { static ant_worker_thread_t *active_workers_head = NULL; -static inline bool wt_is_callable(ant_value_t v) { - uint8_t t = vtype(v); - return t == T_FUNC || t == T_CFUNC; -} - static bool wt_is_worker_mode(void) { const char *mode = getenv(WT_ENV_MODE); return mode && strcmp(mode, "1") == 0; @@ -181,7 +176,7 @@ static void wt_port_queue_push(ant_t *js, ant_value_t port, ant_value_t value) { } static void wt_port_call_listener(ant_t *js, ant_value_t this_obj, ant_value_t fn, ant_value_t arg) { - if (!wt_is_callable(fn)) return; + if (!is_callable(fn)) return; ant_value_t argv[1] = {arg}; sv_vm_call(js->vm, js, fn, this_obj, argv, 1, NULL, false); @@ -194,8 +189,8 @@ static bool wt_port_should_deliver(ant_t *js, ant_value_t port) { ant_value_t once_fn = js_get_slot(port, SLOT_WT_PORT_ONCE_MESSAGE); ant_value_t onmessage = js_get(js, port, "onmessage"); - bool has_event_listener = wt_is_callable(on_fn) || wt_is_callable(once_fn); - if (wt_is_callable(onmessage)) return true; + bool has_event_listener = is_callable(on_fn) || is_callable(once_fn); + if (is_callable(onmessage)) return true; return started && has_event_listener; } @@ -210,13 +205,13 @@ static void wt_port_drain(ant_t *js, ant_value_t port) { wt_port_call_listener(js, port, on_fn, msg); ant_value_t once_fn = js_get_slot(port, SLOT_WT_PORT_ONCE_MESSAGE); - if (wt_is_callable(once_fn)) { + if (is_callable(once_fn)) { wt_port_call_listener(js, port, once_fn, msg); js_set_slot(port, SLOT_WT_PORT_ONCE_MESSAGE, js_mkundef()); } ant_value_t onmessage = js_get(js, port, "onmessage"); - if (wt_is_callable(onmessage)) { + if (is_callable(onmessage)) { ant_value_t event_obj = js_mkobj(js); js_set(js, event_obj, "data", msg); wt_port_call_listener(js, port, onmessage, event_obj); @@ -233,7 +228,7 @@ static ant_value_t wt_make_resolved_promise(ant_t *js, ant_value_t value) { } static void wt_call_listener(ant_t *js, ant_value_t this_obj, ant_value_t fn, ant_value_t arg) { - if (!wt_is_callable(fn)) return; + if (!is_callable(fn)) return; ant_value_t argv[1] = {arg}; sv_vm_call(js->vm, js, fn, this_obj, argv, 1, NULL, false); @@ -258,7 +253,7 @@ static void wt_emit(ant_worker_thread_t *wt, const char *event, ant_value_t arg) wt_call_listener(js, this_obj, on_fn, arg); ant_value_t once_fn = js_get_slot(this_obj, once_slot); - if (wt_is_callable(once_fn)) { + if (is_callable(once_fn)) { wt_call_listener(js, this_obj, once_fn, arg); js_set_slot(this_obj, once_slot, js_mkundef()); } @@ -579,7 +574,7 @@ static int wt_spawn_worker( } static ant_value_t worker_threads_worker_on(ant_t *js, ant_value_t *args, int nargs) { - if (nargs < 2 || vtype(args[0]) != T_STR || !wt_is_callable(args[1])) { + if (nargs < 2 || vtype(args[0]) != T_STR || !is_callable(args[1])) { return js_mkerr(js, "Worker.on(event, listener) requires (string, function)"); } @@ -600,7 +595,7 @@ static ant_value_t worker_threads_worker_on(ant_t *js, ant_value_t *args, int na } static ant_value_t worker_threads_worker_once(ant_t *js, ant_value_t *args, int nargs) { - if (nargs < 2 || vtype(args[0]) != T_STR || !wt_is_callable(args[1])) { + if (nargs < 2 || vtype(args[0]) != T_STR || !is_callable(args[1])) { return js_mkerr(js, "Worker.once(event, listener) requires (string, function)"); } @@ -688,7 +683,7 @@ static ant_value_t worker_threads_message_port_post_message(ant_t *js, ant_value } static ant_value_t worker_threads_message_port_on(ant_t *js, ant_value_t *args, int nargs) { - if (nargs < 2 || vtype(args[0]) != T_STR || !wt_is_callable(args[1])) { + if (nargs < 2 || vtype(args[0]) != T_STR || !is_callable(args[1])) { return js_mkerr(js, "MessagePort.on(event, listener) requires (string, function)"); } ant_value_t this_obj = js_getthis(js); @@ -706,7 +701,7 @@ static ant_value_t worker_threads_message_port_on(ant_t *js, ant_value_t *args, } static ant_value_t worker_threads_message_port_once(ant_t *js, ant_value_t *args, int nargs) { - if (nargs < 2 || vtype(args[0]) != T_STR || !wt_is_callable(args[1])) { + if (nargs < 2 || vtype(args[0]) != T_STR || !is_callable(args[1])) { return js_mkerr(js, "MessagePort.once(event, listener) requires (string, function)"); } ant_value_t this_obj = js_getthis(js);