Skip to content

Commit

Permalink
Call functions from Bun when BUN_EVENT_LOOP is used
Browse files Browse the repository at this point in the history
  • Loading branch information
190n committed Nov 28, 2024
1 parent 60d90a5 commit dad6190
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Source/WTF/wtf/RunLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class WTF_CAPABILITY("is current") RunLoop final : public GuaranteedSerialFuncti
#if USE(GLIB_EVENT_LOOP)
WTF_EXPORT_PRIVATE void setName(ASCIILiteral);
WTF_EXPORT_PRIVATE void setPriority(int);
#elif USE(BUN_EVENT_LOOP)
// WTFTimer in Timer.zig
struct Bun__WTFTimer;
#endif

private:
Expand Down Expand Up @@ -172,6 +175,7 @@ class WTF_CAPABILITY("is current") RunLoop final : public GuaranteedSerialFuncti
class ScheduledTask;
Ref<ScheduledTask> m_scheduledTask;
#elif USE(BUN_EVENT_LOOP)
Bun__WTFTimer* m_zigTimer;
#endif
};

Expand Down
72 changes: 63 additions & 9 deletions Source/WTF/wtf/bun/RunLoopBun.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,93 @@
#include "config.h"
#include <wtf/RunLoop.h>


namespace WTF {

// Functions exported by Timer.zig
extern "C" __attribute__((weak)) RunLoop::TimerBase::Bun__WTFTimer* WTFTimer__create(RunLoop::TimerBase*);
extern "C" __attribute__((weak)) void WTFTimer__update(RunLoop::TimerBase::Bun__WTFTimer*, double seconds, bool repeat);
extern "C" __attribute__((weak)) void WTFTimer__deinit(RunLoop::TimerBase::Bun__WTFTimer*);
extern "C" __attribute__((weak)) bool WTFTimer__isActive(const RunLoop::TimerBase::Bun__WTFTimer*);
extern "C" __attribute__((weak)) double WTFTimer__secondsUntilTimer(const RunLoop::TimerBase::Bun__WTFTimer*);
extern "C" __attribute__((weak)) void WTFTimer__cancel(RunLoop::TimerBase::Bun__WTFTimer*);

RunLoop::TimerBase::TimerBase(Ref<RunLoop>&& loop)
: m_runLoop(WTFMove(loop))
// check if the zig function is actually available (it won't be in JSC shell, since that doesn't
// link Bun's zig code)
, m_zigTimer(&WTFTimer__create ? WTFTimer__create(this) : nullptr)
{
}

RunLoop::TimerBase::~TimerBase()
{
if (&WTFTimer__deinit) {
ASSERT(m_zigTimer);
WTFTimer__deinit(m_zigTimer);
}
}

void RunLoop::TimerBase::stop() {}
void RunLoop::TimerBase::stop() {
if (&WTFTimer__cancel) {
ASSERT(m_zigTimer);
WTFTimer__cancel(m_zigTimer);
}
}

bool RunLoop::TimerBase::isActive() const {}
bool RunLoop::TimerBase::isActive() const {
if (&WTFTimer__isActive) {
ASSERT(m_zigTimer);
return WTFTimer__isActive(m_zigTimer);
}
return false;
}

Seconds RunLoop::TimerBase::secondsUntilFire() const {}
Seconds RunLoop::TimerBase::secondsUntilFire() const {
if (&WTFTimer__secondsUntilTimer) {
ASSERT(m_zigTimer);
return Seconds(WTFTimer__secondsUntilTimer(m_zigTimer));
}
return -1.0_s;
}

void RunLoop::TimerBase::start(Seconds interval, bool repeat) {
if (&WTFTimer__update) {
ASSERT(m_zigTimer);
WTFTimer__update(m_zigTimer, interval.value(), repeat);
}
}

void RunLoop::TimerBase::start(Seconds interval, bool repeat) {}
extern "C" void WTFTimer__fire(RunLoop::TimerBase* timer) {
timer->fired();
}

// probably more Bun-specific TimerBase methods

RunLoop::RunLoop()
{
}

RunLoop::~RunLoop() {}
RunLoop::~RunLoop()
{
}

void RunLoop::run() {}
void RunLoop::run() {
ASSERT_NOT_REACHED();
}

void RunLoop::stop() {}
void RunLoop::stop() {
ASSERT_NOT_REACHED();
}

void RunLoop::wakeUp() {}
void RunLoop::wakeUp() {
ASSERT_NOT_REACHED();
}

RunLoop::CycleResult RunLoop::cycle(RunLoopMode mode) {}
RunLoop::CycleResult RunLoop::cycle(RunLoopMode mode) {
(void) mode;
ASSERT_NOT_REACHED();
return RunLoop::CycleResult::Stop;
}

} // namespace WTF
9 changes: 9 additions & 0 deletions Source/cmake/WebKitCompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ if (COMPILER_IS_GCC_OR_CLANG)
# Makes builds faster. The GCC manual warns about the possibility that the assembler being
# used may not support input from a pipe, but in practice the toolchains we support all do.
WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS(-pipe)

if (USE_BUN_JSC_ADDITIONS)
WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS(-Wl,-U,_WTFTimer__create
-Wl,-U,_WTFTimer__update
-Wl,-U,_WTFTimer__deinit
-Wl,-U,_WTFTimer__isActive
-Wl,-U,_WTFTimer__secondsUntilTimer
-Wl,-U,_WTFTimer__cancel)
endif ()
endif ()

if (COMPILER_IS_GCC_OR_CLANG AND NOT MSVC)
Expand Down

0 comments on commit dad6190

Please sign in to comment.