Skip to content
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
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 5 additions & 14 deletions Sources/OpenGraph/Attribute/Attribute/AnyAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ private func OGGraphMutateAttribute(
)

extension AnyAttribute {
public typealias Flags = OGAttributeTypeFlags

public init<Value>(_ attribute: Attribute<Value>) {
self = attribute.identifier
}
Expand All @@ -35,7 +33,7 @@ extension AnyAttribute {
create(offset: offset)
}

public func setFlags(_ newFlags: OGAttributeFlags, mask: OGAttributeFlags) {
public func setFlags(_ newFlags: Subgraph.Flags, mask: Subgraph.Flags) {
flags = flags.subtracting(mask).union(newFlags.intersection(mask))
}

Expand All @@ -49,7 +47,7 @@ extension AnyAttribute {

// FIXME: Use AttributeType instead
public func visitBody<Body: AttributeBodyVisitor>(_ visitor: inout Body) {
let bodyType = info.type.advanced(by: 1).pointee.typeID.type as! _AttributeBody.Type
let bodyType = info.type.advanced(by: 1).pointee.self_id.type as! _AttributeBody.Type
bodyType._visitBody(&visitor, info.body)
}

Expand All @@ -63,20 +61,20 @@ extension AnyAttribute {
}
}

public func breadthFirstSearch(options _: OGSearchOptions = [], _: (AnyAttribute) -> Bool) -> Bool {
public func breadthFirstSearch(options _: SearchOptions = [], _: (AnyAttribute) -> Bool) -> Bool {
fatalError("TODO")
}

public var _bodyType: Any.Type {
info.type.pointee.typeID.type
info.type.pointee.self_id.type
}

public var _bodyPointer: UnsafeRawPointer {
info.body
}

public var valueType: Any.Type {
info.type.pointee.valueTypeID.type
info.type.pointee.value_id.type
}

public var indirectDependency: AnyAttribute? {
Expand All @@ -98,10 +96,3 @@ extension AnyAttribute: Swift.CustomStringConvertible {
}

public typealias AttributeUpdateBlock = () -> (UnsafeMutableRawPointer, AnyAttribute) -> Void

extension [AnyAttribute] {
@_transparent
public var anyInputsChanged: Bool {
__OGGraphAnyInputsChanged(self, count)
}
}
24 changes: 9 additions & 15 deletions Sources/OpenGraph/Attribute/Attribute/Attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct Attribute<Value> {
public init(value: Value) {
self = withUnsafePointer(to: value) { valuePointer in
withUnsafePointer(to: External<Value>()) { bodyPointer in
Attribute(body: bodyPointer, value: valuePointer, flags: ._16) {
Attribute(body: bodyPointer, value: valuePointer, flags: .external) {
External<Value>._update
}
}
Expand All @@ -28,7 +28,7 @@ public struct Attribute<Value> {

public init(type _: Value.Type) {
self = withUnsafePointer(to: External<Value>()) { bodyPointer in
Attribute(body: bodyPointer, value: nil, flags: ._16) {
Attribute(body: bodyPointer, value: nil, flags: .external) {
External<Value>._update
}
}
Expand All @@ -37,7 +37,7 @@ public struct Attribute<Value> {
public init<Body: _AttributeBody>(
body: UnsafePointer<Body>,
value: UnsafePointer<Value>?,
flags: OGAttributeTypeFlags = [],
flags: _AttributeType.Flags = [],
update: AttributeUpdateBlock
) {
#if os(WASI)
Expand Down Expand Up @@ -120,7 +120,7 @@ public struct Attribute<Value> {
identifier.mutateBody(as: type, invalidating: invalidating, body)
}

public func breadthFirstSearch(options: OGSearchOptions = [], _ body: (AnyAttribute) -> Bool) -> Bool {
public func breadthFirstSearch(options: SearchOptions = [], _ body: (AnyAttribute) -> Bool) -> Bool {
identifier.breadthFirstSearch(options: options, body)
}

Expand Down Expand Up @@ -152,21 +152,21 @@ public struct Attribute<Value> {
nonmutating set { _ = setValue(newValue) }
}

public var valueState: OGValueState { identifier.valueState }
public var valueState: ValueState { identifier.valueState }

public func valueAndFlags(options: OGValueOptions = []) -> (value: Value, flags: OGChangedValueFlags) {
let value = OGGraphGetValue(identifier, options: options, type: Value.self)
return (
value.value.assumingMemoryBound(to: Value.self).pointee,
value.changed ? ._1 : []
value.flags
)
}

public func changedValue(options: OGValueOptions = []) -> (value: Value, changed: Bool) {
let value = OGGraphGetValue(identifier, options: options, type: Value.self)
return (
value.value.assumingMemoryBound(to: Value.self).pointee,
value.changed
value.flags.contains(.changed)
)
}

Expand Down Expand Up @@ -194,12 +194,12 @@ public struct Attribute<Value> {

// MARK: - Flags

public var flags: OGAttributeFlags {
public var flags: Subgraph.Flags {
get { identifier.flags }
nonmutating set { identifier.flags = newValue }
}

public func setFlags(_ newFlags: OGAttributeFlags, mask: OGAttributeFlags) {
public func setFlags(_ newFlags: Subgraph.Flags, mask: Subgraph.Flags) {
identifier.setFlags(newFlags, mask: mask)
}
}
Expand Down Expand Up @@ -228,12 +228,6 @@ extension Attribute {
}
}

// TODO:
private struct AttributeType {
var graphType: OGAttributeType
var type: _AttributeBody.Type
}

@_silgen_name("OGGraphCreateAttribute")
@inline(__always)
@inlinable
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenGraph/Attribute/Attribute/External.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct External<Value> {
extension External: _AttributeBody {
public static var comparisonMode: ComparisonMode { .equatableAlways }

public static var flags: OGAttributeTypeFlags { [] }
public static var flags: _AttributeType.Flags { [] }

public static func _update(_: UnsafeMutableRawPointer, attribute _: AnyAttribute) {}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenGraph/Attribute/Body/AttributeBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public protocol _AttributeBody {
static var _hasDestroySelf: Bool { get }
static func _updateDefault(_ pointer: UnsafeMutableRawPointer)
static var comparisonMode: ComparisonMode { get }
static var flags: OGAttributeTypeFlags { get }
static var flags: _AttributeType.Flags { get }
}

// MARK: - Protocol Default implementation
Expand All @@ -22,7 +22,7 @@ extension _AttributeBody {
public static var _hasDestroySelf: Bool { false }
public static func _updateDefault(_ pointer: UnsafeMutableRawPointer) {}
public static var comparisonMode: ComparisonMode { .equatableUnlessPOD }
public static var flags: OGAttributeTypeFlags { .mainThread }
public static var flags: _AttributeType.Flags { .mainThread }
}

extension _AttributeBody {
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenGraph/Attribute/Rule/Focus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct Focus<Root, Value>: Rule, CustomStringConvertible {

public var value: Value { root.value[keyPath: keyPath] }

public static var flags: OGAttributeTypeFlags { [] }
public static var flags: _AttributeType.Flags { [] }

public var description: String { "• \(Metadata(Value.self).description)" }
}
2 changes: 1 addition & 1 deletion Sources/OpenGraph/Attribute/Rule/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct Map<Source, Value>: Rule, CustomStringConvertible {

public var value: Value { body(arg.value) }

public static var flags: OGAttributeTypeFlags { [] }
public static var flags: _AttributeType.Flags { [] }

public var description: String { "λ \(Value.self)" }
}
4 changes: 2 additions & 2 deletions Sources/OpenGraph/Attribute/RuleContext/AnyRuleContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public struct AnyRuleContext: Equatable {
let value = OGGraphGetInputValue(attribute, input: input.identifier, options: options, type: V.self)
return (
value.value.assumingMemoryBound(to: V.self).pointee,
value.changed ? ._1 : []
value.flags
)
}

public func changedValue<V>(of input: Attribute<V>, options: OGValueOptions = []) -> (value: V, changed: Bool) {
let value = OGGraphGetInputValue(attribute, input: input.identifier, options: options, type: V.self)
return (
value.value.assumingMemoryBound(to: V.self).pointee,
value.changed
value.flags.contains(.changed)
)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenGraph/Attribute/RuleContext/RuleContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ public struct RuleContext<Value>: Equatable {
let value = OGGraphGetInputValue(attribute.identifier, input: input.identifier, options: options, type: V.self)
return (
value.value.assumingMemoryBound(to: V.self).pointee,
value.changed ? ._1 : []
value.flags
)
}

public func changedValue<V>(of input: Attribute<V>, options: OGValueOptions = []) -> (value: V, changed: Bool) {
let value = OGGraphGetInputValue(attribute.identifier, input: input.identifier, options: options, type: V.self)
return (
value.value.assumingMemoryBound(to: V.self).pointee,
value.changed
value.flags.contains(.changed)
)
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/OpenGraph/Attribute/Weak/AnyWeakAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension AnyWeakAttribute {
public init(_ attribute: AnyAttribute?) {
self = __OGCreateWeakAttribute(attribute ?? .nil)
}

@_alwaysEmitIntoClient
public init<Value>(_ weakAttribute: WeakAttribute<Value>) {
self = weakAttribute.base
Expand All @@ -40,13 +40,13 @@ extension AnyWeakAttribute {
extension AnyWeakAttribute: Swift.Hashable {
@_alwaysEmitIntoClient
public static func == (lhs: AnyWeakAttribute, rhs: AnyWeakAttribute) -> Bool {
lhs.raw_attribute == rhs.raw_attribute && lhs.subgraph_id == rhs.subgraph_id
lhs._details.identifier == rhs._details.identifier && lhs._details.seed == rhs._details.seed
}

@_alwaysEmitIntoClient
public func hash(into hasher: inout Hasher) {
hasher.combine(raw_attribute)
hasher.combine(subgraph_id)
hasher.combine(_details.identifier)
hasher.combine(_details.seed)
}

@_alwaysEmitIntoClient
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenGraph/Attribute/Weak/WeakAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct WeakAttribute<Value> {
}

public init() {
base = AnyWeakAttribute(raw_attribute: AnyAttribute(rawValue: 0), subgraph_id: 0)
base = AnyWeakAttribute(_details: AnyWeakAttribute.__Unnamed_struct__details(identifier: AnyAttribute(rawValue: 0), seed: 0))
}

public init(_ attribute: Attribute<Value>) {
Expand Down
15 changes: 11 additions & 4 deletions Sources/OpenGraph/Graph/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension Graph {
ctx: GraphContext,
body: _AttributeBody.Type,
valueType: Metadata,
flags: OGAttributeTypeFlags,
flags: _AttributeType.Flags,
update: AttributeUpdateBlock
) -> Int {
// TODO: __AGGraphInternAttributeType
Expand Down Expand Up @@ -81,7 +81,7 @@ extension Graph {
extension Graph {
@_transparent
@inline(__always)
public var mainUpdates: Int { numericCast(counter(for: ._10)) }
public var mainUpdates: Int { numericCast(counter(for: .mainThreadUpdates)) }
}

extension Graph {
Expand All @@ -97,6 +97,13 @@ extension Graph {
public static func setOutputValue<Value>(_ value: UnsafePointer<Value>)
}

extension Graph {
@_transparent
public static func anyInputsChanged(excluding excludedInputs: [AnyAttribute]) -> Bool {
return __OGGraphAnyInputsChanged(excludedInputs, excludedInputs.count)
}
}

#if canImport(Darwin)
import Foundation
#endif
Expand All @@ -105,8 +112,8 @@ extension Graph {
public func archiveJSON(name: String?) {
#if canImport(Darwin)
let options: NSDictionary = [
Graph.descriptionFormat: "graph/dict",
Graph.descriptionIncludeValues: true,
DescriptionOption.format: "graph/dict",
DescriptionOption.includeValues: true,
]
guard let description = Graph.description(self, options: options) as? [String: Any] else {
return
Expand Down
26 changes: 11 additions & 15 deletions Sources/OpenGraph/Graph/Subgraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public import OpenGraphCxx

extension Subgraph {
public func addObserver(_ observer: () -> Void) -> Int {
OGSubgraphAddObserver(self, observer: observer)
Subgraph.addObserver(self, observer: observer)
}

public func apply<Value>(_ body: () -> Value) -> Value {
Expand All @@ -27,8 +27,8 @@ extension Subgraph {
#endif
}

public func forEach(_ flags: OGAttributeFlags, _ callback: (AnyAttribute) -> Void) {
OGSubgraphApply(self, flags: flags, callback: callback)
public func forEach(_ flags: Subgraph.Flags, _ callback: (AnyAttribute) -> Void) {
Subgraph.apply(self, flags: flags, callback: callback)
}
}

Expand All @@ -52,15 +52,11 @@ extension Subgraph {
}
}

@_silgen_name("OGSubgraphApply")
private func OGSubgraphApply(
_ graph: Subgraph,
flags: OGAttributeFlags,
callback: (AnyAttribute) -> Void
)

@_silgen_name("OGSubgraphAddObserver")
private func OGSubgraphAddObserver(
_ graph: Subgraph,
observer: () -> Void
) -> Int
// FIXME: migrate to use @_extern(c, "xx") in Swift 6
extension Subgraph {
@_silgen_name("OGSubgraphApply")
private static func apply(_ graph: Subgraph, flags: Subgraph.Flags, callback: (AnyAttribute) -> Void)

@_silgen_name("OGSubgraphAddObserver")
private static func addObserver(_ graph: Subgraph, observer: () -> Void) -> Int
}
Comment on lines -55 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to change this? Does it related with https://forums.swift.org/t/understanding-calling-conventions-passing-closure-from-swift-to-c-and-calling-it-from-c/79995?

If so, please add some comments context here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the time this was the only way I could get the parameters to line up between C++ and Swift. But I was using @_extern(c) instead of @_silgen_name. I will experiment with @_silgen_name to see if it works without changes.

2 changes: 1 addition & 1 deletion Sources/OpenGraphCxx/Attribute/OGAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ OGAttributeFlags OGGraphGetFlags(OGAttribute attribute) {
const OG::AttributeID id = OG::AttributeID(attribute);
id.checkIsDirect();
// TODO: data/table
return OGAttributeFlagsDefault;
return OGAttributeFlagsNone;
}

void OGGraphSetFlags(OGAttribute attribute, OGAttributeFlags flags) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenGraphCxx/Attribute/OGWeakAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ OGAttribute OGWeakAttributeGetAttribute(OGWeakAttribute weakAttribute) {
return OGAttributeNil;
}

OGValue OGGraphGetWeakValue(OGWeakAttribute weakAttribute, OGValueOptions options, OGTypeID type) {
OGWeakValue OGGraphGetWeakValue(OGWeakAttribute weakAttribute, OGValueOptions options, OGTypeID type) {
// TODO
return {nullptr, false};
}
Loading