diff --git a/src/bun.js/bindings/glibc-versions-hack.cpp b/src/bun.js/bindings/workaround-missing-symbols.cpp similarity index 63% rename from src/bun.js/bindings/glibc-versions-hack.cpp rename to src/bun.js/bindings/workaround-missing-symbols.cpp index 33d94c80965bd..1904eb9d8b7c8 100644 --- a/src/bun.js/bindings/glibc-versions-hack.cpp +++ b/src/bun.js/bindings/workaround-missing-symbols.cpp @@ -1,3 +1,7 @@ +#ifndef UNLIKELY +#define UNLIKELY(x) __builtin_expect(!!(x), 0) +#endif + // if linux #if defined(__linux__) @@ -8,10 +12,6 @@ #include #include -#ifndef UNLIKELY -#define UNLIKELY(x) __builtin_expect(!!(x), 0) -#endif - #ifndef _STAT_VER #if defined(__aarch64__) #define _STAT_VER 0 @@ -182,3 +182,74 @@ extern "C" int __wrap_mknodat(int dirfd, const char* path, __mode_t mode, __dev_ } #endif + +// macOS +#if defined(__APPLE__) + +#include + +extern "C" int pthread_self_is_exiting_np() +{ + static void* pthread_self_is_exiting_np_ptr = nullptr; + static bool pthread_self_is_exiting_np_ptr_initialized = false; + if (UNLIKELY(!pthread_self_is_exiting_np_ptr_initialized)) { + pthread_self_is_exiting_np_ptr_initialized = true; + pthread_self_is_exiting_np_ptr = dlsym(RTLD_DEFAULT, "pthread_self_is_exiting_np"); + } + + if (UNLIKELY(pthread_self_is_exiting_np_ptr == nullptr)) + return 0; + + return ((int (*)())pthread_self_is_exiting_np_ptr)(); +} + +extern "C" int posix_spawn_file_actions_addchdir_np( + void* file_actions, + const char* path) +{ + static void* posix_spawn_file_actions_addchdir_np_ptr = nullptr; + static bool posix_spawn_file_actions_addchdir_np_ptr_initialized = false; + if (UNLIKELY(!posix_spawn_file_actions_addchdir_np_ptr_initialized)) { + posix_spawn_file_actions_addchdir_np_ptr_initialized = true; + posix_spawn_file_actions_addchdir_np_ptr = dlsym(RTLD_DEFAULT, "posix_spawn_file_actions_addchdir_np"); + } + + if (UNLIKELY(posix_spawn_file_actions_addchdir_np_ptr == nullptr)) + return 0; + + return ((int (*)(void*, const char*))posix_spawn_file_actions_addchdir_np_ptr)(file_actions, path); +} + +extern "C" int posix_spawn_file_actions_addinherit_np(void* ptr, + int status) +{ + static void* posix_spawn_file_actions_addinherit_np_ptr = nullptr; + static bool posix_spawn_file_actions_addinherit_np_ptr_initialized = false; + if (UNLIKELY(!posix_spawn_file_actions_addinherit_np_ptr_initialized)) { + posix_spawn_file_actions_addinherit_np_ptr_initialized = true; + posix_spawn_file_actions_addinherit_np_ptr = dlsym(RTLD_DEFAULT, "posix_spawn_file_actions_addinherit_np"); + } + + if (UNLIKELY(posix_spawn_file_actions_addinherit_np_ptr == nullptr)) + return 0; + + return ((int (*)(void*, int))posix_spawn_file_actions_addinherit_np_ptr)(ptr, status); +} + +extern "C" int posix_spawn_file_actions_addfchdir_np(void* ptr, + int fd) +{ + static void* posix_spawn_file_actions_addfchdir_np_ptr = nullptr; + static bool posix_spawn_file_actions_addfchdir_np_ptr_initialized = false; + if (UNLIKELY(!posix_spawn_file_actions_addfchdir_np_ptr_initialized)) { + posix_spawn_file_actions_addfchdir_np_ptr_initialized = true; + posix_spawn_file_actions_addfchdir_np_ptr = dlsym(RTLD_DEFAULT, "posix_spawn_file_actions_addfchdir_np"); + } + + if (UNLIKELY(posix_spawn_file_actions_addfchdir_np_ptr == nullptr)) + return 0; + + return ((int (*)(void*, int))posix_spawn_file_actions_addfchdir_np_ptr)(ptr, fd); +} + +#endif