Skip to content

Commit b5f31a6

Browse files
190n190nJarred-Sumner
authored
node:worker_threads: improve error messages, support environmentData, emit worker event (#18768)
Co-authored-by: 190n <[email protected]> Co-authored-by: Jarred Sumner <[email protected]>
1 parent a8cc395 commit b5f31a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+917
-580
lines changed

src/Global.zig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,6 @@ pub export fn Bun__onExit() void {
225225
std.mem.doNotOptimizeAway(&Bun__atexit);
226226

227227
Output.Source.Stdio.restore();
228-
229-
if (Environment.isWindows) {
230-
bun.windows.libuv.uv_library_shutdown();
231-
}
232228
}
233229

234230
comptime {

src/bun.js/api/bun/udp_socket.zig

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn onDrain(socket: *uws.udp.Socket) callconv(.C) void {
4343
event_loop.enter();
4444
defer event_loop.exit();
4545
_ = callback.call(this.globalThis, this.thisValue, &.{this.thisValue}) catch |err| {
46-
_ = this.callErrorHandler(.zero, &.{this.globalThis.takeException(err)});
46+
this.callErrorHandler(.zero, this.globalThis.takeException(err));
4747
};
4848
}
4949

@@ -111,7 +111,7 @@ fn onData(socket: *uws.udp.Socket, buf: *uws.udp.PacketBuffer, packets: c_int) c
111111
JSC.jsNumber(port),
112112
hostname_string.transferToJS(globalThis),
113113
}) catch |err| {
114-
_ = udpSocket.callErrorHandler(.zero, &.{udpSocket.globalThis.takeException(err)});
114+
udpSocket.callErrorHandler(.zero, udpSocket.globalThis.takeException(err));
115115
};
116116
}
117117
}
@@ -375,22 +375,21 @@ pub const UDPSocket = struct {
375375
pub fn callErrorHandler(
376376
this: *This,
377377
thisValue: JSValue,
378-
err: []const JSValue,
379-
) bool {
378+
err: JSValue,
379+
) void {
380380
const callback = this.config.on_error;
381381
const globalThis = this.globalThis;
382382
const vm = globalThis.bunVM();
383383

384+
if (err.isTerminationException(vm.jsc)) {
385+
return;
386+
}
384387
if (callback == .zero) {
385-
if (err.len > 0)
386-
_ = vm.uncaughtException(globalThis, err[0], false);
387-
388-
return false;
388+
_ = vm.uncaughtException(globalThis, err, false);
389+
return;
389390
}
390391

391-
_ = callback.call(globalThis, thisValue, err) catch |e| globalThis.reportActiveExceptionAsUnhandled(e);
392-
393-
return true;
392+
_ = callback.call(globalThis, thisValue, &.{err}) catch |e| globalThis.reportActiveExceptionAsUnhandled(e);
394393
}
395394

396395
pub fn setBroadcast(this: *This, globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue {

src/bun.js/bindings/BunInjectedScriptHost.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ static JSObject* objectForEventTargetListeners(VM& vm, JSGlobalObject* exec, Eve
5454
auto* scriptExecutionContext = eventTarget->scriptExecutionContext();
5555
if (!scriptExecutionContext)
5656
return nullptr;
57+
auto scope = DECLARE_THROW_SCOPE(vm);
5758

5859
JSObject* listeners = nullptr;
5960

6061
for (auto& eventType : eventTarget->eventTypes()) {
6162
unsigned listenersForEventIndex = 0;
6263
auto* listenersForEvent = constructEmptyArray(exec, nullptr);
64+
RETURN_IF_EXCEPTION(scope, {});
6365

6466
for (auto& eventListener : eventTarget->eventListeners(eventType)) {
6567
if (!is<JSEventListener>(eventListener->callback()))
@@ -74,6 +76,7 @@ static JSObject* objectForEventTargetListeners(VM& vm, JSGlobalObject* exec, Eve
7476
continue;
7577

7678
auto* propertiesForListener = constructEmptyObject(exec);
79+
RETURN_IF_EXCEPTION(scope, {});
7780
propertiesForListener->putDirect(vm, Identifier::fromString(vm, "callback"_s), jsFunction);
7881
propertiesForListener->putDirect(vm, Identifier::fromString(vm, "capture"_s), jsBoolean(eventListener->useCapture()));
7982
propertiesForListener->putDirect(vm, Identifier::fromString(vm, "passive"_s), jsBoolean(eventListener->isPassive()));
@@ -82,8 +85,10 @@ static JSObject* objectForEventTargetListeners(VM& vm, JSGlobalObject* exec, Eve
8285
}
8386

8487
if (listenersForEventIndex) {
85-
if (!listeners)
88+
if (!listeners) {
8689
listeners = constructEmptyObject(exec);
90+
RETURN_IF_EXCEPTION(scope, {});
91+
}
8792
listeners->putDirect(vm, Identifier::fromString(vm, eventType), listenersForEvent);
8893
}
8994
}
@@ -121,6 +126,7 @@ JSValue BunInjectedScriptHost::getInternalProperties(VM& vm, JSGlobalObject* exe
121126
if (auto* worker = JSWorker::toWrapped(vm, value)) {
122127
unsigned index = 0;
123128
auto* array = constructEmptyArray(exec, nullptr);
129+
RETURN_IF_EXCEPTION(scope, {});
124130

125131
String name = worker->name();
126132
if (!name.isEmpty())
@@ -142,13 +148,15 @@ JSValue BunInjectedScriptHost::getInternalProperties(VM& vm, JSGlobalObject* exe
142148
if (type == JSDOMWrapperType) {
143149
if (auto* headers = jsDynamicCast<JSFetchHeaders*>(value)) {
144150
auto* array = constructEmptyArray(exec, nullptr);
151+
RETURN_IF_EXCEPTION(scope, {});
145152
constructDataProperties(vm, exec, array, WebCore::getInternalProperties(vm, exec, headers));
146153
RETURN_IF_EXCEPTION(scope, {});
147154
return array;
148155
}
149156

150157
if (auto* formData = jsDynamicCast<JSDOMFormData*>(value)) {
151158
auto* array = constructEmptyArray(exec, nullptr);
159+
RETURN_IF_EXCEPTION(scope, {});
152160
constructDataProperties(vm, exec, array, WebCore::getInternalProperties(vm, exec, formData));
153161
RETURN_IF_EXCEPTION(scope, {});
154162
return array;
@@ -157,20 +165,23 @@ JSValue BunInjectedScriptHost::getInternalProperties(VM& vm, JSGlobalObject* exe
157165
} else if (type == JSAsJSONType) {
158166
if (auto* params = jsDynamicCast<JSURLSearchParams*>(value)) {
159167
auto* array = constructEmptyArray(exec, nullptr);
168+
RETURN_IF_EXCEPTION(scope, {});
160169
constructDataProperties(vm, exec, array, WebCore::getInternalProperties(vm, exec, params));
161170
RETURN_IF_EXCEPTION(scope, {});
162171
return array;
163172
}
164173

165174
if (auto* cookie = jsDynamicCast<JSCookie*>(value)) {
166175
auto* array = constructEmptyArray(exec, nullptr);
176+
RETURN_IF_EXCEPTION(scope, {});
167177
constructDataProperties(vm, exec, array, WebCore::getInternalProperties(vm, exec, cookie));
168178
RETURN_IF_EXCEPTION(scope, {});
169179
return array;
170180
}
171181

172182
if (auto* cookieMap = jsDynamicCast<JSCookieMap*>(value)) {
173183
auto* array = constructEmptyArray(exec, nullptr);
184+
RETURN_IF_EXCEPTION(scope, {});
174185
constructDataProperties(vm, exec, array, WebCore::getInternalProperties(vm, exec, cookieMap));
175186
RETURN_IF_EXCEPTION(scope, {});
176187
return array;
@@ -181,11 +192,13 @@ JSValue BunInjectedScriptHost::getInternalProperties(VM& vm, JSGlobalObject* exe
181192
if (auto* eventTarget = JSEventTarget::toWrapped(vm, value)) {
182193
unsigned index = 0;
183194
auto* array = constructEmptyArray(exec, nullptr);
195+
RETURN_IF_EXCEPTION(scope, {});
184196

185-
if (auto* listeners = objectForEventTargetListeners(vm, exec, eventTarget))
197+
if (auto* listeners = objectForEventTargetListeners(vm, exec, eventTarget)) {
186198
array->putDirectIndex(exec, index++, constructInternalProperty(vm, exec, "listeners"_s, listeners));
199+
RETURN_IF_EXCEPTION(scope, {});
200+
}
187201

188-
RETURN_IF_EXCEPTION(scope, {});
189202
return array;
190203
}
191204

0 commit comments

Comments
 (0)