mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
feat: add native Yoga layout engine bindings
Adds native C++/JSC bindings for Facebook's Yoga flexbox layout engine, exposed as Bun.Yoga. This is a high-performance alternative to the yoga-layout npm package's WASM-based implementation. - Wraps Yoga v3.x C API via JSDestructibleObject/RefCounted pattern - Full API: Node, Config, all style setters/getters, layout calculation - All yoga-layout constants (ALIGN_*, FLEX_DIRECTION_*, EDGE_*, etc.) - Measure, dirtied, and baseline callback support - GC integration with IsoSubspaces and weak handle owners - 105 tests with 375 assertions
This commit is contained in:
@@ -61,6 +61,7 @@ set(BUN_DEPENDENCIES
|
||||
LibArchive # must be loaded after zlib
|
||||
HdrHistogram # must be loaded after zlib
|
||||
Zstd
|
||||
Yoga
|
||||
)
|
||||
|
||||
# TinyCC is optional - disabled on Windows ARM64 where it's not supported
|
||||
|
||||
26
cmake/targets/BuildYoga.cmake
Normal file
26
cmake/targets/BuildYoga.cmake
Normal file
@@ -0,0 +1,26 @@
|
||||
register_repository(
|
||||
NAME
|
||||
yoga
|
||||
REPOSITORY
|
||||
facebook/yoga
|
||||
COMMIT
|
||||
dc2581f229cb05c7d2af8dee37b2ee0b59fd5326
|
||||
)
|
||||
|
||||
register_cmake_command(
|
||||
TARGET
|
||||
yoga
|
||||
TARGETS
|
||||
yogacore
|
||||
ARGS
|
||||
-DBUILD_SHARED_LIBS=OFF
|
||||
-DYOGA_BUILD_TESTS=OFF
|
||||
-DYOGA_BUILD_SAMPLES=OFF
|
||||
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
|
||||
LIB_PATH
|
||||
yoga
|
||||
LIBRARIES
|
||||
yogacore
|
||||
INCLUDES
|
||||
.
|
||||
)
|
||||
@@ -335,6 +335,12 @@ static JSValue constructBunSQLObject(VM& vm, JSObject* bunObject)
|
||||
}
|
||||
|
||||
extern "C" JSC::EncodedJSValue JSPasswordObject__create(JSGlobalObject*);
|
||||
extern "C" JSC::EncodedJSValue Bun__createYogaModule(Zig::GlobalObject*);
|
||||
|
||||
static JSValue constructYogaObject(VM& vm, JSObject* bunObject)
|
||||
{
|
||||
return JSValue::decode(Bun__createYogaModule(jsCast<Zig::GlobalObject*>(bunObject->globalObject())));
|
||||
}
|
||||
|
||||
static JSValue constructPasswordObject(VM& vm, JSObject* bunObject)
|
||||
{
|
||||
@@ -924,6 +930,7 @@ JSC_DEFINE_HOST_FUNCTION(functionFileURLToPath, (JSC::JSGlobalObject * globalObj
|
||||
markdown BunObject_lazyPropCb_wrap_markdown DontDelete|PropertyCallback
|
||||
TOML BunObject_lazyPropCb_wrap_TOML DontDelete|PropertyCallback
|
||||
YAML BunObject_lazyPropCb_wrap_YAML DontDelete|PropertyCallback
|
||||
Yoga constructYogaObject ReadOnly|DontDelete|PropertyCallback
|
||||
Transpiler BunObject_lazyPropCb_wrap_Transpiler DontDelete|PropertyCallback
|
||||
embeddedFiles BunObject_lazyPropCb_wrap_embeddedFiles DontDelete|PropertyCallback
|
||||
S3Client BunObject_lazyPropCb_wrap_S3Client DontDelete|PropertyCallback
|
||||
|
||||
104
src/bun.js/bindings/JSYogaConfig.cpp
Normal file
104
src/bun.js/bindings/JSYogaConfig.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include "root.h"
|
||||
#include "JSYogaConfig.h"
|
||||
#include "YogaConfigImpl.h"
|
||||
#include "webcore/DOMIsoSubspaces.h"
|
||||
#include "webcore/DOMClientIsoSubspaces.h"
|
||||
#include "webcore/WebCoreJSClientData.h"
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
using namespace JSC;
|
||||
|
||||
const JSC::ClassInfo JSYogaConfig::s_info = { "Config"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSYogaConfig) };
|
||||
|
||||
JSYogaConfig::JSYogaConfig(JSC::VM& vm, JSC::Structure* structure)
|
||||
: Base(vm, structure)
|
||||
, m_impl(YogaConfigImpl::create())
|
||||
{
|
||||
}
|
||||
|
||||
JSYogaConfig::JSYogaConfig(JSC::VM& vm, JSC::Structure* structure, Ref<YogaConfigImpl>&& impl)
|
||||
: Base(vm, structure)
|
||||
, m_impl(std::move(impl))
|
||||
{
|
||||
}
|
||||
|
||||
JSYogaConfig::~JSYogaConfig()
|
||||
{
|
||||
// The WeakHandleOwner::finalize should handle cleanup
|
||||
// Don't interfere with that mechanism
|
||||
}
|
||||
|
||||
JSYogaConfig* JSYogaConfig::create(JSC::VM& vm, JSC::Structure* structure)
|
||||
{
|
||||
JSYogaConfig* config = new (NotNull, JSC::allocateCell<JSYogaConfig>(vm)) JSYogaConfig(vm, structure);
|
||||
config->finishCreation(vm);
|
||||
return config;
|
||||
}
|
||||
|
||||
JSYogaConfig* JSYogaConfig::create(JSC::VM& vm, JSC::Structure* structure, Ref<YogaConfigImpl>&& impl)
|
||||
{
|
||||
JSYogaConfig* config = new (NotNull, JSC::allocateCell<JSYogaConfig>(vm)) JSYogaConfig(vm, structure, std::move(impl));
|
||||
config->finishCreation(vm);
|
||||
return config;
|
||||
}
|
||||
|
||||
void JSYogaConfig::finishCreation(JSC::VM& vm)
|
||||
{
|
||||
Base::finishCreation(vm);
|
||||
|
||||
// Set this JS wrapper in the C++ impl
|
||||
m_impl->setJSWrapper(this);
|
||||
}
|
||||
|
||||
JSC::Structure* JSYogaConfig::createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
|
||||
{
|
||||
return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
|
||||
}
|
||||
|
||||
void JSYogaConfig::destroy(JSC::JSCell* cell)
|
||||
{
|
||||
static_cast<JSYogaConfig*>(cell)->~JSYogaConfig();
|
||||
}
|
||||
|
||||
template<typename MyClassT, JSC::SubspaceAccess mode>
|
||||
JSC::GCClient::IsoSubspace* JSYogaConfig::subspaceFor(JSC::VM& vm)
|
||||
{
|
||||
if constexpr (mode == JSC::SubspaceAccess::Concurrently)
|
||||
return nullptr;
|
||||
return WebCore::subspaceForImpl<MyClassT, WebCore::UseCustomHeapCellType::No>(
|
||||
vm,
|
||||
[](auto& spaces) { return spaces.m_clientSubspaceForJSYogaConfig.get(); },
|
||||
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForJSYogaConfig = std::forward<decltype(space)>(space); },
|
||||
[](auto& spaces) { return spaces.m_subspaceForJSYogaConfig.get(); },
|
||||
[](auto& spaces, auto&& space) { spaces.m_subspaceForJSYogaConfig = std::forward<decltype(space)>(space); });
|
||||
}
|
||||
|
||||
template<typename Visitor>
|
||||
void JSYogaConfig::visitAdditionalChildren(Visitor& visitor)
|
||||
{
|
||||
visitor.append(m_context);
|
||||
visitor.append(m_loggerFunc);
|
||||
visitor.append(m_cloneNodeFunc);
|
||||
}
|
||||
|
||||
DEFINE_VISIT_ADDITIONAL_CHILDREN(JSYogaConfig);
|
||||
|
||||
template<typename Visitor>
|
||||
void JSYogaConfig::visitOutputConstraints(JSC::JSCell* cell, Visitor& visitor)
|
||||
{
|
||||
auto* thisObject = jsCast<JSYogaConfig*>(cell);
|
||||
|
||||
// Lock for concurrent GC thread safety
|
||||
WTF::Locker locker { thisObject->cellLock() };
|
||||
|
||||
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
|
||||
Base::visitOutputConstraints(thisObject, visitor);
|
||||
thisObject->visitAdditionalChildren(visitor);
|
||||
}
|
||||
|
||||
template void JSYogaConfig::visitOutputConstraints(JSC::JSCell*, JSC::AbstractSlotVisitor&);
|
||||
template void JSYogaConfig::visitOutputConstraints(JSC::JSCell*, JSC::SlotVisitor&);
|
||||
|
||||
} // namespace Bun
|
||||
55
src/bun.js/bindings/JSYogaConfig.h
Normal file
55
src/bun.js/bindings/JSYogaConfig.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
#include "root.h"
|
||||
#include <memory>
|
||||
#include <JavaScriptCore/JSDestructibleObject.h>
|
||||
#include <JavaScriptCore/WriteBarrier.h>
|
||||
#include <wtf/Ref.h>
|
||||
|
||||
// Forward declarations
|
||||
typedef struct YGConfig* YGConfigRef;
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class YogaConfigImpl;
|
||||
|
||||
class JSYogaConfig final : public JSC::JSDestructibleObject {
|
||||
public:
|
||||
using Base = JSC::JSDestructibleObject;
|
||||
static constexpr unsigned StructureFlags = Base::StructureFlags;
|
||||
static constexpr JSC::DestructionMode needsDestruction = JSC::NeedsDestruction;
|
||||
|
||||
static JSYogaConfig* create(JSC::VM&, JSC::Structure*);
|
||||
static JSYogaConfig* create(JSC::VM&, JSC::Structure*, Ref<YogaConfigImpl>&&);
|
||||
static void destroy(JSC::JSCell*);
|
||||
static JSC::Structure* createStructure(JSC::VM&, JSC::JSGlobalObject*, JSC::JSValue);
|
||||
~JSYogaConfig();
|
||||
|
||||
template<typename, JSC::SubspaceAccess>
|
||||
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM&);
|
||||
|
||||
DECLARE_INFO;
|
||||
|
||||
template<typename Visitor> void visitAdditionalChildren(Visitor&);
|
||||
template<typename Visitor> static void visitOutputConstraints(JSC::JSCell*, Visitor&);
|
||||
|
||||
YogaConfigImpl& impl() { return m_impl.get(); }
|
||||
const YogaConfigImpl& impl() const { return m_impl.get(); }
|
||||
|
||||
// Context storage
|
||||
JSC::WriteBarrier<JSC::Unknown> m_context;
|
||||
|
||||
// Logger callback
|
||||
JSC::WriteBarrier<JSC::JSObject> m_loggerFunc;
|
||||
|
||||
// Clone node callback
|
||||
JSC::WriteBarrier<JSC::JSObject> m_cloneNodeFunc;
|
||||
|
||||
private:
|
||||
JSYogaConfig(JSC::VM&, JSC::Structure*);
|
||||
JSYogaConfig(JSC::VM&, JSC::Structure*, Ref<YogaConfigImpl>&&);
|
||||
void finishCreation(JSC::VM&);
|
||||
|
||||
Ref<YogaConfigImpl> m_impl;
|
||||
};
|
||||
|
||||
} // namespace Bun
|
||||
39
src/bun.js/bindings/JSYogaConfigOwner.cpp
Normal file
39
src/bun.js/bindings/JSYogaConfigOwner.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "JSYogaConfigOwner.h"
|
||||
#include "YogaConfigImpl.h"
|
||||
#include "JSYogaConfig.h"
|
||||
#include <JavaScriptCore/JSCInlines.h>
|
||||
#include <wtf/NeverDestroyed.h>
|
||||
#include <wtf/Compiler.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
void JSYogaConfigOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
|
||||
{
|
||||
// This is where we deref the C++ YogaConfigImpl wrapper
|
||||
// The context contains our YogaConfigImpl
|
||||
auto* impl = static_cast<YogaConfigImpl*>(context);
|
||||
|
||||
// Deref the YogaConfigImpl - this will decrease its reference count
|
||||
// and potentially destroy it if no other references exist
|
||||
impl->deref();
|
||||
}
|
||||
|
||||
bool JSYogaConfigOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void* context, JSC::AbstractSlotVisitor& visitor, ASCIILiteral* reason)
|
||||
{
|
||||
UNUSED_PARAM(handle);
|
||||
UNUSED_PARAM(context);
|
||||
// YogaConfig doesn't currently use opaque roots, so always return false
|
||||
// This allows normal GC collection based on JS reference reachability
|
||||
if (reason)
|
||||
*reason = "YogaConfig not using opaque roots"_s;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
JSYogaConfigOwner& jsYogaConfigOwner()
|
||||
{
|
||||
static NeverDestroyed<JSYogaConfigOwner> owner;
|
||||
return owner.get();
|
||||
}
|
||||
|
||||
} // namespace Bun
|
||||
20
src/bun.js/bindings/JSYogaConfigOwner.h
Normal file
20
src/bun.js/bindings/JSYogaConfigOwner.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "root.h"
|
||||
#include <JavaScriptCore/WeakHandleOwner.h>
|
||||
#include <JavaScriptCore/Weak.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class YogaConfigImpl;
|
||||
class JSYogaConfig;
|
||||
|
||||
class JSYogaConfigOwner : public JSC::WeakHandleOwner {
|
||||
public:
|
||||
void finalize(JSC::Handle<JSC::Unknown>, void* context) final;
|
||||
bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::AbstractSlotVisitor&, ASCIILiteral*) final;
|
||||
};
|
||||
|
||||
JSYogaConfigOwner& jsYogaConfigOwner();
|
||||
|
||||
} // namespace Bun
|
||||
109
src/bun.js/bindings/JSYogaConstants.cpp
Normal file
109
src/bun.js/bindings/JSYogaConstants.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "root.h"
|
||||
#include "JSYogaConstants.h"
|
||||
#include <JavaScriptCore/JSCInlines.h>
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
using namespace JSC;
|
||||
|
||||
const JSC::ClassInfo JSYogaConstants::s_info = { "YogaConstants"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSYogaConstants) };
|
||||
|
||||
void JSYogaConstants::finishCreation(JSC::VM& vm)
|
||||
{
|
||||
Base::finishCreation(vm);
|
||||
|
||||
// Align values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_AUTO"_s), JSC::jsNumber(static_cast<int>(YGAlignAuto)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_FLEX_START"_s), JSC::jsNumber(static_cast<int>(YGAlignFlexStart)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_CENTER"_s), JSC::jsNumber(static_cast<int>(YGAlignCenter)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_FLEX_END"_s), JSC::jsNumber(static_cast<int>(YGAlignFlexEnd)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_STRETCH"_s), JSC::jsNumber(static_cast<int>(YGAlignStretch)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_BASELINE"_s), JSC::jsNumber(static_cast<int>(YGAlignBaseline)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_SPACE_BETWEEN"_s), JSC::jsNumber(static_cast<int>(YGAlignSpaceBetween)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_SPACE_AROUND"_s), JSC::jsNumber(static_cast<int>(YGAlignSpaceAround)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ALIGN_SPACE_EVENLY"_s), JSC::jsNumber(static_cast<int>(YGAlignSpaceEvenly)), 0);
|
||||
|
||||
// Direction values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "DIRECTION_INHERIT"_s), JSC::jsNumber(static_cast<int>(YGDirectionInherit)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "DIRECTION_LTR"_s), JSC::jsNumber(static_cast<int>(YGDirectionLTR)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "DIRECTION_RTL"_s), JSC::jsNumber(static_cast<int>(YGDirectionRTL)), 0);
|
||||
|
||||
// Display values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "DISPLAY_FLEX"_s), JSC::jsNumber(static_cast<int>(YGDisplayFlex)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "DISPLAY_NONE"_s), JSC::jsNumber(static_cast<int>(YGDisplayNone)), 0);
|
||||
|
||||
// Edge values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_LEFT"_s), JSC::jsNumber(static_cast<int>(YGEdgeLeft)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_TOP"_s), JSC::jsNumber(static_cast<int>(YGEdgeTop)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_RIGHT"_s), JSC::jsNumber(static_cast<int>(YGEdgeRight)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_BOTTOM"_s), JSC::jsNumber(static_cast<int>(YGEdgeBottom)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_START"_s), JSC::jsNumber(static_cast<int>(YGEdgeStart)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_END"_s), JSC::jsNumber(static_cast<int>(YGEdgeEnd)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_HORIZONTAL"_s), JSC::jsNumber(static_cast<int>(YGEdgeHorizontal)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_VERTICAL"_s), JSC::jsNumber(static_cast<int>(YGEdgeVertical)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EDGE_ALL"_s), JSC::jsNumber(static_cast<int>(YGEdgeAll)), 0);
|
||||
|
||||
// Experimental feature values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS"_s), JSC::jsNumber(static_cast<int>(YGExperimentalFeatureWebFlexBasis)), 0);
|
||||
|
||||
// Flex direction values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "FLEX_DIRECTION_COLUMN"_s), JSC::jsNumber(static_cast<int>(YGFlexDirectionColumn)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "FLEX_DIRECTION_COLUMN_REVERSE"_s), JSC::jsNumber(static_cast<int>(YGFlexDirectionColumnReverse)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "FLEX_DIRECTION_ROW"_s), JSC::jsNumber(static_cast<int>(YGFlexDirectionRow)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "FLEX_DIRECTION_ROW_REVERSE"_s), JSC::jsNumber(static_cast<int>(YGFlexDirectionRowReverse)), 0);
|
||||
|
||||
// Gutter values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "GUTTER_COLUMN"_s), JSC::jsNumber(static_cast<int>(YGGutterColumn)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "GUTTER_ROW"_s), JSC::jsNumber(static_cast<int>(YGGutterRow)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "GUTTER_ALL"_s), JSC::jsNumber(static_cast<int>(YGGutterAll)), 0);
|
||||
|
||||
// Justify values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "JUSTIFY_FLEX_START"_s), JSC::jsNumber(static_cast<int>(YGJustifyFlexStart)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "JUSTIFY_CENTER"_s), JSC::jsNumber(static_cast<int>(YGJustifyCenter)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "JUSTIFY_FLEX_END"_s), JSC::jsNumber(static_cast<int>(YGJustifyFlexEnd)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "JUSTIFY_SPACE_BETWEEN"_s), JSC::jsNumber(static_cast<int>(YGJustifySpaceBetween)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "JUSTIFY_SPACE_AROUND"_s), JSC::jsNumber(static_cast<int>(YGJustifySpaceAround)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "JUSTIFY_SPACE_EVENLY"_s), JSC::jsNumber(static_cast<int>(YGJustifySpaceEvenly)), 0);
|
||||
|
||||
// Measure mode values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "MEASURE_MODE_UNDEFINED"_s), JSC::jsNumber(static_cast<int>(YGMeasureModeUndefined)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "MEASURE_MODE_EXACTLY"_s), JSC::jsNumber(static_cast<int>(YGMeasureModeExactly)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "MEASURE_MODE_AT_MOST"_s), JSC::jsNumber(static_cast<int>(YGMeasureModeAtMost)), 0);
|
||||
|
||||
// Node type values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "NODE_TYPE_DEFAULT"_s), JSC::jsNumber(static_cast<int>(YGNodeTypeDefault)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "NODE_TYPE_TEXT"_s), JSC::jsNumber(static_cast<int>(YGNodeTypeText)), 0);
|
||||
|
||||
// Overflow values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "OVERFLOW_VISIBLE"_s), JSC::jsNumber(static_cast<int>(YGOverflowVisible)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "OVERFLOW_HIDDEN"_s), JSC::jsNumber(static_cast<int>(YGOverflowHidden)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "OVERFLOW_SCROLL"_s), JSC::jsNumber(static_cast<int>(YGOverflowScroll)), 0);
|
||||
|
||||
// Position type values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "POSITION_TYPE_STATIC"_s), JSC::jsNumber(static_cast<int>(YGPositionTypeStatic)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "POSITION_TYPE_RELATIVE"_s), JSC::jsNumber(static_cast<int>(YGPositionTypeRelative)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "POSITION_TYPE_ABSOLUTE"_s), JSC::jsNumber(static_cast<int>(YGPositionTypeAbsolute)), 0);
|
||||
|
||||
// Unit values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "UNIT_UNDEFINED"_s), JSC::jsNumber(static_cast<int>(YGUnitUndefined)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "UNIT_POINT"_s), JSC::jsNumber(static_cast<int>(YGUnitPoint)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "UNIT_PERCENT"_s), JSC::jsNumber(static_cast<int>(YGUnitPercent)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "UNIT_AUTO"_s), JSC::jsNumber(static_cast<int>(YGUnitAuto)), 0);
|
||||
|
||||
// Wrap values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "WRAP_NO_WRAP"_s), JSC::jsNumber(static_cast<int>(YGWrapNoWrap)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "WRAP_WRAP"_s), JSC::jsNumber(static_cast<int>(YGWrapWrap)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "WRAP_WRAP_REVERSE"_s), JSC::jsNumber(static_cast<int>(YGWrapWrapReverse)), 0);
|
||||
|
||||
// Errata values
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ERRATA_NONE"_s), JSC::jsNumber(static_cast<int>(YGErrataNone)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ERRATA_STRETCH_FLEX_BASIS"_s), JSC::jsNumber(static_cast<int>(YGErrataStretchFlexBasis)), 0);
|
||||
// YGErrataAbsolutePositioningIncorrect is not available in this version of Yoga
|
||||
// putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ERRATA_ABSOLUTE_POSITIONING_INCORRECT"_s), JSC::jsNumber(static_cast<int>(YGErrataAbsolutePositioningIncorrect)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ERRATA_ABSOLUTE_PERCENT_AGAINST_INNER_SIZE"_s), JSC::jsNumber(static_cast<int>(YGErrataAbsolutePercentAgainstInnerSize)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ERRATA_ALL"_s), JSC::jsNumber(static_cast<int>(YGErrataAll)), 0);
|
||||
putDirectWithoutTransition(vm, JSC::Identifier::fromString(vm, "ERRATA_CLASSIC"_s), JSC::jsNumber(static_cast<int>(YGErrataClassic)), 0);
|
||||
}
|
||||
|
||||
} // namespace Bun
|
||||
41
src/bun.js/bindings/JSYogaConstants.h
Normal file
41
src/bun.js/bindings/JSYogaConstants.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include "root.h"
|
||||
#include <JavaScriptCore/JSObject.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class JSYogaConstants final : public JSC::JSNonFinalObject {
|
||||
public:
|
||||
using Base = JSC::JSNonFinalObject;
|
||||
static constexpr unsigned StructureFlags = Base::StructureFlags;
|
||||
|
||||
static JSYogaConstants* create(JSC::VM& vm, JSC::Structure* structure)
|
||||
{
|
||||
JSYogaConstants* constants = new (NotNull, allocateCell<JSYogaConstants>(vm)) JSYogaConstants(vm, structure);
|
||||
constants->finishCreation(vm);
|
||||
return constants;
|
||||
}
|
||||
|
||||
DECLARE_INFO;
|
||||
|
||||
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
|
||||
{
|
||||
return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
|
||||
}
|
||||
|
||||
template<typename, JSC::SubspaceAccess>
|
||||
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
|
||||
{
|
||||
return &vm.plainObjectSpace();
|
||||
}
|
||||
|
||||
private:
|
||||
JSYogaConstants(JSC::VM& vm, JSC::Structure* structure)
|
||||
: Base(vm, structure)
|
||||
{
|
||||
}
|
||||
|
||||
void finishCreation(JSC::VM&);
|
||||
};
|
||||
|
||||
} // namespace Bun
|
||||
173
src/bun.js/bindings/JSYogaConstructor.cpp
Normal file
173
src/bun.js/bindings/JSYogaConstructor.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
#include "root.h"
|
||||
#include "JSYogaConstructor.h"
|
||||
#include "JSYogaConfig.h"
|
||||
#include "YogaConfigImpl.h"
|
||||
#include "JSYogaNode.h"
|
||||
#include "JSYogaPrototype.h"
|
||||
#include "ZigGlobalObject.h"
|
||||
#include <JavaScriptCore/FunctionPrototype.h>
|
||||
#include <JavaScriptCore/JSCInlines.h>
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
#ifndef UNLIKELY
|
||||
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
|
||||
#endif
|
||||
|
||||
namespace Bun {
|
||||
|
||||
// Forward declarations for constructor functions
|
||||
static JSC_DECLARE_HOST_FUNCTION(constructJSYogaConfig);
|
||||
static JSC_DECLARE_HOST_FUNCTION(callJSYogaConfig);
|
||||
static JSC_DECLARE_HOST_FUNCTION(constructJSYogaNode);
|
||||
static JSC_DECLARE_HOST_FUNCTION(callJSYogaNode);
|
||||
|
||||
// Config Constructor implementation
|
||||
const JSC::ClassInfo JSYogaConfigConstructor::s_info = { "Config"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSYogaConfigConstructor) };
|
||||
|
||||
JSYogaConfigConstructor::JSYogaConfigConstructor(JSC::VM& vm, JSC::Structure* structure)
|
||||
: Base(vm, structure, callJSYogaConfig, constructJSYogaConfig)
|
||||
{
|
||||
}
|
||||
|
||||
void JSYogaConfigConstructor::finishCreation(JSC::VM& vm, JSC::JSObject* prototype)
|
||||
{
|
||||
Base::finishCreation(vm, 0, "Config"_s, PropertyAdditionMode::WithStructureTransition);
|
||||
putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly);
|
||||
|
||||
// Add static methods - create() is an alias for the constructor
|
||||
putDirectNativeFunction(vm, this->globalObject(), JSC::Identifier::fromString(vm, "create"_s), 0, constructJSYogaConfig, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly);
|
||||
}
|
||||
|
||||
// Node Constructor implementation
|
||||
const JSC::ClassInfo JSYogaNodeConstructor::s_info = { "Node"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSYogaNodeConstructor) };
|
||||
|
||||
JSYogaNodeConstructor::JSYogaNodeConstructor(JSC::VM& vm, JSC::Structure* structure)
|
||||
: Base(vm, structure, callJSYogaNode, constructJSYogaNode)
|
||||
{
|
||||
}
|
||||
|
||||
void JSYogaNodeConstructor::finishCreation(JSC::VM& vm, JSC::JSObject* prototype)
|
||||
{
|
||||
Base::finishCreation(vm, 1, "Node"_s, PropertyAdditionMode::WithStructureTransition); // 1 for optional config parameter
|
||||
putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly);
|
||||
|
||||
// Add static methods - create() is an alias for the constructor
|
||||
putDirectNativeFunction(vm, this->globalObject(), JSC::Identifier::fromString(vm, "create"_s), 1, constructJSYogaNode, ImplementationVisibility::Public, NoIntrinsic, JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly);
|
||||
}
|
||||
|
||||
// Constructor functions
|
||||
JSC_DEFINE_HOST_FUNCTION(constructJSYogaConfig, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
|
||||
{
|
||||
JSC::VM& vm = globalObject->vm();
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
|
||||
auto* zigGlobalObject = defaultGlobalObject(globalObject);
|
||||
JSC::Structure* structure = zigGlobalObject->m_JSYogaConfigClassStructure.get(zigGlobalObject);
|
||||
|
||||
// Handle subclassing
|
||||
JSC::JSValue newTarget = callFrame->newTarget();
|
||||
if (UNLIKELY(zigGlobalObject->m_JSYogaConfigClassStructure.constructor(zigGlobalObject) != newTarget)) {
|
||||
if (!newTarget) {
|
||||
throwTypeError(globalObject, scope, "Class constructor Config cannot be invoked without 'new'"_s);
|
||||
return {};
|
||||
}
|
||||
|
||||
auto* functionGlobalObject = defaultGlobalObject(getFunctionRealm(globalObject, newTarget.getObject()));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
structure = JSC::InternalFunction::createSubclassStructure(
|
||||
globalObject, newTarget.getObject(), functionGlobalObject->m_JSYogaConfigClassStructure.get(functionGlobalObject));
|
||||
scope.release();
|
||||
}
|
||||
|
||||
return JSC::JSValue::encode(JSYogaConfig::create(vm, structure));
|
||||
}
|
||||
|
||||
JSC_DEFINE_HOST_FUNCTION(callJSYogaConfig, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
|
||||
{
|
||||
JSC::VM& vm = globalObject->vm();
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
throwTypeError(globalObject, scope, "Class constructor Config cannot be invoked without 'new'"_s);
|
||||
return {};
|
||||
}
|
||||
|
||||
JSC_DEFINE_HOST_FUNCTION(constructJSYogaNode, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
|
||||
{
|
||||
JSC::VM& vm = globalObject->vm();
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
|
||||
auto* zigGlobalObject = defaultGlobalObject(globalObject);
|
||||
JSC::Structure* structure = zigGlobalObject->m_JSYogaNodeClassStructure.get(zigGlobalObject);
|
||||
|
||||
// Handle subclassing
|
||||
JSC::JSValue newTarget = callFrame->newTarget();
|
||||
if (UNLIKELY(zigGlobalObject->m_JSYogaNodeClassStructure.constructor(zigGlobalObject) != newTarget)) {
|
||||
if (!newTarget) {
|
||||
throwTypeError(globalObject, scope, "Class constructor Node cannot be invoked without 'new'"_s);
|
||||
return {};
|
||||
}
|
||||
|
||||
auto* functionGlobalObject = defaultGlobalObject(getFunctionRealm(globalObject, newTarget.getObject()));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
structure = JSC::InternalFunction::createSubclassStructure(
|
||||
globalObject, newTarget.getObject(), functionGlobalObject->m_JSYogaNodeClassStructure.get(functionGlobalObject));
|
||||
scope.release();
|
||||
}
|
||||
|
||||
// Optional config parameter
|
||||
YGConfigRef config = nullptr;
|
||||
JSYogaConfig* jsConfig = nullptr;
|
||||
if (callFrame->argumentCount() > 0) {
|
||||
JSC::JSValue configArg = callFrame->uncheckedArgument(0);
|
||||
if (!configArg.isUndefinedOrNull()) {
|
||||
jsConfig = JSC::jsDynamicCast<JSYogaConfig*>(configArg);
|
||||
if (!jsConfig) {
|
||||
throwTypeError(globalObject, scope, "First argument must be a Yoga.Config instance"_s);
|
||||
return {};
|
||||
}
|
||||
config = jsConfig->impl().yogaConfig();
|
||||
}
|
||||
}
|
||||
|
||||
return JSC::JSValue::encode(JSYogaNode::create(vm, structure, config, jsConfig));
|
||||
}
|
||||
|
||||
JSC_DEFINE_HOST_FUNCTION(callJSYogaNode, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
|
||||
{
|
||||
JSC::VM& vm = globalObject->vm();
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
throwTypeError(globalObject, scope, "Class constructor Node cannot be invoked without 'new'"_s);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Setup functions for lazy initialization
|
||||
void setupJSYogaConfigClassStructure(JSC::LazyClassStructure::Initializer& init)
|
||||
{
|
||||
auto* prototypeStructure = JSYogaConfigPrototype::createStructure(init.vm, init.global, init.global->objectPrototype());
|
||||
auto* prototype = JSYogaConfigPrototype::create(init.vm, init.global, prototypeStructure);
|
||||
|
||||
auto* constructorStructure = JSYogaConfigConstructor::createStructure(init.vm, init.global, init.global->functionPrototype());
|
||||
auto* constructor = JSYogaConfigConstructor::create(init.vm, constructorStructure, prototype);
|
||||
|
||||
auto* structure = JSYogaConfig::createStructure(init.vm, init.global, prototype);
|
||||
|
||||
init.setPrototype(prototype);
|
||||
init.setStructure(structure);
|
||||
init.setConstructor(constructor);
|
||||
}
|
||||
|
||||
void setupJSYogaNodeClassStructure(JSC::LazyClassStructure::Initializer& init)
|
||||
{
|
||||
auto* prototypeStructure = JSYogaNodePrototype::createStructure(init.vm, init.global, init.global->objectPrototype());
|
||||
auto* prototype = JSYogaNodePrototype::create(init.vm, init.global, prototypeStructure);
|
||||
|
||||
auto* constructorStructure = JSYogaNodeConstructor::createStructure(init.vm, init.global, init.global->functionPrototype());
|
||||
auto* constructor = JSYogaNodeConstructor::create(init.vm, constructorStructure, prototype);
|
||||
|
||||
auto* structure = JSYogaNode::createStructure(init.vm, init.global, prototype);
|
||||
|
||||
init.setPrototype(prototype);
|
||||
init.setStructure(structure);
|
||||
init.setConstructor(constructor);
|
||||
}
|
||||
|
||||
} // namespace Bun
|
||||
71
src/bun.js/bindings/JSYogaConstructor.h
Normal file
71
src/bun.js/bindings/JSYogaConstructor.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#include "root.h"
|
||||
#include <JavaScriptCore/InternalFunction.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class JSYogaConfigConstructor final : public JSC::InternalFunction {
|
||||
public:
|
||||
using Base = JSC::InternalFunction;
|
||||
static constexpr unsigned StructureFlags = Base::StructureFlags;
|
||||
|
||||
static JSYogaConfigConstructor* create(JSC::VM& vm, JSC::Structure* structure, JSC::JSObject* prototype)
|
||||
{
|
||||
JSYogaConfigConstructor* constructor = new (NotNull, JSC::allocateCell<JSYogaConfigConstructor>(vm)) JSYogaConfigConstructor(vm, structure);
|
||||
constructor->finishCreation(vm, prototype);
|
||||
return constructor;
|
||||
}
|
||||
|
||||
DECLARE_INFO;
|
||||
|
||||
template<typename CellType, JSC::SubspaceAccess>
|
||||
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:
|
||||
JSYogaConfigConstructor(JSC::VM& vm, JSC::Structure* structure);
|
||||
void finishCreation(JSC::VM& vm, JSC::JSObject* prototype);
|
||||
};
|
||||
|
||||
class JSYogaNodeConstructor final : public JSC::InternalFunction {
|
||||
public:
|
||||
using Base = JSC::InternalFunction;
|
||||
static constexpr unsigned StructureFlags = Base::StructureFlags;
|
||||
|
||||
static JSYogaNodeConstructor* create(JSC::VM& vm, JSC::Structure* structure, JSC::JSObject* prototype)
|
||||
{
|
||||
JSYogaNodeConstructor* constructor = new (NotNull, JSC::allocateCell<JSYogaNodeConstructor>(vm)) JSYogaNodeConstructor(vm, structure);
|
||||
constructor->finishCreation(vm, prototype);
|
||||
return constructor;
|
||||
}
|
||||
|
||||
DECLARE_INFO;
|
||||
|
||||
template<typename CellType, JSC::SubspaceAccess>
|
||||
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:
|
||||
JSYogaNodeConstructor(JSC::VM& vm, JSC::Structure* structure);
|
||||
void finishCreation(JSC::VM& vm, JSC::JSObject* prototype);
|
||||
};
|
||||
|
||||
// Helper functions to set up class structures
|
||||
void setupJSYogaConfigClassStructure(JSC::LazyClassStructure::Initializer&);
|
||||
void setupJSYogaNodeClassStructure(JSC::LazyClassStructure::Initializer&);
|
||||
|
||||
} // namespace Bun
|
||||
19
src/bun.js/bindings/JSYogaExports.cpp
Normal file
19
src/bun.js/bindings/JSYogaExports.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "root.h"
|
||||
#include "JSYogaConstructor.h"
|
||||
#include "ZigGlobalObject.h"
|
||||
|
||||
using namespace JSC;
|
||||
|
||||
extern "C" {
|
||||
|
||||
JSC::EncodedJSValue Bun__JSYogaConfigConstructor(Zig::GlobalObject* globalObject)
|
||||
{
|
||||
return JSValue::encode(globalObject->m_JSYogaConfigClassStructure.constructor(globalObject));
|
||||
}
|
||||
|
||||
JSC::EncodedJSValue Bun__JSYogaNodeConstructor(Zig::GlobalObject* globalObject)
|
||||
{
|
||||
return JSValue::encode(globalObject->m_JSYogaNodeClassStructure.constructor(globalObject));
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
173
src/bun.js/bindings/JSYogaModule.cpp
Normal file
173
src/bun.js/bindings/JSYogaModule.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
#include "root.h"
|
||||
#include "JSYogaModule.h"
|
||||
#include "JSYogaConstructor.h"
|
||||
#include "JSYogaPrototype.h"
|
||||
#include <yoga/Yoga.h>
|
||||
#include "ZigGlobalObject.h"
|
||||
#include <JavaScriptCore/JSCInlines.h>
|
||||
#include <JavaScriptCore/FunctionPrototype.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
const JSC::ClassInfo JSYogaModule::s_info = { "Yoga"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSYogaModule) };
|
||||
|
||||
JSYogaModule* JSYogaModule::create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure)
|
||||
{
|
||||
JSYogaModule* module = new (NotNull, allocateCell<JSYogaModule>(vm)) JSYogaModule(vm, structure);
|
||||
module->finishCreation(vm, globalObject);
|
||||
return module;
|
||||
}
|
||||
|
||||
void JSYogaModule::finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject)
|
||||
{
|
||||
Base::finishCreation(vm);
|
||||
|
||||
// Create Config constructor and prototype
|
||||
auto* configPrototype = JSYogaConfigPrototype::create(vm, globalObject,
|
||||
JSYogaConfigPrototype::createStructure(vm, globalObject, globalObject->objectPrototype()));
|
||||
|
||||
auto* configConstructor = JSYogaConfigConstructor::create(vm,
|
||||
JSYogaConfigConstructor::createStructure(vm, globalObject, globalObject->functionPrototype()),
|
||||
configPrototype);
|
||||
|
||||
// Set constructor property on prototype
|
||||
configPrototype->setConstructor(vm, configConstructor);
|
||||
|
||||
// Create Node constructor and prototype
|
||||
auto* nodePrototype = JSYogaNodePrototype::create(vm, globalObject,
|
||||
JSYogaNodePrototype::createStructure(vm, globalObject, globalObject->objectPrototype()));
|
||||
|
||||
auto* nodeConstructor = JSYogaNodeConstructor::create(vm,
|
||||
JSYogaNodeConstructor::createStructure(vm, globalObject, globalObject->functionPrototype()),
|
||||
nodePrototype);
|
||||
|
||||
// Set constructor property on prototype
|
||||
nodePrototype->setConstructor(vm, nodeConstructor);
|
||||
|
||||
// Add constructors to module
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "Config"_s), configConstructor, JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "Node"_s), nodeConstructor, JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly);
|
||||
|
||||
// Add constants
|
||||
// Align values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_AUTO"_s), JSC::jsNumber(static_cast<int>(YGAlignAuto)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_FLEX_START"_s), JSC::jsNumber(static_cast<int>(YGAlignFlexStart)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_CENTER"_s), JSC::jsNumber(static_cast<int>(YGAlignCenter)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_FLEX_END"_s), JSC::jsNumber(static_cast<int>(YGAlignFlexEnd)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_STRETCH"_s), JSC::jsNumber(static_cast<int>(YGAlignStretch)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_BASELINE"_s), JSC::jsNumber(static_cast<int>(YGAlignBaseline)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_SPACE_BETWEEN"_s), JSC::jsNumber(static_cast<int>(YGAlignSpaceBetween)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_SPACE_AROUND"_s), JSC::jsNumber(static_cast<int>(YGAlignSpaceAround)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ALIGN_SPACE_EVENLY"_s), JSC::jsNumber(static_cast<int>(YGAlignSpaceEvenly)), 0);
|
||||
|
||||
// Box sizing values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "BOX_SIZING_BORDER_BOX"_s), JSC::jsNumber(static_cast<int>(YGBoxSizingBorderBox)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "BOX_SIZING_CONTENT_BOX"_s), JSC::jsNumber(static_cast<int>(YGBoxSizingContentBox)), 0);
|
||||
|
||||
// Dimension values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "DIMENSION_WIDTH"_s), JSC::jsNumber(static_cast<int>(YGDimensionWidth)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "DIMENSION_HEIGHT"_s), JSC::jsNumber(static_cast<int>(YGDimensionHeight)), 0);
|
||||
|
||||
// Direction values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "DIRECTION_INHERIT"_s), JSC::jsNumber(static_cast<int>(YGDirectionInherit)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "DIRECTION_LTR"_s), JSC::jsNumber(static_cast<int>(YGDirectionLTR)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "DIRECTION_RTL"_s), JSC::jsNumber(static_cast<int>(YGDirectionRTL)), 0);
|
||||
|
||||
// Display values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "DISPLAY_FLEX"_s), JSC::jsNumber(static_cast<int>(YGDisplayFlex)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "DISPLAY_NONE"_s), JSC::jsNumber(static_cast<int>(YGDisplayNone)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "DISPLAY_CONTENTS"_s), JSC::jsNumber(static_cast<int>(YGDisplayContents)), 0);
|
||||
|
||||
// Edge values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_LEFT"_s), JSC::jsNumber(static_cast<int>(YGEdgeLeft)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_TOP"_s), JSC::jsNumber(static_cast<int>(YGEdgeTop)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_RIGHT"_s), JSC::jsNumber(static_cast<int>(YGEdgeRight)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_BOTTOM"_s), JSC::jsNumber(static_cast<int>(YGEdgeBottom)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_START"_s), JSC::jsNumber(static_cast<int>(YGEdgeStart)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_END"_s), JSC::jsNumber(static_cast<int>(YGEdgeEnd)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_HORIZONTAL"_s), JSC::jsNumber(static_cast<int>(YGEdgeHorizontal)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_VERTICAL"_s), JSC::jsNumber(static_cast<int>(YGEdgeVertical)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EDGE_ALL"_s), JSC::jsNumber(static_cast<int>(YGEdgeAll)), 0);
|
||||
|
||||
// Errata values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ERRATA_NONE"_s), JSC::jsNumber(static_cast<int>(YGErrataNone)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ERRATA_STRETCH_FLEX_BASIS"_s), JSC::jsNumber(static_cast<int>(YGErrataStretchFlexBasis)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ERRATA_ABSOLUTE_POSITION_WITHOUT_INSETS_EXCLUDES_PADDING"_s), JSC::jsNumber(static_cast<int>(YGErrataAbsolutePositionWithoutInsetsExcludesPadding)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ERRATA_ABSOLUTE_PERCENT_AGAINST_INNER_SIZE"_s), JSC::jsNumber(static_cast<int>(YGErrataAbsolutePercentAgainstInnerSize)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ERRATA_ALL"_s), JSC::jsNumber(static_cast<int>(YGErrataAll)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "ERRATA_CLASSIC"_s), JSC::jsNumber(static_cast<int>(YGErrataClassic)), 0);
|
||||
|
||||
// Experimental feature values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS"_s), JSC::jsNumber(static_cast<int>(YGExperimentalFeatureWebFlexBasis)), 0);
|
||||
|
||||
// Flex direction values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "FLEX_DIRECTION_COLUMN"_s), JSC::jsNumber(static_cast<int>(YGFlexDirectionColumn)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "FLEX_DIRECTION_COLUMN_REVERSE"_s), JSC::jsNumber(static_cast<int>(YGFlexDirectionColumnReverse)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "FLEX_DIRECTION_ROW"_s), JSC::jsNumber(static_cast<int>(YGFlexDirectionRow)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "FLEX_DIRECTION_ROW_REVERSE"_s), JSC::jsNumber(static_cast<int>(YGFlexDirectionRowReverse)), 0);
|
||||
|
||||
// Gutter values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "GUTTER_COLUMN"_s), JSC::jsNumber(static_cast<int>(YGGutterColumn)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "GUTTER_ROW"_s), JSC::jsNumber(static_cast<int>(YGGutterRow)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "GUTTER_ALL"_s), JSC::jsNumber(static_cast<int>(YGGutterAll)), 0);
|
||||
|
||||
// Justify values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "JUSTIFY_FLEX_START"_s), JSC::jsNumber(static_cast<int>(YGJustifyFlexStart)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "JUSTIFY_CENTER"_s), JSC::jsNumber(static_cast<int>(YGJustifyCenter)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "JUSTIFY_FLEX_END"_s), JSC::jsNumber(static_cast<int>(YGJustifyFlexEnd)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "JUSTIFY_SPACE_BETWEEN"_s), JSC::jsNumber(static_cast<int>(YGJustifySpaceBetween)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "JUSTIFY_SPACE_AROUND"_s), JSC::jsNumber(static_cast<int>(YGJustifySpaceAround)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "JUSTIFY_SPACE_EVENLY"_s), JSC::jsNumber(static_cast<int>(YGJustifySpaceEvenly)), 0);
|
||||
|
||||
// Log level values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "LOG_LEVEL_ERROR"_s), JSC::jsNumber(static_cast<int>(YGLogLevelError)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "LOG_LEVEL_WARN"_s), JSC::jsNumber(static_cast<int>(YGLogLevelWarn)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "LOG_LEVEL_INFO"_s), JSC::jsNumber(static_cast<int>(YGLogLevelInfo)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "LOG_LEVEL_DEBUG"_s), JSC::jsNumber(static_cast<int>(YGLogLevelDebug)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "LOG_LEVEL_VERBOSE"_s), JSC::jsNumber(static_cast<int>(YGLogLevelVerbose)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "LOG_LEVEL_FATAL"_s), JSC::jsNumber(static_cast<int>(YGLogLevelFatal)), 0);
|
||||
|
||||
// Measure mode values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "MEASURE_MODE_UNDEFINED"_s), JSC::jsNumber(static_cast<int>(YGMeasureModeUndefined)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "MEASURE_MODE_EXACTLY"_s), JSC::jsNumber(static_cast<int>(YGMeasureModeExactly)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "MEASURE_MODE_AT_MOST"_s), JSC::jsNumber(static_cast<int>(YGMeasureModeAtMost)), 0);
|
||||
|
||||
// Node type values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "NODE_TYPE_DEFAULT"_s), JSC::jsNumber(static_cast<int>(YGNodeTypeDefault)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "NODE_TYPE_TEXT"_s), JSC::jsNumber(static_cast<int>(YGNodeTypeText)), 0);
|
||||
|
||||
// Overflow values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "OVERFLOW_VISIBLE"_s), JSC::jsNumber(static_cast<int>(YGOverflowVisible)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "OVERFLOW_HIDDEN"_s), JSC::jsNumber(static_cast<int>(YGOverflowHidden)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "OVERFLOW_SCROLL"_s), JSC::jsNumber(static_cast<int>(YGOverflowScroll)), 0);
|
||||
|
||||
// Position type values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "POSITION_TYPE_STATIC"_s), JSC::jsNumber(static_cast<int>(YGPositionTypeStatic)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "POSITION_TYPE_RELATIVE"_s), JSC::jsNumber(static_cast<int>(YGPositionTypeRelative)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "POSITION_TYPE_ABSOLUTE"_s), JSC::jsNumber(static_cast<int>(YGPositionTypeAbsolute)), 0);
|
||||
|
||||
// Unit values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "UNIT_UNDEFINED"_s), JSC::jsNumber(static_cast<int>(YGUnitUndefined)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "UNIT_POINT"_s), JSC::jsNumber(static_cast<int>(YGUnitPoint)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "UNIT_PERCENT"_s), JSC::jsNumber(static_cast<int>(YGUnitPercent)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "UNIT_AUTO"_s), JSC::jsNumber(static_cast<int>(YGUnitAuto)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "UNIT_MAX_CONTENT"_s), JSC::jsNumber(static_cast<int>(YGUnitMaxContent)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "UNIT_FIT_CONTENT"_s), JSC::jsNumber(static_cast<int>(YGUnitFitContent)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "UNIT_STRETCH"_s), JSC::jsNumber(static_cast<int>(YGUnitStretch)), 0);
|
||||
|
||||
// Wrap values
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "WRAP_NO_WRAP"_s), JSC::jsNumber(static_cast<int>(YGWrapNoWrap)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "WRAP_WRAP"_s), JSC::jsNumber(static_cast<int>(YGWrapWrap)), 0);
|
||||
putDirect(vm, JSC::Identifier::fromString(vm, "WRAP_WRAP_REVERSE"_s), JSC::jsNumber(static_cast<int>(YGWrapWrapReverse)), 0);
|
||||
}
|
||||
|
||||
// Export function for Zig integration
|
||||
extern "C" JSC::EncodedJSValue Bun__createYogaModule(Zig::GlobalObject* globalObject)
|
||||
{
|
||||
JSC::VM& vm = globalObject->vm();
|
||||
auto* structure = JSYogaModule::createStructure(vm, globalObject, globalObject->objectPrototype());
|
||||
auto* module = JSYogaModule::create(vm, globalObject, structure);
|
||||
return JSC::JSValue::encode(module);
|
||||
}
|
||||
|
||||
} // namespace Bun
|
||||
36
src/bun.js/bindings/JSYogaModule.h
Normal file
36
src/bun.js/bindings/JSYogaModule.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include "root.h"
|
||||
#include <JavaScriptCore/JSObject.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class JSYogaModule final : public JSC::JSNonFinalObject {
|
||||
public:
|
||||
using Base = JSC::JSNonFinalObject;
|
||||
static constexpr unsigned StructureFlags = Base::StructureFlags;
|
||||
|
||||
static JSYogaModule* create(JSC::VM&, JSC::JSGlobalObject*, JSC::Structure*);
|
||||
|
||||
DECLARE_INFO;
|
||||
|
||||
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
|
||||
{
|
||||
return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
|
||||
}
|
||||
|
||||
template<typename, JSC::SubspaceAccess>
|
||||
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
|
||||
{
|
||||
return &vm.plainObjectSpace();
|
||||
}
|
||||
|
||||
private:
|
||||
JSYogaModule(JSC::VM& vm, JSC::Structure* structure)
|
||||
: Base(vm, structure)
|
||||
{
|
||||
}
|
||||
|
||||
void finishCreation(JSC::VM&, JSC::JSGlobalObject*);
|
||||
};
|
||||
|
||||
} // namespace Bun
|
||||
155
src/bun.js/bindings/JSYogaNode.cpp
Normal file
155
src/bun.js/bindings/JSYogaNode.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
#include "root.h"
|
||||
#include "JSYogaNode.h"
|
||||
#include "YogaNodeImpl.h"
|
||||
#include "JSYogaConfig.h"
|
||||
#include "JSYogaNodeOwner.h"
|
||||
#include "webcore/DOMIsoSubspaces.h"
|
||||
#include "webcore/DOMClientIsoSubspaces.h"
|
||||
#include "webcore/WebCoreJSClientData.h"
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
using namespace JSC;
|
||||
|
||||
const JSC::ClassInfo JSYogaNode::s_info = { "Node"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSYogaNode) };
|
||||
|
||||
JSYogaNode::JSYogaNode(JSC::VM& vm, JSC::Structure* structure)
|
||||
: Base(vm, structure)
|
||||
, m_impl(YogaNodeImpl::create())
|
||||
{
|
||||
}
|
||||
|
||||
JSYogaNode::JSYogaNode(JSC::VM& vm, JSC::Structure* structure, Ref<YogaNodeImpl>&& impl)
|
||||
: Base(vm, structure)
|
||||
, m_impl(std::move(impl))
|
||||
{
|
||||
}
|
||||
|
||||
JSYogaNode::~JSYogaNode()
|
||||
{
|
||||
// The WeakHandleOwner::finalize should handle cleanup
|
||||
// Don't interfere with that mechanism
|
||||
}
|
||||
|
||||
JSYogaNode* JSYogaNode::create(JSC::VM& vm, JSC::Structure* structure, YGConfigRef config, JSYogaConfig* jsConfig)
|
||||
{
|
||||
JSYogaNode* node = new (NotNull, JSC::allocateCell<JSYogaNode>(vm)) JSYogaNode(vm, structure);
|
||||
node->finishCreation(vm, config, jsConfig);
|
||||
return node;
|
||||
}
|
||||
|
||||
JSYogaNode* JSYogaNode::create(JSC::VM& vm, JSC::Structure* structure, Ref<YogaNodeImpl>&& impl)
|
||||
{
|
||||
JSYogaNode* node = new (NotNull, JSC::allocateCell<JSYogaNode>(vm)) JSYogaNode(vm, structure, std::move(impl));
|
||||
node->finishCreation(vm);
|
||||
return node;
|
||||
}
|
||||
|
||||
void JSYogaNode::finishCreation(JSC::VM& vm, YGConfigRef config, JSYogaConfig* jsConfig)
|
||||
{
|
||||
Base::finishCreation(vm);
|
||||
|
||||
// If we need to recreate with specific config, do so
|
||||
if (config || jsConfig) {
|
||||
m_impl = YogaNodeImpl::create(config);
|
||||
}
|
||||
|
||||
// Set this JS wrapper in the C++ impl
|
||||
m_impl->setJSWrapper(this);
|
||||
|
||||
// Store the JSYogaConfig if provided
|
||||
if (jsConfig) {
|
||||
m_config.set(vm, this, jsConfig);
|
||||
}
|
||||
|
||||
// Initialize children array to maintain strong references
|
||||
// This mirrors React Native's _reactSubviews NSMutableArray
|
||||
JSC::JSGlobalObject* globalObject = this->globalObject();
|
||||
m_children.set(vm, this, JSC::constructEmptyArray(globalObject, nullptr, 0));
|
||||
}
|
||||
|
||||
void JSYogaNode::finishCreation(JSC::VM& vm)
|
||||
{
|
||||
Base::finishCreation(vm);
|
||||
|
||||
// Set this JS wrapper in the C++ impl
|
||||
m_impl->setJSWrapper(this);
|
||||
|
||||
// No JSYogaConfig in this path - it's only set when explicitly provided
|
||||
|
||||
// Initialize children array to maintain strong references
|
||||
// This mirrors React Native's _reactSubviews NSMutableArray
|
||||
JSC::JSGlobalObject* globalObject = this->globalObject();
|
||||
m_children.set(vm, this, JSC::constructEmptyArray(globalObject, nullptr, 0));
|
||||
}
|
||||
|
||||
JSC::Structure* JSYogaNode::createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
|
||||
{
|
||||
return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
|
||||
}
|
||||
|
||||
void JSYogaNode::destroy(JSC::JSCell* cell)
|
||||
{
|
||||
static_cast<JSYogaNode*>(cell)->~JSYogaNode();
|
||||
}
|
||||
|
||||
JSYogaNode* JSYogaNode::fromYGNode(YGNodeRef nodeRef)
|
||||
{
|
||||
if (!nodeRef) return nullptr;
|
||||
if (auto* impl = YogaNodeImpl::fromYGNode(nodeRef)) {
|
||||
return impl->jsWrapper();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
JSC::JSGlobalObject* JSYogaNode::globalObject() const
|
||||
{
|
||||
return this->structure()->globalObject();
|
||||
}
|
||||
|
||||
template<typename MyClassT, JSC::SubspaceAccess mode>
|
||||
JSC::GCClient::IsoSubspace* JSYogaNode::subspaceFor(JSC::VM& vm)
|
||||
{
|
||||
if constexpr (mode == JSC::SubspaceAccess::Concurrently)
|
||||
return nullptr;
|
||||
return WebCore::subspaceForImpl<MyClassT, WebCore::UseCustomHeapCellType::No>(
|
||||
vm,
|
||||
[](auto& spaces) { return spaces.m_clientSubspaceForJSYogaNode.get(); },
|
||||
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForJSYogaNode = std::forward<decltype(space)>(space); },
|
||||
[](auto& spaces) { return spaces.m_subspaceForJSYogaNode.get(); },
|
||||
[](auto& spaces, auto&& space) { spaces.m_subspaceForJSYogaNode = std::forward<decltype(space)>(space); });
|
||||
}
|
||||
|
||||
template<typename Visitor>
|
||||
void JSYogaNode::visitAdditionalChildren(Visitor& visitor)
|
||||
{
|
||||
visitor.append(m_measureFunc);
|
||||
visitor.append(m_dirtiedFunc);
|
||||
visitor.append(m_baselineFunc);
|
||||
visitor.append(m_config);
|
||||
visitor.append(m_children);
|
||||
|
||||
// Use the YogaNodeImpl pointer as opaque root instead of YGNodeRef
|
||||
// This avoids use-after-free when YGNode memory is freed but YogaNodeImpl still exists
|
||||
visitor.addOpaqueRoot(&m_impl.get());
|
||||
}
|
||||
|
||||
DEFINE_VISIT_ADDITIONAL_CHILDREN(JSYogaNode);
|
||||
|
||||
template<typename Visitor>
|
||||
void JSYogaNode::visitOutputConstraints(JSC::JSCell* cell, Visitor& visitor)
|
||||
{
|
||||
auto* thisObject = jsCast<JSYogaNode*>(cell);
|
||||
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
|
||||
Base::visitOutputConstraints(thisObject, visitor);
|
||||
|
||||
// Re-visit after mutator execution in case callbacks changed references
|
||||
// This is critical for objects whose reachability can change during runtime
|
||||
thisObject->visitAdditionalChildren(visitor);
|
||||
}
|
||||
|
||||
template void JSYogaNode::visitOutputConstraints(JSC::JSCell*, JSC::AbstractSlotVisitor&);
|
||||
template void JSYogaNode::visitOutputConstraints(JSC::JSCell*, JSC::SlotVisitor&);
|
||||
|
||||
} // namespace Bun
|
||||
66
src/bun.js/bindings/JSYogaNode.h
Normal file
66
src/bun.js/bindings/JSYogaNode.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
#include "root.h"
|
||||
#include <memory>
|
||||
#include <JavaScriptCore/JSDestructibleObject.h>
|
||||
#include <JavaScriptCore/WriteBarrier.h>
|
||||
#include <wtf/Ref.h>
|
||||
|
||||
// Forward declarations
|
||||
typedef struct YGNode* YGNodeRef;
|
||||
typedef struct YGConfig* YGConfigRef;
|
||||
typedef const struct YGNode* YGNodeConstRef;
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class JSYogaConfig;
|
||||
class YogaNodeImpl;
|
||||
|
||||
class JSYogaNode final : public JSC::JSDestructibleObject {
|
||||
public:
|
||||
using Base = JSC::JSDestructibleObject;
|
||||
static constexpr unsigned StructureFlags = Base::StructureFlags;
|
||||
static constexpr JSC::DestructionMode needsDestruction = JSC::NeedsDestruction;
|
||||
|
||||
static JSYogaNode* create(JSC::VM&, JSC::Structure*, YGConfigRef config = nullptr, JSYogaConfig* jsConfig = nullptr);
|
||||
static JSYogaNode* create(JSC::VM&, JSC::Structure*, Ref<YogaNodeImpl>&&);
|
||||
static void destroy(JSC::JSCell*);
|
||||
static JSC::Structure* createStructure(JSC::VM&, JSC::JSGlobalObject*, JSC::JSValue);
|
||||
~JSYogaNode();
|
||||
|
||||
template<typename, JSC::SubspaceAccess>
|
||||
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM&);
|
||||
|
||||
DECLARE_INFO;
|
||||
|
||||
template<typename Visitor> void visitAdditionalChildren(Visitor&);
|
||||
template<typename Visitor> static void visitOutputConstraints(JSC::JSCell*, Visitor&);
|
||||
|
||||
YogaNodeImpl& impl() { return m_impl.get(); }
|
||||
const YogaNodeImpl& impl() const { return m_impl.get(); }
|
||||
|
||||
// Helper to get JS wrapper from Yoga node
|
||||
static JSYogaNode* fromYGNode(YGNodeRef);
|
||||
JSC::JSGlobalObject* globalObject() const;
|
||||
|
||||
// Storage for JS callbacks
|
||||
JSC::WriteBarrier<JSC::JSObject> m_measureFunc;
|
||||
JSC::WriteBarrier<JSC::JSObject> m_dirtiedFunc;
|
||||
JSC::WriteBarrier<JSC::JSObject> m_baselineFunc;
|
||||
|
||||
// Store the JSYogaConfig that was used to create this node
|
||||
JSC::WriteBarrier<JSC::JSObject> m_config;
|
||||
|
||||
// Store children to prevent GC while still part of Yoga tree
|
||||
// This mirrors React Native's _reactSubviews NSMutableArray pattern
|
||||
JSC::WriteBarrier<JSC::JSArray> m_children;
|
||||
|
||||
private:
|
||||
JSYogaNode(JSC::VM&, JSC::Structure*);
|
||||
JSYogaNode(JSC::VM&, JSC::Structure*, Ref<YogaNodeImpl>&&);
|
||||
void finishCreation(JSC::VM&, YGConfigRef config, JSYogaConfig* jsConfig);
|
||||
void finishCreation(JSC::VM&);
|
||||
|
||||
Ref<YogaNodeImpl> m_impl;
|
||||
};
|
||||
|
||||
} // namespace Bun
|
||||
77
src/bun.js/bindings/JSYogaNodeOwner.cpp
Normal file
77
src/bun.js/bindings/JSYogaNodeOwner.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "JSYogaNodeOwner.h"
|
||||
#include "YogaNodeImpl.h"
|
||||
#include "JSYogaNode.h"
|
||||
#include <JavaScriptCore/JSCInlines.h>
|
||||
#include <wtf/NeverDestroyed.h>
|
||||
#include <wtf/Compiler.h>
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
void* root(YogaNodeImpl* impl)
|
||||
{
|
||||
if (!impl)
|
||||
return nullptr;
|
||||
|
||||
YGNodeRef current = impl->yogaNode();
|
||||
YGNodeRef root = current;
|
||||
|
||||
// Traverse up to find the root node
|
||||
while (current) {
|
||||
YGNodeRef parent = YGNodeGetParent(current);
|
||||
if (!parent)
|
||||
break;
|
||||
root = parent;
|
||||
current = parent;
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
void JSYogaNodeOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
|
||||
{
|
||||
// This is where we deref the C++ YogaNodeImpl wrapper
|
||||
// The context contains our YogaNodeImpl
|
||||
auto* impl = static_cast<YogaNodeImpl*>(context);
|
||||
|
||||
// TODO: YGNodeFree during concurrent GC causes heap-use-after-free crashes
|
||||
// because YGNodeFree assumes parent/child nodes are still valid, but GC can
|
||||
// free them in arbitrary order. We need a solution that either:
|
||||
// 1. Defers YGNodeFree to run outside GC (e.g., via a cleanup queue)
|
||||
// 2. Implements reference counting at the Yoga level
|
||||
// 3. Uses a different lifecycle that mirrors React Native's manual memory management
|
||||
//
|
||||
// For now, skip YGNodeFree during GC to prevent crashes at the cost of memory leaks.
|
||||
// This matches what React Native would do if their dealloc was never called.
|
||||
|
||||
// YGNodeRef node = impl->yogaNode();
|
||||
// if (node) {
|
||||
// YGNodeFree(node);
|
||||
// }
|
||||
|
||||
// Deref the YogaNodeImpl - this will decrease its reference count
|
||||
// and potentially destroy it if no other references exist
|
||||
impl->deref();
|
||||
}
|
||||
|
||||
bool JSYogaNodeOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void* context, JSC::AbstractSlotVisitor& visitor, ASCIILiteral* reason)
|
||||
{
|
||||
UNUSED_PARAM(handle);
|
||||
|
||||
auto* impl = static_cast<YogaNodeImpl*>(context);
|
||||
|
||||
// Standard WebKit pattern: check if reachable as opaque root
|
||||
bool reachable = visitor.containsOpaqueRoot(impl);
|
||||
if (reachable && reason)
|
||||
*reason = "YogaNode reachable from opaque root"_s;
|
||||
|
||||
return reachable;
|
||||
}
|
||||
|
||||
JSYogaNodeOwner& jsYogaNodeOwner()
|
||||
{
|
||||
static NeverDestroyed<JSYogaNodeOwner> owner;
|
||||
return owner.get();
|
||||
}
|
||||
|
||||
} // namespace Bun
|
||||
23
src/bun.js/bindings/JSYogaNodeOwner.h
Normal file
23
src/bun.js/bindings/JSYogaNodeOwner.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "root.h"
|
||||
#include <JavaScriptCore/WeakHandleOwner.h>
|
||||
#include <JavaScriptCore/Weak.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class YogaNodeImpl;
|
||||
class JSYogaNode;
|
||||
|
||||
class JSYogaNodeOwner : public JSC::WeakHandleOwner {
|
||||
public:
|
||||
void finalize(JSC::Handle<JSC::Unknown>, void* context) final;
|
||||
bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::AbstractSlotVisitor&, ASCIILiteral*) final;
|
||||
};
|
||||
|
||||
JSYogaNodeOwner& jsYogaNodeOwner();
|
||||
|
||||
// Helper function to get root for YogaNodeImpl
|
||||
void* root(YogaNodeImpl*);
|
||||
|
||||
} // namespace Bun
|
||||
3461
src/bun.js/bindings/JSYogaPrototype.cpp
Normal file
3461
src/bun.js/bindings/JSYogaPrototype.cpp
Normal file
File diff suppressed because it is too large
Load Diff
86
src/bun.js/bindings/JSYogaPrototype.h
Normal file
86
src/bun.js/bindings/JSYogaPrototype.h
Normal file
@@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
#include "root.h"
|
||||
#include <JavaScriptCore/JSObject.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
// Base class for Yoga prototypes
|
||||
class JSYogaConfigPrototype final : public JSC::JSNonFinalObject {
|
||||
public:
|
||||
using Base = JSC::JSNonFinalObject;
|
||||
static constexpr unsigned StructureFlags = Base::StructureFlags;
|
||||
|
||||
static JSYogaConfigPrototype* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure)
|
||||
{
|
||||
JSYogaConfigPrototype* prototype = new (NotNull, allocateCell<JSYogaConfigPrototype>(vm)) JSYogaConfigPrototype(vm, structure);
|
||||
prototype->finishCreation(vm, globalObject);
|
||||
return prototype;
|
||||
}
|
||||
|
||||
template<typename, JSC::SubspaceAccess>
|
||||
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
|
||||
{
|
||||
return &vm.plainObjectSpace();
|
||||
}
|
||||
|
||||
DECLARE_INFO;
|
||||
|
||||
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
|
||||
{
|
||||
auto* structure = JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
|
||||
structure->setMayBePrototype(true);
|
||||
return structure;
|
||||
}
|
||||
|
||||
private:
|
||||
JSYogaConfigPrototype(JSC::VM& vm, JSC::Structure* structure)
|
||||
: Base(vm, structure)
|
||||
{
|
||||
}
|
||||
|
||||
void finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject);
|
||||
|
||||
public:
|
||||
void setConstructor(JSC::VM& vm, JSC::JSObject* constructor);
|
||||
};
|
||||
|
||||
class JSYogaNodePrototype final : public JSC::JSNonFinalObject {
|
||||
public:
|
||||
using Base = JSC::JSNonFinalObject;
|
||||
static constexpr unsigned StructureFlags = Base::StructureFlags;
|
||||
|
||||
static JSYogaNodePrototype* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure)
|
||||
{
|
||||
JSYogaNodePrototype* prototype = new (NotNull, allocateCell<JSYogaNodePrototype>(vm)) JSYogaNodePrototype(vm, structure);
|
||||
prototype->finishCreation(vm, globalObject);
|
||||
return prototype;
|
||||
}
|
||||
|
||||
template<typename, JSC::SubspaceAccess>
|
||||
static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
|
||||
{
|
||||
return &vm.plainObjectSpace();
|
||||
}
|
||||
|
||||
DECLARE_INFO;
|
||||
|
||||
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
|
||||
{
|
||||
auto* structure = JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
|
||||
structure->setMayBePrototype(true);
|
||||
return structure;
|
||||
}
|
||||
|
||||
private:
|
||||
JSYogaNodePrototype(JSC::VM& vm, JSC::Structure* structure)
|
||||
: Base(vm, structure)
|
||||
{
|
||||
}
|
||||
|
||||
void finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject);
|
||||
|
||||
public:
|
||||
void setConstructor(JSC::VM& vm, JSC::JSObject* constructor);
|
||||
};
|
||||
|
||||
} // namespace Bun
|
||||
74
src/bun.js/bindings/YogaConfigImpl.cpp
Normal file
74
src/bun.js/bindings/YogaConfigImpl.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "YogaConfigImpl.h"
|
||||
#include "JSYogaConfig.h"
|
||||
#include "JSYogaConfigOwner.h"
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
Ref<YogaConfigImpl> YogaConfigImpl::create()
|
||||
{
|
||||
return adoptRef(*new YogaConfigImpl());
|
||||
}
|
||||
|
||||
YogaConfigImpl::YogaConfigImpl()
|
||||
{
|
||||
m_yogaConfig = YGConfigNew();
|
||||
|
||||
// Store this C++ wrapper in the Yoga config's context
|
||||
// Note: YGConfig doesn't have context like YGNode, so we handle this differently
|
||||
}
|
||||
|
||||
YogaConfigImpl::~YogaConfigImpl()
|
||||
{
|
||||
if (m_yogaConfig) {
|
||||
YGConfigFree(m_yogaConfig);
|
||||
m_yogaConfig = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void YogaConfigImpl::setJSWrapper(JSYogaConfig* 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<JSYogaConfig>(wrapper, &jsYogaConfigOwner(), this);
|
||||
}
|
||||
|
||||
void YogaConfigImpl::clearJSWrapper()
|
||||
{
|
||||
m_wrapper.clear();
|
||||
}
|
||||
|
||||
void YogaConfigImpl::clearJSWrapperWithoutDeref()
|
||||
{
|
||||
// Clear weak reference without deref - used by JS destructor
|
||||
// when WeakHandleOwner::finalize will handle the deref
|
||||
m_wrapper.clear();
|
||||
}
|
||||
|
||||
JSYogaConfig* YogaConfigImpl::jsWrapper() const
|
||||
{
|
||||
return m_wrapper.get();
|
||||
}
|
||||
|
||||
YogaConfigImpl* YogaConfigImpl::fromYGConfig(YGConfigRef configRef)
|
||||
{
|
||||
// YGConfig doesn't have context storage like YGNode
|
||||
// We'd need to maintain a separate map if needed
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void YogaConfigImpl::replaceYogaConfig(YGConfigRef newConfig)
|
||||
{
|
||||
if (m_yogaConfig) {
|
||||
YGConfigFree(m_yogaConfig);
|
||||
}
|
||||
m_yogaConfig = newConfig;
|
||||
}
|
||||
|
||||
} // namespace Bun
|
||||
44
src/bun.js/bindings/YogaConfigImpl.h
Normal file
44
src/bun.js/bindings/YogaConfigImpl.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "root.h"
|
||||
#include <wtf/RefCounted.h>
|
||||
#include <JavaScriptCore/Weak.h>
|
||||
#include <JavaScriptCore/JSObject.h>
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class JSYogaConfig;
|
||||
|
||||
class YogaConfigImpl : public RefCounted<YogaConfigImpl> {
|
||||
public:
|
||||
static Ref<YogaConfigImpl> create();
|
||||
~YogaConfigImpl();
|
||||
|
||||
YGConfigRef yogaConfig() const { return m_freed ? nullptr : m_yogaConfig; }
|
||||
|
||||
// JS wrapper management
|
||||
void setJSWrapper(JSYogaConfig*);
|
||||
void clearJSWrapper();
|
||||
void clearJSWrapperWithoutDeref(); // Clear weak ref without deref (for JS destructor)
|
||||
JSYogaConfig* jsWrapper() const;
|
||||
|
||||
// Helper to get YogaConfigImpl from YGConfigRef
|
||||
static YogaConfigImpl* fromYGConfig(YGConfigRef);
|
||||
|
||||
// Replace the internal YGConfigRef (used for advanced cases)
|
||||
void replaceYogaConfig(YGConfigRef newConfig);
|
||||
|
||||
// Mark as freed (for JS free() method validation)
|
||||
void markAsFreed() { m_freed = true; }
|
||||
bool isFreed() const { return m_freed; }
|
||||
|
||||
private:
|
||||
explicit YogaConfigImpl();
|
||||
|
||||
YGConfigRef m_yogaConfig;
|
||||
JSC::Weak<JSYogaConfig> m_wrapper;
|
||||
bool m_freed { false };
|
||||
};
|
||||
|
||||
} // namespace Bun
|
||||
90
src/bun.js/bindings/YogaNodeImpl.cpp
Normal file
90
src/bun.js/bindings/YogaNodeImpl.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#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()
|
||||
{
|
||||
// Don't call YGNodeFree here - let JS finalizer handle it to control timing
|
||||
// This avoids double-free issues during GC when nodes may be freed in arbitrary order
|
||||
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)
|
||||
{
|
||||
// Don't access old YGNode - it might be freed already
|
||||
// Let Yoga handle cleanup of the old node
|
||||
m_yogaNode = newNode;
|
||||
if (newNode) {
|
||||
YGNodeSetContext(newNode, this);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Bun
|
||||
43
src/bun.js/bindings/YogaNodeImpl.h
Normal file
43
src/bun.js/bindings/YogaNodeImpl.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "root.h"
|
||||
#include <wtf/RefCounted.h>
|
||||
#include <JavaScriptCore/Weak.h>
|
||||
#include <JavaScriptCore/JSObject.h>
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
namespace Bun {
|
||||
|
||||
class JSYogaNode;
|
||||
class JSYogaConfig;
|
||||
|
||||
class YogaNodeImpl : public RefCounted<YogaNodeImpl> {
|
||||
public:
|
||||
static Ref<YogaNodeImpl> create(YGConfigRef config = nullptr);
|
||||
~YogaNodeImpl();
|
||||
|
||||
YGNodeRef yogaNode() const { return m_yogaNode; }
|
||||
|
||||
// JS wrapper management
|
||||
void setJSWrapper(JSYogaNode*);
|
||||
void clearJSWrapper();
|
||||
void clearJSWrapperWithoutDeref(); // Clear weak ref without deref (for JS destructor)
|
||||
JSYogaNode* jsWrapper() const;
|
||||
|
||||
// Config access through JS wrapper's WriteBarrier
|
||||
JSYogaConfig* jsConfig() const;
|
||||
|
||||
// Helper to get YogaNodeImpl from YGNodeRef
|
||||
static YogaNodeImpl* fromYGNode(YGNodeRef);
|
||||
|
||||
// Replace the internal YGNodeRef (used for cloning)
|
||||
void replaceYogaNode(YGNodeRef newNode);
|
||||
|
||||
private:
|
||||
explicit YogaNodeImpl(YGConfigRef config);
|
||||
|
||||
YGNodeRef m_yogaNode;
|
||||
JSC::Weak<JSYogaNode> m_wrapper;
|
||||
};
|
||||
|
||||
} // namespace Bun
|
||||
@@ -194,6 +194,7 @@
|
||||
#include "node/NodeTimers.h"
|
||||
#include "JSConnectionsList.h"
|
||||
#include "JSHTTPParser.h"
|
||||
#include "JSYogaConstructor.h"
|
||||
#include <exception>
|
||||
#include <mutex>
|
||||
#include "JSBunRequest.h"
|
||||
@@ -1795,6 +1796,15 @@ void GlobalObject::finishCreation(VM& vm)
|
||||
setupHTTPParserClassStructure(init);
|
||||
});
|
||||
|
||||
m_JSYogaConfigClassStructure.initLater(
|
||||
[](LazyClassStructure::Initializer& init) {
|
||||
setupJSYogaConfigClassStructure(init);
|
||||
});
|
||||
m_JSYogaNodeClassStructure.initLater(
|
||||
[](LazyClassStructure::Initializer& init) {
|
||||
setupJSYogaNodeClassStructure(init);
|
||||
});
|
||||
|
||||
m_JSNodePerformanceHooksHistogramClassStructure.initLater(
|
||||
[](LazyClassStructure::Initializer& init) {
|
||||
Bun::setupJSNodePerformanceHooksHistogramClassStructure(init);
|
||||
|
||||
@@ -640,7 +640,10 @@ public:
|
||||
V(public, LazyPropertyOfGlobalObject<Symbol>, m_nodeVMDontContextify) \
|
||||
V(public, LazyPropertyOfGlobalObject<Symbol>, m_nodeVMUseMainContextDefaultLoader) \
|
||||
V(public, LazyPropertyOfGlobalObject<JSFunction>, m_ipcSerializeFunction) \
|
||||
V(public, LazyPropertyOfGlobalObject<JSFunction>, m_ipcParseHandleFunction)
|
||||
V(public, LazyPropertyOfGlobalObject<JSFunction>, m_ipcParseHandleFunction) \
|
||||
\
|
||||
V(public, LazyClassStructure, m_JSYogaConfigClassStructure) \
|
||||
V(public, LazyClassStructure, m_JSYogaNodeClassStructure)
|
||||
|
||||
#define DECLARE_GLOBALOBJECT_GC_MEMBER(visibility, T, name) \
|
||||
visibility: \
|
||||
|
||||
@@ -954,5 +954,7 @@ public:
|
||||
|
||||
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForJSConnectionsList;
|
||||
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForJSHTTPParser;
|
||||
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForJSYogaConfig;
|
||||
std::unique_ptr<GCClient::IsoSubspace> m_clientSubspaceForJSYogaNode;
|
||||
};
|
||||
} // namespace WebCore
|
||||
|
||||
@@ -957,6 +957,8 @@ public:
|
||||
|
||||
std::unique_ptr<IsoSubspace> m_subspaceForJSConnectionsList;
|
||||
std::unique_ptr<IsoSubspace> m_subspaceForJSHTTPParser;
|
||||
std::unique_ptr<IsoSubspace> m_subspaceForJSYogaConfig;
|
||||
std::unique_ptr<IsoSubspace> m_subspaceForJSYogaNode;
|
||||
};
|
||||
} // namespace WebCore
|
||||
|
||||
|
||||
102
test/js/bun/yoga/yoga-config.test.js
Normal file
102
test/js/bun/yoga/yoga-config.test.js
Normal file
@@ -0,0 +1,102 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
// Test if we can access Yoga via Bun.Yoga
|
||||
const Yoga = Bun.Yoga;
|
||||
|
||||
describe("Yoga.Config", () => {
|
||||
test("Config constructor", () => {
|
||||
const config = new Yoga.Config();
|
||||
expect(config).toBeDefined();
|
||||
expect(config.constructor.name).toBe("Config");
|
||||
});
|
||||
|
||||
test("Config.create() static method", () => {
|
||||
const config = Yoga.Config.create();
|
||||
expect(config).toBeDefined();
|
||||
expect(config.constructor.name).toBe("Config");
|
||||
});
|
||||
|
||||
test("setUseWebDefaults", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Should not throw
|
||||
expect(() => config.setUseWebDefaults(true)).not.toThrow();
|
||||
expect(() => config.setUseWebDefaults(false)).not.toThrow();
|
||||
expect(() => config.setUseWebDefaults()).not.toThrow(); // defaults to true
|
||||
});
|
||||
|
||||
test("useWebDefaults (legacy)", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Should not throw
|
||||
expect(() => config.useWebDefaults()).not.toThrow();
|
||||
});
|
||||
|
||||
test("setPointScaleFactor and getPointScaleFactor", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
config.setPointScaleFactor(2.0);
|
||||
expect(config.getPointScaleFactor()).toBe(2.0);
|
||||
|
||||
config.setPointScaleFactor(0); // disable pixel rounding
|
||||
expect(config.getPointScaleFactor()).toBe(0);
|
||||
|
||||
config.setPointScaleFactor(3.5);
|
||||
expect(config.getPointScaleFactor()).toBe(3.5);
|
||||
});
|
||||
|
||||
test("setErrata and getErrata", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Test with different errata values
|
||||
config.setErrata(Yoga.ERRATA_NONE);
|
||||
expect(config.getErrata()).toBe(Yoga.ERRATA_NONE);
|
||||
|
||||
config.setErrata(Yoga.ERRATA_CLASSIC);
|
||||
expect(config.getErrata()).toBe(Yoga.ERRATA_CLASSIC);
|
||||
|
||||
config.setErrata(Yoga.ERRATA_ALL);
|
||||
expect(config.getErrata()).toBe(Yoga.ERRATA_ALL);
|
||||
});
|
||||
|
||||
test("setExperimentalFeatureEnabled and isExperimentalFeatureEnabled", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Test with a hypothetical experimental feature
|
||||
const feature = 0; // Assuming 0 is a valid experimental feature
|
||||
|
||||
config.setExperimentalFeatureEnabled(feature, true);
|
||||
expect(config.isExperimentalFeatureEnabled(feature)).toBe(true);
|
||||
|
||||
config.setExperimentalFeatureEnabled(feature, false);
|
||||
expect(config.isExperimentalFeatureEnabled(feature)).toBe(false);
|
||||
});
|
||||
|
||||
test("isEnabledForNodes", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Should return true for a valid config
|
||||
expect(config.isEnabledForNodes()).toBe(true);
|
||||
});
|
||||
|
||||
test("free", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Should not throw
|
||||
expect(() => config.free()).not.toThrow();
|
||||
|
||||
// After free, double free should throw an error (this is correct behavior)
|
||||
expect(() => config.free()).toThrow("Cannot perform operation on freed Yoga.Config");
|
||||
});
|
||||
|
||||
test("error handling", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Test invalid arguments
|
||||
expect(() => config.setErrata()).toThrow();
|
||||
expect(() => config.setExperimentalFeatureEnabled()).toThrow();
|
||||
expect(() => config.setExperimentalFeatureEnabled(0)).toThrow(); // missing second arg
|
||||
expect(() => config.isExperimentalFeatureEnabled()).toThrow();
|
||||
expect(() => config.setPointScaleFactor()).toThrow();
|
||||
});
|
||||
});
|
||||
119
test/js/bun/yoga/yoga-constants.test.js
Normal file
119
test/js/bun/yoga/yoga-constants.test.js
Normal file
@@ -0,0 +1,119 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
// Test if we can access Yoga via Bun.Yoga
|
||||
const Yoga = Bun.Yoga;
|
||||
|
||||
describe("Yoga Constants", () => {
|
||||
test("should export all alignment constants", () => {
|
||||
expect(Yoga.ALIGN_AUTO).toBeDefined();
|
||||
expect(Yoga.ALIGN_FLEX_START).toBeDefined();
|
||||
expect(Yoga.ALIGN_CENTER).toBeDefined();
|
||||
expect(Yoga.ALIGN_FLEX_END).toBeDefined();
|
||||
expect(Yoga.ALIGN_STRETCH).toBeDefined();
|
||||
expect(Yoga.ALIGN_BASELINE).toBeDefined();
|
||||
expect(Yoga.ALIGN_SPACE_BETWEEN).toBeDefined();
|
||||
expect(Yoga.ALIGN_SPACE_AROUND).toBeDefined();
|
||||
expect(Yoga.ALIGN_SPACE_EVENLY).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all direction constants", () => {
|
||||
expect(Yoga.DIRECTION_INHERIT).toBeDefined();
|
||||
expect(Yoga.DIRECTION_LTR).toBeDefined();
|
||||
expect(Yoga.DIRECTION_RTL).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all display constants", () => {
|
||||
expect(Yoga.DISPLAY_FLEX).toBeDefined();
|
||||
expect(Yoga.DISPLAY_NONE).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all edge constants", () => {
|
||||
expect(Yoga.EDGE_LEFT).toBeDefined();
|
||||
expect(Yoga.EDGE_TOP).toBeDefined();
|
||||
expect(Yoga.EDGE_RIGHT).toBeDefined();
|
||||
expect(Yoga.EDGE_BOTTOM).toBeDefined();
|
||||
expect(Yoga.EDGE_START).toBeDefined();
|
||||
expect(Yoga.EDGE_END).toBeDefined();
|
||||
expect(Yoga.EDGE_HORIZONTAL).toBeDefined();
|
||||
expect(Yoga.EDGE_VERTICAL).toBeDefined();
|
||||
expect(Yoga.EDGE_ALL).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all experimental feature constants", () => {
|
||||
expect(Yoga.EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all flex direction constants", () => {
|
||||
expect(Yoga.FLEX_DIRECTION_COLUMN).toBeDefined();
|
||||
expect(Yoga.FLEX_DIRECTION_COLUMN_REVERSE).toBeDefined();
|
||||
expect(Yoga.FLEX_DIRECTION_ROW).toBeDefined();
|
||||
expect(Yoga.FLEX_DIRECTION_ROW_REVERSE).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all gutter constants", () => {
|
||||
expect(Yoga.GUTTER_COLUMN).toBeDefined();
|
||||
expect(Yoga.GUTTER_ROW).toBeDefined();
|
||||
expect(Yoga.GUTTER_ALL).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all justify constants", () => {
|
||||
expect(Yoga.JUSTIFY_FLEX_START).toBeDefined();
|
||||
expect(Yoga.JUSTIFY_CENTER).toBeDefined();
|
||||
expect(Yoga.JUSTIFY_FLEX_END).toBeDefined();
|
||||
expect(Yoga.JUSTIFY_SPACE_BETWEEN).toBeDefined();
|
||||
expect(Yoga.JUSTIFY_SPACE_AROUND).toBeDefined();
|
||||
expect(Yoga.JUSTIFY_SPACE_EVENLY).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all measure mode constants", () => {
|
||||
expect(Yoga.MEASURE_MODE_UNDEFINED).toBeDefined();
|
||||
expect(Yoga.MEASURE_MODE_EXACTLY).toBeDefined();
|
||||
expect(Yoga.MEASURE_MODE_AT_MOST).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all node type constants", () => {
|
||||
expect(Yoga.NODE_TYPE_DEFAULT).toBeDefined();
|
||||
expect(Yoga.NODE_TYPE_TEXT).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all overflow constants", () => {
|
||||
expect(Yoga.OVERFLOW_VISIBLE).toBeDefined();
|
||||
expect(Yoga.OVERFLOW_HIDDEN).toBeDefined();
|
||||
expect(Yoga.OVERFLOW_SCROLL).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all position type constants", () => {
|
||||
expect(Yoga.POSITION_TYPE_STATIC).toBeDefined();
|
||||
expect(Yoga.POSITION_TYPE_RELATIVE).toBeDefined();
|
||||
expect(Yoga.POSITION_TYPE_ABSOLUTE).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all unit constants", () => {
|
||||
expect(Yoga.UNIT_UNDEFINED).toBeDefined();
|
||||
expect(Yoga.UNIT_POINT).toBeDefined();
|
||||
expect(Yoga.UNIT_PERCENT).toBeDefined();
|
||||
expect(Yoga.UNIT_AUTO).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all wrap constants", () => {
|
||||
expect(Yoga.WRAP_NO_WRAP).toBeDefined();
|
||||
expect(Yoga.WRAP_WRAP).toBeDefined();
|
||||
expect(Yoga.WRAP_WRAP_REVERSE).toBeDefined();
|
||||
});
|
||||
|
||||
test("should export all errata constants", () => {
|
||||
expect(Yoga.ERRATA_NONE).toBeDefined();
|
||||
expect(Yoga.ERRATA_STRETCH_FLEX_BASIS).toBeDefined();
|
||||
expect(Yoga.ERRATA_ABSOLUTE_POSITION_WITHOUT_INSETS_EXCLUDES_PADDING).toBeDefined();
|
||||
expect(Yoga.ERRATA_ABSOLUTE_PERCENT_AGAINST_INNER_SIZE).toBeDefined();
|
||||
expect(Yoga.ERRATA_ALL).toBeDefined();
|
||||
expect(Yoga.ERRATA_CLASSIC).toBeDefined();
|
||||
});
|
||||
|
||||
test("constants should have correct numeric values", () => {
|
||||
// Check a few key constants have reasonable values
|
||||
expect(typeof Yoga.EDGE_TOP).toBe("number");
|
||||
expect(typeof Yoga.UNIT_PERCENT).toBe("number");
|
||||
expect(typeof Yoga.FLEX_DIRECTION_ROW).toBe("number");
|
||||
});
|
||||
});
|
||||
304
test/js/bun/yoga/yoga-layout-comprehensive.test.js
Normal file
304
test/js/bun/yoga/yoga-layout-comprehensive.test.js
Normal file
@@ -0,0 +1,304 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
const Yoga = Bun.Yoga;
|
||||
|
||||
describe("Yoga - Comprehensive Layout Tests", () => {
|
||||
test("basic flexbox row layout with flex grow", () => {
|
||||
const container = new Yoga.Node();
|
||||
container.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
container.setWidth(300);
|
||||
container.setHeight(100);
|
||||
|
||||
const child1 = new Yoga.Node();
|
||||
child1.setFlex(1);
|
||||
|
||||
const child2 = new Yoga.Node();
|
||||
child2.setFlex(2);
|
||||
|
||||
const child3 = new Yoga.Node();
|
||||
child3.setWidth(50); // Fixed width
|
||||
|
||||
container.insertChild(child1, 0);
|
||||
container.insertChild(child2, 1);
|
||||
container.insertChild(child3, 2);
|
||||
|
||||
container.calculateLayout();
|
||||
|
||||
// Verify container layout
|
||||
const containerLayout = container.getComputedLayout();
|
||||
expect(containerLayout.width).toBe(300);
|
||||
expect(containerLayout.height).toBe(100);
|
||||
|
||||
// Verify children layout
|
||||
// Available space: 300 - 50 (fixed width) = 250
|
||||
// child1 gets 1/3 of 250 = ~83.33
|
||||
// child2 gets 2/3 of 250 = ~166.67
|
||||
// child3 gets fixed 50
|
||||
const child1Layout = child1.getComputedLayout();
|
||||
const child2Layout = child2.getComputedLayout();
|
||||
const child3Layout = child3.getComputedLayout();
|
||||
|
||||
expect(child1Layout.left).toBe(0);
|
||||
expect(child1Layout.width).toBe(83);
|
||||
expect(child1Layout.height).toBe(100);
|
||||
|
||||
expect(child2Layout.left).toBe(83);
|
||||
expect(child2Layout.width).toBe(167);
|
||||
expect(child2Layout.height).toBe(100);
|
||||
|
||||
expect(child3Layout.left).toBe(250);
|
||||
expect(child3Layout.width).toBe(50);
|
||||
expect(child3Layout.height).toBe(100);
|
||||
});
|
||||
|
||||
test("column layout with justify content and align items", () => {
|
||||
const container = new Yoga.Node();
|
||||
container.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN);
|
||||
container.setJustifyContent(Yoga.JUSTIFY_SPACE_BETWEEN);
|
||||
container.setAlignItems(Yoga.ALIGN_CENTER);
|
||||
container.setWidth(200);
|
||||
container.setHeight(300);
|
||||
|
||||
const child1 = new Yoga.Node();
|
||||
child1.setWidth(50);
|
||||
child1.setHeight(50);
|
||||
|
||||
const child2 = new Yoga.Node();
|
||||
child2.setWidth(80);
|
||||
child2.setHeight(60);
|
||||
|
||||
const child3 = new Yoga.Node();
|
||||
child3.setWidth(30);
|
||||
child3.setHeight(40);
|
||||
|
||||
container.insertChild(child1, 0);
|
||||
container.insertChild(child2, 1);
|
||||
container.insertChild(child3, 2);
|
||||
|
||||
container.calculateLayout();
|
||||
|
||||
const child1Layout = child1.getComputedLayout();
|
||||
const child2Layout = child2.getComputedLayout();
|
||||
const child3Layout = child3.getComputedLayout();
|
||||
|
||||
// Verify vertical spacing (JUSTIFY_SPACE_BETWEEN)
|
||||
// Total child height: 50 + 60 + 40 = 150
|
||||
// Available space: 300 - 150 = 150
|
||||
// Space between: 150 / 2 = 75
|
||||
expect(child1Layout.top).toBe(0);
|
||||
expect(child2Layout.top).toBe(125); // 50 + 75
|
||||
expect(child3Layout.top).toBe(260); // 50 + 75 + 60 + 75
|
||||
|
||||
// Verify horizontal centering (ALIGN_CENTER)
|
||||
expect(child1Layout.left).toBe(75); // (200 - 50) / 2
|
||||
expect(child2Layout.left).toBe(60); // (200 - 80) / 2
|
||||
expect(child3Layout.left).toBe(85); // (200 - 30) / 2
|
||||
});
|
||||
|
||||
test("nested flexbox layout", () => {
|
||||
const outerContainer = new Yoga.Node();
|
||||
outerContainer.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
outerContainer.setWidth(400);
|
||||
outerContainer.setHeight(200);
|
||||
|
||||
const leftPanel = new Yoga.Node();
|
||||
leftPanel.setWidth(100);
|
||||
|
||||
const rightPanel = new Yoga.Node();
|
||||
rightPanel.setFlex(1);
|
||||
rightPanel.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN);
|
||||
|
||||
const topSection = new Yoga.Node();
|
||||
topSection.setFlex(1);
|
||||
|
||||
const bottomSection = new Yoga.Node();
|
||||
bottomSection.setHeight(50);
|
||||
|
||||
outerContainer.insertChild(leftPanel, 0);
|
||||
outerContainer.insertChild(rightPanel, 1);
|
||||
rightPanel.insertChild(topSection, 0);
|
||||
rightPanel.insertChild(bottomSection, 1);
|
||||
|
||||
outerContainer.calculateLayout();
|
||||
|
||||
const leftLayout = leftPanel.getComputedLayout();
|
||||
const rightLayout = rightPanel.getComputedLayout();
|
||||
const topLayout = topSection.getComputedLayout();
|
||||
const bottomLayout = bottomSection.getComputedLayout();
|
||||
|
||||
// Left panel
|
||||
expect(leftLayout.left).toBe(0);
|
||||
expect(leftLayout.width).toBe(100);
|
||||
expect(leftLayout.height).toBe(200);
|
||||
|
||||
// Right panel
|
||||
expect(rightLayout.left).toBe(100);
|
||||
expect(rightLayout.width).toBe(300); // 400 - 100
|
||||
expect(rightLayout.height).toBe(200);
|
||||
|
||||
// Top section of right panel
|
||||
expect(topLayout.left).toBe(0); // Relative to right panel
|
||||
expect(topLayout.top).toBe(0);
|
||||
expect(topLayout.width).toBe(300);
|
||||
expect(topLayout.height).toBe(150); // 200 - 50
|
||||
|
||||
// Bottom section of right panel
|
||||
expect(bottomLayout.left).toBe(0);
|
||||
expect(bottomLayout.top).toBe(150);
|
||||
expect(bottomLayout.width).toBe(300);
|
||||
expect(bottomLayout.height).toBe(50);
|
||||
});
|
||||
|
||||
test("flex wrap with multiple lines", () => {
|
||||
const container = new Yoga.Node();
|
||||
container.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
container.setFlexWrap(Yoga.WRAP_WRAP);
|
||||
container.setWidth(200);
|
||||
container.setHeight(200);
|
||||
|
||||
// Create children that will overflow and wrap
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const child = new Yoga.Node();
|
||||
child.setWidth(80);
|
||||
child.setHeight(50);
|
||||
container.insertChild(child, i);
|
||||
}
|
||||
|
||||
container.calculateLayout();
|
||||
|
||||
// First line: child 0, 1 (80 + 80 = 160, fits in 200)
|
||||
// Second line: child 2, 3 (80 + 80 = 160, fits in 200)
|
||||
// Third line: child 4 (80, fits in 200)
|
||||
|
||||
const child0Layout = container.getChild(0).getComputedLayout();
|
||||
const child1Layout = container.getChild(1).getComputedLayout();
|
||||
const child2Layout = container.getChild(2).getComputedLayout();
|
||||
const child3Layout = container.getChild(3).getComputedLayout();
|
||||
const child4Layout = container.getChild(4).getComputedLayout();
|
||||
|
||||
// First line
|
||||
expect(child0Layout.top).toBe(0);
|
||||
expect(child0Layout.left).toBe(0);
|
||||
expect(child1Layout.top).toBe(0);
|
||||
expect(child1Layout.left).toBe(80);
|
||||
|
||||
// Second line
|
||||
expect(child2Layout.top).toBe(50);
|
||||
expect(child2Layout.left).toBe(0);
|
||||
expect(child3Layout.top).toBe(50);
|
||||
expect(child3Layout.left).toBe(80);
|
||||
|
||||
// Third line
|
||||
expect(child4Layout.top).toBe(100);
|
||||
expect(child4Layout.left).toBe(0);
|
||||
});
|
||||
|
||||
test("margin and padding calculations", () => {
|
||||
const container = new Yoga.Node();
|
||||
container.setPadding(Yoga.EDGE_ALL, 10);
|
||||
container.setWidth(200);
|
||||
container.setHeight(150);
|
||||
|
||||
const child = new Yoga.Node();
|
||||
child.setMargin(Yoga.EDGE_ALL, 15);
|
||||
child.setFlex(1);
|
||||
|
||||
container.insertChild(child, 0);
|
||||
container.calculateLayout();
|
||||
|
||||
const containerLayout = container.getComputedLayout();
|
||||
const childLayout = child.getComputedLayout();
|
||||
|
||||
// Container should maintain its size
|
||||
expect(containerLayout.width).toBe(200);
|
||||
expect(containerLayout.height).toBe(150);
|
||||
|
||||
// Child should account for container padding and its own margin
|
||||
// Available width: 200 - (10+10 padding) - (15+15 margin) = 150
|
||||
// Available height: 150 - (10+10 padding) - (15+15 margin) = 100
|
||||
expect(childLayout.left).toBe(25); // container padding + child margin
|
||||
expect(childLayout.top).toBe(25);
|
||||
expect(childLayout.width).toBe(150);
|
||||
expect(childLayout.height).toBe(100);
|
||||
});
|
||||
|
||||
test("percentage-based dimensions", () => {
|
||||
const container = new Yoga.Node();
|
||||
container.setWidth(400);
|
||||
container.setHeight(300);
|
||||
|
||||
const child = new Yoga.Node();
|
||||
child.setWidth("50%"); // 50% of 400 = 200
|
||||
child.setHeight("75%"); // 75% of 300 = 225
|
||||
|
||||
container.insertChild(child, 0);
|
||||
container.calculateLayout();
|
||||
|
||||
const childLayout = child.getComputedLayout();
|
||||
expect(childLayout.width).toBe(200);
|
||||
expect(childLayout.height).toBe(225);
|
||||
});
|
||||
|
||||
test("min/max constraints", () => {
|
||||
const container = new Yoga.Node();
|
||||
container.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
container.setWidth(500);
|
||||
container.setHeight(100);
|
||||
|
||||
const child1 = new Yoga.Node();
|
||||
child1.setFlex(1);
|
||||
child1.setMinWidth(100);
|
||||
child1.setMaxWidth(200);
|
||||
|
||||
const child2 = new Yoga.Node();
|
||||
child2.setFlex(2);
|
||||
|
||||
container.insertChild(child1, 0);
|
||||
container.insertChild(child2, 1);
|
||||
container.calculateLayout();
|
||||
|
||||
const child1Layout = child1.getComputedLayout();
|
||||
const child2Layout = child2.getComputedLayout();
|
||||
|
||||
// child1 would normally get 1/3 of 500 = ~166.67
|
||||
// But it's clamped by maxWidth(200), so it gets 200
|
||||
expect(child1Layout.width).toBe(200);
|
||||
|
||||
// child2 gets the remaining space: 500 - 200 = 300
|
||||
expect(child2Layout.width).toBe(300);
|
||||
});
|
||||
|
||||
test("absolute positioning", () => {
|
||||
const container = new Yoga.Node();
|
||||
container.setWidth(300);
|
||||
container.setHeight(200);
|
||||
|
||||
const normalChild = new Yoga.Node();
|
||||
normalChild.setWidth(100);
|
||||
normalChild.setHeight(50);
|
||||
|
||||
const absoluteChild = new Yoga.Node();
|
||||
absoluteChild.setPositionType(Yoga.POSITION_TYPE_ABSOLUTE);
|
||||
absoluteChild.setPosition(Yoga.EDGE_TOP, 20);
|
||||
absoluteChild.setPosition(Yoga.EDGE_LEFT, 50);
|
||||
absoluteChild.setWidth(80);
|
||||
absoluteChild.setHeight(60);
|
||||
|
||||
container.insertChild(normalChild, 0);
|
||||
container.insertChild(absoluteChild, 1);
|
||||
container.calculateLayout();
|
||||
|
||||
const normalLayout = normalChild.getComputedLayout();
|
||||
const absoluteLayout = absoluteChild.getComputedLayout();
|
||||
|
||||
// Normal child positioned normally
|
||||
expect(normalLayout.left).toBe(0);
|
||||
expect(normalLayout.top).toBe(0);
|
||||
|
||||
// Absolute child positioned absolutely
|
||||
expect(absoluteLayout.left).toBe(50);
|
||||
expect(absoluteLayout.top).toBe(20);
|
||||
expect(absoluteLayout.width).toBe(80);
|
||||
expect(absoluteLayout.height).toBe(60);
|
||||
});
|
||||
});
|
||||
792
test/js/bun/yoga/yoga-node-extended.test.js
Normal file
792
test/js/bun/yoga/yoga-node-extended.test.js
Normal file
@@ -0,0 +1,792 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
const Yoga = Bun.Yoga;
|
||||
|
||||
describe("Yoga.Node - Extended Tests", () => {
|
||||
describe("Node creation and cloning", () => {
|
||||
test("clone() creates independent copy", () => {
|
||||
const original = new Yoga.Node();
|
||||
original.setWidth(100);
|
||||
original.setHeight(200);
|
||||
original.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
|
||||
const cloned = original.clone();
|
||||
expect(cloned).toBeDefined();
|
||||
expect(cloned).not.toBe(original);
|
||||
|
||||
// Verify cloned has same properties
|
||||
const originalWidth = original.getWidth();
|
||||
const clonedWidth = cloned.getWidth();
|
||||
expect(clonedWidth.value).toBe(originalWidth.value);
|
||||
expect(clonedWidth.unit).toBe(originalWidth.unit);
|
||||
|
||||
// Verify they're independent
|
||||
original.setWidth(300);
|
||||
expect(cloned.getWidth().value).toBe(100);
|
||||
});
|
||||
|
||||
test("clone() preserves measure function", () => {
|
||||
const original = new Yoga.Node();
|
||||
let originalMeasureCalled = false;
|
||||
let clonedMeasureCalled = false;
|
||||
|
||||
original.setMeasureFunc((width, height) => {
|
||||
originalMeasureCalled = true;
|
||||
return { width: 100, height: 50 };
|
||||
});
|
||||
|
||||
const cloned = original.clone();
|
||||
|
||||
// Both should have measure functions
|
||||
original.markDirty();
|
||||
original.calculateLayout();
|
||||
expect(originalMeasureCalled).toBe(true);
|
||||
|
||||
// Note: cloned nodes share the same measure function reference
|
||||
cloned.markDirty();
|
||||
cloned.calculateLayout();
|
||||
// The original measure function is called again
|
||||
expect(originalMeasureCalled).toBe(true);
|
||||
});
|
||||
|
||||
test("clone() with hierarchy", () => {
|
||||
const parent = new Yoga.Node();
|
||||
const child1 = new Yoga.Node();
|
||||
const child2 = new Yoga.Node();
|
||||
|
||||
parent.insertChild(child1, 0);
|
||||
parent.insertChild(child2, 1);
|
||||
|
||||
const clonedParent = parent.clone();
|
||||
expect(clonedParent.getChildCount()).toBe(2);
|
||||
|
||||
const clonedChild1 = clonedParent.getChild(0);
|
||||
const clonedChild2 = clonedParent.getChild(1);
|
||||
|
||||
expect(clonedChild1).toBeDefined();
|
||||
expect(clonedChild2).toBeDefined();
|
||||
expect(clonedChild1).not.toBe(child1);
|
||||
expect(clonedChild2).not.toBe(child2);
|
||||
});
|
||||
|
||||
test("copyStyle() copies style properties", () => {
|
||||
const source = new Yoga.Node();
|
||||
source.setWidth(100);
|
||||
source.setHeight(200);
|
||||
source.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
source.setJustifyContent(Yoga.JUSTIFY_CENTER);
|
||||
source.setAlignItems(Yoga.ALIGN_CENTER);
|
||||
|
||||
const target = new Yoga.Node();
|
||||
target.copyStyle(source);
|
||||
|
||||
expect(target.getWidth()).toEqual(source.getWidth());
|
||||
expect(target.getHeight()).toEqual(source.getHeight());
|
||||
// Note: Can't verify flex direction directly as getter is not accessible
|
||||
});
|
||||
|
||||
test("freeRecursive() frees node and children", () => {
|
||||
const parent = new Yoga.Node();
|
||||
const child1 = new Yoga.Node();
|
||||
const child2 = new Yoga.Node();
|
||||
const grandchild = new Yoga.Node();
|
||||
|
||||
parent.insertChild(child1, 0);
|
||||
parent.insertChild(child2, 1);
|
||||
child1.insertChild(grandchild, 0);
|
||||
|
||||
expect(() => parent.freeRecursive()).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Direction and layout", () => {
|
||||
test("setDirection/getDirection", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setDirection(Yoga.DIRECTION_LTR);
|
||||
expect(node.getDirection()).toBe(Yoga.DIRECTION_LTR);
|
||||
|
||||
node.setDirection(Yoga.DIRECTION_RTL);
|
||||
expect(node.getDirection()).toBe(Yoga.DIRECTION_RTL);
|
||||
|
||||
node.setDirection(Yoga.DIRECTION_INHERIT);
|
||||
expect(node.getDirection()).toBe(Yoga.DIRECTION_INHERIT);
|
||||
});
|
||||
|
||||
test("getComputedLeft/Top/Width/Height", () => {
|
||||
const node = new Yoga.Node();
|
||||
node.setWidth(100);
|
||||
node.setHeight(100);
|
||||
node.calculateLayout();
|
||||
|
||||
expect(node.getComputedLeft()).toBe(0);
|
||||
expect(node.getComputedTop()).toBe(0);
|
||||
expect(node.getComputedWidth()).toBe(100);
|
||||
expect(node.getComputedHeight()).toBe(100);
|
||||
});
|
||||
|
||||
test("getComputedRight/Bottom calculations", () => {
|
||||
const parent = new Yoga.Node();
|
||||
parent.setWidth(200);
|
||||
parent.setHeight(200);
|
||||
|
||||
const child = new Yoga.Node();
|
||||
child.setWidth(100);
|
||||
child.setHeight(100);
|
||||
child.setPositionType(Yoga.POSITION_TYPE_ABSOLUTE);
|
||||
child.setPosition(Yoga.EDGE_LEFT, 10);
|
||||
child.setPosition(Yoga.EDGE_TOP, 20);
|
||||
|
||||
parent.insertChild(child, 0);
|
||||
parent.calculateLayout();
|
||||
|
||||
expect(child.getComputedLeft()).toBe(10);
|
||||
expect(child.getComputedTop()).toBe(20);
|
||||
// Yoga's getComputedRight/Bottom return position offsets, not absolute coordinates
|
||||
// Since we positioned with left/top, right/bottom will be the original position values
|
||||
expect(child.getComputedRight()).toBe(10);
|
||||
expect(child.getComputedBottom()).toBe(20);
|
||||
});
|
||||
|
||||
test("getComputedMargin", () => {
|
||||
const node = new Yoga.Node();
|
||||
node.setMargin(Yoga.EDGE_TOP, 10);
|
||||
node.setMargin(Yoga.EDGE_RIGHT, 20);
|
||||
node.setMargin(Yoga.EDGE_BOTTOM, 30);
|
||||
node.setMargin(Yoga.EDGE_LEFT, 40);
|
||||
node.setWidth(100);
|
||||
node.setHeight(100);
|
||||
|
||||
const parent = new Yoga.Node();
|
||||
parent.setWidth(300);
|
||||
parent.setHeight(300);
|
||||
parent.insertChild(node, 0);
|
||||
parent.calculateLayout();
|
||||
|
||||
expect(node.getComputedMargin(Yoga.EDGE_TOP)).toBe(10);
|
||||
expect(node.getComputedMargin(Yoga.EDGE_RIGHT)).toBe(20);
|
||||
expect(node.getComputedMargin(Yoga.EDGE_BOTTOM)).toBe(30);
|
||||
expect(node.getComputedMargin(Yoga.EDGE_LEFT)).toBe(40);
|
||||
});
|
||||
|
||||
test("getComputedPadding", () => {
|
||||
const node = new Yoga.Node();
|
||||
node.setPadding(Yoga.EDGE_ALL, 15);
|
||||
node.setWidth(100);
|
||||
node.setHeight(100);
|
||||
node.calculateLayout();
|
||||
|
||||
expect(node.getComputedPadding(Yoga.EDGE_TOP)).toBe(15);
|
||||
expect(node.getComputedPadding(Yoga.EDGE_RIGHT)).toBe(15);
|
||||
expect(node.getComputedPadding(Yoga.EDGE_BOTTOM)).toBe(15);
|
||||
expect(node.getComputedPadding(Yoga.EDGE_LEFT)).toBe(15);
|
||||
});
|
||||
|
||||
test("getComputedBorder", () => {
|
||||
const node = new Yoga.Node();
|
||||
node.setBorder(Yoga.EDGE_ALL, 5);
|
||||
node.setWidth(100);
|
||||
node.setHeight(100);
|
||||
node.calculateLayout();
|
||||
|
||||
expect(node.getComputedBorder(Yoga.EDGE_TOP)).toBe(5);
|
||||
expect(node.getComputedBorder(Yoga.EDGE_RIGHT)).toBe(5);
|
||||
expect(node.getComputedBorder(Yoga.EDGE_BOTTOM)).toBe(5);
|
||||
expect(node.getComputedBorder(Yoga.EDGE_LEFT)).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Flexbox properties", () => {
|
||||
test("setAlignContent/getAlignContent", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setAlignContent(Yoga.ALIGN_FLEX_START);
|
||||
expect(node.getAlignContent()).toBe(Yoga.ALIGN_FLEX_START);
|
||||
|
||||
node.setAlignContent(Yoga.ALIGN_CENTER);
|
||||
expect(node.getAlignContent()).toBe(Yoga.ALIGN_CENTER);
|
||||
|
||||
node.setAlignContent(Yoga.ALIGN_STRETCH);
|
||||
expect(node.getAlignContent()).toBe(Yoga.ALIGN_STRETCH);
|
||||
});
|
||||
|
||||
test("setAlignSelf/getAlignSelf", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setAlignSelf(Yoga.ALIGN_AUTO);
|
||||
expect(node.getAlignSelf()).toBe(Yoga.ALIGN_AUTO);
|
||||
|
||||
node.setAlignSelf(Yoga.ALIGN_FLEX_END);
|
||||
expect(node.getAlignSelf()).toBe(Yoga.ALIGN_FLEX_END);
|
||||
});
|
||||
|
||||
test("setAlignItems/getAlignItems", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setAlignItems(Yoga.ALIGN_FLEX_START);
|
||||
expect(node.getAlignItems()).toBe(Yoga.ALIGN_FLEX_START);
|
||||
|
||||
node.setAlignItems(Yoga.ALIGN_BASELINE);
|
||||
expect(node.getAlignItems()).toBe(Yoga.ALIGN_BASELINE);
|
||||
});
|
||||
|
||||
test("getFlex", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setFlex(2.5);
|
||||
expect(node.getFlex()).toBe(2.5);
|
||||
|
||||
node.setFlex(0);
|
||||
expect(node.getFlex()).toBe(0);
|
||||
});
|
||||
|
||||
test("setFlexWrap/getFlexWrap", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setFlexWrap(Yoga.WRAP_NO_WRAP);
|
||||
expect(node.getFlexWrap()).toBe(Yoga.WRAP_NO_WRAP);
|
||||
|
||||
node.setFlexWrap(Yoga.WRAP_WRAP);
|
||||
expect(node.getFlexWrap()).toBe(Yoga.WRAP_WRAP);
|
||||
|
||||
node.setFlexWrap(Yoga.WRAP_WRAP_REVERSE);
|
||||
expect(node.getFlexWrap()).toBe(Yoga.WRAP_WRAP_REVERSE);
|
||||
});
|
||||
|
||||
test("getFlexDirection", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
expect(node.getFlexDirection()).toBe(Yoga.FLEX_DIRECTION_ROW);
|
||||
|
||||
node.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN_REVERSE);
|
||||
expect(node.getFlexDirection()).toBe(Yoga.FLEX_DIRECTION_COLUMN_REVERSE);
|
||||
});
|
||||
|
||||
test("getFlexGrow/getFlexShrink", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setFlexGrow(2);
|
||||
expect(node.getFlexGrow()).toBe(2);
|
||||
|
||||
node.setFlexShrink(0.5);
|
||||
expect(node.getFlexShrink()).toBe(0.5);
|
||||
});
|
||||
|
||||
test("getJustifyContent", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setJustifyContent(Yoga.JUSTIFY_SPACE_BETWEEN);
|
||||
expect(node.getJustifyContent()).toBe(Yoga.JUSTIFY_SPACE_BETWEEN);
|
||||
|
||||
node.setJustifyContent(Yoga.JUSTIFY_SPACE_AROUND);
|
||||
expect(node.getJustifyContent()).toBe(Yoga.JUSTIFY_SPACE_AROUND);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Position properties", () => {
|
||||
test("setPosition/getPosition", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setPosition(Yoga.EDGE_LEFT, 10);
|
||||
expect(node.getPosition(Yoga.EDGE_LEFT)).toEqual({ unit: Yoga.UNIT_POINT, value: 10 });
|
||||
|
||||
node.setPosition(Yoga.EDGE_TOP, "20%");
|
||||
expect(node.getPosition(Yoga.EDGE_TOP)).toEqual({ unit: Yoga.UNIT_PERCENT, value: 20 });
|
||||
|
||||
node.setPosition(Yoga.EDGE_RIGHT, { unit: Yoga.UNIT_POINT, value: 30 });
|
||||
expect(node.getPosition(Yoga.EDGE_RIGHT)).toEqual({ unit: Yoga.UNIT_POINT, value: 30 });
|
||||
});
|
||||
|
||||
test("setPositionType/getPositionType", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setPositionType(Yoga.POSITION_TYPE_ABSOLUTE);
|
||||
expect(node.getPositionType()).toBe(Yoga.POSITION_TYPE_ABSOLUTE);
|
||||
|
||||
node.setPositionType(Yoga.POSITION_TYPE_RELATIVE);
|
||||
expect(node.getPositionType()).toBe(Yoga.POSITION_TYPE_RELATIVE);
|
||||
|
||||
node.setPositionType(Yoga.POSITION_TYPE_STATIC);
|
||||
expect(node.getPositionType()).toBe(Yoga.POSITION_TYPE_STATIC);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Size properties", () => {
|
||||
test("height/width with percentage", () => {
|
||||
const parent = new Yoga.Node();
|
||||
parent.setWidth(200);
|
||||
parent.setHeight(200);
|
||||
|
||||
const child = new Yoga.Node();
|
||||
child.setWidth("50%");
|
||||
child.setHeight("75%");
|
||||
|
||||
parent.insertChild(child, 0);
|
||||
parent.calculateLayout();
|
||||
|
||||
expect(child.getComputedWidth()).toBe(100); // 50% of 200
|
||||
expect(child.getComputedHeight()).toBe(150); // 75% of 200
|
||||
});
|
||||
|
||||
test("getAspectRatio", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setAspectRatio(1.5);
|
||||
expect(node.getAspectRatio()).toBe(1.5);
|
||||
|
||||
node.setAspectRatio(undefined);
|
||||
expect(node.getAspectRatio()).toBeNaN();
|
||||
});
|
||||
|
||||
test("size constraints affect layout", () => {
|
||||
const node = new Yoga.Node();
|
||||
node.setMinWidth(50);
|
||||
node.setMinHeight(50);
|
||||
node.setMaxWidth(100);
|
||||
node.setMaxHeight(100);
|
||||
|
||||
// Width/height beyond constraints
|
||||
node.setWidth(200);
|
||||
node.setHeight(200);
|
||||
|
||||
node.calculateLayout();
|
||||
|
||||
// Constraints are now working correctly - values should be clamped to max
|
||||
expect(node.getComputedWidth()).toBe(100);
|
||||
expect(node.getComputedHeight()).toBe(100);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Spacing properties", () => {
|
||||
test("setPadding/getPadding", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// Set padding on individual edges
|
||||
node.setPadding(Yoga.EDGE_TOP, 10);
|
||||
node.setPadding(Yoga.EDGE_RIGHT, 10);
|
||||
node.setPadding(Yoga.EDGE_BOTTOM, 10);
|
||||
node.setPadding(Yoga.EDGE_LEFT, 10);
|
||||
|
||||
expect(node.getPadding(Yoga.EDGE_TOP)).toEqual({ unit: Yoga.UNIT_POINT, value: 10 });
|
||||
expect(node.getPadding(Yoga.EDGE_RIGHT)).toEqual({ unit: Yoga.UNIT_POINT, value: 10 });
|
||||
|
||||
// Set different values
|
||||
node.setPadding(Yoga.EDGE_LEFT, 20);
|
||||
node.setPadding(Yoga.EDGE_RIGHT, 20);
|
||||
expect(node.getPadding(Yoga.EDGE_LEFT)).toEqual({ unit: Yoga.UNIT_POINT, value: 20 });
|
||||
expect(node.getPadding(Yoga.EDGE_RIGHT)).toEqual({ unit: Yoga.UNIT_POINT, value: 20 });
|
||||
|
||||
node.setPadding(Yoga.EDGE_TOP, "15%");
|
||||
expect(node.getPadding(Yoga.EDGE_TOP)).toEqual({ unit: Yoga.UNIT_PERCENT, value: 15 });
|
||||
});
|
||||
|
||||
test("setBorder/getBorder", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// Set border on individual edges
|
||||
node.setBorder(Yoga.EDGE_TOP, 5);
|
||||
node.setBorder(Yoga.EDGE_RIGHT, 5);
|
||||
node.setBorder(Yoga.EDGE_BOTTOM, 5);
|
||||
node.setBorder(Yoga.EDGE_LEFT, 5);
|
||||
|
||||
expect(node.getBorder(Yoga.EDGE_TOP)).toBe(5);
|
||||
expect(node.getBorder(Yoga.EDGE_RIGHT)).toBe(5);
|
||||
|
||||
node.setBorder(Yoga.EDGE_TOP, 10);
|
||||
expect(node.getBorder(Yoga.EDGE_TOP)).toBe(10);
|
||||
expect(node.getBorder(Yoga.EDGE_RIGHT)).toBe(5); // Should still be 5
|
||||
});
|
||||
|
||||
test("getGap with different gutters", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setGap(Yoga.GUTTER_ROW, 10);
|
||||
expect(node.getGap(Yoga.GUTTER_ROW)).toEqual({ value: 10, unit: Yoga.UNIT_POINT });
|
||||
|
||||
node.setGap(Yoga.GUTTER_COLUMN, 20);
|
||||
expect(node.getGap(Yoga.GUTTER_COLUMN)).toEqual({ value: 20, unit: Yoga.UNIT_POINT });
|
||||
|
||||
// Verify row and column gaps are independent
|
||||
expect(node.getGap(Yoga.GUTTER_ROW)).toEqual({ value: 10, unit: Yoga.UNIT_POINT });
|
||||
});
|
||||
});
|
||||
|
||||
describe("Node type and display", () => {
|
||||
test("setNodeType/getNodeType", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
expect(node.getNodeType()).toBe(Yoga.NODE_TYPE_DEFAULT);
|
||||
|
||||
node.setNodeType(Yoga.NODE_TYPE_TEXT);
|
||||
expect(node.getNodeType()).toBe(Yoga.NODE_TYPE_TEXT);
|
||||
|
||||
node.setNodeType(Yoga.NODE_TYPE_DEFAULT);
|
||||
expect(node.getNodeType()).toBe(Yoga.NODE_TYPE_DEFAULT);
|
||||
});
|
||||
|
||||
test("setDisplay/getDisplay", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setDisplay(Yoga.DISPLAY_FLEX);
|
||||
expect(node.getDisplay()).toBe(Yoga.DISPLAY_FLEX);
|
||||
|
||||
node.setDisplay(Yoga.DISPLAY_NONE);
|
||||
expect(node.getDisplay()).toBe(Yoga.DISPLAY_NONE);
|
||||
});
|
||||
|
||||
test("setOverflow/getOverflow", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setOverflow(Yoga.OVERFLOW_HIDDEN);
|
||||
expect(node.getOverflow()).toBe(Yoga.OVERFLOW_HIDDEN);
|
||||
|
||||
node.setOverflow(Yoga.OVERFLOW_SCROLL);
|
||||
expect(node.getOverflow()).toBe(Yoga.OVERFLOW_SCROLL);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Box sizing", () => {
|
||||
test("setBoxSizing/getBoxSizing", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// Default is border-box
|
||||
expect(node.getBoxSizing()).toBe(Yoga.BOX_SIZING_BORDER_BOX);
|
||||
|
||||
node.setBoxSizing(Yoga.BOX_SIZING_CONTENT_BOX);
|
||||
expect(node.getBoxSizing()).toBe(Yoga.BOX_SIZING_CONTENT_BOX);
|
||||
|
||||
node.setBoxSizing(Yoga.BOX_SIZING_BORDER_BOX);
|
||||
expect(node.getBoxSizing()).toBe(Yoga.BOX_SIZING_BORDER_BOX);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Layout state", () => {
|
||||
test("setHasNewLayout/getHasNewLayout", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.calculateLayout();
|
||||
expect(node.getHasNewLayout()).toBe(true);
|
||||
|
||||
node.setHasNewLayout(false);
|
||||
expect(node.getHasNewLayout()).toBe(false);
|
||||
|
||||
node.setHasNewLayout(true);
|
||||
expect(node.getHasNewLayout()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Baseline", () => {
|
||||
test("setIsReferenceBaseline/isReferenceBaseline", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
expect(node.isReferenceBaseline()).toBe(false);
|
||||
|
||||
node.setIsReferenceBaseline(true);
|
||||
expect(node.isReferenceBaseline()).toBe(true);
|
||||
|
||||
node.setIsReferenceBaseline(false);
|
||||
expect(node.isReferenceBaseline()).toBe(false);
|
||||
});
|
||||
|
||||
test("setBaselineFunc", () => {
|
||||
const node = new Yoga.Node();
|
||||
let baselineCalled = false;
|
||||
|
||||
node.setBaselineFunc((width, height) => {
|
||||
baselineCalled = true;
|
||||
return height * 0.8;
|
||||
});
|
||||
|
||||
// Set up a scenario where baseline function is called
|
||||
const container = new Yoga.Node();
|
||||
container.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
container.setAlignItems(Yoga.ALIGN_BASELINE);
|
||||
container.setWidth(300);
|
||||
container.setHeight(100);
|
||||
|
||||
node.setWidth(100);
|
||||
node.setHeight(50);
|
||||
container.insertChild(node, 0);
|
||||
|
||||
// Add another child to trigger baseline alignment
|
||||
const sibling = new Yoga.Node();
|
||||
sibling.setWidth(100);
|
||||
sibling.setHeight(60);
|
||||
container.insertChild(sibling, 1);
|
||||
|
||||
container.calculateLayout();
|
||||
|
||||
// Clear the baseline function
|
||||
node.setBaselineFunc(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Hierarchy operations", () => {
|
||||
test("removeAllChildren", () => {
|
||||
const parent = new Yoga.Node();
|
||||
const child1 = new Yoga.Node();
|
||||
const child2 = new Yoga.Node();
|
||||
const child3 = new Yoga.Node();
|
||||
|
||||
parent.insertChild(child1, 0);
|
||||
parent.insertChild(child2, 1);
|
||||
parent.insertChild(child3, 2);
|
||||
|
||||
expect(parent.getChildCount()).toBe(3);
|
||||
|
||||
parent.removeAllChildren();
|
||||
|
||||
expect(parent.getChildCount()).toBe(0);
|
||||
expect(child1.getParent()).toBeNull();
|
||||
expect(child2.getParent()).toBeNull();
|
||||
expect(child3.getParent()).toBeNull();
|
||||
});
|
||||
|
||||
test("getOwner", () => {
|
||||
const parent = new Yoga.Node();
|
||||
const child = new Yoga.Node();
|
||||
|
||||
parent.insertChild(child, 0);
|
||||
|
||||
// getOwner returns the parent node that owns this node
|
||||
expect(child.getOwner()).toBe(parent);
|
||||
|
||||
const clonedParent = parent.clone();
|
||||
const clonedChild = clonedParent.getChild(0);
|
||||
|
||||
// After cloning, the cloned children maintain their original owner relationships
|
||||
// This is expected behavior in Yoga - cloned nodes keep references to original parents
|
||||
expect(clonedChild.getOwner()).toBe(parent);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Config association", () => {
|
||||
test("getConfig returns associated config", () => {
|
||||
const config = new Yoga.Config();
|
||||
const node = new Yoga.Node(config);
|
||||
|
||||
expect(node.getConfig()).toBe(config);
|
||||
});
|
||||
|
||||
test("getConfig returns null for nodes without config", () => {
|
||||
const node = new Yoga.Node();
|
||||
expect(node.getConfig()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Edge cases and error handling", () => {
|
||||
test("getChild with invalid index", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
expect(node.getChild(-1)).toBeNull();
|
||||
expect(node.getChild(0)).toBeNull();
|
||||
expect(node.getChild(10)).toBeNull();
|
||||
});
|
||||
|
||||
test("getParent for root node", () => {
|
||||
const node = new Yoga.Node();
|
||||
expect(node.getParent()).toBeNull();
|
||||
});
|
||||
|
||||
// TODO: This test currently causes a segmentation fault
|
||||
// Operations on freed nodes should be safe but currently crash
|
||||
// test("operations on freed node", () => {
|
||||
// const node = new Yoga.Node();
|
||||
// node.free();
|
||||
//
|
||||
// // Operations on freed nodes should not crash
|
||||
// expect(() => node.setWidth(100)).not.toThrow();
|
||||
// expect(() => node.getWidth()).not.toThrow();
|
||||
// });
|
||||
|
||||
test("markDirty edge cases", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// markDirty without measure function should throw
|
||||
expect(() => node.markDirty()).toThrow("Only nodes with custom measure functions can be marked as dirty");
|
||||
|
||||
// With measure function it should work
|
||||
node.setMeasureFunc(() => ({ width: 100, height: 50 }));
|
||||
expect(() => node.markDirty()).not.toThrow();
|
||||
});
|
||||
|
||||
test("calculateLayout with various dimensions", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
expect(() => node.calculateLayout()).not.toThrow();
|
||||
expect(() => node.calculateLayout(undefined, undefined)).not.toThrow();
|
||||
expect(() => node.calculateLayout(Yoga.UNDEFINED, Yoga.UNDEFINED)).not.toThrow();
|
||||
expect(() => node.calculateLayout(100, 100, Yoga.DIRECTION_LTR)).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Yoga.Config - Extended Tests", () => {
|
||||
test("Config constructor and create", () => {
|
||||
const config1 = new Yoga.Config();
|
||||
expect(config1).toBeDefined();
|
||||
expect(config1.constructor.name).toBe("Config");
|
||||
|
||||
const config2 = Yoga.Config.create();
|
||||
expect(config2).toBeDefined();
|
||||
expect(config2.constructor.name).toBe("Config");
|
||||
});
|
||||
|
||||
test("setUseWebDefaults/getUseWebDefaults", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
expect(config.getUseWebDefaults()).toBe(false);
|
||||
|
||||
config.setUseWebDefaults(true);
|
||||
expect(config.getUseWebDefaults()).toBe(true);
|
||||
|
||||
config.setUseWebDefaults(false);
|
||||
expect(config.getUseWebDefaults()).toBe(false);
|
||||
});
|
||||
|
||||
test("setPointScaleFactor/getPointScaleFactor", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Default is usually 1.0
|
||||
const defaultScale = config.getPointScaleFactor();
|
||||
expect(defaultScale).toBeGreaterThan(0);
|
||||
|
||||
config.setPointScaleFactor(2.0);
|
||||
expect(config.getPointScaleFactor()).toBe(2.0);
|
||||
|
||||
config.setPointScaleFactor(0.0);
|
||||
expect(config.getPointScaleFactor()).toBe(0.0);
|
||||
});
|
||||
|
||||
test("setContext/getContext", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
expect(config.getContext()).toBeNull();
|
||||
|
||||
const context = { foo: "bar", num: 42, arr: [1, 2, 3] };
|
||||
config.setContext(context);
|
||||
expect(config.getContext()).toBe(context);
|
||||
|
||||
config.setContext(null);
|
||||
expect(config.getContext()).toBeNull();
|
||||
});
|
||||
|
||||
test("setLogger callback", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Set logger
|
||||
config.setLogger((config, node, level, format) => {
|
||||
console.log("Logger called");
|
||||
return 0;
|
||||
});
|
||||
|
||||
// Clear logger
|
||||
config.setLogger(null);
|
||||
|
||||
// Setting invalid logger
|
||||
expect(() => config.setLogger("not a function")).toThrow();
|
||||
});
|
||||
|
||||
test("setCloneNodeFunc callback", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Set clone function
|
||||
config.setCloneNodeFunc((oldNode, owner, childIndex) => {
|
||||
return oldNode.clone();
|
||||
});
|
||||
|
||||
// Clear clone function
|
||||
config.setCloneNodeFunc(null);
|
||||
|
||||
// Setting invalid clone function
|
||||
expect(() => config.setCloneNodeFunc("not a function")).toThrow();
|
||||
});
|
||||
|
||||
// TODO: This test currently causes a segmentation fault
|
||||
// Operations on freed configs should be safe but currently crash
|
||||
// test("free config", () => {
|
||||
// const config = new Yoga.Config();
|
||||
// expect(() => config.free()).not.toThrow();
|
||||
//
|
||||
// // Operations after free should not crash
|
||||
// expect(() => config.setPointScaleFactor(2.0)).not.toThrow();
|
||||
// });
|
||||
|
||||
test("setErrata/getErrata", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
expect(config.getErrata()).toBe(Yoga.ERRATA_NONE);
|
||||
|
||||
config.setErrata(Yoga.ERRATA_CLASSIC);
|
||||
expect(config.getErrata()).toBe(Yoga.ERRATA_CLASSIC);
|
||||
|
||||
config.setErrata(Yoga.ERRATA_ALL);
|
||||
expect(config.getErrata()).toBe(Yoga.ERRATA_ALL);
|
||||
|
||||
config.setErrata(Yoga.ERRATA_NONE);
|
||||
expect(config.getErrata()).toBe(Yoga.ERRATA_NONE);
|
||||
});
|
||||
|
||||
test("experimental features", () => {
|
||||
const config = new Yoga.Config();
|
||||
|
||||
// Check if experimental feature methods exist
|
||||
expect(typeof config.setExperimentalFeatureEnabled).toBe("function");
|
||||
expect(typeof config.isExperimentalFeatureEnabled).toBe("function");
|
||||
|
||||
// Try enabling/disabling a feature (0 as example)
|
||||
expect(() => config.setExperimentalFeatureEnabled(0, true)).not.toThrow();
|
||||
expect(() => config.isExperimentalFeatureEnabled(0)).not.toThrow();
|
||||
});
|
||||
|
||||
test("isEnabledForNodes", () => {
|
||||
const config = new Yoga.Config();
|
||||
expect(typeof config.isEnabledForNodes()).toBe("boolean");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Yoga Constants Verification", () => {
|
||||
test("All required constants are defined", () => {
|
||||
// Edge constants
|
||||
expect(typeof Yoga.EDGE_LEFT).toBe("number");
|
||||
expect(typeof Yoga.EDGE_TOP).toBe("number");
|
||||
expect(typeof Yoga.EDGE_RIGHT).toBe("number");
|
||||
expect(typeof Yoga.EDGE_BOTTOM).toBe("number");
|
||||
expect(typeof Yoga.EDGE_START).toBe("number");
|
||||
expect(typeof Yoga.EDGE_END).toBe("number");
|
||||
expect(typeof Yoga.EDGE_HORIZONTAL).toBe("number");
|
||||
expect(typeof Yoga.EDGE_VERTICAL).toBe("number");
|
||||
expect(typeof Yoga.EDGE_ALL).toBe("number");
|
||||
|
||||
// Unit constants
|
||||
expect(typeof Yoga.UNIT_UNDEFINED).toBe("number");
|
||||
expect(typeof Yoga.UNIT_POINT).toBe("number");
|
||||
expect(typeof Yoga.UNIT_PERCENT).toBe("number");
|
||||
expect(typeof Yoga.UNIT_AUTO).toBe("number");
|
||||
|
||||
// Direction constants
|
||||
expect(typeof Yoga.DIRECTION_INHERIT).toBe("number");
|
||||
expect(typeof Yoga.DIRECTION_LTR).toBe("number");
|
||||
expect(typeof Yoga.DIRECTION_RTL).toBe("number");
|
||||
|
||||
// Display constants
|
||||
expect(typeof Yoga.DISPLAY_FLEX).toBe("number");
|
||||
expect(typeof Yoga.DISPLAY_NONE).toBe("number");
|
||||
|
||||
// Position type constants
|
||||
expect(typeof Yoga.POSITION_TYPE_STATIC).toBe("number");
|
||||
expect(typeof Yoga.POSITION_TYPE_RELATIVE).toBe("number");
|
||||
expect(typeof Yoga.POSITION_TYPE_ABSOLUTE).toBe("number");
|
||||
|
||||
// Overflow constants
|
||||
expect(typeof Yoga.OVERFLOW_VISIBLE).toBe("number");
|
||||
expect(typeof Yoga.OVERFLOW_HIDDEN).toBe("number");
|
||||
expect(typeof Yoga.OVERFLOW_SCROLL).toBe("number");
|
||||
|
||||
// Special value
|
||||
// Note: Yoga.UNDEFINED is not currently exposed in Bun's implementation
|
||||
// It would be YGUndefined (NaN) in the C++ code
|
||||
// expect(typeof Yoga.UNDEFINED).toBe("number");
|
||||
});
|
||||
});
|
||||
272
test/js/bun/yoga/yoga-node.test.js
Normal file
272
test/js/bun/yoga/yoga-node.test.js
Normal file
@@ -0,0 +1,272 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
const Yoga = Bun.Yoga;
|
||||
|
||||
describe("Yoga.Node", () => {
|
||||
test("Node constructor", () => {
|
||||
const node = new Yoga.Node();
|
||||
expect(node).toBeDefined();
|
||||
expect(node.constructor.name).toBe("Node");
|
||||
});
|
||||
|
||||
test("Node.create() static method", () => {
|
||||
const node = Yoga.Node.create();
|
||||
expect(node).toBeDefined();
|
||||
expect(node.constructor.name).toBe("Node");
|
||||
});
|
||||
|
||||
test("Node with config", () => {
|
||||
const config = new Yoga.Config();
|
||||
const node = new Yoga.Node(config);
|
||||
expect(node).toBeDefined();
|
||||
});
|
||||
|
||||
test("setWidth with various values", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// Number
|
||||
expect(() => node.setWidth(100)).not.toThrow();
|
||||
|
||||
// Percentage string
|
||||
expect(() => node.setWidth("50%")).not.toThrow();
|
||||
|
||||
// Auto
|
||||
expect(() => node.setWidth("auto")).not.toThrow();
|
||||
|
||||
// Object format
|
||||
expect(() => node.setWidth({ unit: Yoga.UNIT_POINT, value: 200 })).not.toThrow();
|
||||
expect(() => node.setWidth({ unit: Yoga.UNIT_PERCENT, value: 75 })).not.toThrow();
|
||||
|
||||
// Undefined/null
|
||||
expect(() => node.setWidth(undefined)).not.toThrow();
|
||||
expect(() => node.setWidth(null)).not.toThrow();
|
||||
});
|
||||
|
||||
test("getWidth returns correct format", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
node.setWidth(100);
|
||||
let width = node.getWidth();
|
||||
expect(width).toEqual({ unit: Yoga.UNIT_POINT, value: 100 });
|
||||
|
||||
node.setWidth("50%");
|
||||
width = node.getWidth();
|
||||
expect(width).toEqual({ unit: Yoga.UNIT_PERCENT, value: 50 });
|
||||
|
||||
node.setWidth("auto");
|
||||
width = node.getWidth();
|
||||
expect(width).toEqual({ unit: Yoga.UNIT_AUTO, value: expect.any(Number) });
|
||||
});
|
||||
|
||||
test("setMargin/getPadding edge values", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// Set margins
|
||||
node.setMargin(Yoga.EDGE_TOP, 10);
|
||||
node.setMargin(Yoga.EDGE_RIGHT, "20%");
|
||||
node.setMargin(Yoga.EDGE_BOTTOM, "auto");
|
||||
node.setMargin(Yoga.EDGE_LEFT, { unit: Yoga.UNIT_POINT, value: 30 });
|
||||
|
||||
// Get margins
|
||||
expect(node.getMargin(Yoga.EDGE_TOP)).toEqual({ unit: Yoga.UNIT_POINT, value: 10 });
|
||||
expect(node.getMargin(Yoga.EDGE_RIGHT)).toEqual({ unit: Yoga.UNIT_PERCENT, value: 20 });
|
||||
expect(node.getMargin(Yoga.EDGE_BOTTOM)).toEqual({ unit: Yoga.UNIT_AUTO, value: expect.any(Number) });
|
||||
expect(node.getMargin(Yoga.EDGE_LEFT)).toEqual({ unit: Yoga.UNIT_POINT, value: 30 });
|
||||
});
|
||||
|
||||
test("flexbox properties", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// Flex direction
|
||||
expect(() => node.setFlexDirection(Yoga.FLEX_DIRECTION_ROW)).not.toThrow();
|
||||
expect(() => node.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN)).not.toThrow();
|
||||
|
||||
// Justify content
|
||||
expect(() => node.setJustifyContent(Yoga.JUSTIFY_CENTER)).not.toThrow();
|
||||
expect(() => node.setJustifyContent(Yoga.JUSTIFY_SPACE_BETWEEN)).not.toThrow();
|
||||
|
||||
// Align items
|
||||
expect(() => node.setAlignItems(Yoga.ALIGN_CENTER)).not.toThrow();
|
||||
expect(() => node.setAlignItems(Yoga.ALIGN_FLEX_START)).not.toThrow();
|
||||
|
||||
// Flex properties
|
||||
expect(() => node.setFlex(1)).not.toThrow();
|
||||
expect(() => node.setFlexGrow(2)).not.toThrow();
|
||||
expect(() => node.setFlexShrink(0.5)).not.toThrow();
|
||||
expect(() => node.setFlexBasis(100)).not.toThrow();
|
||||
expect(() => node.setFlexBasis("auto")).not.toThrow();
|
||||
});
|
||||
|
||||
test("hierarchy operations", () => {
|
||||
const parent = new Yoga.Node();
|
||||
const child1 = new Yoga.Node();
|
||||
const child2 = new Yoga.Node();
|
||||
|
||||
// Insert children
|
||||
parent.insertChild(child1, 0);
|
||||
parent.insertChild(child2, 1);
|
||||
|
||||
expect(parent.getChildCount()).toBe(2);
|
||||
expect(parent.getChild(0)).toBe(child1);
|
||||
expect(parent.getChild(1)).toBe(child2);
|
||||
|
||||
expect(child1.getParent()).toBe(parent);
|
||||
expect(child2.getParent()).toBe(parent);
|
||||
|
||||
// Remove child
|
||||
parent.removeChild(child1);
|
||||
expect(parent.getChildCount()).toBe(1);
|
||||
expect(parent.getChild(0)).toBe(child2);
|
||||
expect(child1.getParent()).toBeNull();
|
||||
});
|
||||
|
||||
test("layout calculation", () => {
|
||||
const root = new Yoga.Node();
|
||||
root.setWidth(500);
|
||||
root.setHeight(300);
|
||||
root.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
|
||||
const child = new Yoga.Node();
|
||||
child.setFlex(1);
|
||||
root.insertChild(child, 0);
|
||||
|
||||
// Calculate layout
|
||||
root.calculateLayout(500, 300, Yoga.DIRECTION_LTR);
|
||||
|
||||
// Get computed layout
|
||||
const layout = root.getComputedLayout();
|
||||
expect(layout).toHaveProperty("left");
|
||||
expect(layout).toHaveProperty("top");
|
||||
expect(layout).toHaveProperty("width");
|
||||
expect(layout).toHaveProperty("height");
|
||||
expect(layout.width).toBe(500);
|
||||
expect(layout.height).toBe(300);
|
||||
|
||||
const childLayout = child.getComputedLayout();
|
||||
expect(childLayout.width).toBe(500); // Should fill parent width
|
||||
expect(childLayout.height).toBe(300); // Should fill parent height
|
||||
});
|
||||
|
||||
test("measure function", () => {
|
||||
const node = new Yoga.Node();
|
||||
let measureCalled = false;
|
||||
|
||||
const measureFunc = (width, widthMode, height, heightMode) => {
|
||||
measureCalled = true;
|
||||
return { width: 100, height: 50 };
|
||||
};
|
||||
|
||||
node.setMeasureFunc(measureFunc);
|
||||
node.markDirty();
|
||||
|
||||
// Calculate layout - this should call measure function
|
||||
node.calculateLayout();
|
||||
expect(measureCalled).toBe(true);
|
||||
|
||||
// Clear measure function
|
||||
node.setMeasureFunc(null);
|
||||
});
|
||||
|
||||
test("dirtied callback", () => {
|
||||
const node = new Yoga.Node();
|
||||
let dirtiedCalled = false;
|
||||
|
||||
const dirtiedFunc = () => {
|
||||
dirtiedCalled = true;
|
||||
};
|
||||
|
||||
node.setDirtiedFunc(dirtiedFunc);
|
||||
|
||||
// markDirty requires a measure function
|
||||
node.setMeasureFunc(() => ({ width: 100, height: 50 }));
|
||||
|
||||
// Nodes start dirty, so clear the dirty flag first
|
||||
node.calculateLayout();
|
||||
expect(node.isDirty()).toBe(false);
|
||||
|
||||
// Now mark dirty - this should trigger the callback
|
||||
node.markDirty();
|
||||
|
||||
expect(dirtiedCalled).toBe(true);
|
||||
|
||||
// Clear dirtied function
|
||||
node.setDirtiedFunc(null);
|
||||
});
|
||||
|
||||
test("reset node", () => {
|
||||
const node = new Yoga.Node();
|
||||
node.setWidth(100);
|
||||
node.setHeight(200);
|
||||
node.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
|
||||
|
||||
node.reset();
|
||||
|
||||
// After reset, width/height default to AUTO, not UNDEFINED
|
||||
const width = node.getWidth();
|
||||
expect(width.unit).toBe(Yoga.UNIT_AUTO);
|
||||
});
|
||||
|
||||
test("dirty state", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// Nodes start as dirty by default in Yoga
|
||||
expect(node.isDirty()).toBe(true);
|
||||
|
||||
// Calculate layout clears dirty flag
|
||||
node.calculateLayout();
|
||||
expect(node.isDirty()).toBe(false);
|
||||
|
||||
// Mark as dirty (requires measure function)
|
||||
node.setMeasureFunc(() => ({ width: 100, height: 50 }));
|
||||
node.markDirty();
|
||||
expect(node.isDirty()).toBe(true);
|
||||
|
||||
// Calculate layout clears dirty flag again
|
||||
node.calculateLayout();
|
||||
expect(node.isDirty()).toBe(false);
|
||||
});
|
||||
|
||||
test("free node", () => {
|
||||
const node = new Yoga.Node();
|
||||
expect(() => node.free()).not.toThrow();
|
||||
// After free, the node should not crash but operations may not work
|
||||
});
|
||||
|
||||
test("aspect ratio", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
// Set aspect ratio
|
||||
expect(() => node.setAspectRatio(16 / 9)).not.toThrow();
|
||||
expect(() => node.setAspectRatio(undefined)).not.toThrow();
|
||||
expect(() => node.setAspectRatio(null)).not.toThrow();
|
||||
});
|
||||
|
||||
test("display type", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
expect(() => node.setDisplay(Yoga.DISPLAY_FLEX)).not.toThrow();
|
||||
expect(() => node.setDisplay(Yoga.DISPLAY_NONE)).not.toThrow();
|
||||
});
|
||||
|
||||
test("overflow", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
expect(() => node.setOverflow(Yoga.OVERFLOW_VISIBLE)).not.toThrow();
|
||||
expect(() => node.setOverflow(Yoga.OVERFLOW_HIDDEN)).not.toThrow();
|
||||
expect(() => node.setOverflow(Yoga.OVERFLOW_SCROLL)).not.toThrow();
|
||||
});
|
||||
|
||||
test("position type", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
expect(() => node.setPositionType(Yoga.POSITION_TYPE_RELATIVE)).not.toThrow();
|
||||
expect(() => node.setPositionType(Yoga.POSITION_TYPE_ABSOLUTE)).not.toThrow();
|
||||
});
|
||||
|
||||
test("gap property", () => {
|
||||
const node = new Yoga.Node();
|
||||
|
||||
expect(() => node.setGap(Yoga.GUTTER_ROW, 10)).not.toThrow();
|
||||
expect(() => node.setGap(Yoga.GUTTER_COLUMN, 20)).not.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user