From d1c1da51165bea67e47f76bd5c320b979b4426ee Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 25 Feb 2024 18:43:54 +0800 Subject: [PATCH] Add BodyAccessor.makeBody logic --- .../DynamicProperty/DynamicProperty.swift | 77 +++++++++++++++++-- .../DynamicPropertyBehaviors.swift | 1 + .../DynamicPropertyBuffer.swift | 2 +- .../Internal/Graph/GraphValue.swift | 10 ++- 4 files changed, 82 insertions(+), 8 deletions(-) diff --git a/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicProperty.swift b/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicProperty.swift index 05e00e34..f52c5d3e 100644 --- a/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicProperty.swift +++ b/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicProperty.swift @@ -76,7 +76,7 @@ extension DynamicProperty { container: _GraphValue, fieldOffset: Int, inputs: inout _GraphInputs - ) -> () { + ) { let fields = DynamicPropertyCache.fields(of: self) buffer.addFields( fields, @@ -88,14 +88,45 @@ extension DynamicProperty { } extension BodyAccessor { - func makeBody(container: _GraphValue, inputs: inout _GraphInputs, fields: DynamicPropertyCache.Fields) -> (_GraphValue, _DynamicPropertyBuffer?) { + func makeBody( + container: _GraphValue, + inputs: inout _GraphInputs, + fields: DynamicPropertyCache.Fields + ) -> (_GraphValue, _DynamicPropertyBuffer?) { guard Body.self != Never.self else { fatalError("\(Body.self) may not have Body == Never") } - withUnsafeMutablePointer(to: &inputs) { inputs in - // TODO + return withUnsafeMutablePointer(to: &inputs) { inputsPointer in + func project(flags _: Flags.Type) -> (_GraphValue, _DynamicPropertyBuffer?) { + let buffer = _DynamicPropertyBuffer( + fields: fields, + container: container, + inputs: &inputsPointer.pointee + ) + if buffer._count == 0 { + buffer.destroy() + let body = StaticBody( + accessor: self, + container: container.value + ) + return (_GraphValue(body), nil) + } else { + let body = DynamicBody( + accessor: self, + container: container.value, + phase: inputsPointer.pointee.phase, + links: buffer, + resetSeed: 0 + ) + return (_GraphValue(body), buffer) + } + } + if fields.behaviors.contains(.asyncThread) { + return project(flags: AsyncThreadFlags.self) + } else { + return project(flags: MainThreadFlags.self) + } } - fatalError("TODO") } } @@ -167,3 +198,39 @@ extension StaticBody: BodyAccessorRule { extension StaticBody: CustomStringConvertible { var description: String { "\(Accessor.Body.self)" } } + +// MARK: - DynamicBody + +// TODO +private struct DynamicBody { + let accessor: Accessor + @Attribute + var container: Accessor.Container + @Attribute + var phase: _GraphInputs.Phase + var links: _DynamicPropertyBuffer + var resetSeed: UInt32 + + init( + accessor: Accessor, + container: Attribute, + phase: Attribute<_GraphInputs.Phase>, + links: _DynamicPropertyBuffer, + resetSeed: UInt32 + ) { + fatalError("TODO") +// self.accessor = accessor +// self._container = container +// self._phase = phase +// self.links = links +// self.resetSeed = resetSeed + } +} + +extension DynamicBody: StatefulRule { + typealias Value = Accessor.Body + + func updateValue() { + // TODO + } +} diff --git a/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBehaviors.swift b/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBehaviors.swift index eb755baa..48a12bd7 100644 --- a/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBehaviors.swift +++ b/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBehaviors.swift @@ -8,4 +8,5 @@ struct DynamicPropertyBehaviors: OptionSet { let rawValue: UInt32 + static var asyncThread: DynamicPropertyBehaviors { .init(rawValue: 1 << 0) } } diff --git a/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBuffer.swift b/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBuffer.swift index 7c194f7e..a5821c50 100644 --- a/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBuffer.swift +++ b/Sources/OpenSwiftUI/DataAndStorage/ModelData/DynamicProperty/DynamicPropertyBuffer.swift @@ -26,7 +26,7 @@ public struct _DynamicPropertyBuffer { fields: DynamicPropertyCache.Fields, container: _GraphValue, inputs: inout _GraphInputs, - baseOffset: Int + baseOffset: Int = 0 ) { self.init() addFields(fields, container: container, inputs: &inputs, baseOffset: baseOffset) diff --git a/Sources/OpenSwiftUI/Internal/Graph/GraphValue.swift b/Sources/OpenSwiftUI/Internal/Graph/GraphValue.swift index 43b2a681..328418e9 100644 --- a/Sources/OpenSwiftUI/Internal/Graph/GraphValue.swift +++ b/Sources/OpenSwiftUI/Internal/Graph/GraphValue.swift @@ -22,8 +22,14 @@ public struct _GraphValue: Equatable { init(_ value: Attribute) { self.value = value } -// init(_ value: Rule) -// init(_ value: StatefulRule) + + init(_ rule: R) where R.Value == Value { + fatalError("TODO") + } + + init(_ rule: R) where R.Value == Value { + fatalError("TODO") + } subscript(offset body: (inout Value) -> PointerOffset) -> _GraphValue { .init(value[offset: body])