Skip to content

Add DynamicBody implementation #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/OpenSwiftUI/Core/Graph/GraphInputs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public struct _GraphInputs {
extension _GraphInputs {
struct Phase: Equatable {
var value: UInt32

@inline(__always)
var seed: UInt32 {
get { value >> 1 }
// TODO
// set
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUI/Data/Model/Binding/Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ extension Binding: DynamicProperty {
} else {
location = LocationBox(location: ScopedLocation(base: property.location))
}
let (value, isUpdated) = location!.update()
let (value, changed) = location!.update()
property.location = location!
property._value = value
return isUpdated ? location!.wasRead : false
return changed ? location!.wasRead : false
}
}

Expand Down
106 changes: 73 additions & 33 deletions Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ private struct MainThreadFlags: RuleThreadFlags {
static var value: OGAttributeTypeFlags { .mainThread }
}

#if canImport(Darwin)

// MARK: - StaticBody

private struct StaticBody<Accessor: BodyAccessor, ThreadFlags: RuleThreadFlags> {
Expand All @@ -165,55 +167,43 @@ extension StaticBody: StatefulRule {
func updateValue() {
accessor.updateBody(of: container, changed: true)
}

static var flags: OGAttributeTypeFlags { ThreadFlags.value }
}

extension StaticBody: _AttributeBody {
static var flags: OGAttributeTypeFlags {
ThreadFlags.value
}
}

#if canImport(Darwin)

extension StaticBody: BodyAccessorRule {
static var container: Any.Type {
Accessor.Container.self
}

static func buffer<Value>(as type: Value.Type, attribute: OGAttribute) -> _DynamicPropertyBuffer? {
nil
}

static func value<Value>(as type: Value.Type, attribute: OGAttribute) -> Value? {
guard container == type else {
static func value<Value>(as _: Value.Type, attribute: OGAttribute) -> Value? {
guard container == Value.self else {
return nil
}
return (attribute.info.body.assumingMemoryBound(to: Self.self).pointee.container as! Value)
return unsafeBitCast(attribute.info.body.assumingMemoryBound(to: Self.self).pointee.container, to: Value.self)
}

static func buffer<Value>(as _: Value.Type, attribute _: OGAttribute) -> _DynamicPropertyBuffer? {
nil
}

static func metaProperties<Value>(as type: Value.Type, attribute: OGAttribute) -> [(String, OGAttribute)] {
guard container == type else {
static func metaProperties<Value>(as _: Value.Type, attribute: OGAttribute) -> [(String, OGAttribute)] {
guard container == Value.self else {
return []
}
return [("@self", attribute.info.body.assumingMemoryBound(to: Self.self).pointee._container.identifier)]
}
}

#endif

extension StaticBody: CustomStringConvertible {
var description: String { "\(Accessor.Body.self)" }
}

// MARK: - DynamicBody

// TODO
private struct DynamicBody<Accessor: BodyAccessor, ThreadFlags: RuleThreadFlags> {
let accessor: Accessor
@Attribute
var container: Accessor.Container
@Attribute
var phase: _GraphInputs.Phase
@Attribute var container: Accessor.Container
@Attribute var phase: _GraphInputs.Phase
var links: _DynamicPropertyBuffer
var resetSeed: UInt32

Expand All @@ -224,19 +214,69 @@ private struct DynamicBody<Accessor: BodyAccessor, ThreadFlags: RuleThreadFlags>
links: _DynamicPropertyBuffer,
resetSeed: UInt32
) {
fatalError("TODO")
// self.accessor = accessor
// self._container = container
// self._phase = phase
// self.links = links
// self.resetSeed = resetSeed
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
mutating func updateValue() {
if resetSeed != phase.seed {
links.reset()
resetSeed = phase.seed
}
var (container, containerChanged) = $container.changedValue()
let linkChanged = withUnsafeMutablePointer(to: &container) {
links.update(container: $0, phase: phase)
}
let changed = linkChanged || containerChanged || !hasValue
accessor.updateBody(of: container, changed: changed)
}

static var flags: OGAttributeTypeFlags { ThreadFlags.value }
}

extension DynamicBody: ObservedAttribute {
func destroy() { links.destroy() }
}

extension DynamicBody: BodyAccessorRule {
static var container: Any.Type {
Accessor.Container.self
}

static func value<Value>(as _: Value.Type, attribute: OGAttribute) -> Value? {
guard container == Value.self else {
return nil
}
return unsafeBitCast(attribute.info.body.assumingMemoryBound(to: Self.self).pointee.container, to: Value.self)
}

static func buffer<Value>(as _: Value.Type, attribute: OGAttribute) -> _DynamicPropertyBuffer? {
guard container == Value.self else {
return nil
}
return attribute.info.body.assumingMemoryBound(to: Self.self).pointee.links
}

static func metaProperties<Value>(as _: Value.Type, attribute: OGAttribute) -> [(String, OGAttribute)] {
guard container == Value.self else {
return []
}
return [
("@self", attribute.info.body.assumingMemoryBound(to: Self.self).pointee._container.identifier),
("@identity", attribute.info.body.assumingMemoryBound(to: Self.self).pointee._phase.identifier)
]
}
}

extension DynamicBody: CustomStringConvertible {
var description: String { "\(Accessor.Body.self)" }
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public struct _DynamicPropertyBuffer {

func update(container: UnsafeMutableRawPointer, phase: _GraphInputs.Phase) -> Bool {
precondition(_count >= 0)
var isUpdated = false
var changed = false
var count = _count
var pointer = buf
while count > 0 {
Expand All @@ -155,11 +155,11 @@ public struct _DynamicPropertyBuffer {
phase: phase
)
itemPointer.pointee.lastChanged = updateResult
isUpdated = isUpdated || updateResult
changed = changed || updateResult
pointer += Int(itemPointer.pointee.size)
count &-= 1
}
return isUpdated
return changed
}

private mutating func allocate(bytes: Int) -> UnsafeMutableRawPointer {
Expand Down Expand Up @@ -309,11 +309,11 @@ private class BoxVTable<Box: DynamicPropertyBox>: BoxVTableBase {
) -> Bool {
let boxPointer = ptr.assumingMemoryBound(to: Box.self)
let propertyPointer = property.assumingMemoryBound(to: Box.Property.self)
let isUpdated = boxPointer.pointee.update(property: &propertyPointer.pointee, phase: phase)
if isUpdated {
let changed = boxPointer.pointee.update(property: &propertyPointer.pointee, phase: phase)
if changed {
// TODO: OSSignpost
}
return isUpdated
return changed
}

override class func getState<Value>(ptr: UnsafeMutableRawPointer, type: Value.Type) -> Binding<Value>? {
Expand Down Expand Up @@ -353,26 +353,26 @@ private class EnumVTable<Enum>: BoxVTableBase {
}

override class func update(ptr: UnsafeMutableRawPointer, property: UnsafeMutableRawPointer, phase: _GraphInputs.Phase) -> Bool {
var isUpdated = false
var changed = false
withUnsafeMutablePointerToEnumCase(of: property.assumingMemoryBound(to: Enum.self)) { tag, _, pointer in
let boxPointer = ptr.assumingMemoryBound(to: EnumBox.self)
if let (activeTag, index) = boxPointer.pointee.active, activeTag != tag {
boxPointer.pointee.cases[index].links.reset()
boxPointer.pointee.active = nil
isUpdated = true
changed = true
}
if boxPointer.pointee.active == nil {
guard let matchedIndex = boxPointer.pointee.cases.firstIndex(where: { $0.tag == tag }) else {
return
}
boxPointer.pointee.active = (tag, matchedIndex)
isUpdated = true
changed = true
}
if let (_, index) = boxPointer.pointee.active {
isUpdated = boxPointer.pointee.cases[index].links.update(container: pointer, phase: phase)
changed = boxPointer.pointee.cases[index].links.update(container: pointer, phase: phase)
}
}
return isUpdated
return changed
}

override class func getState<Value>(ptr: UnsafeMutableRawPointer, type: Value.Type) -> Binding<Value>? {
Expand Down
Loading