Skip to content

Commit

Permalink
Static classes now surface static methods, without having to use the …
Browse files Browse the repository at this point in the history
…'.shared' idiom; Following what C# does here
  • Loading branch information
migueldeicaza committed Sep 27, 2023
1 parent 2f58b5e commit dbc0a00
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
16 changes: 9 additions & 7 deletions Generator/Generator/ClassGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,14 @@ func generateMethods (_ p: Printer,
cdef: JGodotExtensionAPIClass,
docClass: DocClass?,
methods: [JGodotClassMethod],
usedMethods: Set<String>) -> [String:(String, JGodotClassMethod)] {
usedMethods: Set<String>,
isSingleton: Bool) -> [String:(String, JGodotClassMethod)] {
p ("/* Methods */")

var virtuals: [String:(String, JGodotClassMethod)] = [:]

for method in methods {
if let virtualMethodName = methodGen (p, method: method, className: cdef.name, cdef: cdef, docClass: docClass, usedMethods: usedMethods, kind: .class) {
if let virtualMethodName = methodGen (p, method: method, className: cdef.name, cdef: cdef, docClass: docClass, usedMethods: usedMethods, kind: .class, isSingleton: isSingleton) {
virtuals [method.name] = (virtualMethodName, method)
}
}
Expand Down Expand Up @@ -264,7 +265,8 @@ func generateProperties (_ p: Printer,
docClass: DocClass?,
_ properties: [JGodotProperty],
_ methods: [JGodotClassMethod],
_ referencedMethods: inout Set<String>)
_ referencedMethods: inout Set<String>,
isSingleton: Bool)
{
p ("\n/* Properties */\n")

Expand Down Expand Up @@ -387,7 +389,7 @@ func generateProperties (_ p: Printer,
doc (p, cdef, docMember.value)
}
}
p ("final public var \(godotPropertyToSwift (property.name)): \(type!)"){
p ("\(isSingleton ? "static" : "final") public var \(godotPropertyToSwift (property.name)): \(type!)"){
p ("get"){
p ("return \(getterName) (\(gettterArgName)\(access))")
}
Expand Down Expand Up @@ -629,7 +631,7 @@ func processClass (cdef: JGodotExtensionAPIClass, outputDir: String) {
p (typeDecl) {
if isSingleton {
p ("/// The shared instance of this class")
p ("public static var shared: \(cdef.name) =", suffix: "()") {
p ("static var shared: \(cdef.name) =", suffix: "()") {
p ("\(cdef.name) (nativeHandle: gi.global_get_singleton (UnsafeRawPointer (&\(cdef.name).className.content))!)")
}
}
Expand Down Expand Up @@ -677,10 +679,10 @@ func processClass (cdef: JGodotExtensionAPIClass, outputDir: String) {
}

if let properties = cdef.properties {
generateProperties (p, cdef: cdef, docClass: docClass, properties, cdef.methods ?? [], &referencedMethods)
generateProperties (p, cdef: cdef, docClass: docClass, properties, cdef.methods ?? [], &referencedMethods, isSingleton: isSingleton)
}
if let methods = cdef.methods {
virtuals = generateMethods (p, cdef: cdef, docClass: docClass, methods: methods, usedMethods: referencedMethods)
virtuals = generateMethods (p, cdef: cdef, docClass: docClass, methods: methods, usedMethods: referencedMethods, isSingleton: isSingleton)
}

if let signals = cdef.signals {
Expand Down
6 changes: 3 additions & 3 deletions Generator/Generator/MethodGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func isRefParameterOptional (className: String, method: String, arg: String) ->
/// - className: the name of the class where this is being generated
/// - usedMethods: a set of methods that have been referenced by properties, to determine whether we make this public or private
/// - Returns: nil, or the method we surfaced that needs to have the virtual supporting infrastructured wired up
func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef: JClassInfo?, docClass: DocClass?, usedMethods: Set<String>, kind: MethodGenType) -> String? {
func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef: JClassInfo?, docClass: DocClass?, usedMethods: Set<String>, kind: MethodGenType, isSingleton: Bool) -> String? {
var registerVirtualMethodName: String? = nil

//let loc = "\(cdef.name).\(method.name)"
Expand All @@ -87,7 +87,7 @@ func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef:
var finalp: String
// Default method name
var methodName: String = godotMethodToSwift (method.name)
let instanceOrStatic = method.isStatic ? " static" : ""
let instanceOrStatic = method.isStatic || isSingleton ? " static" : ""
var inline = ""
if let methodHash = method.hash {
assert (!method.isVirtual)
Expand Down Expand Up @@ -338,7 +338,7 @@ func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef:

switch kind {
case .class:
let instanceHandle = method.isStatic ? "nil, " : "UnsafeMutableRawPointer (mutating: handle), "
let instanceHandle = method.isStatic ? "nil, " : "UnsafeMutableRawPointer (mutating: \(isSingleton ? "shared." : "")handle), "
if method.isVararg {
p ("gi.object_method_bind_call (\(className).method_\(method.name), \(instanceHandle)\(ptrArgs), Int64 (_args.count), \(ptrResult), nil)")
} else {
Expand Down
2 changes: 1 addition & 1 deletion Generator/Generator/UtilityGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func generateUtility(values: [JGodotUtilityFunction], outputDir: String) {
for method in values {
// We ignore the request for virtual methods, should not happen for these

_ = methodGen (p, method: method, className: "Godot", cdef: nil, docClass: docClass, usedMethods: emptyUsedMethods, kind: .utility)
_ = methodGen (p, method: method, className: "Godot", cdef: nil, docClass: docClass, usedMethods: emptyUsedMethods, kind: .utility, isSingleton: false)

}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SimpleExtension/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SwiftSprite: Sprite2D {
override func _process (delta: Double) {
time_passed += delta

let imageVariant = ProjectSettings.shared.getSetting(name: "shader_globals/heightmap", defaultValue: Variant(-1))
let imageVariant = ProjectSettings.getSetting(name: "shader_globals/heightmap", defaultValue: Variant(-1))
GD.print("Found this value IMAGE: \(imageVariant.gtype) variant: \(imageVariant) desc: \(imageVariant.description)")

let dict2: GDictionary? = GDictionary(imageVariant)
Expand Down Expand Up @@ -146,7 +146,7 @@ class SwiftSprite2: Sprite2D {
override func _process (delta: Double) {
time_passed += delta

let imageVariant = ProjectSettings.shared.getSetting(name: "shader_globals/heightmap", defaultValue: Variant(-1))
let imageVariant = ProjectSettings.getSetting(name: "shader_globals/heightmap", defaultValue: Variant(-1))
GD.print("Found this value IMAGE: \(imageVariant.gtype) variant: \(imageVariant) desc: \(imageVariant.description)")

let dict2: GDictionary? = GDictionary(imageVariant)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftGodot/Extensions/GD+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension GD {
/// - Parameter path: Path of the `Resource` to load.
/// - Returns: The loaded `Resource`.
public static func load(path: String) -> Resource? {
return ResourceLoader().load(path: path, cacheMode: .reuse)
return ResourceLoader.load(path: path, cacheMode: .reuse)
}

/// Loads a resource from the filesystem located at `path`.
Expand All @@ -39,7 +39,7 @@ extension GD {
/// - Parameter path: Path of the `Resource` to load.
/// - Returns: The loaded `Resource`.
public static func load<T>(path: String) -> T? {
return ResourceLoader().load(path: path, cacheMode: .reuse) as? T
return ResourceLoader.load(path: path, cacheMode: .reuse) as? T
}

/// Pushes an error message to Godot's built-in debugger and to the OS terminal.
Expand Down

0 comments on commit dbc0a00

Please sign in to comment.