Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/bun.js/bindings/ProcessIdentifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,12 @@
namespace WebCore {
namespace Process {

static std::optional<ProcessIdentifier> globalIdentifier;

void setIdentifier(ProcessIdentifier processIdentifier)
{
ASSERT(isUIThread());
globalIdentifier = processIdentifier;
}
// Bun only has 1 process
static ProcessIdentifier globalIdentifier { 1 };

ProcessIdentifier identifier()
{
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
if (!globalIdentifier)
globalIdentifier = ProcessIdentifier::generate();
});

return *globalIdentifier;
return globalIdentifier;
}

} // namespace ProcessIdent
Expand Down
1 change: 0 additions & 1 deletion src/bun.js/bindings/ProcessIdentifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ using ProcessIdentifier = ObjectIdentifier<ProcessIdentifierType>;

namespace Process {

WEBCORE_EXPORT void setIdentifier(ProcessIdentifier);
WEBCORE_EXPORT ProcessIdentifier identifier();

} // namespace Process
Expand Down
1 change: 0 additions & 1 deletion src/bun.js/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@
#include "Performance.h"
#include "ProcessBindingConstants.h"
#include "ProcessBindingTTYWrap.h"
#include "ProcessIdentifier.h"
#include "ReadableStream.h"
#include "SerializedScriptValue.h"
#include "StructuredClone.h"
Expand Down
6 changes: 3 additions & 3 deletions src/bun.js/bindings/webcore/JSWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ template<> JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES JSWorkerDOMConstructor::
}
RETURN_IF_EXCEPTION(throwScope, {});
EnsureStillAliveScope argument1 = callFrame->argument(1);

WorkerOptions options {};
JSValue nodeWorkerObject {};
if (callFrame->argumentCount() == 3) {
nodeWorkerObject = callFrame->argument(2);
options.kind = WorkerOptions::Kind::Node;
}
RETURN_IF_EXCEPTION(throwScope, {});

auto options = WorkerOptions {};
JSValue workerData = jsUndefined();
Vector<JSC::Strong<JSC::JSObject>> transferList;

Expand Down
38 changes: 33 additions & 5 deletions src/bun.js/bindings/webcore/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,10 @@ void Worker::fireEarlyMessages(Zig::GlobalObject* workerGlobalObject)
}
}

void Worker::dispatchError(WTF::String message)
void Worker::dispatchErrorWithMessage(WTF::String message)
{

auto* ctx = scriptExecutionContext();
if (!ctx)
return;
if (!ctx) return;

ScriptExecutionContext::postTaskTo(ctx->identifier(), [protectedThis = Ref { *this }, message = message.isolatedCopy()](ScriptExecutionContext& context) -> void {
ErrorEvent::Init init;
Expand All @@ -404,6 +402,27 @@ void Worker::dispatchError(WTF::String message)
protectedThis->dispatchEvent(event);
});
}

bool Worker::dispatchErrorWithValue(Zig::GlobalObject* workerGlobalObject, JSValue value)
{
auto* ctx = scriptExecutionContext();
if (!ctx) return false;
auto serialized = SerializedScriptValue::create(*workerGlobalObject, value, SerializationForStorage::No, SerializationErrorMode::NonThrowing);
if (!serialized) return false;

ScriptExecutionContext::postTaskTo(ctx->identifier(), [protectedThis = Ref { *this }, serialized](ScriptExecutionContext& context) -> void {
auto* globalObject = context.globalObject();
ErrorEvent::Init init;
JSValue deserialized = serialized->deserialize(*globalObject, globalObject, SerializationErrorMode::NonThrowing);
if (!deserialized) return;
init.error = deserialized;

auto event = ErrorEvent::create(eventNames().errorEvent, init, EventIsTrusted::Yes);
protectedThis->dispatchEvent(event);
});
return true;
}

void Worker::dispatchExit(int32_t exitCode)
{
auto* ctx = scriptExecutionContext();
Expand Down Expand Up @@ -483,7 +502,16 @@ extern "C" void WebWorker__dispatchError(Zig::GlobalObject* globalObject, Worker
init.bubbles = false;

globalObject->globalEventScope->dispatchEvent(ErrorEvent::create(eventNames().errorEvent, init, EventIsTrusted::Yes));
worker->dispatchError(message.toWTFString(BunString::ZeroCopy));
switch (worker->options().kind) {
case WorkerOptions::Kind::Web:
return worker->dispatchErrorWithMessage(message.toWTFString(BunString::ZeroCopy));
case WorkerOptions::Kind::Node:
if (!worker->dispatchErrorWithValue(globalObject, error)) {
// If serialization threw an error, use the string instead
worker->dispatchErrorWithMessage(message.toWTFString(BunString::ZeroCopy));
}
return;
}
}

extern "C" WebCore::Worker* WebWorker__getParentWorker(void* bunVM);
Expand Down
33 changes: 3 additions & 30 deletions src/bun.js/bindings/webcore/Worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@

#include "ActiveDOMObject.h"
#include "EventTarget.h"
// #include "MessagePort.h"
#include "WorkerOptions.h"
// #include "WorkerScriptLoaderClient.h"
// #include "WorkerType.h"
#include <JavaScriptCore/RuntimeFlags.h>
#include <wtf/Deque.h>
#include <wtf/MonotonicTime.h>
Expand All @@ -50,7 +47,6 @@ class RTCRtpScriptTransform;
class RTCRtpScriptTransformer;
class ScriptExecutionContext;
class WorkerGlobalScopeProxy;
// class WorkerScriptLoader;

struct StructuredSerializeOptions;
struct WorkerOptions;
Expand Down Expand Up @@ -80,15 +76,6 @@ class Worker final : public ThreadSafeRefCounted<Worker>, public EventTargetWith
void dispatchCloseEvent(Event&);
void setKeepAlive(bool);

#if ENABLE(WEB_RTC)
void createRTCRtpScriptTransformer(RTCRtpScriptTransform&, MessageWithMessagePorts&&);
#endif

// WorkerType type() const
// {
// return m_options.type;
// }

void postTaskToWorkerGlobalScope(Function<void(ScriptExecutionContext&)>&&);

static void forEachWorker(const Function<Function<void(ScriptExecutionContext&)>()>&);
Expand All @@ -97,7 +84,9 @@ class Worker final : public ThreadSafeRefCounted<Worker>, public EventTargetWith
void dispatchOnline(Zig::GlobalObject* workerGlobalObject);
// Fire a 'message' event in the Worker for messages that were sent before the Worker started running
void fireEarlyMessages(Zig::GlobalObject* workerGlobalObject);
void dispatchError(WTF::String message);
void dispatchErrorWithMessage(WTF::String message);
// true if successful
bool dispatchErrorWithValue(Zig::GlobalObject* workerGlobalObject, JSValue value);
void dispatchExit(int32_t exitCode);
ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
ScriptExecutionContextIdentifier clientIdentifier() const { return m_clientIdentifier; }
Expand All @@ -111,32 +100,16 @@ class Worker final : public ThreadSafeRefCounted<Worker>, public EventTargetWith
void derefEventTarget() final { deref(); }
void eventListenersDidChange() final {};

// void didReceiveResponse(ResourceLoaderIdentifier, const ResourceResponse&) final;
// void notifyFinished() final;

// ActiveDOMObject.
// void stop() final;
// void suspend(ReasonForSuspension) final;
// void resume() final;
// const char* activeDOMObjectName() const final;
// bool virtualHasPendingActivity() const final;

static void networkStateChanged(bool isOnLine);

static constexpr uint8_t OnlineFlag = 1 << 0;
static constexpr uint8_t ClosingFlag = 1 << 1;
static constexpr uint8_t TerminateRequestedFlag = 1 << 0;
static constexpr uint8_t TerminatedFlag = 1 << 1;

// RefPtr<WorkerScriptLoader> m_scriptLoader;
WorkerOptions m_options;
String m_identifier;
// WorkerGlobalScopeProxy& m_contextProxy; // The proxy outlives the worker to perform thread shutdown.
// std::optional<ContentSecurityPolicyResponseHeaders> m_contentSecurityPolicyResponseHeaders;
MonotonicTime m_workerCreationTime;
// bool m_shouldBypassMainWorldContentSecurityPolicy { false };
// bool m_isSuspendedForBackForwardCache { false };
// JSC::RuntimeFlags m_runtimeFlags;
Deque<RefPtr<Event>> m_pendingEvents;
Lock m_pendingTasksMutex;
Deque<Function<void(ScriptExecutionContext&)>> m_pendingTasks;
Expand Down
8 changes: 8 additions & 0 deletions src/bun.js/bindings/webcore/WorkerOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
namespace WebCore {

struct WorkerOptions {
enum class Kind : uint8_t {
// Created by the global Worker constructor
Web,
// Created by the `require("node:worker_threads").Worker` constructor
Node,
};

String name;
bool mini { false };
bool unref { false };
Expand All @@ -16,6 +23,7 @@ struct WorkerOptions {
// true, then we need to make sure that `process.argv` contains "[worker eval]" instead of the
// Blob URL.
bool evalMode { false };
Kind kind { Kind::Web };
// Serialized array containing [workerData, environmentData]
// (environmentData is always a Map)
RefPtr<SerializedScriptValue> workerDataAndEnvironmentData;
Expand Down
Loading