Skip to content

Commit dad6190

Browse files
author
Ben Grant
committed
Call functions from Bun when BUN_EVENT_LOOP is used
1 parent 60d90a5 commit dad6190

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

Source/WTF/wtf/RunLoop.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ class WTF_CAPABILITY("is current") RunLoop final : public GuaranteedSerialFuncti
144144
#if USE(GLIB_EVENT_LOOP)
145145
WTF_EXPORT_PRIVATE void setName(ASCIILiteral);
146146
WTF_EXPORT_PRIVATE void setPriority(int);
147+
#elif USE(BUN_EVENT_LOOP)
148+
// WTFTimer in Timer.zig
149+
struct Bun__WTFTimer;
147150
#endif
148151

149152
private:
@@ -172,6 +175,7 @@ class WTF_CAPABILITY("is current") RunLoop final : public GuaranteedSerialFuncti
172175
class ScheduledTask;
173176
Ref<ScheduledTask> m_scheduledTask;
174177
#elif USE(BUN_EVENT_LOOP)
178+
Bun__WTFTimer* m_zigTimer;
175179
#endif
176180
};
177181

Source/WTF/wtf/bun/RunLoopBun.cpp

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,93 @@
11
#include "config.h"
22
#include <wtf/RunLoop.h>
33

4+
45
namespace WTF {
56

7+
// Functions exported by Timer.zig
8+
extern "C" __attribute__((weak)) RunLoop::TimerBase::Bun__WTFTimer* WTFTimer__create(RunLoop::TimerBase*);
9+
extern "C" __attribute__((weak)) void WTFTimer__update(RunLoop::TimerBase::Bun__WTFTimer*, double seconds, bool repeat);
10+
extern "C" __attribute__((weak)) void WTFTimer__deinit(RunLoop::TimerBase::Bun__WTFTimer*);
11+
extern "C" __attribute__((weak)) bool WTFTimer__isActive(const RunLoop::TimerBase::Bun__WTFTimer*);
12+
extern "C" __attribute__((weak)) double WTFTimer__secondsUntilTimer(const RunLoop::TimerBase::Bun__WTFTimer*);
13+
extern "C" __attribute__((weak)) void WTFTimer__cancel(RunLoop::TimerBase::Bun__WTFTimer*);
14+
615
RunLoop::TimerBase::TimerBase(Ref<RunLoop>&& loop)
716
: m_runLoop(WTFMove(loop))
17+
// check if the zig function is actually available (it won't be in JSC shell, since that doesn't
18+
// link Bun's zig code)
19+
, m_zigTimer(&WTFTimer__create ? WTFTimer__create(this) : nullptr)
820
{
921
}
1022

1123
RunLoop::TimerBase::~TimerBase()
1224
{
25+
if (&WTFTimer__deinit) {
26+
ASSERT(m_zigTimer);
27+
WTFTimer__deinit(m_zigTimer);
28+
}
1329
}
1430

15-
void RunLoop::TimerBase::stop() {}
31+
void RunLoop::TimerBase::stop() {
32+
if (&WTFTimer__cancel) {
33+
ASSERT(m_zigTimer);
34+
WTFTimer__cancel(m_zigTimer);
35+
}
36+
}
1637

17-
bool RunLoop::TimerBase::isActive() const {}
38+
bool RunLoop::TimerBase::isActive() const {
39+
if (&WTFTimer__isActive) {
40+
ASSERT(m_zigTimer);
41+
return WTFTimer__isActive(m_zigTimer);
42+
}
43+
return false;
44+
}
1845

19-
Seconds RunLoop::TimerBase::secondsUntilFire() const {}
46+
Seconds RunLoop::TimerBase::secondsUntilFire() const {
47+
if (&WTFTimer__secondsUntilTimer) {
48+
ASSERT(m_zigTimer);
49+
return Seconds(WTFTimer__secondsUntilTimer(m_zigTimer));
50+
}
51+
return -1.0_s;
52+
}
53+
54+
void RunLoop::TimerBase::start(Seconds interval, bool repeat) {
55+
if (&WTFTimer__update) {
56+
ASSERT(m_zigTimer);
57+
WTFTimer__update(m_zigTimer, interval.value(), repeat);
58+
}
59+
}
2060

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

2365
// probably more Bun-specific TimerBase methods
2466

2567
RunLoop::RunLoop()
2668
{
2769
}
2870

29-
RunLoop::~RunLoop() {}
71+
RunLoop::~RunLoop()
72+
{
73+
}
3074

31-
void RunLoop::run() {}
75+
void RunLoop::run() {
76+
ASSERT_NOT_REACHED();
77+
}
3278

33-
void RunLoop::stop() {}
79+
void RunLoop::stop() {
80+
ASSERT_NOT_REACHED();
81+
}
3482

35-
void RunLoop::wakeUp() {}
83+
void RunLoop::wakeUp() {
84+
ASSERT_NOT_REACHED();
85+
}
3686

37-
RunLoop::CycleResult RunLoop::cycle(RunLoopMode mode) {}
87+
RunLoop::CycleResult RunLoop::cycle(RunLoopMode mode) {
88+
(void) mode;
89+
ASSERT_NOT_REACHED();
90+
return RunLoop::CycleResult::Stop;
91+
}
3892

3993
} // namespace WTF

Source/cmake/WebKitCompilerFlags.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ if (COMPILER_IS_GCC_OR_CLANG)
228228
# Makes builds faster. The GCC manual warns about the possibility that the assembler being
229229
# used may not support input from a pipe, but in practice the toolchains we support all do.
230230
WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS(-pipe)
231+
232+
if (USE_BUN_JSC_ADDITIONS)
233+
WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS(-Wl,-U,_WTFTimer__create
234+
-Wl,-U,_WTFTimer__update
235+
-Wl,-U,_WTFTimer__deinit
236+
-Wl,-U,_WTFTimer__isActive
237+
-Wl,-U,_WTFTimer__secondsUntilTimer
238+
-Wl,-U,_WTFTimer__cancel)
239+
endif ()
231240
endif ()
232241

233242
if (COMPILER_IS_GCC_OR_CLANG AND NOT MSVC)

0 commit comments

Comments
 (0)