Skip to content
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

Add Mint #13

Merged
merged 3 commits into from
Apr 26, 2023
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
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ swift_imgui_src := Sources/ImGui
release_dir := .build/release
autowrapper_assets := Sources/AutoWrapper/Assets

.PHONY: lint
lint:
swiftlint autocorrect --format
swiftlint lint --quiet
SWIFT_PACKAGE_VERSION := $(shell swift package tools-version)

# Lint fix and format code.
.PHONY: lint-fix
lint-fix:
mint run swiftlint --fix --quiet
mint run swiftformat --quiet --swiftversion ${SWIFT_PACKAGE_VERSION} .

.PHONY: setupEnv
setupEnv:
Expand Down
2 changes: 2 additions & 0 deletions Mintfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
realm/[email protected]
nicklockwood/[email protected]
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PackageDescription
var package = Package(
name: "ImGui",
products: [
.library(name: "ImGui", targets: ["ImGui"])
.library(name: "ImGui", targets: ["ImGui"]),
],
targets: [
.target(name: "ImGui", dependencies: ["CImGui"]),
Expand All @@ -15,9 +15,9 @@ var package = Package(
linkerSettings: [.linkedLibrary("m", .when(platforms: [.linux]))]),
.target(name: "AutoWrapper",
resources: [
.copy("Assets/definitions.json")
.copy("Assets/definitions.json"),
]),
.testTarget(name: "ImGuiTests", dependencies: ["ImGui"])
.testTarget(name: "ImGuiTests", dependencies: ["ImGui"]),
],
cLanguageStandard: .c11,
cxxLanguageStandard: .cxx11
Expand All @@ -27,6 +27,6 @@ package.products.append(.executable(name: "DemoMinimal", targets: ["DemoMinimal"
package.targets.append(.target(name: "DemoMinimal", dependencies: ["ImGui"], path: "Sources/Demos/Minimal"))

#if canImport(Metal) && os(macOS)
package.products.append(.executable(name: "DemoMetal-macOS", targets: ["DemoMetal"]))
package.targets.append(.target(name: "DemoMetal", dependencies: ["ImGui"], path: "Sources/Demos/Metal"))
package.products.append(.executable(name: "DemoMetal-macOS", targets: ["DemoMetal"]))
package.targets.append(.target(name: "DemoMetal", dependencies: ["ImGui"], path: "Sources/Demos/Metal"))
#endif
37 changes: 19 additions & 18 deletions Sources/AutoWrapper/ArgT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,31 @@ public struct ArgType: Decodable {

// const
if let range = raw.range(of: "const") {
self.isConst = true
isConst = true
raw.removeSubrange(range)
raw = raw.replacingOccurrences(of: "const", with: "")
raw = raw.trimmingCharacters(in: .whitespaces)
} else {
self.isConst = false
isConst = false
}

// unsigned
if let unsigned = raw.range(of: "unsigned") {
self.isUnsigned = true
isUnsigned = true
raw.removeSubrange(unsigned)
raw = raw.trimmingCharacters(in: .whitespaces)
} else {
self.isUnsigned = false
isUnsigned = false
}

precondition(!raw.contains("const"))
precondition(!raw.contains("unsigned"))
self.type = DataType(string: raw)
type = DataType(string: raw)
}
}
extension ArgType: Equatable { }
extension ArgType: Hashable { }

extension ArgType: Equatable {}
extension ArgType: Hashable {}

public struct ArgsT: Decodable {
public let escapedName: String
Expand All @@ -53,7 +54,7 @@ public struct ArgsT: Decodable {
public let signature: String?

private let escapingCallbackExceptions: Set<String> = [
"ImGuiErrorLogCallback"
"ImGuiErrorLogCallback",
]

public enum Keys: String, CodingKey {
Expand All @@ -66,9 +67,9 @@ public struct ArgsT: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
let rawName = try container.decode(String.self, forKey: .name)
self.type = try container.decode(DataType.self, forKey: .type)
type = try container.decode(DataType.self, forKey: .type)

self.name = rawName
name = rawName
let escapedName = rawName.swiftEscaped
switch escapedName {
case "...":
Expand All @@ -77,24 +78,24 @@ public struct ArgsT: Decodable {
self.escapedName = escapedName
}

self.ret = try container.decodeIfPresent(String.self, forKey: .ret)
self.signature = try container.decodeIfPresent(String.self, forKey: .signature)
ret = try container.decodeIfPresent(String.self, forKey: .ret)
signature = try container.decodeIfPresent(String.self, forKey: .signature)
}

@inlinable public var isValid: Bool {
type.isValid && name != "..."
}

public var toSwift: String {
switch self.type.type {
switch type.type {
case let .custom(name) where name.hasSuffix("Callback") && escapedName.contains("callback")
&& !escapingCallbackExceptions.contains(name):
return "_ \(escapedName): @escaping \(self.type.toString(self, .argSwift))"
&& !escapingCallbackExceptions.contains(name):
return "_ \(escapedName): @escaping \(type.toString(self, .argSwift))"
default:
return "_ \(escapedName): \(self.type.toString(self, .argSwift, defaultArg: true))"
return "_ \(escapedName): \(type.toString(self, .argSwift, defaultArg: true))"
}
}
}

extension ArgsT: Equatable { }
extension ArgsT: Hashable { }
extension ArgsT: Equatable {}
extension ArgsT: Hashable {}
8 changes: 4 additions & 4 deletions Sources/AutoWrapper/Converter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
// Created by Christian Treffs on 25.10.19.
//

import struct Foundation.URL
import struct Foundation.Data
import class Foundation.JSONDecoder
import struct Foundation.URL

public func convert(filePath: String, validOnly: Bool, to convertedOutput: (String) throws -> Void = { print($0) }) throws {
let file = URL(fileURLWithPath: filePath)
let data: Data = try Data(contentsOf: file)
let decoder = JSONDecoder()

let defs = try decoder.decode(Definitions.self, from: data)
var invalidFuncsCount: Int = 0
var validFuncsCount: Int = 0
var invalidFuncsCount = 0
var validFuncsCount = 0

let getValidFunctionDefs: (Definition) -> Set<FunctionDef> = { defs in
let valid = defs.validFunctions
Expand All @@ -32,7 +32,7 @@ public func convert(filePath: String, validOnly: Bool, to convertedOutput: (Stri
.flatMap { $0 }
.flatMap { getFunctionDefs($0) }
.sorted()
.map { $0.toSwift }
.map(\.toSwift)
.joined(separator: "\n\n")

defer {
Expand Down
71 changes: 36 additions & 35 deletions Sources/AutoWrapper/DataType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,72 +54,71 @@ public struct DataType: Decodable {

if let primitive = ValueType(rawValue: string) {
// primitive types int, char, float ....
self.type = primitive
self.meta = .primitive
type = primitive
meta = .primitive
return
}

if let startFixArr = string.firstIndex(of: "["), let endFixArr = string.firstIndex(of: "]") {
// i.e. float[4]
let numRange = string.index(after: startFixArr)..<endFixArr
let numRange = string.index(after: startFixArr) ..< endFixArr
let count = Int(string[numRange]) ?? -1
let dataType = DataType(string: String(string[string.startIndex..<startFixArr]))
let dataType = DataType(string: String(string[string.startIndex ..< startFixArr]))

if count == -1 {
self.meta = .array
self.type = dataType.type
meta = .array
type = dataType.type
} else {
self.meta = .arrayFixedSize(count)
self.type = dataType.type
meta = .arrayFixedSize(count)
type = dataType.type
}
} else if let firstAsterisk = string.firstIndex(of: "*") {
// TODO: parse complex types

guard let lastAsertisk = string.lastIndex(of: "*") else {
assertionFailure("should not happen since we already tested for at least one asterisk")
self.meta = .unknown
self.type = .unknown
meta = .unknown
type = .unknown
return
}

if firstAsterisk == lastAsertisk {
// only one '*' present -> simple pointer
let dataType = DataType(string: String(string[string.startIndex..<firstAsterisk].trimmingCharacters(in: .whitespaces)))
let dataType = DataType(string: String(string[string.startIndex ..< firstAsterisk].trimmingCharacters(in: .whitespaces)))

switch dataType.meta {
case .exception:
self.meta = dataType.meta
self.type = dataType.type
meta = dataType.meta
type = dataType.type

default:
self.meta = .pointer
self.type = dataType.type
meta = .pointer
type = dataType.type
}
} else {
// TODO: handle array pointer
self.type = .unknown
self.meta = .unknown
type = .unknown
meta = .unknown
}
} else if let ref = string.firstIndex(of: "&") {
// i.e. float&, ImVector&
let dataType = DataType(string: String(string[string.startIndex..<ref]))
self.meta = .reference
self.type = dataType.type
let dataType = DataType(string: String(string[string.startIndex ..< ref]))
meta = .reference
type = dataType.type
} else {
// primitive custom types ImVec2, ImVector, T ...

if let exceptionType = Exceptions.undeclardTypes[string] {
self.meta = .exception(exceptionType)
self.type = .custom(string)
meta = .exception(exceptionType)
type = .custom(string)
return
}

self.meta = .primitive
self.type = .custom(string)
meta = .primitive
type = .custom(string)
}

// FIXME: special handle 'T'

}

public enum Context {
Expand All @@ -144,16 +143,16 @@ public struct DataType: Decodable {
return "inout [\(toWrap)]"
// return "inout UnsafePointer<\(toWrap)>!"
case let .arrayFixedSize(size) where isConst == false:
if type.isNumber && size < 5 {
if type.isNumber, size < 5 {
// SIMD type
return "inout SIMD\(size)<\(toWrap)>"
} else {
// tuple
return "inout (\((0..<size).map({ _ in toWrap }).joined(separator: ",")))"
return "inout (\((0 ..< size).map { _ in toWrap }.joined(separator: ",")))"
}

case let .arrayFixedSize(size):
return "(\((0..<size).map({ _ in toWrap }).joined(separator: ",")))"
return "(\((0 ..< size).map { _ in toWrap }.joined(separator: ",")))"
case .pointer where isConst == true && type == .char:
// const char* -> String
return toWrap
Expand Down Expand Up @@ -187,7 +186,7 @@ public struct DataType: Decodable {
}
}

public func toString(_ argsT: ArgsT?, _ context: Context, wrapped: Bool = true, defaultArg: Bool = false) -> String {
public func toString(_: ArgsT?, _ context: Context, wrapped: Bool = true, defaultArg: Bool = false) -> String {
let out: String

switch type {
Expand Down Expand Up @@ -232,12 +231,13 @@ public struct DataType: Decodable {
}
}

extension DataType: Equatable { }
extension DataType: Hashable { }
extension DataType: Equatable {}
extension DataType: Hashable {}

// MARK: - MetaType
extension DataType {
public enum MetaType: Equatable, Hashable {

public extension DataType {
enum MetaType: Equatable, Hashable {
case primitive
case arrayFixedSize(Int)
case array
Expand All @@ -250,8 +250,9 @@ extension DataType {
}

// MARK: - Value Type
extension DataType {
public enum ValueType: Equatable, Hashable {

public extension DataType {
enum ValueType: Equatable, Hashable {
case void
case bool
case int
Expand Down
Loading