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
4 changes: 2 additions & 2 deletions Package.resolved

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

3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ let openGraphSPITarget = Target.target(
name: "OpenGraph_SPI",
cSettings: sharedCSettings + [
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
],
linkerSettings: [
.linkedLibrary("z"),
]
)
let openGraphShimsTarget = Target.target(
Expand Down
30 changes: 30 additions & 0 deletions Sources/OpenGraph/Graph/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,33 @@ extension Graph {
@inlinable
public static func setOutputValue<Value>(_ value: UnsafePointer<Value>)
}

#if canImport(Darwin)
import Foundation
#endif

extension Graph {
public func archiveJSON(name: String?) {
#if canImport(Darwin)
let options: NSDictionary = [
Graph.descriptionFormat: "graph/dict",
Graph.descriptionIncludeValues: true,
]
guard let description = Graph.description(self, options: options) as? [String: Any] else {
return
}
var name = name ?? "graph"
name.append(".ag-json")
let path = (NSTemporaryDirectory() as NSString).appendingPathComponent(name)
guard let data = try? JSONSerialization.data(withJSONObject: description, options: []) else {
return
}
let url = URL(fileURLWithPath: path)
do {
try data.write(to: url, options: [])
print("Wrote graph data to \"\(path)\".")
} catch {
}
#endif
}
}
8 changes: 6 additions & 2 deletions Sources/OpenGraphShims/Graph+Debug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import Foundation
@_spi(Debug)
extension Graph {
public var dict: [String: Any]? {
let options = ["format": "graph/dict"] as NSDictionary
let options = [
Graph.descriptionFormat: Graph.descriptionFormatDictionary
] as NSDictionary
guard let description = Graph.description(nil, options: options) else {
return nil
}
Expand All @@ -25,7 +27,9 @@ extension Graph {
// color:
// - red: is_changed
public var dot: String? {
let options = ["format": "graph/dot"] as NSDictionary
let options = [
Graph.descriptionFormat: Graph.descriptionFormatDot
] as NSDictionary
guard let description = Graph.description(self, options: options)
else {
return nil
Expand Down
6 changes: 3 additions & 3 deletions Sources/OpenGraph_SPI/Attribute/AttributeID.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AttributeID final {
enum class Kind: uint8_t {
Direct = 0x0,
Indirect = 0x1,
Nil = 0x2,
Null = 0x2,
};
private:
uint32_t _rawValue;
Expand Down Expand Up @@ -59,8 +59,8 @@ class AttributeID final {
}

OG_INLINE OG_CONSTEXPR
const bool isNil() const OG_NOEXCEPT {
return getKind() == Kind::Nil;
const bool isNull() const OG_NOEXCEPT {
return getKind() == Kind::Null;
}

OG_INLINE OG_CONSTEXPR
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenGraph_SPI/Attribute/OGAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "../Util/assert.hpp"
#include <optional>

const OGAttribute OGAttributeNil = OGAttribute(OG::AttributeID::Kind::Nil);
const OGAttribute OGAttributeNil = OGAttribute(OG::AttributeID::Kind::Null);

OGAttribute OGGraphGetCurrentAttribute() {
// TODO
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenGraph_SPI/Debug/og-debug-server.mm
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
NSString *command = dic[@"command"];
if ([command isEqual:@"graph/description"]) {
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:dic];
mutableDic[(__bridge NSString *)OGDescriptionFormat] = @"graph/dict";
mutableDic[(__bridge NSString *)OGDescriptionFormat] = (__bridge NSString *)OGDescriptionFormatDictionary;
CFTypeRef description = OG::Graph::description(nullptr, mutableDic);
if (description) {
NSData *descriptionData = [NSJSONSerialization dataWithJSONObject:(__bridge id)description options:0 error:NULL];
Expand Down
15 changes: 5 additions & 10 deletions Sources/OpenGraph_SPI/Graph/Graph.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
//
// Graph.cpp
//
//
// Created by Kyle on 2024/1/18.
//
// OpenGraph_SPI

#include "Graph.hpp"
#include "Subgraph.hpp"
#include "OGGraphDescription.h"

#if !OG_TARGET_OS_WASI
#include <dispatch/dispatch.h>
#endif

#include <pthread.h>

pthread_key_t OG::Graph::_current_update_key;

OG::Graph::Graph() OG_NOEXCEPT {
// TODO

// libdispatch is not supported on WASI
// Tracked via https://github.com/swiftwasm/swift/issues/5565
#if !OG_TARGET_OS_WASI
Expand All @@ -27,7 +26,7 @@ OG::Graph::Graph() OG_NOEXCEPT {
OG::Subgraph::make_current_subgraph_key();
});
#endif

// TODO
}

Expand All @@ -48,10 +47,6 @@ void OG::Graph::stop_profiling() OG_NOEXCEPT {
// TODO
}

void OG::Graph::write_to_file(const Graph * _Nullable, const char * _Nullable) OG_NOEXCEPT {
// TODO
}

const bool OG::Graph::thread_is_updating() const OG_NOEXCEPT {
void *current = pthread_getspecific(current_key());
if (!current) {
Expand Down
14 changes: 7 additions & 7 deletions Sources/OpenGraph_SPI/Graph/Graph.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//
// Graph.hpp
//
//
// Created by Kyle on 2024/1/18.
//
// OpenGraph_SPI


#ifndef Graph_hpp
#define Graph_hpp
Expand Down Expand Up @@ -122,9 +120,11 @@ class Graph final {
static void all_stop_profiling() OG_NOEXCEPT;
void start_profiling(uint32_t) OG_NOEXCEPT;
void stop_profiling() OG_NOEXCEPT;

static void write_to_file(const Graph * _Nullable, const char * _Nullable) OG_NOEXCEPT;


#if OG_OBJC_FOUNDATION
static void write_to_file(const Graph * _Nullable, const char * _Nullable, uint8_t) OG_NOEXCEPT;
#endif

const bool thread_is_updating() const OG_NOEXCEPT;
const bool is_context_updating(const OG::Graph::Context&) const OG_NOEXCEPT;

Expand Down
59 changes: 59 additions & 0 deletions Sources/OpenGraph_SPI/Graph/Graph.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Graph.mm
// OpenGraph_SPI

#include "Graph.hpp"
#include "OGGraphDescription.h"

#if OG_OBJC_FOUNDATION
#include <Foundation/Foundation.h>
#include <zlib.h>

void OG::Graph::write_to_file(const Graph * _Nullable graph, const char * _Nullable name, uint8_t options) OG_NOEXCEPT {
@autoreleasepool {
NSDictionary <NSString *, id> *options_dict = @{
(__bridge NSString *)OGDescriptionFormat: (__bridge NSString *)OGDescriptionFormatDictionary,
(__bridge NSString *)OGDescriptionIncludeValues: @((options & 1) == 0),
(__bridge NSString *)OGDescriptionAllGraphs: @(graph == nil)
};
NSString *description = (__bridge NSString *)Graph::description(graph, options_dict);
if (description == nil) {
return;
}
const char *cstring_name = name ?: "graph.ag-gzon";

NSData *data = [NSJSONSerialization dataWithJSONObject:description options:0 error:nil];
NSString *file_path = [NSString stringWithUTF8String:cstring_name];
if (cstring_name[0] != '/') {
file_path = [NSTemporaryDirectory() stringByAppendingPathComponent:file_path];
}
NSError *error = nil;
BOOL success = YES;
if ([file_path.pathExtension isEqualToString:@"ag-gzon"]) {
gzFile file = gzopen(file_path.fileSystemRepresentation, "wb");
if (file != NULL) {
const char *bytes = (const char *)[data bytes];
NSUInteger remaining = [data length];
while (remaining != 0) {
int bytes_written = gzwrite(file, bytes, (unsigned int)remaining);
if (bytes_written <= 0) {
success = NO;
break;
}
bytes += bytes_written;
remaining -= bytes_written;
}
gzclose(file);
}
} else {
success = [data writeToFile:file_path options:0 error:&error];
}
if (success) {
fprintf(stderr, "Wrote graph data to \"%s\".\n", file_path.UTF8String);
} else {
fprintf(stderr, "Unable to write to \"%s\": %s\n", file_path.UTF8String, error.description.UTF8String);
}
}
}

#endif /* OG_OBJC_FOUNDATION */
33 changes: 33 additions & 0 deletions Sources/OpenGraph_SPI/Graph/GraphDescription.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// GraphDescription.cpp
// OpenGraph_SPI

#include "OGGraphDescription.h"
#include "OGGraph.h"
#include "Graph.hpp"
#include "../Util/assert.hpp"

CFTypeRef OGGraphDescription(OGGraphRef graph, CFDictionaryRef options) {
#if OG_OBJC_FOUNDATION
if (graph == nullptr) {
return OG::Graph::description(nullptr, (__bridge NSDictionary*)options);
}
if (graph->context.isInvalid()) {
OG::precondition_failure("invalidated graph");
}
return OG::Graph::description(&graph->context.get_graph(), (__bridge NSDictionary*)options);
#endif
}

void OGGraphArchiveJSON(char const * _Nullable name) {
#if OG_OBJC_FOUNDATION
OG::Graph::write_to_file(nullptr, name, 0);
#endif
}

void OGGraphArchiveJSON2(char const * _Nullable name, uint8_t options) {
#if OG_OBJC_FOUNDATION
OG::Graph::write_to_file(nullptr, name, options);
#endif
}

18 changes: 4 additions & 14 deletions Sources/OpenGraph_SPI/Graph/GraphDescription.mm
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//
// GraphDescription.mm
//
//
// Created by Kyle on 2024/1/21.
//
// OpenGraph_SPI

#include "OGGraphDescription.h"
#include "OGGraph.h"
#include "Graph.hpp"
#include "../Util/assert.hpp"

#if OG_OBJC_FOUNDATION

#include <Foundation/Foundation.h>

const CFStringRef OGDescriptionFormat = CFSTR("format");
const CFStringRef OGDescriptionIncludeValues = CFSTR("include-values");

CFTypeRef OG::Graph::description(const Graph * _Nullable graph, NSDictionary* dic) {
// TODO
Expand All @@ -21,14 +21,4 @@
return NULL;
}

CFTypeRef OGGraphDescription(OGGraphRef graph, CFDictionaryRef options) {
if (graph == nullptr) {
return OG::Graph::description(nullptr, (__bridge NSDictionary*)options);
}
if (graph->context.isInvalid()) {
OG::precondition_failure("invalidated graph");
}
return OG::Graph::description(&graph->context.get_graph(), (__bridge NSDictionary*)options);
}

#endif /* OG_OBJC_FOUNDATION */
4 changes: 0 additions & 4 deletions Sources/OpenGraph_SPI/Graph/OGGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ OGGraphRef OGGraphCreateShared(OGGraphRef storage) {
return instance;
}

void OGGraphArchiveJSON(char const * _Nullable name) {
OG::Graph::write_to_file(nullptr, name);
}

namespace {
CFRuntimeClass &graph_type_id() {
static auto dealloc = [](const void* ptr) {
Expand Down
5 changes: 5 additions & 0 deletions Sources/OpenGraph_SPI/include/OGDebugServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ typedef const OGDebugServerStorage *OGDebugServer OG_SWIFT_STRUCT;
// MARK: - Exported C functions

OG_EXTERN_C_BEGIN

OG_EXPORT
OGDebugServer _Nullable OGDebugServerStart(unsigned int mode) OG_SWIFT_NAME(OGDebugServer.start(mode:));

OG_EXPORT
void OGDebugServerStop(void) OG_SWIFT_NAME(OGDebugServer.stop());

OG_EXPORT
CFURLRef _Nullable OGDebugServerCopyURL(void) OG_SWIFT_NAME(OGDebugServer.copyURL());

OG_EXPORT
void OGDebugServerRun(int timeout) OG_SWIFT_NAME(OGDebugServer.run(timeout:));

OG_EXTERN_C_END

OG_IMPLICIT_BRIDGING_DISABLED
Expand Down
8 changes: 0 additions & 8 deletions Sources/OpenGraph_SPI/include/OGGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ OG_EXPORT
OG_REFINED_FOR_SWIFT
OGGraphRef OGGraphCreateShared(_Nullable OGGraphRef graph) OG_SWIFT_NAME(OGGraphRef.init(shared:));

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGGraphArchiveJSON(char const * _Nullable name) OG_SWIFT_NAME(OGGraphRef.archiveJSON(name:));

OG_EXPORT
OG_REFINED_FOR_SWIFT
_Nullable CFTypeRef OGGraphDescription(_Nullable OGGraphRef graph, CFDictionaryRef options) OG_SWIFT_NAME(OGGraphRef.description(_:options:));

OG_EXPORT
OG_REFINED_FOR_SWIFT
CFTypeID OGGraphGetTypeID(void) OG_SWIFT_NAME(getter:OGGraphRef.typeID());
Expand Down
Loading