Skip to content

Commit

Permalink
Prep a fix for the dangling pointer issue
Browse files Browse the repository at this point in the history
... should work, but needs testing. Use a proper
pointer closure instead of a direct pointer into
the value.
  • Loading branch information
helje5 committed Jun 27, 2020
1 parent 544b433 commit 16b84d4
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions Sources/SwiftWebUI/VirtualDOM/Components/ComponentReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,46 @@ enum ComponentTypeInfo: Equatable {
let typeInstance : _DynamicViewPropertyType.Type
let stateInstance : _StateType.Type?

func mutablePointerIntoView<T: View>(_ view: inout T)
-> UnsafeMutableRawPointer
{
// TODO: Swift-5.2 (maybe before):
// We probably should pass down actual pointers
// Note: We do not really need the `T` here.
let viewPtr = UnsafeMutablePointer(&view) // gives warning on 5.2
let rawViewPtr = UnsafeMutableRawPointer(viewPtr)
let rawPropPtr = rawViewPtr.advanced(by: offset)
return rawPropPtr
}
#if true
func mutablePointerIntoView<T: View>(_ view: inout T)
-> UnsafeMutableRawPointer
{
// TODO: Swift-5.2 (maybe before):
// We probably should pass down actual pointers
// Note: We do not really need the `T` here.
let viewPtr = UnsafeMutablePointer(&view) // gives warning on 5.2
let rawViewPtr = UnsafeMutableRawPointer(viewPtr)
let rawPropPtr = rawViewPtr.advanced(by: offset)
return rawPropPtr
}

func updateInView<T: View>(_ view: inout T) {
// TODO: Swift-5.2 (maybe before):
// We probably should pass down actual pointers
// Note: We do not really need the `T` here.
let rawPropPtr = mutablePointerIntoView(&view)
typeInstance._updateInstance(at: rawPropPtr)
}
func updateInView<T: View>(_ view: inout T) {
// TODO: Swift-5.2 (maybe before):
// We probably should pass down actual pointers
// Note: We do not really need the `T` here.
let rawPropPtr = mutablePointerIntoView(&view)
typeInstance._updateInstance(at: rawPropPtr)
}
#else // new version to be tested
func withMutablePointerIntoView<T: View>
(_ view: inout T, execute: ( UnsafeMutableRawPointer ) -> Void)
{
withUnsafeMutablePointer(to: &view) { viewPtr in
let rawViewPtr = UnsafeMutableRawPointer(viewPtr)
let rawPropPtr = rawViewPtr.advanced(by: offset)
execute(rawPropPtr)
}
}

func updateInView<T: View>(_ view: inout T) {
// TODO: Swift-5.2 (maybe before):
// We probably should pass down actual pointers
// Note: We do not really need the `T` here.
withMutablePointerIntoView(&view) { rawPropPtr in
typeInstance._updateInstance(at: rawPropPtr, context: context)
}
}
#endif

static func ==(lhs: DynamicPropertyInfo, rhs: DynamicPropertyInfo)
-> Bool
Expand Down

0 comments on commit 16b84d4

Please sign in to comment.