Skip to content

Commit 900a721

Browse files
committed
Add OGGraphArchiveJSON2 and update OGDescription
1 parent c46bb60 commit 900a721

File tree

13 files changed

+156
-76
lines changed

13 files changed

+156
-76
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ let openGraphSPITarget = Target.target(
147147
name: "OpenGraph_SPI",
148148
cSettings: sharedCSettings + [
149149
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
150+
],
151+
linkerSettings: [
152+
.linkedLibrary("z"),
150153
]
151154
)
152155
let openGraphShimsTarget = Target.target(

Sources/OpenGraphShims/Graph+Debug.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import Foundation
99
@_spi(Debug)
1010
extension Graph {
1111
public var dict: [String: Any]? {
12-
let options = ["format": "graph/dict"] as NSDictionary
12+
let options = [
13+
Graph.descriptionFormat: Graph.descriptionFormatDictionary
14+
] as NSDictionary
1315
guard let description = Graph.description(nil, options: options) else {
1416
return nil
1517
}
@@ -25,7 +27,9 @@ extension Graph {
2527
// color:
2628
// - red: is_changed
2729
public var dot: String? {
28-
let options = ["format": "graph/dot"] as NSDictionary
30+
let options = [
31+
Graph.descriptionFormat: Graph.descriptionFormatDot
32+
] as NSDictionary
2933
guard let description = Graph.description(self, options: options)
3034
else {
3135
return nil

Sources/OpenGraph_SPI/Debug/og-debug-server.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
NSString *command = dic[@"command"];
203203
if ([command isEqual:@"graph/description"]) {
204204
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:dic];
205-
mutableDic[(__bridge NSString *)OGDescriptionFormat] = @"graph/dict";
205+
mutableDic[(__bridge NSString *)OGDescriptionFormat] = (__bridge NSString *)OGDescriptionFormatDictionary;
206206
CFTypeRef description = OG::Graph::description(nullptr, mutableDic);
207207
if (description) {
208208
NSData *descriptionData = [NSJSONSerialization dataWithJSONObject:(__bridge id)description options:0 error:NULL];

Sources/OpenGraph_SPI/Graph/Graph.cpp

Lines changed: 0 additions & 67 deletions
This file was deleted.

Sources/OpenGraph_SPI/Graph/Graph.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class Graph final {
123123
void start_profiling(uint32_t) OG_NOEXCEPT;
124124
void stop_profiling() OG_NOEXCEPT;
125125

126-
static void write_to_file(const Graph * _Nullable, const char * _Nullable) OG_NOEXCEPT;
126+
static void write_to_file(const Graph * _Nullable, const char * _Nullable, uint8_t) OG_NOEXCEPT;
127127

128128
const bool thread_is_updating() const OG_NOEXCEPT;
129129
const bool is_context_updating(const OG::Graph::Context&) const OG_NOEXCEPT;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//
2+
// Graph.mm
3+
// OpenGraph_SPI
4+
5+
#include "Graph.hpp"
6+
#include "Subgraph.hpp"
7+
#include "OGGraphDescription.h"
8+
9+
#if OG_OBJC_FOUNDATION
10+
#include <dispatch/dispatch.h>
11+
#include <pthread.h>
12+
#include <Foundation/Foundation.h>
13+
#include <zlib.h>
14+
15+
pthread_key_t OG::Graph::_current_update_key;
16+
17+
OG::Graph::Graph() OG_NOEXCEPT {
18+
// TODO
19+
20+
// libdispatch is not supported on WASI
21+
// Tracked via https://github.com/swiftwasm/swift/issues/5565
22+
#if !OG_TARGET_OS_WASI
23+
static dispatch_once_t make_keys;
24+
dispatch_once_f(&make_keys, nullptr, [](void * _Nullable context){
25+
pthread_key_create(&_current_update_key, nullptr);
26+
OG::Subgraph::make_current_subgraph_key();
27+
});
28+
#endif
29+
30+
// TODO
31+
}
32+
33+
const void OG::Graph::value_mark_all() const OG_NOEXCEPT {
34+
// TODO
35+
}
36+
37+
void OG::Graph::all_start_profiling(uint32_t) OG_NOEXCEPT {
38+
// TODO
39+
}
40+
void OG::Graph::all_stop_profiling() OG_NOEXCEPT {
41+
// TODO
42+
}
43+
void OG::Graph::start_profiling(uint32_t) OG_NOEXCEPT {
44+
// TODO
45+
}
46+
void OG::Graph::stop_profiling() OG_NOEXCEPT {
47+
// TODO
48+
}
49+
50+
void OG::Graph::write_to_file(const Graph * _Nullable graph, const char * _Nullable name, uint8_t options) OG_NOEXCEPT {
51+
@autoreleasepool {
52+
NSDictionary <NSString *, id> *options_dict = @{
53+
(__bridge NSString *)OGDescriptionFormat: (__bridge NSString *)OGDescriptionFormatDictionary,
54+
(__bridge NSString *)OGDescriptionIncludeValues: @((options & 1) == 0),
55+
(__bridge NSString *)OGDescriptionAllGraphs: @(graph == nil)
56+
};
57+
NSString *description = (__bridge NSString *)Graph::description(graph, options_dict);
58+
if (description == nil) {
59+
return;
60+
}
61+
const char *cstring_name = name ?: "graph.ag-gzon";
62+
63+
NSData *data = [NSJSONSerialization dataWithJSONObject:description options:0 error:nil];
64+
NSString *file_path = [NSString stringWithUTF8String:cstring_name];
65+
if (cstring_name[0] != '/') {
66+
file_path = [NSTemporaryDirectory() stringByAppendingPathComponent:file_path];
67+
}
68+
NSError *error = nil;
69+
BOOL success = YES;
70+
if ([file_path.pathExtension isEqualToString:@"ag-gzon"]) {
71+
gzFile file = gzopen(file_path.fileSystemRepresentation, "wb");
72+
if (file != NULL) {
73+
const char *bytes = (const char *)[data bytes];
74+
NSUInteger remaining = [data length];
75+
while (remaining != 0) {
76+
int bytes_written = gzwrite(file, bytes, (unsigned int)remaining);
77+
if (bytes_written <= 0) {
78+
success = NO;
79+
break;
80+
}
81+
bytes += bytes_written;
82+
remaining -= bytes_written;
83+
}
84+
gzclose(file);
85+
}
86+
} else {
87+
success = [data writeToFile:file_path options:0 error:&error];
88+
}
89+
if (success) {
90+
fprintf(stderr, "Wrote graph data to \"%s\".\n", file_path.UTF8String);
91+
} else {
92+
fprintf(stderr, "Unable to write to \"%s\": %s\n", file_path.UTF8String, error.description.UTF8String);
93+
}
94+
}
95+
}
96+
97+
const bool OG::Graph::thread_is_updating() const OG_NOEXCEPT {
98+
void *current = pthread_getspecific(current_key());
99+
if (!current) {
100+
return false;
101+
}
102+
// TODO
103+
return false;
104+
}
105+
106+
const bool OG::Graph::is_context_updating(const OG::Graph::Context&) const OG_NOEXCEPT {
107+
// TODO
108+
return false;
109+
}
110+
#endif /* OG_OBJC_FOUNDATION */

Sources/OpenGraph_SPI/Graph/GraphDescription.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
#include "../Util/assert.hpp"
1212

1313
#if OG_OBJC_FOUNDATION
14+
1415
#include <Foundation/Foundation.h>
16+
1517
const CFStringRef OGDescriptionFormat = CFSTR("format");
18+
const CFStringRef OGDescriptionIncludeValues = CFSTR("include-values");
1619

1720
CFTypeRef OG::Graph::description(const Graph * _Nullable graph, NSDictionary* dic) {
1821
// TODO

Sources/OpenGraph_SPI/Graph/OGGraph.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ OGGraphRef OGGraphCreateShared(OGGraphRef storage) {
4242
}
4343

4444
void OGGraphArchiveJSON(char const * _Nullable name) {
45-
OG::Graph::write_to_file(nullptr, name);
45+
OG::Graph::write_to_file(nullptr, name, 0);
46+
}
47+
48+
void OGGraphArchiveJSON2(char const * _Nullable name, uint8_t options) {
49+
OG::Graph::write_to_file(nullptr, name, options);
4650
}
4751

4852
namespace {

Sources/OpenGraph_SPI/include/OGDebugServer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ typedef const OGDebugServerStorage *OGDebugServer OG_SWIFT_STRUCT;
2020
// MARK: - Exported C functions
2121

2222
OG_EXTERN_C_BEGIN
23+
2324
OG_EXPORT
2425
OGDebugServer _Nullable OGDebugServerStart(unsigned int mode) OG_SWIFT_NAME(OGDebugServer.start(mode:));
26+
2527
OG_EXPORT
2628
void OGDebugServerStop(void) OG_SWIFT_NAME(OGDebugServer.stop());
29+
2730
OG_EXPORT
2831
CFURLRef _Nullable OGDebugServerCopyURL(void) OG_SWIFT_NAME(OGDebugServer.copyURL());
32+
2933
OG_EXPORT
3034
void OGDebugServerRun(int timeout) OG_SWIFT_NAME(OGDebugServer.run(timeout:));
35+
3136
OG_EXTERN_C_END
3237

3338
OG_IMPLICIT_BRIDGING_DISABLED

0 commit comments

Comments
 (0)