diff options
Diffstat (limited to 'src/bun.js/bindings/ZigGlobalObject.cpp')
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index b7171b21c..1aefe476c 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -119,6 +119,9 @@ #include "DOMIsoSubspaces.h" #include "BunWorkerGlobalScope.h" #include "JSWorker.h" +#include "JSMessageChannel.h" +#include "JSMessagePort.h" +// #include "JSBroadcastChannel.h" #if ENABLE(REMOTE_INSPECTOR) #include "JavaScriptCore/RemoteInspectorServer.h" @@ -718,6 +721,7 @@ GlobalObject::GlobalObject(JSC::VM& vm, JSC::Structure* structure) , m_scriptExecutionContext(new WebCore::ScriptExecutionContext(&vm, this)) , globalEventScope(*new Bun::GlobalScope(m_scriptExecutionContext)) { + // m_scriptExecutionContext = globalEventScope.m_context; mockModule = Bun::JSMockModule::create(this); globalEventScope.m_context = m_scriptExecutionContext; } @@ -732,6 +736,7 @@ GlobalObject::GlobalObject(JSC::VM& vm, JSC::Structure* structure, WebCore::Scri , m_scriptExecutionContext(new WebCore::ScriptExecutionContext(&vm, this, contextId)) , globalEventScope(*new Bun::GlobalScope(m_scriptExecutionContext)) { + // m_scriptExecutionContext = globalEventScope.m_context; mockModule = Bun::JSMockModule::create(this); globalEventScope.m_context = m_scriptExecutionContext; } @@ -931,6 +936,15 @@ WEBCORE_GENERATED_CONSTRUCTOR_SETTER(JSDOMFormData); WEBCORE_GENERATED_CONSTRUCTOR_GETTER(JSWorker); WEBCORE_GENERATED_CONSTRUCTOR_SETTER(JSWorker); +WEBCORE_GENERATED_CONSTRUCTOR_GETTER(JSMessageChannel); +WEBCORE_GENERATED_CONSTRUCTOR_SETTER(JSMessageChannel); + +WEBCORE_GENERATED_CONSTRUCTOR_GETTER(JSMessagePort); +WEBCORE_GENERATED_CONSTRUCTOR_SETTER(JSMessagePort); + +// WEBCORE_GENERATED_CONSTRUCTOR_GETTER(JSBroadcastChannel); +// WEBCORE_GENERATED_CONSTRUCTOR_SETTER(JSBroadcastChannel); + JSC_DECLARE_CUSTOM_GETTER(JSEvent_getter); JSC_DEFINE_CUSTOM_GETTER(JSEvent_getter, @@ -1285,13 +1299,14 @@ JSC_DEFINE_HOST_FUNCTION(functionStructuredClone, } } - ExceptionOr<Ref<SerializedScriptValue>> serialized = SerializedScriptValue::create(*globalObject, value, WTFMove(transferList)); + Vector<RefPtr<MessagePort>> ports; + ExceptionOr<Ref<SerializedScriptValue>> serialized = SerializedScriptValue::create(*globalObject, value, WTFMove(transferList), ports); if (serialized.hasException()) { WebCore::propagateException(*globalObject, throwScope, serialized.releaseException()); return JSValue::encode(jsUndefined()); } - JSValue deserialized = serialized.releaseReturnValue()->deserialize(*globalObject, globalObject); + JSValue deserialized = serialized.releaseReturnValue()->deserialize(*globalObject, globalObject, ports); return JSValue::encode(deserialized); } @@ -3665,26 +3680,28 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionPostMessage, } } - ExceptionOr<Ref<SerializedScriptValue>> serialized = SerializedScriptValue::create(*globalObject, value, WTFMove(transferList)); + Vector<RefPtr<MessagePort>> ports; + ExceptionOr<Ref<SerializedScriptValue>> serialized = SerializedScriptValue::create(*globalObject, value, WTFMove(transferList), ports); if (serialized.hasException()) { WebCore::propagateException(*globalObject, throwScope, serialized.releaseException()); return JSValue::encode(jsUndefined()); } - RefPtr<SerializedScriptValue> message = serialized.releaseReturnValue(); - ScriptExecutionContext::postTaskTo(context->identifier(), [message = WTFMove(message), protectedThis = Ref { *worker }](ScriptExecutionContext& context) { + ExceptionOr<Vector<TransferredMessagePort>> disentangledPorts = MessagePort::disentanglePorts(WTFMove(ports)); + if (disentangledPorts.hasException()) { + WebCore::propagateException(*globalObject, throwScope, serialized.releaseException()); + return JSValue::encode(jsUndefined()); + } + + MessageWithMessagePorts messageWithMessagePorts { serialized.releaseReturnValue(), disentangledPorts.releaseReturnValue() }; + + ScriptExecutionContext::postTaskTo(context->identifier(), [message = messageWithMessagePorts, protectedThis = Ref { *worker }, ports](ScriptExecutionContext& context) mutable { Zig::GlobalObject* globalObject = jsCast<Zig::GlobalObject*>(context.jsGlobalObject()); - bool didFail = false; - JSValue value = message->deserialize(*globalObject, globalObject, SerializationErrorMode::NonThrowing, &didFail); - if (didFail) { - protectedThis->dispatchEvent(MessageEvent::create(eventNames().messageerrorEvent, MessageEvent::Init {}, MessageEvent::IsTrusted::Yes)); - return; - } + auto ports = MessagePort::entanglePorts(context, WTFMove(message.transferredPorts)); + auto event = MessageEvent::create(*globalObject, message.message.releaseNonNull(), std::nullopt, WTFMove(ports)); - WebCore::MessageEvent::Init init; - init.data = value; - protectedThis->dispatchEvent(MessageEvent::create(eventNames().messageEvent, WTFMove(init), MessageEvent::IsTrusted::Yes)); + protectedThis->dispatchEvent(event.event); }); return JSValue::encode(jsUndefined()); @@ -4227,6 +4244,9 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm) PUT_WEBCORE_GENERATED_CONSTRUCTOR("Headers"_s, JSFetchHeaders); PUT_WEBCORE_GENERATED_CONSTRUCTOR("URLSearchParams"_s, JSURLSearchParams); PUT_WEBCORE_GENERATED_CONSTRUCTOR("Worker"_s, JSWorker); + PUT_WEBCORE_GENERATED_CONSTRUCTOR("MessageChannel"_s, JSMessageChannel); + PUT_WEBCORE_GENERATED_CONSTRUCTOR("MessagePort"_s, JSMessagePort); + // PUT_WEBCORE_GENERATED_CONSTRUCTOR("BroadcastChannel"_s, JSBroadcastChannel); putDirectCustomAccessor(vm, builtinNames.TransformStreamPublicName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_TransformStreamConstructor, nullptr), attributesForStructure(static_cast<unsigned>(JSC::PropertyAttribute::DontEnum))); putDirectCustomAccessor(vm, builtinNames.TransformStreamPrivateName(), CustomGetterSetter::create(vm, jsServiceWorkerGlobalScope_TransformStreamConstructor, nullptr), attributesForStructure(static_cast<unsigned>(JSC::PropertyAttribute::DontEnum))); @@ -4619,6 +4639,9 @@ void GlobalObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) visitor.append(thisObject->m_JSURLSearchParamsSetterValue); visitor.append(thisObject->m_JSDOMFormDataSetterValue); visitor.append(thisObject->m_JSWorkerSetterValue); + visitor.append(thisObject->m_JSMessageChannelSetterValue); + visitor.append(thisObject->m_JSMessagePortSetterValue); + visitor.append(thisObject->m_JSBroadcastChannelSetterValue); thisObject->m_JSArrayBufferSinkClassStructure.visit(visitor); thisObject->m_JSBufferListClassStructure.visit(visitor); |