Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 1 addition & 4 deletions .claude/skills/implementing-jsc-classes-cpp/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm) {
return nullptr;
return WebCore::subspaceForImpl<MyClassT, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForMyClassT.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForMyClassT = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForMyClassT.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForMyClassT = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForMyClassT, &WebCore::DOMIsoSubspaces::m_subspaceForMyClassT);
}
```

Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/source-lints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ on:
branches: [main]
paths:
- "src/**/*.rs"
- "src/**/*.h"
- "src/**/*.cpp"
- "src/**/*.classes.ts"
- "src/codegen/class-definitions.ts"
- "src/codegen/**"
- "src/js/builtins.d.ts"
- "src/jsc/bindings/**"
- "packages/bun-types/redis.d.ts"
Expand All @@ -34,8 +36,10 @@ on:
pull_request:
paths:
- "src/**/*.rs"
- "src/**/*.h"
- "src/**/*.cpp"
- "src/**/*.classes.ts"
- "src/codegen/class-definitions.ts"
- "src/codegen/**"
- "src/js/builtins.d.ts"
- "src/jsc/bindings/**"
- "packages/bun-types/redis.d.ts"
Expand Down
10 changes: 2 additions & 8 deletions src/codegen/generate-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,7 @@ class ${name} final : public JSC::InternalFunction {

return WebCore::subspaceForImpl<${name}, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.${clientSubspaceFor("BunClass")}Constructor.get(); },
[](auto& spaces, auto&& space) { spaces.${clientSubspaceFor("BunClass")}Constructor = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.${subspaceFor("BunClass")}Constructor.get(); },
[](auto& spaces, auto&& space) { spaces.${subspaceFor("BunClass")}Constructor = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::${clientSubspaceFor("BunClass")}Constructor, &WebCore::DOMIsoSubspaces::${subspaceFor("BunClass")}Constructor);
}

// Must be defined for each specialization class.
Expand Down Expand Up @@ -1129,10 +1126,7 @@ function generateClassHeader(typeName, obj: ClassDefinition) {
return nullptr;
return WebCore::subspaceForImpl<${name}, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.${clientSubspaceFor(typeName)}.get(); },
[](auto& spaces, auto&& space) { spaces.${clientSubspaceFor(typeName)} = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.${subspaceFor(typeName)}.get(); },
[](auto& spaces, auto&& space) { spaces.${subspaceFor(typeName)} = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::${clientSubspaceFor(typeName)}, &WebCore::DOMIsoSubspaces::${subspaceFor(typeName)});
}

static void destroy(JSC::JSCell*);
Expand Down
15 changes: 3 additions & 12 deletions src/codegen/generate-jssink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ function header() {
return nullptr;
return WebCore::subspaceForImpl<${constructor}, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForJSSinkConstructor.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForJSSinkConstructor = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForJSSinkConstructor.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForJSSinkConstructor = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForJSSinkConstructor, &WebCore::DOMIsoSubspaces::m_subspaceForJSSinkConstructor);
}

static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
Expand Down Expand Up @@ -81,10 +78,7 @@ function header() {
return nullptr;
return WebCore::subspaceForImpl<${className}, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForJSSink.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForJSSink = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForJSSink.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForJSSink = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForJSSink, &WebCore::DOMIsoSubspaces::m_subspaceForJSSink);
}

static void destroy(JSC::JSCell*);
Expand Down Expand Up @@ -142,10 +136,7 @@ function header() {
return nullptr;
return WebCore::subspaceForImpl<${controller}, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForJSSinkController.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForJSSinkController = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForJSSinkController.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForJSSinkController = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForJSSinkController, &WebCore::DOMIsoSubspaces::m_subspaceForJSSinkController);
}

static void destroy(JSC::JSCell*);
Expand Down
5 changes: 1 addition & 4 deletions src/jsc/bindings/AsyncContextFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ class AsyncContextFrame : public JSC::JSNonFinalObject {
return nullptr;
return WebCore::subspaceForImpl<AsyncContextFrame, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForAsyncContextFrame.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForAsyncContextFrame = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForAsyncContextFrame.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForAsyncContextFrame = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForAsyncContextFrame, &WebCore::DOMIsoSubspaces::m_subspaceForAsyncContextFrame);
}

AsyncContextFrame(JSC::VM& vm, JSC::Structure* structure, JSC::JSValue callback_, JSC::JSValue context_)
Expand Down
19 changes: 19 additions & 0 deletions src/jsc/bindings/BunClientData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,23 @@ void JSVMClientData::create(VM* vm, void* bunVM, bool isWorkerVM)
clientData->builtinFunctions().exportNames();
}

NEVER_INLINE JSC::GCClient::IsoSubspace* subspaceForImplSlow(JSC::VM& vm, ClientIsoSubspaceSlot clientSlot, ServerIsoSubspaceSlot serverSlot, const JSC::HeapCellType& cellType, size_t cellSize, uint8_t numberOfLowerTierPreciseCells, bool hasOutputConstraints)
{
auto& clientData = *downcast<JSVMClientData>(vm.clientData);
auto& heapData = clientData.heapData();
Locker locker { heapData.lock() };

auto& space = heapData.subspaces().*serverSlot;
if (!space) {
// "T" matches what ISO_SUBSPACE_INIT(heap, cellType, T) stringized before.
space = makeUnique<JSC::IsoSubspace>("T"_s, vm.heap, cellType, cellSize, numberOfLowerTierPreciseCells);
if (hasOutputConstraints)
heapData.outputConstraintSpaces().append(space.get());
}

auto& clientSpace = clientData.clientSubspaces().*clientSlot;
clientSpace = makeUnique<JSC::GCClient::IsoSubspace>(*space);
return clientSpace.get();
}

} // namespace WebCore
66 changes: 28 additions & 38 deletions src/jsc/bindings/BunClientData.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,48 +257,38 @@ SPECIALIZE_TYPE_TRAITS_END()

namespace WebCore {

template<typename T, UseCustomHeapCellType useCustomHeapCellType, typename GetClient, typename SetClient, typename GetServer, typename SetServer>
ALWAYS_INLINE JSC::GCClient::IsoSubspace* subspaceForImpl(JSC::VM& vm, GetClient getClient, SetClient setClient, GetServer getServer, SetServer setServer, JSC::HeapCellType& (*getCustomHeapCellType)(JSHeapData&) = nullptr)
using ClientIsoSubspaceSlot = std::unique_ptr<JSC::GCClient::IsoSubspace> DOMClientIsoSubspaces::*;
using ServerIsoSubspaceSlot = std::unique_ptr<JSC::IsoSubspace> DOMIsoSubspaces::*;

// Creates the server + client IsoSubspace for one cell type. Runs once per
// (VM, type); everything hot stays in subspaceForImpl's inline fast path.
JSC::GCClient::IsoSubspace* subspaceForImplSlow(JSC::VM&, ClientIsoSubspaceSlot, ServerIsoSubspaceSlot, const JSC::HeapCellType&, size_t cellSize, uint8_t numberOfLowerTierPreciseCells, bool hasOutputConstraints);

template<typename T, UseCustomHeapCellType useCustomHeapCellType>
ALWAYS_INLINE JSC::GCClient::IsoSubspace* subspaceForImpl(JSC::VM& vm, ClientIsoSubspaceSlot clientSlot, ServerIsoSubspaceSlot serverSlot, JSC::HeapCellType& (*getCustomHeapCellType)(JSHeapData&) = nullptr)
Comment thread
robobun marked this conversation as resolved.
Resolved
{
auto& clientData = *downcast<JSVMClientData>(vm.clientData);
auto& clientSubspaces = clientData.clientSubspaces();
if (auto* clientSpace = getClient(clientSubspaces))
if (auto* clientSpace = (clientData.clientSubspaces().*clientSlot).get())
return clientSpace;

auto& heapData = clientData.heapData();
Locker locker { heapData.lock() };

auto& subspaces = heapData.subspaces();
JSC::IsoSubspace* space = getServer(subspaces);
if (!space) {
JSC::Heap& heap = vm.heap;
std::unique_ptr<JSC::IsoSubspace> uniqueSubspace;
static_assert(useCustomHeapCellType == UseCustomHeapCellType::Yes || std::is_base_of_v<JSC::JSDestructibleObject, T> || T::needsDestruction == JSC::DoesNotNeedDestruction);
if constexpr (useCustomHeapCellType == UseCustomHeapCellType::Yes)
uniqueSubspace = makeUnique<JSC::IsoSubspace> ISO_SUBSPACE_INIT(heap, getCustomHeapCellType(heapData), T);
else {
if constexpr (std::is_base_of_v<JSC::JSDestructibleObject, T>)
uniqueSubspace = makeUnique<JSC::IsoSubspace> ISO_SUBSPACE_INIT(heap, heap.destructibleObjectHeapCellType, T);
else
uniqueSubspace = makeUnique<JSC::IsoSubspace> ISO_SUBSPACE_INIT(heap, heap.cellHeapCellType, T);
}
space = uniqueSubspace.get();
setServer(subspaces, WTF::move(uniqueSubspace));

IGNORE_WARNINGS_BEGIN("unreachable-code")
IGNORE_WARNINGS_BEGIN("tautological-compare")
void (*myVisitOutputConstraint)(JSC::JSCell*, JSC::SlotVisitor&) = T::visitOutputConstraints;
void (*jsCellVisitOutputConstraint)(JSC::JSCell*, JSC::SlotVisitor&) = JSC::JSCell::visitOutputConstraints;
if (myVisitOutputConstraint != jsCellVisitOutputConstraint)
heapData.outputConstraintSpaces().append(space);
IGNORE_WARNINGS_END
IGNORE_WARNINGS_END
}

auto uniqueClientSubspace = makeUnique<JSC::GCClient::IsoSubspace>(*space);
auto* clientSpace = uniqueClientSubspace.get();
setClient(clientSubspaces, WTF::move(uniqueClientSubspace));
return clientSpace;
static_assert(useCustomHeapCellType == UseCustomHeapCellType::Yes || std::is_base_of_v<JSC::JSDestructibleObject, T> || T::needsDestruction == JSC::DoesNotNeedDestruction);
const JSC::HeapCellType* cellType;
if constexpr (useCustomHeapCellType == UseCustomHeapCellType::Yes)
cellType = &getCustomHeapCellType(clientData.heapData());
else if constexpr (std::is_base_of_v<JSC::JSDestructibleObject, T>)
cellType = &vm.heap.destructibleObjectHeapCellType;
else
cellType = &vm.heap.cellHeapCellType;

IGNORE_WARNINGS_BEGIN("unreachable-code")
IGNORE_WARNINGS_BEGIN("tautological-compare")
void (*myVisitOutputConstraint)(JSC::JSCell*, JSC::SlotVisitor&) = T::visitOutputConstraints;
void (*jsCellVisitOutputConstraint)(JSC::JSCell*, JSC::SlotVisitor&) = JSC::JSCell::visitOutputConstraints;
bool hasOutputConstraints = myVisitOutputConstraint != jsCellVisitOutputConstraint;
IGNORE_WARNINGS_END
IGNORE_WARNINGS_END

return subspaceForImplSlow(vm, clientSlot, serverSlot, *cellType, sizeof(T), T::numberOfLowerTierPreciseCells, hasOutputConstraints);
}

static JSVMClientData* clientData(JSC::VM& vm)
Expand Down
5 changes: 1 addition & 4 deletions src/jsc/bindings/BunDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,7 @@ class JSBunInspectorConnection final : public JSC::JSDestructibleObject {
return nullptr;
return WebCore::subspaceForImpl<JSBunInspectorConnection, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForBunInspectorConnection.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForBunInspectorConnection = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForBunInspectorConnection.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForBunInspectorConnection = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForBunInspectorConnection, &WebCore::DOMIsoSubspaces::m_subspaceForBunInspectorConnection);
}
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
{
Expand Down
5 changes: 1 addition & 4 deletions src/jsc/bindings/BunPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,7 @@ class JSModuleMock final : public JSC::JSNonFinalObject {
return nullptr;
return WebCore::subspaceForImpl<JSModuleMock, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForJSModuleMock.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForJSModuleMock = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForJSModuleMock.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForJSModuleMock = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForJSModuleMock, &WebCore::DOMIsoSubspaces::m_subspaceForJSModuleMock);
}

void finishCreation(JSC::VM&);
Expand Down
5 changes: 1 addition & 4 deletions src/jsc/bindings/BunProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ class Process : public WebCore::JSEventEmitter {
return nullptr;
return WebCore::subspaceForImpl<Process, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForProcessObject.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForProcessObject = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForProcessObject.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForProcessObject = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForProcessObject, &WebCore::DOMIsoSubspaces::m_subspaceForProcessObject);
}

void finishCreation(JSC::VM& vm);
Expand Down
5 changes: 1 addition & 4 deletions src/jsc/bindings/CallSite.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ class CallSite final : public JSC::JSNonFinalObject {

return WebCore::subspaceForImpl<CallSite, UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForCallSite.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForCallSite = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForCallSite.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForCallSite = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForCallSite, &WebCore::DOMIsoSubspaces::m_subspaceForCallSite);
}

JSC::JSValue thisValue() const { return m_thisValue.get(); }
Expand Down
5 changes: 1 addition & 4 deletions src/jsc/bindings/ErrorCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ class ErrorCodeCache : public JSC::JSInternalFieldObjectImpl<NODE_ERROR_COUNT> {
return nullptr;
return WebCore::subspaceForImpl<ErrorCodeCache, WebCore::UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForErrorCodeCache.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForErrorCodeCache = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForErrorCodeCache.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForErrorCodeCache = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForErrorCodeCache, &WebCore::DOMIsoSubspaces::m_subspaceForErrorCodeCache);
}

static ErrorCodeCache* create(VM& vm, Structure* structure);
Expand Down
5 changes: 1 addition & 4 deletions src/jsc/bindings/ImportMetaObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ class ImportMetaObject final : public JSC::JSNonFinalObject {

return WebCore::subspaceForImpl<ImportMetaObject, UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForImportMeta.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForImportMeta = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForImportMeta.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForImportMeta = std::forward<decltype(space)>(space); });
&WebCore::DOMClientIsoSubspaces::m_clientSubspaceForImportMeta, &WebCore::DOMIsoSubspaces::m_subspaceForImportMeta);
}

static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, bool isBake = false);
Expand Down
Loading
Loading