Skip to content

Commit

Permalink
Add support for additional default values: empty typedarrays, empty d…
Browse files Browse the repository at this point in the history
…ictionaries and optionsets
  • Loading branch information
migueldeicaza committed May 28, 2023
1 parent 8b76a62 commit 5be45fc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
56 changes: 54 additions & 2 deletions Generator/Generator/Arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ func getArgumentDeclaration (_ argument: JGodotArgument, eliminate: String, kind

var def: String = ""
if let dv = argument.defaultValue, dv != "" {
let argumentType = argument.type

// TODO:
// - handle creating initializers from enums (builtint)
// - empty arrays
// - bitfield defaults
// - Structure with initialized values (Color (1,1,1,1))
// - NodePath ("") ctor
// - nil values (needs to both turn the value nullable and handle that in the marshal code
// - typedarrays, the default values need to be handled one by one, or a general conversion
// system needs to be implemented
if !argument.type.starts(with: "Array") && !argument.type.starts(with: "bitfield::") && (!(isStructMap [argument.type] ?? false) || isPrimitiveType(name: argument.type)) && argument.type != "NodePath" && !argument.type.starts(with: "typedarray::") && !argument.type.starts (with: "Dictionary") && dv != "null" {
if !argumentType.starts(with: "Array") && !argumentType.starts(with: "bitfield::") && (!(isStructMap [argumentType] ?? false) || isPrimitiveType(name: argumentType)) && argumentType != "NodePath" && !argumentType.starts(with: "typedarray::") && !argumentType.starts (with: "Dictionary") && dv != "null" {
if argument.type == "String" {
def = " = \(dv)"
} else if argument.type == "StringName" {
Expand All @@ -50,6 +51,57 @@ func getArgumentDeclaration (_ argument: JGodotArgument, eliminate: String, kind
} else {
def = " = \(dv)"
}
} else {
// Here we add the new conversions, will eventually replace everything
// above, as the do-not-run conditions are becoming large and difficult
// to parse - they were fine to bootstrap, but not for the long term.

// Handle empty type arrays
if argumentType.starts(with: "typedarray::") {
if dv == "[]" {
def = " = \(getGodotType(argument, kind: kind)) ()"
} else {
print ("Generator: \(argumentType) support for default value: \(dv)")
}
} else if argumentType == "Dictionary" {
if dv == "{}" {
def = " = SwiftGodot.Dictionary ()"
} else {
print ("Generator: \(argumentType) missing support for default value: \(dv)")
}
} else if argumentType.starts(with: "bitfield::") {
if let defIntValue = Int (dv) {
if defIntValue == 0 {
def = " = []"
} else {
// Need to look it up
if let optionType = findEnumDef(name: argumentType) {
var setValues = ""

for value in optionType.values {
if (defIntValue & value.value) != 0 {
let name = dropMatchingPrefix(optionType.name, value.name)
if setValues != "" {
setValues += ", "
}
setValues += ".\(name)"
}
}
def = " = [\(setValues)]"
} else {
print ("Generator: \(argumentType) could not produce default value for \(dv) because I can not find the type")
}
}
} else {
print ("Generator: bitfield:: with a non-integer default value")
}
} else if argumentType == "Array" {
if dv == "[]" {
def = " = GArray ()"
} else {
print ("Generator: no support for arrays with values: \(dv)")
}
}
}
}
return "\(eliminate)\(godotArgumentToSwift (argument.name)): \(optNeedInOut)\(getGodotType(argument, kind: kind))\(isOptional ? "?" : "")\(def)"
Expand Down
6 changes: 4 additions & 2 deletions Generator/Generator/ClassGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ func generateProperties (_ p: Printer,
var getterName = property.getter
var gettterArgName = ""
guard let method = findMethod (forProperty: property, startAt: cdef, name: property.getter, resolvedName: &getterName, argName: &gettterArgName) else {
print ("GodotBug: \(loc): property declared \(property.getter), but it does not exist with that name")
// Not a bug, but needs to be handled https://github.com/migueldeicaza/SwiftGodot/issues/67
//print ("GodotBug: \(loc): property declared \(property.getter), but it does not exist with that name")
continue
}
var setterName = property.setter ?? ""
Expand All @@ -320,7 +321,8 @@ func generateProperties (_ p: Printer,
if let psetter = property.setter {
setterMethod = findMethod(forProperty: property, startAt: cdef, name: psetter, resolvedName: &setterName, argName: &setterArgName)
if setterMethod == nil {
print ("GodotBug \(loc) property declared \(property.setter!) but it does not exist with that name")
// Not a bug, but needs to be handled: https://github.com/migueldeicaza/SwiftGodot/issues/67
//print ("GodotBug \(loc) property declared \(property.setter!) but it does not exist with that name")
continue
}
}
Expand Down
36 changes: 34 additions & 2 deletions Generator/Generator/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,46 @@

import Foundation

// The name of the form 'bitfield::'
func findEnumDef (name: String) -> JGodotGlobalEnumElement? {
guard name.starts(with: "bitfield::") else {
return nil
}

let full = name.dropFirst(10)
guard let split = full.firstIndex(of: ".") else {
print ("No support for global bitfields for \(name)")
return nil
}
let type = full [full.startIndex..<split]
guard let cdef = classMap [String (type)] else {
print ("Could not find class \(type) for \(name)")
return nil
}
let enumName = full [full.index(split, offsetBy: 1)...]
guard let enums = cdef.enums else {
print ("Could not find an enum \(enumName) in \(type)")
return nil
}
for x in enums {
if x.name == enumName {
return x
}
}
return nil
}

func generateEnums (_ p: Printer, cdef: JClassInfo?, values: [JGodotGlobalEnumElement], constantDocs: [DocConstant]? , prefix: String?) {

var docEnumToValue: [String:String] = [:]
for d in constantDocs ?? [] {
docEnumToValue [d.name] = d.rest
}

let classPrefix = cdef == nil ? "" : cdef!.name + "."

for enumDef in values {
if (enumDef.isBitfield ?? false) || enumDef.name == "ConnectFlags" {
let isBitField = enumDef.isBitfield ?? false
if isBitField || enumDef.name == "ConnectFlags" {
p ("public struct \(getGodotType (SimpleType (type: enumDef.name))): OptionSet") {
p ("public let rawValue: Int")
p ("public init (rawValue: Int)") {
Expand All @@ -32,6 +63,7 @@ func generateEnums (_ p: Printer, cdef: JClassInfo?, values: [JGodotGlobalEnumEl
continue
}
var enumDefName = enumDef.name

if enumDefName.starts(with: "Variant") {
p ("extension Variant {")
p.indent += 1
Expand Down

0 comments on commit 5be45fc

Please sign in to comment.