diff --git a/src/bun.js/bindings/AsyncContextFrame.cpp b/src/bun.js/bindings/AsyncContextFrame.cpp index 1c393fed39..44de71206d 100644 --- a/src/bun.js/bindings/AsyncContextFrame.cpp +++ b/src/bun.js/bindings/AsyncContextFrame.cpp @@ -14,10 +14,8 @@ const ClassInfo AsyncContextFrame::s_info = { "AsyncContextFrame"_s, &Base::s_in AsyncContextFrame* AsyncContextFrame::create(VM& vm, JSC::Structure* structure, JSValue callback, JSValue context) { - AsyncContextFrame* asyncContextData = new (NotNull, allocateCell(vm)) AsyncContextFrame(vm, structure); + AsyncContextFrame* asyncContextData = new (NotNull, allocateCell(vm)) AsyncContextFrame(vm, structure, callback, context); asyncContextData->finishCreation(vm); - asyncContextData->callback.set(vm, asyncContextData, callback); - asyncContextData->context.set(vm, asyncContextData, context); return asyncContextData; } @@ -26,10 +24,8 @@ AsyncContextFrame* AsyncContextFrame::create(JSGlobalObject* global, JSValue cal auto& vm = global->vm(); ASSERT(callback.isCallable()); auto* structure = jsCast(global)->AsyncContextFrameStructure(); - AsyncContextFrame* asyncContextData = new (NotNull, allocateCell(vm)) AsyncContextFrame(vm, structure); + AsyncContextFrame* asyncContextData = new (NotNull, allocateCell(vm)) AsyncContextFrame(vm, structure, callback, context); asyncContextData->finishCreation(vm); - asyncContextData->callback.set(vm, asyncContextData, callback); - asyncContextData->context.set(vm, asyncContextData, context); return asyncContextData; } diff --git a/src/bun.js/bindings/AsyncContextFrame.h b/src/bun.js/bindings/AsyncContextFrame.h index 164550cdd0..abfb8444f6 100644 --- a/src/bun.js/bindings/AsyncContextFrame.h +++ b/src/bun.js/bindings/AsyncContextFrame.h @@ -57,8 +57,10 @@ public: [](auto& spaces, auto&& space) { spaces.m_subspaceForAsyncContextFrame = std::forward(space); }); } - AsyncContextFrame(JSC::VM& vm, JSC::Structure* structure) + AsyncContextFrame(JSC::VM& vm, JSC::Structure* structure, JSC::JSValue callback_, JSC::JSValue context_) : JSNonFinalObject(vm, structure) + , callback(callback_, JSC::WriteBarrierEarlyInit) + , context(context_, JSC::WriteBarrierEarlyInit) { } }; diff --git a/src/bun.js/bindings/BunPlugin.cpp b/src/bun.js/bindings/BunPlugin.cpp index 61b9d5a84d..4c831054f1 100644 --- a/src/bun.js/bindings/BunPlugin.cpp +++ b/src/bun.js/bindings/BunPlugin.cpp @@ -426,29 +426,29 @@ public: [](auto& spaces, auto&& space) { spaces.m_subspaceForJSModuleMock = std::forward(space); }); } - void finishCreation(JSC::VM&, JSC::JSObject* callback); + void finishCreation(JSC::VM&); private: - JSModuleMock(JSC::VM&, JSC::Structure*); + JSModuleMock(JSC::VM&, JSC::Structure*, JSC::JSObject* callback); }; const JSC::ClassInfo JSModuleMock::s_info = { "ModuleMock"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSModuleMock) }; JSModuleMock* JSModuleMock::create(JSC::VM& vm, JSC::Structure* structure, JSC::JSObject* callback) { - JSModuleMock* ptr = new (NotNull, JSC::allocateCell(vm)) JSModuleMock(vm, structure); - ptr->finishCreation(vm, callback); + JSModuleMock* ptr = new (NotNull, JSC::allocateCell(vm)) JSModuleMock(vm, structure, callback); + ptr->finishCreation(vm); return ptr; } -void JSModuleMock::finishCreation(JSC::VM& vm, JSObject* callback) +void JSModuleMock::finishCreation(JSC::VM& vm) { Base::finishCreation(vm); - callbackFunctionOrCachedResult.set(vm, this, callback); } -JSModuleMock::JSModuleMock(JSC::VM& vm, JSC::Structure* structure) +JSModuleMock::JSModuleMock(JSC::VM& vm, JSC::Structure* structure, JSC::JSObject* callback) : Base(vm, structure) + , callbackFunctionOrCachedResult(callback, JSC::WriteBarrierEarlyInit) { } diff --git a/src/bun.js/bindings/InternalModuleRegistry.cpp b/src/bun.js/bindings/InternalModuleRegistry.cpp index def6cddfb4..4aaa21703a 100644 --- a/src/bun.js/bindings/InternalModuleRegistry.cpp +++ b/src/bun.js/bindings/InternalModuleRegistry.cpp @@ -124,6 +124,11 @@ const ClassInfo InternalModuleRegistry::s_info = { "InternalModuleRegistry"_s, & InternalModuleRegistry::InternalModuleRegistry(VM& vm, Structure* structure) : Base(vm, structure) { + // Initialize all internal fields to jsUndefined() using setWithoutWriteBarrier + // to avoid triggering write barriers during construction + for (uint8_t i = 0; i < BUN_INTERNAL_MODULE_COUNT; i++) { + this->internalField(static_cast(i)).setWithoutWriteBarrier(jsUndefined()); + } } template @@ -147,10 +152,6 @@ void InternalModuleRegistry::finishCreation(VM& vm) { Base::finishCreation(vm); ASSERT(inherits(info())); - - for (uint8_t i = 0; i < BUN_INTERNAL_MODULE_COUNT; i++) { - this->internalField(static_cast(i)).set(vm, this, jsUndefined()); - } } Structure* InternalModuleRegistry::createStructure(VM& vm, JSGlobalObject* globalObject) diff --git a/src/bun.js/bindings/JSBunRequest.cpp b/src/bun.js/bindings/JSBunRequest.cpp index 1dcdf1e47a..3dd0416011 100644 --- a/src/bun.js/bindings/JSBunRequest.cpp +++ b/src/bun.js/bindings/JSBunRequest.cpp @@ -58,8 +58,8 @@ JSBunRequest* JSBunRequest::create(JSC::VM& vm, JSC::Structure* structure, void* // We do not want to risk the GC running before this function is called. Bun__JSRequest__calculateEstimatedByteSize(sinkPtr); - JSBunRequest* ptr = new (NotNull, JSC::allocateCell(vm)) JSBunRequest(vm, structure, sinkPtr); - ptr->finishCreation(vm, params); + JSBunRequest* ptr = new (NotNull, JSC::allocateCell(vm)) JSBunRequest(vm, structure, sinkPtr, params); + ptr->finishCreation(vm); return ptr; } @@ -148,17 +148,17 @@ void JSBunRequest::setCookies(JSObject* cookies) Request__setCookiesOnRequestContext(this->wrapped(), WebCoreCast(JSValue::encode(cookies))); } -JSBunRequest::JSBunRequest(JSC::VM& vm, JSC::Structure* structure, void* sinkPtr) +JSBunRequest::JSBunRequest(JSC::VM& vm, JSC::Structure* structure, void* sinkPtr, JSC::JSObject* params) : Base(vm, structure, sinkPtr) + , m_params(params, JSC::WriteBarrierEarlyInit) + , m_cookies(nullptr, JSC::WriteBarrierEarlyInit) { } extern SYSV_ABI "C" size_t Request__estimatedSize(void* requestPtr); extern "C" void Bun__JSRequest__calculateEstimatedByteSize(void* requestPtr); -void JSBunRequest::finishCreation(JSC::VM& vm, JSObject* params) +void JSBunRequest::finishCreation(JSC::VM& vm) { Base::finishCreation(vm); - m_params.setMayBeNull(vm, this, params); - m_cookies.clear(); auto size = Request__estimatedSize(this->wrapped()); vm.heap.reportExtraMemoryAllocated(this, size); diff --git a/src/bun.js/bindings/JSBunRequest.h b/src/bun.js/bindings/JSBunRequest.h index 0b28b3f391..1eb26605a8 100644 --- a/src/bun.js/bindings/JSBunRequest.h +++ b/src/bun.js/bindings/JSBunRequest.h @@ -39,8 +39,8 @@ public: JSBunRequest* clone(JSC::VM& vm, JSGlobalObject* globalObject); private: - JSBunRequest(JSC::VM& vm, JSC::Structure* structure, void* sinkPtr); - void finishCreation(JSC::VM& vm, JSObject* params); + JSBunRequest(JSC::VM& vm, JSC::Structure* structure, void* sinkPtr, JSC::JSObject* params); + void finishCreation(JSC::VM& vm); mutable JSC::WriteBarrier m_params; mutable JSC::WriteBarrier m_cookies; diff --git a/src/bun.js/bindings/JSCommonJSModule.cpp b/src/bun.js/bindings/JSCommonJSModule.cpp index c0c740ec02..deb611a810 100644 --- a/src/bun.js/bindings/JSCommonJSModule.cpp +++ b/src/bun.js/bindings/JSCommonJSModule.cpp @@ -817,13 +817,10 @@ public: const JSC::ClassInfo JSCommonJSModulePrototype::s_info = { "Module"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCommonJSModulePrototype) }; -void JSCommonJSModule::finishCreation(JSC::VM& vm, JSC::JSString* id, JSValue filename, JSC::JSString* dirname, const JSC::SourceCode& sourceCode) +void JSCommonJSModule::finishCreation(JSC::VM& vm, const JSC::SourceCode& sourceCode) { Base::finishCreation(vm); ASSERT(inherits(info())); - m_id.set(vm, this, id); - m_filename.set(vm, this, filename); - m_dirname.set(vm, this, dirname); this->sourceCode = sourceCode; } @@ -847,8 +844,8 @@ JSCommonJSModule* JSCommonJSModule::create( JSC::JSString* dirname, const JSC::SourceCode& sourceCode) { - JSCommonJSModule* cell = new (NotNull, JSC::allocateCell(vm)) JSCommonJSModule(vm, structure); - cell->finishCreation(vm, id, filename, dirname, sourceCode); + JSCommonJSModule* cell = new (NotNull, JSC::allocateCell(vm)) JSCommonJSModule(vm, structure, id, filename, dirname); + cell->finishCreation(vm, sourceCode); return cell; } diff --git a/src/bun.js/bindings/JSCommonJSModule.h b/src/bun.js/bindings/JSCommonJSModule.h index 0e12fe8316..544289ab85 100644 --- a/src/bun.js/bindings/JSCommonJSModule.h +++ b/src/bun.js/bindings/JSCommonJSModule.h @@ -83,9 +83,7 @@ public: void clearSourceCode() { sourceCode = JSC::SourceCode(); } - void finishCreation(JSC::VM& vm, - JSC::JSString* id, JSValue filename, - JSC::JSString* dirname, const JSC::SourceCode& sourceCode); + void finishCreation(JSC::VM& vm, const JSC::SourceCode& sourceCode); static JSC::Structure* createStructure(JSC::JSGlobalObject* globalObject); @@ -153,8 +151,11 @@ public: bool hasEvaluated = false; - JSCommonJSModule(JSC::VM& vm, JSC::Structure* structure) + JSCommonJSModule(JSC::VM& vm, JSC::Structure* structure, JSC::JSString* id, JSC::JSValue filename, JSC::JSString* dirname) : Base(vm, structure) + , m_id(id, JSC::WriteBarrierEarlyInit) + , m_filename(filename, JSC::WriteBarrierEarlyInit) + , m_dirname(dirname, JSC::WriteBarrierEarlyInit) { } }; diff --git a/src/bun.js/bindings/JSMockFunction.cpp b/src/bun.js/bindings/JSMockFunction.cpp index 34f19ea65f..c705e01bf1 100644 --- a/src/bun.js/bindings/JSMockFunction.cpp +++ b/src/bun.js/bindings/JSMockFunction.cpp @@ -154,8 +154,8 @@ public: static JSMockImplementation* create(JSC::JSGlobalObject* globalObject, JSC::Structure* structure, Kind kind, JSC::JSValue heldValue, bool isOnce) { auto& vm = JSC::getVM(globalObject); - JSMockImplementation* impl = new (NotNull, allocateCell(vm)) JSMockImplementation(vm, structure, kind); - impl->finishCreation(vm, heldValue, isOnce ? jsNumber(1) : jsUndefined()); + JSMockImplementation* impl = new (NotNull, allocateCell(vm)) JSMockImplementation(vm, structure, kind, heldValue, isOnce ? jsNumber(1) : jsUndefined()); + impl->finishCreation(vm); return impl; } @@ -195,17 +195,17 @@ public: return !nextValueOrSentinel.get().isUndefined(); } - JSMockImplementation(JSC::VM& vm, JSC::Structure* structure, Kind kind) + JSMockImplementation(JSC::VM& vm, JSC::Structure* structure, Kind kind, JSC::JSValue first, JSC::JSValue second) : Base(vm, structure) + , underlyingValue(first, JSC::WriteBarrierEarlyInit) + , nextValueOrSentinel(second, JSC::WriteBarrierEarlyInit) , kind(kind) { } - void finishCreation(JSC::VM& vm, JSC::JSValue first, JSC::JSValue second) + void finishCreation(JSC::VM& vm) { Base::finishCreation(vm); - this->underlyingValue.set(vm, this, first); - this->nextValueOrSentinel.set(vm, this, second); } }; diff --git a/src/bun.js/bindings/JSWrappingFunction.cpp b/src/bun.js/bindings/JSWrappingFunction.cpp index d86804128f..5ae814ab01 100644 --- a/src/bun.js/bindings/JSWrappingFunction.cpp +++ b/src/bun.js/bindings/JSWrappingFunction.cpp @@ -33,12 +33,10 @@ JS_EXPORT_PRIVATE JSWrappingFunction* JSWrappingFunction::create( // Structure* structure = globalObject->FFIFunctionStructure(); Structure* structure = JSWrappingFunction::createStructure(vm, globalObject, globalObject->objectPrototype()); - JSWrappingFunction* function = new (NotNull, allocateCell(vm)) JSWrappingFunction(vm, executable, globalObject, structure); + JSWrappingFunction* function = new (NotNull, allocateCell(vm)) JSWrappingFunction(vm, executable, globalObject, structure, wrappedFn); ASSERT(function->structure()->globalObject()); function->finishCreation(vm, executable, 0, nameStr); - function->m_wrappedFn.set(vm, globalObject, wrappedFn); - return function; } diff --git a/src/bun.js/bindings/JSWrappingFunction.h b/src/bun.js/bindings/JSWrappingFunction.h index 1b11fb45de..df86f8334c 100644 --- a/src/bun.js/bindings/JSWrappingFunction.h +++ b/src/bun.js/bindings/JSWrappingFunction.h @@ -59,8 +59,9 @@ public: } private: - JSWrappingFunction(JSC::VM& vm, JSC::NativeExecutable* native, JSC::JSGlobalObject* globalObject, JSC::Structure* structure) + JSWrappingFunction(JSC::VM& vm, JSC::NativeExecutable* native, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, JSC::JSFunction* wrappedFn) : Base(vm, native, globalObject, structure) + , m_wrappedFn(wrappedFn, JSC::WriteBarrierEarlyInit) { } diff --git a/src/bun.js/bindings/NodeVMModule.cpp b/src/bun.js/bindings/NodeVMModule.cpp index f58e20133c..61e479317b 100644 --- a/src/bun.js/bindings/NodeVMModule.cpp +++ b/src/bun.js/bindings/NodeVMModule.cpp @@ -152,11 +152,9 @@ JSValue NodeVMModule::evaluate(JSGlobalObject* globalObject, uint32_t timeout, b NodeVMModule::NodeVMModule(JSC::VM& vm, JSC::Structure* structure, WTF::String identifier, JSValue context, JSValue moduleWrapper) : Base(vm, structure) , m_identifier(WTFMove(identifier)) + , m_context(context && context.isObject() ? asObject(context) : nullptr, JSC::WriteBarrierEarlyInit) , m_moduleWrapper(vm, this, moduleWrapper) { - if (context.isObject()) { - m_context.set(vm, this, asObject(context)); - } } void NodeVMModule::evaluateDependencies(JSGlobalObject* globalObject, AbstractModuleRecord* record, uint32_t timeout, bool breakOnSigint) diff --git a/src/bun.js/bindings/NodeVMSourceTextModule.cpp b/src/bun.js/bindings/NodeVMSourceTextModule.cpp index 0f0b2d8893..af84196723 100644 --- a/src/bun.js/bindings/NodeVMSourceTextModule.cpp +++ b/src/bun.js/bindings/NodeVMSourceTextModule.cpp @@ -105,14 +105,12 @@ NodeVMSourceTextModule* NodeVMSourceTextModule::create(VM& vm, JSGlobalObject* g auto* zigGlobalObject = defaultGlobalObject(globalObject); WTF::String identifier = identifierValue.toWTFString(globalObject); RETURN_IF_EXCEPTION(scope, nullptr); - NodeVMSourceTextModule* ptr = new (NotNull, allocateCell(vm)) NodeVMSourceTextModule(vm, zigGlobalObject->NodeVMSourceTextModuleStructure(), WTFMove(identifier), contextValue, WTFMove(sourceCode), moduleWrapper); + NodeVMSourceTextModule* ptr = new (NotNull, allocateCell(vm)) NodeVMSourceTextModule( + vm, zigGlobalObject->NodeVMSourceTextModuleStructure(), WTFMove(identifier), contextValue, + WTFMove(sourceCode), moduleWrapper, initializeImportMeta); RETURN_IF_EXCEPTION(scope, nullptr); ptr->finishCreation(vm); - if (!initializeImportMeta.isUndefined()) { - ptr->m_initializeImportMeta.set(vm, ptr, initializeImportMeta); - } - if (cachedData.isEmpty()) { return ptr; } @@ -417,7 +415,7 @@ JSUint8Array* NodeVMSourceTextModule::cachedData(JSGlobalObject* globalObject) void NodeVMSourceTextModule::initializeImportMeta(JSGlobalObject* globalObject) { - if (!m_initializeImportMeta) { + if (!m_initializeImportMeta || !m_initializeImportMeta.get().isCallable()) { return; } @@ -429,8 +427,9 @@ void NodeVMSourceTextModule::initializeImportMeta(JSGlobalObject* globalObject) JSValue metaValue = moduleEnvironment->get(globalObject, globalObject->vm().propertyNames->builtinNames().metaPrivateName()); scope.assertNoExceptionExceptTermination(); RETURN_IF_EXCEPTION(scope, ); - ASSERT(metaValue); - ASSERT(metaValue.isObject()); + if (!metaValue || !metaValue.isObject()) { + return; + } CallData callData = JSC::getCallData(m_initializeImportMeta.get()); diff --git a/src/bun.js/bindings/NodeVMSourceTextModule.h b/src/bun.js/bindings/NodeVMSourceTextModule.h index 3b4f78cab6..e7e27cfdfc 100644 --- a/src/bun.js/bindings/NodeVMSourceTextModule.h +++ b/src/bun.js/bindings/NodeVMSourceTextModule.h @@ -57,8 +57,9 @@ private: RefPtr m_bytecode; SourceCode m_sourceCode; - NodeVMSourceTextModule(JSC::VM& vm, JSC::Structure* structure, WTF::String identifier, JSValue context, SourceCode sourceCode, JSValue moduleWrapper) + NodeVMSourceTextModule(JSC::VM& vm, JSC::Structure* structure, WTF::String identifier, JSValue context, SourceCode sourceCode, JSValue moduleWrapper, JSValue initializeImportMeta) : Base(vm, structure, WTFMove(identifier), context, moduleWrapper) + , m_initializeImportMeta(initializeImportMeta && !initializeImportMeta.isUndefined() ? initializeImportMeta : JSValue(), JSC::WriteBarrierEarlyInit) , m_sourceCode(WTFMove(sourceCode)) { } diff --git a/src/bun.js/bindings/node/JSNodeHTTPServerSocket.cpp b/src/bun.js/bindings/node/JSNodeHTTPServerSocket.cpp index 1e5700262b..d9e684fb15 100644 --- a/src/bun.js/bindings/node/JSNodeHTTPServerSocket.cpp +++ b/src/bun.js/bindings/node/JSNodeHTTPServerSocket.cpp @@ -97,8 +97,8 @@ JSNodeHTTPServerSocket::JSNodeHTTPServerSocket(JSC::VM& vm, JSC::Structure* stru : JSC::JSDestructibleObject(vm, structure) , socket(socket) , is_ssl(is_ssl) + , currentResponseObject(response, JSC::WriteBarrierEarlyInit) { - currentResponseObject.setEarlyValue(vm, this, response); } void JSNodeHTTPServerSocket::detach() diff --git a/src/bun.js/bindings/node/http/JSConnectionsList.cpp b/src/bun.js/bindings/node/http/JSConnectionsList.cpp index bdc549c83c..a1660fa836 100644 --- a/src/bun.js/bindings/node/http/JSConnectionsList.cpp +++ b/src/bun.js/bindings/node/http/JSConnectionsList.cpp @@ -12,13 +12,10 @@ using namespace JSC; const ClassInfo JSConnectionsList::s_info = { "ConnectionsList"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSConnectionsList) }; -void JSConnectionsList::finishCreation(VM& vm, JSGlobalObject* globalObject, JSSet* allConnections, JSSet* activeConnections) +void JSConnectionsList::finishCreation(VM& vm, JSGlobalObject* globalObject) { Base::finishCreation(vm); ASSERT(inherits(info())); - - m_allConnections.set(vm, this, allConnections); - m_activeConnections.set(vm, this, activeConnections); } template diff --git a/src/bun.js/bindings/node/http/JSConnectionsList.h b/src/bun.js/bindings/node/http/JSConnectionsList.h index 8df792af17..3c52f82c29 100644 --- a/src/bun.js/bindings/node/http/JSConnectionsList.h +++ b/src/bun.js/bindings/node/http/JSConnectionsList.h @@ -20,8 +20,8 @@ public: static JSConnectionsList* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, JSC::JSSet* allConnectionsSet, JSC::JSSet* activeConnectionsSet) { - JSConnectionsList* instance = new (NotNull, JSC::allocateCell(vm)) JSConnectionsList(vm, structure); - instance->finishCreation(vm, globalObject, allConnectionsSet, activeConnectionsSet); + JSConnectionsList* instance = new (NotNull, JSC::allocateCell(vm)) JSConnectionsList(vm, structure, allConnectionsSet, activeConnectionsSet); + instance->finishCreation(vm, globalObject); return instance; } @@ -41,10 +41,12 @@ public: DECLARE_INFO; DECLARE_VISIT_CHILDREN; - void finishCreation(JSC::VM&, JSC::JSGlobalObject*, JSC::JSSet* allConnectionsSet, JSC::JSSet* activeConnectionsSet); + void finishCreation(JSC::VM&, JSC::JSGlobalObject*); - JSConnectionsList(JSC::VM& vm, JSC::Structure* structure) + JSConnectionsList(JSC::VM& vm, JSC::Structure* structure, JSC::JSSet* allConnections, JSC::JSSet* activeConnections) : Base(vm, structure) + , m_allConnections(allConnections, JSC::WriteBarrierEarlyInit) + , m_activeConnections(activeConnections, JSC::WriteBarrierEarlyInit) { }