From 403e7620058886dba9bcc2ba8a38cc80365179a7 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 29 Sep 2025 16:48:07 -0700 Subject: [PATCH] a --- src/bun.js/bindings/JSBuildMessage.cpp | 24 +++++---- src/bun.js/bindings/JSBuildMessage.h | 3 +- .../bindings/JSBuildMessageConstructor.cpp | 27 ++++++++++ .../bindings/JSBuildMessageConstructor.h | 49 +++++++++++++++++++ src/bun.js/bindings/JSResolveMessage.cpp | 24 +++++---- src/bun.js/bindings/JSResolveMessage.h | 3 +- .../bindings/JSResolveMessageConstructor.cpp | 27 ++++++++++ .../bindings/JSResolveMessageConstructor.h | 49 +++++++++++++++++++ src/bun.js/bindings/ZigGlobalObject.cpp | 12 ++--- src/bun.js/bindings/ZigGlobalObject.h | 4 +- src/bun.js/bindings/ZigGlobalObject.lut.txt | 4 ++ 11 files changed, 198 insertions(+), 28 deletions(-) create mode 100644 src/bun.js/bindings/JSBuildMessageConstructor.cpp create mode 100644 src/bun.js/bindings/JSBuildMessageConstructor.h create mode 100644 src/bun.js/bindings/JSResolveMessageConstructor.cpp create mode 100644 src/bun.js/bindings/JSResolveMessageConstructor.h diff --git a/src/bun.js/bindings/JSBuildMessage.cpp b/src/bun.js/bindings/JSBuildMessage.cpp index 0c31c87c0a..9a86e426ed 100644 --- a/src/bun.js/bindings/JSBuildMessage.cpp +++ b/src/bun.js/bindings/JSBuildMessage.cpp @@ -4,7 +4,9 @@ #include #include #include +#include #include "JSBuildMessage.h" +#include "JSBuildMessageConstructor.h" #include "ZigGlobalObject.h" #include "BunClientData.h" #include "WebCoreJSBuiltins.h" @@ -176,15 +178,19 @@ const ClassInfo BuildMessagePrototype::s_info = { CREATE_METHOD_TABLE(BuildMessagePrototype) }; -// Create the ErrorInstance structure with our custom prototype -JSC::Structure* createBuildMessageStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject) +void setupJSBuildMessageClassStructure(JSC::LazyClassStructure::Initializer& init) { - BuildMessagePrototype* prototype = BuildMessagePrototype::create( - vm, - globalObject, - BuildMessagePrototype::createStructure(vm, globalObject)); + auto* prototypeStructure = BuildMessagePrototype::createStructure(init.vm, init.global); + auto* prototype = BuildMessagePrototype::create(init.vm, init.global, prototypeStructure); - return JSC::ErrorInstance::createStructure(vm, globalObject, prototype); + JSC::FunctionPrototype* functionPrototype = init.global->functionPrototype(); + auto* constructorStructure = JSBuildMessageConstructor::createStructure(init.vm, init.global, functionPrototype); + auto* constructor = JSBuildMessageConstructor::create(init.vm, constructorStructure, prototype); + + auto* structure = JSC::ErrorInstance::createStructure(init.vm, init.global, prototype); + init.setPrototype(prototype); + init.setStructure(structure); + init.setConstructor(constructor); } // Main toJS function called from Zig @@ -197,8 +203,8 @@ extern "C" JSC::EncodedJSValue BuildMessage__toJS(void* buildMessage, JSC::JSGlo BunString messageString = BuildMessage__getMessageString(buildMessage); WTF::String message = messageString.transferToWTFString(); - // Get or create the structure using the lazy property - JSC::Structure* structure = zigGlobalObject->m_buildMessageStructure.getInitializedOnMainThread(zigGlobalObject); + // Get or create the structure using the lazy class structure + JSC::Structure* structure = zigGlobalObject->m_JSBuildMessageClassStructure.get(zigGlobalObject); // Create the ErrorInstance with our custom structure JSC::ErrorInstance* errorInstance = JSC::ErrorInstance::create(vm, structure, message, {}); diff --git a/src/bun.js/bindings/JSBuildMessage.h b/src/bun.js/bindings/JSBuildMessage.h index c16197f97c..736b270d88 100644 --- a/src/bun.js/bindings/JSBuildMessage.h +++ b/src/bun.js/bindings/JSBuildMessage.h @@ -3,10 +3,11 @@ #include "root.h" #include #include +#include namespace Bun { -JSC::Structure* createBuildMessageStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject); +void setupJSBuildMessageClassStructure(JSC::LazyClassStructure::Initializer& init); } // namespace Bun diff --git a/src/bun.js/bindings/JSBuildMessageConstructor.cpp b/src/bun.js/bindings/JSBuildMessageConstructor.cpp new file mode 100644 index 0000000000..de732748bc --- /dev/null +++ b/src/bun.js/bindings/JSBuildMessageConstructor.cpp @@ -0,0 +1,27 @@ +#include "JSBuildMessageConstructor.h" +#include "JSBuildMessage.h" +#include + +using namespace JSC; + +namespace Bun { + +const JSC::ClassInfo JSBuildMessageConstructor::s_info = { "BuildMessage"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSBuildMessageConstructor) }; + +JSC_DEFINE_HOST_FUNCTION(callBuildMessage, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame)) +{ + VM& vm = lexicalGlobalObject->vm(); + ThrowScope scope = DECLARE_THROW_SCOPE(vm); + throwConstructorCannotBeCalledAsFunctionTypeError(lexicalGlobalObject, scope, "BuildMessage"_s); + return {}; +} + +JSC_DEFINE_HOST_FUNCTION(constructBuildMessage, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame)) +{ + JSC::VM& vm = lexicalGlobalObject->vm(); + ThrowScope scope = DECLARE_THROW_SCOPE(vm); + throwTypeError(lexicalGlobalObject, scope, "BuildMessage cannot be constructed directly"_s); + return {}; +} + +} // namespace Bun \ No newline at end of file diff --git a/src/bun.js/bindings/JSBuildMessageConstructor.h b/src/bun.js/bindings/JSBuildMessageConstructor.h new file mode 100644 index 0000000000..f40299fe2d --- /dev/null +++ b/src/bun.js/bindings/JSBuildMessageConstructor.h @@ -0,0 +1,49 @@ +#pragma once + +#include "root.h" +#include + +namespace Bun { + +JSC_DECLARE_HOST_FUNCTION(callBuildMessage); +JSC_DECLARE_HOST_FUNCTION(constructBuildMessage); + +class JSBuildMessageConstructor final : public JSC::InternalFunction { +public: + using Base = JSC::InternalFunction; + static constexpr unsigned StructureFlags = Base::StructureFlags; + + static JSBuildMessageConstructor* create(JSC::VM& vm, JSC::Structure* structure, JSC::JSObject* prototype) + { + JSBuildMessageConstructor* constructor = new (NotNull, JSC::allocateCell(vm)) JSBuildMessageConstructor(vm, structure); + constructor->finishCreation(vm, prototype); + return constructor; + } + + DECLARE_INFO; + + template + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + { + return &vm.internalFunctionSpace(); + } + + static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype) + { + return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::InternalFunctionType, StructureFlags), info()); + } + +private: + JSBuildMessageConstructor(JSC::VM& vm, JSC::Structure* structure) + : Base(vm, structure, callBuildMessage, constructBuildMessage) + { + } + + void finishCreation(JSC::VM& vm, JSC::JSObject* prototype) + { + Base::finishCreation(vm, 0, "BuildMessage"_s); + putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + } +}; + +} // namespace Bun diff --git a/src/bun.js/bindings/JSResolveMessage.cpp b/src/bun.js/bindings/JSResolveMessage.cpp index fdecf4316e..8b88aa6c6a 100644 --- a/src/bun.js/bindings/JSResolveMessage.cpp +++ b/src/bun.js/bindings/JSResolveMessage.cpp @@ -4,7 +4,9 @@ #include #include #include +#include #include "JSResolveMessage.h" +#include "JSResolveMessageConstructor.h" #include "ZigGlobalObject.h" #include "BunClientData.h" #include "WebCoreJSBuiltins.h" @@ -208,15 +210,19 @@ const ClassInfo ResolveMessagePrototype::s_info = { CREATE_METHOD_TABLE(ResolveMessagePrototype) }; -// Create the ErrorInstance structure with our custom prototype -JSC::Structure* createResolveMessageStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject) +void setupJSResolveMessageClassStructure(JSC::LazyClassStructure::Initializer& init) { - ResolveMessagePrototype* prototype = ResolveMessagePrototype::create( - vm, - globalObject, - ResolveMessagePrototype::createStructure(vm, globalObject)); + auto* prototypeStructure = ResolveMessagePrototype::createStructure(init.vm, init.global); + auto* prototype = ResolveMessagePrototype::create(init.vm, init.global, prototypeStructure); - return JSC::ErrorInstance::createStructure(vm, globalObject, prototype); + JSC::FunctionPrototype* functionPrototype = init.global->functionPrototype(); + auto* constructorStructure = JSResolveMessageConstructor::createStructure(init.vm, init.global, functionPrototype); + auto* constructor = JSResolveMessageConstructor::create(init.vm, constructorStructure, prototype); + + auto* structure = JSC::ErrorInstance::createStructure(init.vm, init.global, prototype); + init.setPrototype(prototype); + init.setStructure(structure); + init.setConstructor(constructor); } // Note: Bun__errorInstance__finalize is implemented in ZigGlobalObject.cpp @@ -232,8 +238,8 @@ extern "C" JSC::EncodedJSValue ResolveMessage__toJS(void* resolveMessage, JSC::J BunString messageString = ResolveMessage__getMessageString(resolveMessage); WTF::String message = messageString.transferToWTFString(); - // Get or create the structure using the lazy property - JSC::Structure* structure = zigGlobalObject->m_resolveMessageStructure.getInitializedOnMainThread(zigGlobalObject); + // Get or create the structure using the lazy class structure + JSC::Structure* structure = zigGlobalObject->m_JSResolveMessageClassStructure.get(zigGlobalObject); // Create the ErrorInstance with our custom structure JSC::ErrorInstance* errorInstance = JSC::ErrorInstance::create(vm, structure, message, {}); diff --git a/src/bun.js/bindings/JSResolveMessage.h b/src/bun.js/bindings/JSResolveMessage.h index 7613869cba..44c49a2cda 100644 --- a/src/bun.js/bindings/JSResolveMessage.h +++ b/src/bun.js/bindings/JSResolveMessage.h @@ -3,10 +3,11 @@ #include "root.h" #include #include +#include namespace Bun { -JSC::Structure* createResolveMessageStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject); +void setupJSResolveMessageClassStructure(JSC::LazyClassStructure::Initializer& init); } // namespace Bun diff --git a/src/bun.js/bindings/JSResolveMessageConstructor.cpp b/src/bun.js/bindings/JSResolveMessageConstructor.cpp new file mode 100644 index 0000000000..5874dbcfbf --- /dev/null +++ b/src/bun.js/bindings/JSResolveMessageConstructor.cpp @@ -0,0 +1,27 @@ +#include "JSResolveMessageConstructor.h" +#include "JSResolveMessage.h" +#include + +using namespace JSC; + +namespace Bun { + +const JSC::ClassInfo JSResolveMessageConstructor::s_info = { "ResolveMessage"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSResolveMessageConstructor) }; + +JSC_DEFINE_HOST_FUNCTION(callResolveMessage, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame)) +{ + VM& vm = lexicalGlobalObject->vm(); + ThrowScope scope = DECLARE_THROW_SCOPE(vm); + throwConstructorCannotBeCalledAsFunctionTypeError(lexicalGlobalObject, scope, "ResolveMessage"_s); + return {}; +} + +JSC_DEFINE_HOST_FUNCTION(constructResolveMessage, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame)) +{ + JSC::VM& vm = lexicalGlobalObject->vm(); + ThrowScope scope = DECLARE_THROW_SCOPE(vm); + throwTypeError(lexicalGlobalObject, scope, "ResolveMessage cannot be constructed directly"_s); + return {}; +} + +} // namespace Bun \ No newline at end of file diff --git a/src/bun.js/bindings/JSResolveMessageConstructor.h b/src/bun.js/bindings/JSResolveMessageConstructor.h new file mode 100644 index 0000000000..343196cf7f --- /dev/null +++ b/src/bun.js/bindings/JSResolveMessageConstructor.h @@ -0,0 +1,49 @@ +#pragma once + +#include "root.h" +#include + +namespace Bun { + +JSC_DECLARE_HOST_FUNCTION(callResolveMessage); +JSC_DECLARE_HOST_FUNCTION(constructResolveMessage); + +class JSResolveMessageConstructor final : public JSC::InternalFunction { +public: + using Base = JSC::InternalFunction; + static constexpr unsigned StructureFlags = Base::StructureFlags; + + static JSResolveMessageConstructor* create(JSC::VM& vm, JSC::Structure* structure, JSC::JSObject* prototype) + { + JSResolveMessageConstructor* constructor = new (NotNull, JSC::allocateCell(vm)) JSResolveMessageConstructor(vm, structure); + constructor->finishCreation(vm, prototype); + return constructor; + } + + DECLARE_INFO; + + template + static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) + { + return &vm.internalFunctionSpace(); + } + + static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype) + { + return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::InternalFunctionType, StructureFlags), info()); + } + +private: + JSResolveMessageConstructor(JSC::VM& vm, JSC::Structure* structure) + : Base(vm, structure, callResolveMessage, constructResolveMessage) + { + } + + void finishCreation(JSC::VM& vm, JSC::JSObject* prototype) + { + Base::finishCreation(vm, 0, "ResolveMessage"_s); + putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly); + } +}; + +} // namespace Bun diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index af6e3aa0b5..a4f2c8bf86 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -2941,14 +2941,14 @@ void GlobalObject::finishCreation(VM& vm) init.vm, static_cast(init.owner))); }); - m_resolveMessageStructure.initLater( - [](const JSC::LazyProperty::Initializer& init) { - init.set(Bun::createResolveMessageStructure(init.vm, init.owner)); + m_JSBuildMessageClassStructure.initLater( + [](LazyClassStructure::Initializer& init) { + Bun::setupJSBuildMessageClassStructure(init); }); - m_buildMessageStructure.initLater( - [](const JSC::LazyProperty::Initializer& init) { - init.set(Bun::createBuildMessageStructure(init.vm, init.owner)); + m_JSResolveMessageClassStructure.initLater( + [](LazyClassStructure::Initializer& init) { + Bun::setupJSResolveMessageClassStructure(init); }); m_errorConstructorPrepareStackTraceInternalValue.initLater( diff --git a/src/bun.js/bindings/ZigGlobalObject.h b/src/bun.js/bindings/ZigGlobalObject.h index 66f5723cea..42927c8003 100644 --- a/src/bun.js/bindings/ZigGlobalObject.h +++ b/src/bun.js/bindings/ZigGlobalObject.h @@ -507,14 +507,14 @@ public: \ V(public, LazyPropertyOfGlobalObject, m_JSS3FileStructure) \ V(public, LazyPropertyOfGlobalObject, m_S3ErrorStructure) \ - V(public, LazyPropertyOfGlobalObject, m_resolveMessageStructure) \ - V(public, LazyPropertyOfGlobalObject, m_buildMessageStructure) \ \ V(public, JSC::LazyClassStructure, m_JSStatsClassStructure) \ V(public, JSC::LazyClassStructure, m_JSStatsBigIntClassStructure) \ V(public, JSC::LazyClassStructure, m_JSStatFSClassStructure) \ V(public, JSC::LazyClassStructure, m_JSStatFSBigIntClassStructure) \ V(public, JSC::LazyClassStructure, m_JSDirentClassStructure) \ + V(public, JSC::LazyClassStructure, m_JSBuildMessageClassStructure) \ + V(public, JSC::LazyClassStructure, m_JSResolveMessageClassStructure) \ \ V(private, WebCore::JSBuiltinInternalFunctions, m_builtinInternalFunctions) \ V(private, std::unique_ptr, m_constructors) \ diff --git a/src/bun.js/bindings/ZigGlobalObject.lut.txt b/src/bun.js/bindings/ZigGlobalObject.lut.txt index 0516a6fc05..30eb46fc26 100644 --- a/src/bun.js/bindings/ZigGlobalObject.lut.txt +++ b/src/bun.js/bindings/ZigGlobalObject.lut.txt @@ -42,6 +42,8 @@ AbortController AbortControllerConstructorCallback PropertyCallback AbortSignal AbortSignalConstructorCallback PropertyCallback BroadcastChannel BroadcastChannelConstructorCallback PropertyCallback + BuildError GlobalObject::m_JSBuildMessageClassStructure ClassStructure + BuildMessage GlobalObject::m_JSBuildMessageClassStructure ClassStructure ByteLengthQueuingStrategy ByteLengthQueuingStrategyConstructorCallback PropertyCallback CloseEvent CloseEventConstructorCallback PropertyCallback CountQueuingStrategy CountQueuingStrategyConstructorCallback PropertyCallback @@ -71,6 +73,8 @@ ReadableStreamBYOBRequest ReadableStreamBYOBRequestConstructorCallback PropertyCallback ReadableStreamDefaultController ReadableStreamDefaultControllerConstructorCallback PropertyCallback ReadableStreamDefaultReader ReadableStreamDefaultReaderConstructorCallback PropertyCallback + ResolveError GlobalObject::m_JSResolveMessageClassStructure ClassStructure + ResolveMessage GlobalObject::m_JSResolveMessageClassStructure ClassStructure SubtleCrypto SubtleCryptoConstructorCallback PropertyCallback TextDecoderStream TextDecoderStreamConstructorCallback PropertyCallback TextEncoder TextEncoderConstructorCallback PropertyCallback