Skip to content

Commit

Permalink
Make @export work with GArray and Variant values, fixes #410
Browse files Browse the repository at this point in the history
  • Loading branch information
migueldeicaza committed Feb 27, 2024
1 parent ebc26d0 commit 1790059
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Sources/SwiftGodotMacroLibrary/MacroExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public struct GodotExport: PeerMacro {
let name = "_mproxy_set_\(varName)"
var body: String = ""

if godotVariants [typeName] == nil {
if typeName == "Variant" {
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 Down
4 changes: 2 additions & 2 deletions Sources/SwiftGodotMacroLibrary/MacroGodot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class GodotMacroProcessor {
godotArrayElementTypeName = gArrayCollectionElementTypeName
}

propType = godotTypeToProp (typeName: "Array")
propType = godotTypeToProp (typeName: "GArray")
className = "Array[\(godotArrayElementTypeName)]"
hintStr = godotArrayElementTypeName
} else {
Expand Down Expand Up @@ -375,7 +375,7 @@ class GodotMacroProcessor {
ctor.append (
"""
let \(pinfo) = PropInfo (
propertyType: \(godotTypeToProp(typeName: "Array")),
propertyType: \(godotTypeToProp(typeName: "GArray")),
propertyName: "\(varNameWithPrefix.camelCaseToSnakeCase())",
className: StringName("\(godotArrayTypeName)"),
hint: .arrayType,
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftGodotMacroLibrary/MacroSharedApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func getIdentifier (_ typeSyntax: TypeSyntax?) -> (typeName: String, generics: [
return (typeName: identifier.name.text, generics: genericTypeNames, isOptional: opt)
} else if let array = typeSyntax.as(ArrayTypeSyntax.self),
let elementTypeName = array.element.as(IdentifierTypeSyntax.self)?.name.text {
return (typeName: "Array", generics: [elementTypeName], isOptional: opt)
return (typeName: "GArray", generics: [elementTypeName], isOptional: opt)
}
return nil
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func getTypeName (_ parameter: FunctionParameterSyntax) -> String? {
parameter.isObjectCollection,
parameter.isVariantCollection
].allSatisfy ({ $0 == false }) else {
return "Array"
return "GArray"
}
guard let typeName = parameter.type.as (IdentifierTypeSyntax.self)?.name.text else {
return nil
Expand All @@ -164,7 +164,7 @@ var godotVariants = [
"Double": ".float",
"Bool": ".bool",
"AABB": ".aabb",
"Array": ".array",
"GArray": ".array",
"Basis": ".basis",
"Callable": ".callable",
"Color": ".color",
Expand Down
21 changes: 21 additions & 0 deletions Tests/SwiftGodotMacrosTests/MacroGodotBuildTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// This file is solely here to ensure that we can compile the resulting macros,
// which is not covered by the macro output generation.
//
//
// Created by Miguel de Icaza on 2/27/24.
//

import Foundation
import SwiftGodot

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

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

56 changes: 56 additions & 0 deletions Tests/SwiftGodotMacrosTests/MacroGodotExportCollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,62 @@ func _mproxy_set_greetings(args: [Variant]) -> Variant? {
)
}

func testExportGArray() {
assertMacroExpansion(
"""
@Godot
class SomeNode: Node {
@Export var someArray: GArray = GArray()
}
""",
expandedSource:
"""
class SomeNode: Node {
var someArray: GArray = GArray()
func _mproxy_set_someArray (args: [Variant]) -> Variant? {
guard let arg = args.first else {
return nil
}
if let value = GArray (arg) {
self.someArray = value
} else {
GD.printErr ("Unable to set `someArray` value: ", arg)
}
return nil
}
func _mproxy_get_someArray (args: [Variant]) -> Variant? {
return Variant (someArray)
}
override open class var classInitializer: Void {
let _ = super.classInitializer
return _initializeClass
}
private static var _initializeClass: Void = {
let className = StringName("SomeNode")
assert(ClassDB.classExists(class: className))
let classInfo = ClassInfo<SomeNode> (name: className)
let _psomeArray = PropInfo (
propertyType: .array,
propertyName: "someArray",
className: className,
hint: .none,
hintStr: "",
usage: .default)
classInfo.registerMethod (name: "_mproxy_get_someArray", flags: .default, returnValue: _psomeArray, arguments: [], function: SomeNode._mproxy_get_someArray)
classInfo.registerMethod (name: "_mproxy_set_someArray", flags: .default, returnValue: nil, arguments: [_psomeArray], function: SomeNode._mproxy_set_someArray)
classInfo.registerProperty (_psomeArray, getter: "_mproxy_get_someArray", setter: "_mproxy_set_someArray")
} ()
}
""",
macros: testMacros
)
}

func testExportArrayIntGodotMacro() {
assertMacroExpansion(
"""
Expand Down

0 comments on commit 1790059

Please sign in to comment.