Files
bun.sh/src/bun.js/bindings/YogaNodeImpl.cpp
Ciro Spaciari MacBook fcee6de2a9 revert(yoga): remove YGNodeFinalize from destructor to fix GC sweep crash
Calling delete on yoga::Node during GC finalization is unsafe because
the GC sweeps objects in arbitrary order and the allocator behavior
on musl can cause null-pointer dereferences. The LSAN suppressions
in test/leaksan.supp handle the leak detection false positives.

This reverts the destructor and replaceYogaNode changes from 660e4b3c
while keeping the LSAN suppressions from c17c345d.
2026-02-03 21:33:24 -08:00

84 lines
1.9 KiB
C++

#include "YogaNodeImpl.h"
#include "JSYogaNode.h"
#include "JSYogaConfig.h"
#include "JSYogaNodeOwner.h"
#include <yoga/Yoga.h>
#include <wtf/HashSet.h>
#include <wtf/Lock.h>
namespace Bun {
Ref<YogaNodeImpl> YogaNodeImpl::create(YGConfigRef config)
{
return adoptRef(*new YogaNodeImpl(config));
}
YogaNodeImpl::YogaNodeImpl(YGConfigRef config)
{
if (config) {
m_yogaNode = YGNodeNewWithConfig(config);
} else {
m_yogaNode = YGNodeNew();
}
// Store this C++ wrapper in the Yoga node's context
YGNodeSetContext(m_yogaNode, this);
}
YogaNodeImpl::~YogaNodeImpl()
{
m_yogaNode = nullptr;
}
void YogaNodeImpl::setJSWrapper(JSYogaNode* wrapper)
{
// Only increment ref count if we don't already have a wrapper
// This prevents ref count leaks if setJSWrapper is called multiple times
if (!m_wrapper) {
// Increment ref count for the weak handle context
this->ref();
}
// Create weak reference with our JS owner
m_wrapper = JSC::Weak<JSYogaNode>(wrapper, &jsYogaNodeOwner(), this);
}
void YogaNodeImpl::clearJSWrapper()
{
m_wrapper.clear();
}
void YogaNodeImpl::clearJSWrapperWithoutDeref()
{
// Clear weak reference without deref - used by JS destructor
// when WeakHandleOwner::finalize will handle the deref
m_wrapper.clear();
}
JSYogaNode* YogaNodeImpl::jsWrapper() const
{
return m_wrapper.get();
}
JSYogaConfig* YogaNodeImpl::jsConfig() const
{
// Access config through JS wrapper's WriteBarrier - this is GC-safe
if (auto* jsWrapper = m_wrapper.get()) {
return jsCast<JSYogaConfig*>(jsWrapper->m_config.get());
}
return nullptr;
}
YogaNodeImpl* YogaNodeImpl::fromYGNode(YGNodeRef nodeRef)
{
if (!nodeRef) return nullptr;
return static_cast<YogaNodeImpl*>(YGNodeGetContext(nodeRef));
}
void YogaNodeImpl::replaceYogaNode(YGNodeRef newNode)
{
m_yogaNode = newNode;
}
} // namespace Bun