1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
#include "root.h"
#include "headers.h"
#include "ScriptExecutionContext.h"
#include "MessagePort.h"
#include "webcore/WebSocket.h"
#include "libusockets.h"
#include "_libusockets.h"
#include "BunClientData.h"
extern "C" void Bun__startLoop(us_loop_t* loop);
namespace WebCore {
static std::atomic<unsigned> lastUniqueIdentifier = 0;
WTF_MAKE_ISO_ALLOCATED_IMPL(EventLoopTask);
WTF_MAKE_ISO_ALLOCATED_IMPL(ScriptExecutionContext);
static Lock allScriptExecutionContextsMapLock;
static HashMap<ScriptExecutionContextIdentifier, ScriptExecutionContext*>& allScriptExecutionContextsMap() WTF_REQUIRES_LOCK(allScriptExecutionContextsMapLock)
{
static NeverDestroyed<HashMap<ScriptExecutionContextIdentifier, ScriptExecutionContext*>> contexts;
ASSERT(allScriptExecutionContextsMapLock.isLocked());
return contexts;
}
ScriptExecutionContext* ScriptExecutionContext::getScriptExecutionContext(ScriptExecutionContextIdentifier identifier)
{
Locker locker { allScriptExecutionContextsMapLock };
return allScriptExecutionContextsMap().get(identifier);
}
template<bool SSL, bool isServer>
static void registerHTTPContextForWebSocket(ScriptExecutionContext* script, us_socket_context_t* ctx, us_loop_t* loop)
{
if constexpr (!isServer) {
if constexpr (SSL) {
Bun__WebSocketHTTPSClient__register(script->jsGlobalObject(), loop, ctx);
} else {
Bun__WebSocketHTTPClient__register(script->jsGlobalObject(), loop, ctx);
}
} else {
RELEASE_ASSERT_NOT_REACHED();
}
}
us_socket_context_t* ScriptExecutionContext::webSocketContextSSL()
{
if (!m_ssl_client_websockets_ctx) {
us_loop_t* loop = (us_loop_t*)uws_get_loop();
us_bun_socket_context_options_t opts;
memset(&opts, 0, sizeof(us_bun_socket_context_options_t));
// adds root ca
opts.request_cert = true;
// but do not reject unauthorized
opts.reject_unauthorized = false;
this->m_ssl_client_websockets_ctx = us_create_bun_socket_context(1, loop, sizeof(size_t), opts);
void** ptr = reinterpret_cast<void**>(us_socket_context_ext(1, m_ssl_client_websockets_ctx));
*ptr = this;
registerHTTPContextForWebSocket<true, false>(this, m_ssl_client_websockets_ctx, loop);
}
return m_ssl_client_websockets_ctx;
}
extern "C" void Bun__eventLoop__incrementRefConcurrently(void* bunVM, int delta);
void ScriptExecutionContext::refEventLoop()
{
Bun__eventLoop__incrementRefConcurrently(WebCore::clientData(vm())->bunVM, 1);
}
void ScriptExecutionContext::unrefEventLoop()
{
Bun__eventLoop__incrementRefConcurrently(WebCore::clientData(vm())->bunVM, -1);
}
ScriptExecutionContext::~ScriptExecutionContext()
{
checkConsistency();
{
Locker locker { allScriptExecutionContextsMapLock };
ASSERT_WITH_MESSAGE(!allScriptExecutionContextsMap().contains(m_identifier), "A ScriptExecutionContext subclass instance implementing postTask should have already removed itself from the map");
}
auto postMessageCompletionHandlers = WTFMove(m_processMessageWithMessagePortsSoonHandlers);
for (auto& completionHandler : postMessageCompletionHandlers)
completionHandler();
while (auto* destructionObserver = m_destructionObservers.takeAny())
destructionObserver->contextDestroyed();
}
bool ScriptExecutionContext::postTaskTo(ScriptExecutionContextIdentifier identifier, Function<void(ScriptExecutionContext&)>&& task)
{
Locker locker { allScriptExecutionContextsMapLock };
auto* context = allScriptExecutionContextsMap().get(identifier);
if (!context)
return false;
context->postTaskConcurrently(WTFMove(task));
return true;
}
void ScriptExecutionContext::didCreateDestructionObserver(ContextDestructionObserver& observer)
{
ASSERT(!m_inScriptExecutionContextDestructor);
m_destructionObservers.add(&observer);
}
void ScriptExecutionContext::willDestroyDestructionObserver(ContextDestructionObserver& observer)
{
m_destructionObservers.remove(&observer);
}
bool ScriptExecutionContext::isJSExecutionForbidden()
{
return !m_vm || m_vm->executionForbidden();
}
extern "C" void* Bun__getVM();
bool ScriptExecutionContext::isContextThread()
{
auto clientData = WebCore::clientData(vm());
return clientData && clientData->bunVM == Bun__getVM();
}
bool ScriptExecutionContext::ensureOnContextThread(ScriptExecutionContextIdentifier identifier, Function<void(ScriptExecutionContext&)>&& task)
{
ScriptExecutionContext* context = nullptr;
{
Locker locker { allScriptExecutionContextsMapLock };
context = allScriptExecutionContextsMap().get(identifier);
if (!context)
return false;
if (!context->isContextThread()) {
context->postTaskConcurrently(WTFMove(task));
return true;
}
}
task(*context);
return true;
}
bool ScriptExecutionContext::ensureOnMainThread(Function<void(ScriptExecutionContext&)>&& task)
{
Locker locker { allScriptExecutionContextsMapLock };
auto* context = allScriptExecutionContextsMap().get(1);
if (!context) {
return false;
}
context->postTaskConcurrently(WTFMove(task));
return true;
}
void ScriptExecutionContext::processMessageWithMessagePortsSoon(CompletionHandler<void()>&& completionHandler)
{
ASSERT(isContextThread());
m_processMessageWithMessagePortsSoonHandlers.append(WTFMove(completionHandler));
if (m_willProcessMessageWithMessagePortsSoon) {
return;
}
m_willProcessMessageWithMessagePortsSoon = true;
postTask([](ScriptExecutionContext& context) {
context.dispatchMessagePortEvents();
});
}
void ScriptExecutionContext::dispatchMessagePortEvents()
{
ASSERT(isContextThread());
checkConsistency();
ASSERT(m_willprocessMessageWithMessagePortsSoon);
m_willProcessMessageWithMessagePortsSoon = false;
auto completionHandlers = std::exchange(m_processMessageWithMessagePortsSoonHandlers, Vector<CompletionHandler<void()>> {});
// Make a frozen copy of the ports so we can iterate while new ones might be added or destroyed.
for (auto* messagePort : copyToVector(m_messagePorts)) {
// The port may be destroyed, and another one created at the same address,
// but this is harmless. The worst that can happen as a result is that
// dispatchMessages() will be called needlessly.
if (m_messagePorts.contains(messagePort) && messagePort->started())
messagePort->dispatchMessages();
}
for (auto& completionHandler : completionHandlers)
completionHandler();
}
void ScriptExecutionContext::checkConsistency() const
{
for (auto* messagePort : m_messagePorts)
ASSERT(messagePort->scriptExecutionContext() == this);
for (auto* destructionObserver : m_destructionObservers)
ASSERT(destructionObserver->scriptExecutionContext() == this);
// for (auto* activeDOMObject : m_activeDOMObjects) {
// ASSERT(activeDOMObject->scriptExecutionContext() == this);
// activeDOMObject->assertSuspendIfNeededWasCalled();
// }
}
void ScriptExecutionContext::createdMessagePort(MessagePort& messagePort)
{
ASSERT(isContextThread());
m_messagePorts.add(&messagePort);
}
void ScriptExecutionContext::destroyedMessagePort(MessagePort& messagePort)
{
ASSERT(isContextThread());
m_messagePorts.remove(&messagePort);
}
us_socket_context_t* ScriptExecutionContext::webSocketContextNoSSL()
{
if (!m_client_websockets_ctx) {
us_loop_t* loop = (us_loop_t*)uws_get_loop();
us_socket_context_options_t opts;
memset(&opts, 0, sizeof(us_socket_context_options_t));
this->m_client_websockets_ctx = us_create_socket_context(0, loop, sizeof(size_t), opts);
void** ptr = reinterpret_cast<void**>(us_socket_context_ext(0, m_client_websockets_ctx));
*ptr = this;
registerHTTPContextForWebSocket<false, false>(this, m_client_websockets_ctx, loop);
}
return m_client_websockets_ctx;
}
template<bool SSL>
static us_socket_context_t* registerWebSocketClientContext(ScriptExecutionContext* script, us_socket_context_t* parent)
{
us_loop_t* loop = (us_loop_t*)uws_get_loop();
if constexpr (SSL) {
us_socket_context_t* child = us_create_child_socket_context(1, parent, sizeof(size_t));
Bun__WebSocketClientTLS__register(script->jsGlobalObject(), loop, child);
return child;
} else {
us_socket_context_t* child = us_create_child_socket_context(0, parent, sizeof(size_t));
Bun__WebSocketClient__register(script->jsGlobalObject(), loop, child);
return child;
}
}
us_socket_context_t* ScriptExecutionContext::connectedWebSocketKindClient()
{
return registerWebSocketClientContext<false>(this, webSocketContextNoSSL());
}
us_socket_context_t* ScriptExecutionContext::connectedWebSocketKindClientSSL()
{
return registerWebSocketClientContext<true>(this, webSocketContextSSL());
}
ScriptExecutionContextIdentifier ScriptExecutionContext::generateIdentifier()
{
return ++lastUniqueIdentifier;
}
void ScriptExecutionContext::regenerateIdentifier()
{
m_identifier = ++lastUniqueIdentifier;
addToContextsMap();
}
void ScriptExecutionContext::addToContextsMap()
{
Locker locker { allScriptExecutionContextsMapLock };
ASSERT(!allScriptExecutionContextsMap().contains(m_identifier));
allScriptExecutionContextsMap().add(m_identifier, this);
}
void ScriptExecutionContext::removeFromContextsMap()
{
Locker locker { allScriptExecutionContextsMapLock };
ASSERT(allScriptExecutionContextsMap().contains(m_identifier));
allScriptExecutionContextsMap().remove(m_identifier);
}
ScriptExecutionContext* executionContext(JSC::JSGlobalObject* globalObject)
{
if (!globalObject || !globalObject->inherits<JSDOMGlobalObject>())
return nullptr;
return JSC::jsCast<JSDOMGlobalObject*>(globalObject)->scriptExecutionContext();
}
}
|