Skip to content

Commit

Permalink
Special case some static classes for internals work
Browse files Browse the repository at this point in the history
  • Loading branch information
migueldeicaza committed Sep 27, 2023
1 parent 0bdd4fe commit 3b274c0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions Generator/Generator/ClassGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ func generateMethods (_ p: Printer,
docClass: DocClass?,
methods: [JGodotClassMethod],
usedMethods: Set<String>,
isSingleton: Bool) -> [String:(String, JGodotClassMethod)] {
asSingleton: 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, isSingleton: isSingleton) {
if let virtualMethodName = methodGen (p, method: method, className: cdef.name, cdef: cdef, docClass: docClass, usedMethods: usedMethods, kind: .class, asSingleton: asSingleton) {
virtuals [method.name] = (virtualMethodName, method)
}
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func generateProperties (_ p: Printer,
_ properties: [JGodotProperty],
_ methods: [JGodotClassMethod],
_ referencedMethods: inout Set<String>,
isSingleton: Bool)
asSingleton: Bool)
{
p ("\n/* Properties */\n")

Expand Down Expand Up @@ -389,7 +389,7 @@ func generateProperties (_ p: Printer,
doc (p, cdef, docMember.value)
}
}
p ("\(isSingleton ? "static" : "final") public var \(godotPropertyToSwift (property.name)): \(type!)"){
p ("\(asSingleton ? "static" : "final") public var \(godotPropertyToSwift (property.name)): \(type!)"){
p ("get"){
p ("return \(getterName) (\(gettterArgName)\(access))")
}
Expand Down Expand Up @@ -594,7 +594,10 @@ func generateSignalDocAppendix (_ p: Printer, cdef: JGodotExtensionAPIClass, sig

func processClass (cdef: JGodotExtensionAPIClass, outputDir: String) {
let docClass = loadClassDoc(base: docRoot, name: cdef.name)

// Determine if it is a singleton, but exclude EditorInterface
let isSingleton = jsonApi.singletons.contains (where: { $0.name == cdef.name })
let asSingleton = isSingleton && cdef.name != "EditorInterface"

// Clear the result
let p = Printer ()
Expand Down Expand Up @@ -679,10 +682,10 @@ func processClass (cdef: JGodotExtensionAPIClass, outputDir: String) {
}

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

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, isSingleton: Bool) -> String? {
func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef: JClassInfo?, docClass: DocClass?, usedMethods: Set<String>, kind: MethodGenType, asSingleton: 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 || isSingleton ? " static" : ""
let instanceOrStatic = method.isStatic || asSingleton ? " 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: \(isSingleton ? "shared." : "")handle), "
let instanceHandle = method.isStatic ? "nil, " : "UnsafeMutableRawPointer (mutating: \(asSingleton ? "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, isSingleton: false)
_ = methodGen (p, method: method, className: "Godot", cdef: nil, docClass: docClass, usedMethods: emptyUsedMethods, kind: .utility, asSingleton: false)

}
}
Expand Down

0 comments on commit 3b274c0

Please sign in to comment.