Skip to content

Commit

Permalink
Fix (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-semyonov authored Oct 7, 2024
1 parent 44b7e26 commit d2feb88
Show file tree
Hide file tree
Showing 15 changed files with 1,194 additions and 1,193 deletions.
16 changes: 8 additions & 8 deletions Sources/SwiftGodot/Core/GArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public enum ArrayError {
extension GArray {
/// Initializes an empty, but typed `GArray`. For example: `GArray(Node.self)`
/// - Parameter type: `T` the type of the elements in the GArray, must conform to `VariantStorable`.
public convenience init<T: VariantStorable>(_ type: T.Type = T.self) {
self.init(
base: GArray(),
type: Int32(T.Representable.godotType.rawValue),
className: T.Representable.godotType == .object ? StringName("\(T.self)") : StringName(),
script: Variant()
)
}
public convenience init<T: VariantStorable>(_ type: T.Type = T.self) {
self.init(
base: GArray(),
type: Int32(T.Representable.godotType.rawValue),
className: T.Representable.godotType == .object ? StringName("\(T.self)") : StringName(),
script: Variant()
)
}

public subscript (index: Int) -> Variant {
get {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGodot/Core/GArrayCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

// If our exported Collections conform to this protocol, then we can use the same Macro code while exporting them
protocol GArrayCollection: Collection where Element: VariantStorable {
var array: GArray { set get }
var array: GArray { set get }
}
14 changes: 7 additions & 7 deletions Sources/SwiftGodot/Core/ObjectCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extension ObjectCollection: VariantStorable {

/// This represents a typed array of one of the built-in types from Godot
public class ObjectCollection<Element: Object>: Collection, ExpressibleByArrayLiteral, GArrayCollection {
public typealias ArrayLiteralElement = Element
public typealias ArrayLiteralElement = Element
/// The underlying GArray, passed to the Godot client, and reassigned by the Godot client via the proxy accessors
/// In general you should not be modifying this property directly
public var array: GArray
Expand All @@ -34,13 +34,13 @@ public class ObjectCollection<Element: Object>: Collection, ExpressibleByArrayLi
// Array took a reference, we do not need to take it.
GArray.destructor (&copy)
}
/// Initializes the collection using an array literal, for example: `let objectCollection: ObjectCollection<Node> = [Node()]`
public required init(arrayLiteral elements: ArrayLiteralElement...) {
array = elements.reduce(into: .init(Element.self)) {
public required init(arrayLiteral elements: ArrayLiteralElement...) {
array = elements.reduce(into: .init(Element.self)) {
$0.append(Variant($1))
}
}
}
}

/// Initializes the collection using an array
public init(_ elements: [Element]) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftGodot/Core/VariantCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class VariantCollection<Element: VariantStorable>: Collection, Expressibl

/// Initializes the collection using an array literal, for example: `let variantCollection: VariantCollection<Int> = [0]`
public required init(arrayLiteral elements: ArrayLiteralElement...) {
array = elements.reduce(into: .init(Element.self)) {
array = elements.reduce(into: .init(Element.self)) {
$0.append(Variant($1))
}
}
}

init (content: Int64) {
Expand Down
22 changes: 11 additions & 11 deletions Sources/SwiftGodot/Extensions/NodeExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public struct BindNode<Value: Node> {
) -> Value {
get {
if #available(macOS 13.3, iOS 16.4, tvOS 16.4, *) {
if let node = instance[keyPath: storageKeyPath].cachedNode {
return node as! Value
}
if let node = instance[keyPath: storageKeyPath].cachedNode {
return node as! Value
}

if !instance[keyPath: storageKeyPath].path.isEmpty {
let nodePath = NodePath(from: instance[keyPath: storageKeyPath].path)
instance[keyPath: storageKeyPath].cachedNode = instance.getNode(path: nodePath)
return instance[keyPath: storageKeyPath].cachedNode as! Value
}
if !instance[keyPath: storageKeyPath].path.isEmpty {
let nodePath = NodePath(from: instance[keyPath: storageKeyPath].path)
instance[keyPath: storageKeyPath].cachedNode = instance.getNode(path: nodePath)
return instance[keyPath: storageKeyPath].cachedNode as! Value
}

let name: String
let fullName = wrappedKeyPath.debugDescription
Expand All @@ -52,7 +52,7 @@ public struct BindNode<Value: Node> {
}
let nodePath = NodePath(from: name)

instance[keyPath: storageKeyPath].cachedNode = instance.getNode(path: nodePath)
instance[keyPath: storageKeyPath].cachedNode = instance.getNode(path: nodePath)
return instance[keyPath: storageKeyPath].cachedNode as! Value
} else {
fatalError ("BindNode is not supported with current swift, or older Mac")
Expand All @@ -74,7 +74,7 @@ public struct BindNode<Value: Node> {
fatalError()
}
}
private var cachedNode: Node?
private var path: String
private var cachedNode: Node?
private var path: String
}

14 changes: 7 additions & 7 deletions Sources/SwiftGodotMacroLibrary/MacroCallable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct GodotCallable: PeerMacro {
var genMethod = "func _mproxy_\(funcName) (args: borrowing Arguments) -> SwiftGodot.Variant? {\n"
var retProp: String? = nil
var retOptional: Bool = false
if let effects = funcDecl.signature.effectSpecifiers,
effects.asyncSpecifier?.presence == .present ||
effects.throwsSpecifier?.presence == .present {
Expand All @@ -27,10 +27,10 @@ public struct GodotCallable: PeerMacro {

if let (retType, _, ro) = getIdentifier (funcDecl.signature.returnClause?.type) {
retProp = godotTypeToProp (typeName: retType)
genMethod.append ("\tlet result = \(funcName) (")
genMethod.append (" let result = \(funcName) (")
retOptional = ro
} else {
genMethod.append ("\t\(funcName) (")
genMethod.append (" \(funcName) (")
}
// let result = computeGodot (String (args [0]), Int (args [1]))

Expand Down Expand Up @@ -69,15 +69,15 @@ public struct GodotCallable: PeerMacro {
genMethod.append (")\n")
if retProp != nil {
if retOptional {
genMethod.append ("\tguard let result else { return nil }\n")
genMethod.append (" guard let result else { return nil }\n")
}
if funcDecl.returnTypeIsArray, let elementTypeName = funcDecl.arrayElementType {
genMethod.append ("\treturn Variant ( result.reduce(into: GArray(\(elementTypeName).self)) { $0.append(Variant($1)) })\n")
genMethod.append (" return Variant ( result.reduce(into: GArray(\(elementTypeName).self)) { $0.append(Variant($1)) })\n")
} else {
genMethod.append ("\treturn Variant (result)\n")
genMethod.append (" return Variant (result)\n")
}
} else {
genMethod.append ("\treturn nil\n")
genMethod.append (" return nil\n")
}
if genMethod != "" {
genMethod.append("}\n")
Expand Down
58 changes: 29 additions & 29 deletions Sources/SwiftGodotMacroLibrary/MacroExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public struct GodotExport: PeerMacro {
}
"""
} else if typeName == "Variant" {
body = "\(varName) = args [0]"
} else if godotVariants [typeName] == nil {
body = "\(varName) = args [0]"
} else if godotVariants [typeName] == nil {
let optBody = isOptional ? " else { \(varName) = nil }" : ""

// The use of the local function dynamicCast here is such that the compiler
Expand All @@ -77,23 +77,23 @@ public struct GodotExport: PeerMacro {
if isOptional {
body =
"""
\(varName) = \(typeName) (args [0])
\(varName) = \(typeName) (args [0])
"""
} else {
body =
"""
guard let arg = args.first else {
return nil
}
if let value = \(typeName) (arg) {
self.\(varName) = value
} else {
GD.printErr ("Unable to set `\(varName)` value: ", arg)
}
guard let arg = args.first else {
return nil
}
if let value = \(typeName) (arg) {
self.\(varName) = value
} else {
GD.printErr ("Unable to set `\(varName)` value: ", arg)
}
"""
}
}
return "func \(name) (args: borrowing Arguments) -> Variant? {\n\(body)\n\treturn nil\n}"
return "func \(name) (args: borrowing Arguments) -> Variant? {\n\(body)\n return nil\n}"
}


Expand Down Expand Up @@ -197,25 +197,25 @@ public struct GodotExport: PeerMacro {

private extension GodotExport {
private static func makeGArrayCollectionGetProxyAccessor(varName: String, elementTypeName: String) -> String {
"""
func _mproxy_get_\(varName)(args: borrowing Arguments) -> Variant? {
return Variant(\(varName).array)
}
"""
"""
func _mproxy_get_\(varName)(args: borrowing Arguments) -> Variant? {
return Variant(\(varName).array)
}
"""
}

private static func makeGArrayCollectionSetProxyAccessor(varName: String, elementTypeName: String) -> String {
"""
func _mproxy_set_\(varName)(args: borrowing Arguments) -> Variant? {
guard let arg = args.first,
let gArray = GArray(arg),
gArray.isTyped(),
gArray.isSameTyped(array: GArray(\(elementTypeName).self)) else {
return nil
}
\(varName).array = gArray
return nil
}
"""
"""
func _mproxy_set_\(varName)(args: borrowing Arguments) -> Variant? {
guard let arg = args.first,
let gArray = GArray(arg),
gArray.isTyped(),
gArray.isSameTyped(array: GArray(\(elementTypeName).self)) else {
return nil
}
\(varName).array = gArray
return nil
}
"""
}
}
36 changes: 18 additions & 18 deletions Sources/SwiftGodotMacroLibrary/MacroGodot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class GodotMacroProcessor {

// TODO: perhaps for these prop infos that are parameters to functions, we should not bother making them unique
// and instead share all the Ints, all the Floats and so on.
ctor.append ("\tlet \(name) = PropInfo (propertyType: \(propType), propertyName: \"\(parameterName)\", className: StringName(\"\(className)\"), hint: \(hint), hintStr: \"\(hintStr)\", usage: .default)\n")
ctor.append (" let \(name) = PropInfo (propertyType: \(propType), propertyName: \"\(parameterName)\", className: StringName(\"\(className)\"), hint: \(hint), hintStr: \"\(hintStr)\", usage: .default)\n")
propertyDeclarations [key] = name
return name
}
Expand Down Expand Up @@ -125,7 +125,7 @@ class GodotMacroProcessor {
let hint = propType == ".array" ? ".arrayType" : ".none"
// TODO: perhaps for these prop infos that are parameters to functions, we should not bother making them unique
// and instead share all the Ints, all the Floats and so on.
ctor.append ("\tlet \(name) = PropInfo (propertyType: \(propType), propertyName: \"\", className: StringName(\"\(className)\"), hint: \(hint), hintStr: \"\(hintStr)\", usage: .default)\n")
ctor.append (" let \(name) = PropInfo (propertyType: \(propType), propertyName: \"\", className: StringName(\"\(className)\"), hint: \(hint), hintStr: \"\(hintStr)\", usage: .default)\n")
propertyDeclarations [key] = name
return name
}
Expand Down Expand Up @@ -193,15 +193,15 @@ class GodotMacroProcessor {
parameterName: pname
)
if funcArgs == "" {
funcArgs = "\tlet \(funcName)Args = [\n"
funcArgs = " let \(funcName)Args = [\n"
}
funcArgs.append ("\t\t\(propInfo),\n")
funcArgs.append (" \(propInfo),\n")
}
if funcArgs != "" {
funcArgs.append ("\t]\n")
funcArgs.append (" ]\n")
}
ctor.append (funcArgs)
ctor.append ("\tclassInfo.registerMethod(name: StringName(\"\(funcName)\"), flags: .default, returnValue: \(retProp ?? "nil"), arguments: \(funcArgs == "" ? "[]" : "\(funcName)Args"), function: \(className)._mproxy_\(funcName))\n")
ctor.append (" classInfo.registerMethod(name: StringName(\"\(funcName)\"), flags: .default, returnValue: \(retProp ?? "nil"), arguments: \(funcArgs == "" ? "[]" : "\(funcName)Args"), function: \(className)._mproxy_\(funcName))\n")
}

// Returns true if it used "tryCase"
Expand Down Expand Up @@ -306,9 +306,9 @@ class GodotMacroProcessor {
""")

ctor.append("\tclassInfo.registerMethod (name: \"\(getterName)\", flags: .default, returnValue: \(pinfo), arguments: [], function: \(className).\(proxyGetterName))\n")
ctor.append("\tclassInfo.registerMethod (name: \"\(setterName)\", flags: .default, returnValue: nil, arguments: [\(pinfo)], function: \(className).\(proxySetterName))\n")
ctor.append("\tclassInfo.registerProperty (\(pinfo), getter: \"\(getterName)\", setter: \"\(setterName)\")\n")
ctor.append(" classInfo.registerMethod (name: \"\(getterName)\", flags: .default, returnValue: \(pinfo), arguments: [], function: \(className).\(proxyGetterName))\n")
ctor.append(" classInfo.registerMethod (name: \"\(setterName)\", flags: .default, returnValue: nil, arguments: [\(pinfo)], function: \(className).\(proxySetterName))\n")
ctor.append(" classInfo.registerProperty (\(pinfo), getter: \"\(getterName)\", setter: \"\(setterName)\")\n")
}
if usedTryCase {
return true
Expand Down Expand Up @@ -404,9 +404,9 @@ class GodotMacroProcessor {
usage: .default)\n
""")

ctor.append("\tclassInfo.registerMethod (name: \"\(getterName)\", flags: .default, returnValue: \(pinfo), arguments: [], function: \(className).\(proxyGetterName))\n")
ctor.append("\tclassInfo.registerMethod (name: \"\(setterName)\", flags: .default, returnValue: nil, arguments: [\(pinfo)], function: \(className).\(proxySetterName))\n")
ctor.append("\tclassInfo.registerProperty (\(pinfo), getter: \"\(getterName)\", setter: \"\(setterName)\")\n")
ctor.append(" classInfo.registerMethod (name: \"\(getterName)\", flags: .default, returnValue: \(pinfo), arguments: [], function: \(className).\(proxyGetterName))\n")
ctor.append(" classInfo.registerMethod (name: \"\(setterName)\", flags: .default, returnValue: nil, arguments: [\(pinfo)], function: \(className).\(proxySetterName))\n")
ctor.append(" classInfo.registerProperty (\(pinfo), getter: \"\(getterName)\", setter: \"\(setterName)\")\n")
}
}

Expand Down Expand Up @@ -436,15 +436,15 @@ class GodotMacroProcessor {
previousSubgroupPrefix = macroExpansion.exportSubgroupPrefix ?? ""
processExportSubgroup(name: name, prefix: previousSubgroupPrefix ?? "")
} else if let funcDecl = FunctionDeclSyntax(decl) {
try processFunction (funcDecl)
} else if let varDecl = VariableDeclSyntax(decl) {
if varDecl.isGArrayCollection {
try processFunction (funcDecl)
} else if let varDecl = VariableDeclSyntax(decl) {
if varDecl.isGArrayCollection {
try processGArrayCollectionVariable(varDecl, prefix: previousSubgroupPrefix ?? previousGroupPrefix)
} else {
} else {
if try processVariable(varDecl, prefix: previousSubgroupPrefix ?? previousGroupPrefix) {
needTrycase = true
}
}
}
} else if let macroDecl = MacroExpansionDeclSyntax(decl) {
try classInitSignals(macroDecl)
}
Expand Down Expand Up @@ -590,7 +590,7 @@ public struct GodotMacro: MemberMacro {
}
implementedOverridesDecl += "return super.implementedOverrides () + [\n"
for name in stringNames {
implementedOverridesDecl.append("\t\(name),\n")
implementedOverridesDecl.append(" \(name),\n")
}
implementedOverridesDecl.append("]\n}")
decls.append (DeclSyntax(extendedGraphemeClusterLiteral: implementedOverridesDecl))
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGodotMacroLibrary/MacroSharedApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ enum GodotMacroError: Error, DiagnosticMessage {
"@Callable does not support asynchronous or throwing functions"
case .noSupportForOptionalEnums:
"@Export(.enum) does not support optional values for the enumeration"
}
}
}

var diagnosticID: MessageID {
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftGodotMacrosTests/MacroGodotBuildTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import SwiftGodot

@Godot
class Demo: Object {
@Export var demo: GArray = GArray()
@Export var demo: GArray = GArray()
}

@Godot
class Demo2: Object {
@Export var demo: Variant = Variant()
@Export var demo: Variant = Variant()
}

Loading

0 comments on commit d2feb88

Please sign in to comment.