Skip to content

Update struct definitions #134

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions Sources/OpenGraph/Attribute/Attribute/AnyAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public import OpenGraph_SPI

extension AnyAttribute {
public typealias Flags = OGAttributeTypeFlags
public typealias Flags = AttributeType.Flags

public init<Value>(_ attribute: Attribute<Value>) {
self = attribute.identifier
Expand Down Expand Up @@ -41,7 +41,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 @@ -56,15 +56,15 @@ extension AnyAttribute {
}

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 Down
16 changes: 5 additions & 11 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 @@ -158,15 +158,15 @@ public struct Attribute<Value> {
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 @@ -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
4 changes: 2 additions & 2 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 @@ -48,7 +48,7 @@ extension Graph {
extension Graph {
@_transparent
@inline(__always)
public var mainUpdates: Int { numericCast(counter(for: ._10)) }
public var mainUpdates: Int { numericCast(counter(for: .mainThreadUpdateCount)) }
}

extension Graph {
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenGraph_SPI/Graph/GraphDescription.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#if OG_OBJC_FOUNDATION
#include <Foundation/Foundation.h>
CFStringRef OGDescriptionFormat = CFSTR("format");
const CFStringRef OGDescriptionFormat = CFSTR("format");

CFTypeRef OG::Graph::description(const Graph * _Nullable graph, NSDictionary* dic) {
// TODO
Expand Down
22 changes: 11 additions & 11 deletions Sources/OpenGraph_SPI/Graph/OGGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,27 +161,27 @@ uint64_t OGGraphGetCounter(OGGraphRef graph, OGCounterQueryType query) {
}
OG::Graph::Context& context = graph->context;
switch (query) {
case OGCounterQueryType_0:
case OGCounterQueryTypeNodeCount:
return context.get_graph().get_counter_0();
case OGCounterQueryType_1:
case OGCounterQueryTypeTransactionCount:
return context.get_graph().get_counter_1();
case OGCounterQueryType_2:
case OGCounterQueryTypeUpdateCount:
return context.get_graph().get_counter_2();
case OGCounterQueryType_3:
case OGCounterQueryTypeChangeCount:
return context.get_graph().get_counter_3();
case OGCounterQueryType_4:
case OGCounterQueryTypeContextID:
return context.get_graph().get_counter_4();
case OGCounterQueryType_5:
case OGCounterQueryTypeGraphID:
return context.get_graph().get_counter_5();
case OGCounterQueryType_6:
case OGCounterQueryTypeContextThreadUpdating:
return context.thread_is_updating();
case OGCounterQueryType_7:
case OGCounterQueryTypeThreadUpdating:
return context.get_graph().thread_is_updating();
case OGCounterQueryType_8:
case OGCounterQueryTypeContextNeedsUpdate:
return context.get_graph().get_counter_8();
case OGCounterQueryType_9:
case OGCounterQueryTypeNeedsUpdate:
return context.get_graph().get_counter_9();
case OGCounterQueryType_10:
case OGCounterQueryTypeMainThreadUpdateCount:
return context.get_graph().get_counter_10();
default:
return 0;
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenGraph_SPI/include/OGAttributeFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "OGBase.h"

typedef OG_OPTIONS(uint32_t, OGAttributeFlags) {
typedef OG_OPTIONS(uint8_t, OGAttributeFlags) {
Copy link
Author

Choose a reason for hiding this comment

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

This gets stored as an 8-bit field in AG::Node.

OGAttributeFlagsDefault = 0,
OGAttributeFlagsActive = 1 << 0,
OGAttributeFlagsRemovable = 1 << 1,
Expand Down
31 changes: 27 additions & 4 deletions Sources/OpenGraph_SPI/include/OGAttributeType.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@
#define OGAttributeType_h

#include "OGBase.h"
#include "OGAttributeTypeFlags.h"
#include "OGClosure.h"
#include "OGTypeID.h"

OG_ASSUME_NONNULL_BEGIN

typedef struct OGAttributeType {
OGTypeID typeID;
OGTypeID valueTypeID;
// TODO
typedef struct OGAttributeType OGAttributeType;

typedef struct OGAttributeVTable {
unsigned long version;
void (*_Nullable type_destroy)(OGAttributeType *);
void (*_Nullable self_destroy)(const OGAttributeType *, void *);
CFStringRef _Nullable (*_Nullable self_description)(const OGAttributeType *, const void *);
CFStringRef _Nullable (*_Nullable value_description)(const OGAttributeType *, const void *);
void (*_Nullable update_default)(const OGAttributeType *, void *);
} OGAttributeVTable OG_SWIFT_NAME(_AttributeVTable);

typedef struct OG_SWIFT_NAME(AttributeType) OGAttributeType {
OGTypeID self_id;
OGTypeID value_id;
OGClosureStorage update;
const OGAttributeVTable *vtable;
OGAttributeTypeFlags flags;

uint32_t internal_offset;
const unsigned char *_Nullable value_layout;

struct {
OGTypeID type_id;
const void *witness_table;
} body_conformance;
Comment on lines +36 to +39
Copy link
Author

Choose a reason for hiding this comment

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

@Kyle-Ye This doesn't appear in the swift section dump you provided, but I can see it being set in Ghidra's disassembly. I'm not sure if there is a difference in behaviour depending on version.

Copy link
Member

Choose a reason for hiding this comment

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

Do we miss something in swift section? Maybe we can try to use Ghidra and align those logic. cc @Mx-Iris

Copy link
Author

Choose a reason for hiding this comment

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

I'm now on macOS Sequoia and the Swift section is reporting the structure is 56 bytes, which would exclude this field. But compatibility tests are confirming that it is definitely set.
https://github.com/jcmosc/Compute/blob/updating/Tests/ComputeTests/Shared/Attribute/AttributeTests.swift#L39

It's possible that it's not included in the attribute type struct, but instead part of a wrapper struct returned from the closure passed to AGGraphInternAttributeType.

} OGAttributeType;

OG_ASSUME_NONNULL_END
Expand Down
15 changes: 9 additions & 6 deletions Sources/OpenGraph_SPI/include/OGAttributeTypeFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
#include "OGBase.h"

typedef OG_OPTIONS(uint32_t, OGAttributeTypeFlags) {
OGAttributeTypeFlagsDefault = 0,
OGAttributeTypeFlags_1 = 1 << 0,
OGAttributeTypeFlags_2 = 1 << 1,
OGAttributeTypeFlags_4 = 1 << 2,
OGAttributeTypeFlagsComparisonModeBitwise = 0,
OGAttributeTypeFlagsComparisonModeIndirect = 1,
OGAttributeTypeFlagsComparisonModeEquatableUnlessPOD = 2,
OGAttributeTypeFlagsComparisonModeEquatableAlways = 3,
OGAttributeTypeFlagsComparisonModeMask = 0x03,

OGAttributeTypeFlagsHasDestroySelf = 1 << 2,
OGAttributeTypeFlagsMainThread = 1 << 3,
OGAttributeTypeFlags_16 = 1 << 4,
OGAttributeTypeFlagsExternal = 1 << 4,
OGAttributeTypeFlagsAsyncThread = 1 << 5,
};
} OG_SWIFT_NAME(AttributeType.Flags);

#endif /* OGAttributeTypeFlags_h */

2 changes: 1 addition & 1 deletion Sources/OpenGraph_SPI/include/OGChangedValueFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "OGBase.h"

typedef OG_OPTIONS(uint32_t, OGChangedValueFlags) {
OGChangedValueFlags_1 = 1 << 0,
OGChangedValueFlagsChanged = 1 << 0,
OGChangedValueFlagsRequiresMainThread = 1 << 1,
};

Expand Down
32 changes: 32 additions & 0 deletions Sources/OpenGraph_SPI/include/OGClosure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// OGClosure.h
// OpenGraph_SPI

#ifndef OGClosure_h
#define OGClosure_h

#include "OGBase.h"

OG_ASSUME_NONNULL_BEGIN

OG_EXTERN_C_BEGIN

typedef struct OGClosureStorage {
const void *thunk;
const void *_Nullable context;
} OGClosureStorage;

OG_EXPORT
OG_REFINED_FOR_SWIFT
OGClosureStorage OGRetainClosure(void (*thunk)(void *_Nullable context OG_SWIFT_CONTEXT) OG_SWIFT_CC(swift),
void *_Nullable context);

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGReleaseClosure(OGClosureStorage closure);

OG_EXTERN_C_END

OG_ASSUME_NONNULL_END

#endif /* OGClosure_h */
Loading
Loading