Skip to content

Commit eea7e78

Browse files
committed
The Variant(fromContent:) and Variant(fromContentPtr:) now make a
copy of the variants that they are passed upon creation. This should fix #390.
1 parent 663b13c commit eea7e78

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

Generator/Generator/BuiltinGen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func generateBuiltinMethods (_ p: Printer,
434434
p ("var result = Variant.zero")
435435
p ("if Self.keyed_checker (&content, &keyCopy.content) != 0") {
436436
p ("Self.keyed_getter (&content, &keyCopy.content, &result)")
437-
p ("return Variant (fromContent: result)")
437+
p ("return Variant (fromContentPtr: &result)")
438438
}
439439
p ("else") {
440440
p ("return nil")

Generator/Generator/MethodGen.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef:
307307
guard returnType != "" else { return "" }
308308
if method.isVararg {
309309
if returnType == "Variant" {
310-
return "return Variant (fromContent: _result)"
310+
return "return Variant (fromContentPtr: &_result)"
311311
} else if returnType == "GodotError" {
312-
return "return GodotError (rawValue: Int64 (Variant (fromContent: _result))!)!"
312+
return "return GodotError (rawValue: Int64 (Variant (fromContentPtr: &_result))!)!"
313313
} else if returnType == "String" {
314-
return "return GString (Variant (fromContent: _result))?.description ?? \"\""
314+
return "return GString (Variant (fromContentPtr: &_result))?.description ?? \"\""
315315
} else {
316316
fatalError("Do not support this return type = \(returnType)")
317317
}

Sources/SwiftGodot/Core/VariantCollection.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public class VariantCollection<Element: VariantStorable>: Collection, Expressibl
124124
public final func append (value: Element) {
125125
array.append (value: Variant(value))
126126
}
127+
128+
/// Appends an element at the end of the array (alias of ``pushBack(value:)``).
129+
public final func append (value: Element) where Element: ContentTypeStoring {
130+
array.append (value: Variant(value))
131+
}
127132

128133
/// Resizes the array to contain a different number of elements. If the array size is smaller, elements are cleared, if bigger, new elements are `null`. Returns ``GodotError/ok`` on success, or one of the other ``GodotError`` values if the operation failed.
129134
///

Sources/SwiftGodot/Variant.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,23 @@ public class Variant: Hashable, Equatable, CustomDebugStringConvertible {
6767
var content: ContentType = (0, 0, 0)
6868
static var zero: ContentType = (0, 0, 0)
6969

70-
/// Initializes from the raw contents of another Variant
70+
/// Initializes from the raw contents of another Variant, this will make a copy of the variant contents
7171
init (fromContent: ContentType) {
72-
content = fromContent
72+
var copy = fromContent
73+
gi.variant_new_copy (&content, &copy)
7374
}
74-
75+
76+
/// Initializes from the raw contents of another Variant, this will make a copy of the variant contents
77+
init (fromContentPtr: inout ContentType) {
78+
gi.variant_new_copy (&content, &fromContentPtr)
79+
}
80+
7581
deinit {
7682
if experimentalDisableVariantUnref { return }
7783
gi.variant_destroy (&content)
7884
}
7985

86+
/// Creates an empty Variant, that represents the Godot type `nil`
8087
public init () {
8188
withUnsafeMutablePointer(to: &content) { ptr in
8289
gi.variant_new_nil (ptr)

0 commit comments

Comments
 (0)