Skip to content

Commit

Permalink
The Variant(fromContent:) and Variant(fromContentPtr:) now make a
Browse files Browse the repository at this point in the history
copy of the variants that they are passed upon creation.

This should fix #390.
  • Loading branch information
migueldeicaza committed Feb 13, 2024
1 parent 663b13c commit eea7e78
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Generator/Generator/BuiltinGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func generateBuiltinMethods (_ p: Printer,
p ("var result = Variant.zero")
p ("if Self.keyed_checker (&content, &keyCopy.content) != 0") {
p ("Self.keyed_getter (&content, &keyCopy.content, &result)")
p ("return Variant (fromContent: result)")
p ("return Variant (fromContentPtr: &result)")
}
p ("else") {
p ("return nil")
Expand Down
6 changes: 3 additions & 3 deletions Generator/Generator/MethodGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef:
guard returnType != "" else { return "" }
if method.isVararg {
if returnType == "Variant" {
return "return Variant (fromContent: _result)"
return "return Variant (fromContentPtr: &_result)"
} else if returnType == "GodotError" {
return "return GodotError (rawValue: Int64 (Variant (fromContent: _result))!)!"
return "return GodotError (rawValue: Int64 (Variant (fromContentPtr: &_result))!)!"
} else if returnType == "String" {
return "return GString (Variant (fromContent: _result))?.description ?? \"\""
return "return GString (Variant (fromContentPtr: &_result))?.description ?? \"\""
} else {
fatalError("Do not support this return type = \(returnType)")
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftGodot/Core/VariantCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public class VariantCollection<Element: VariantStorable>: Collection, Expressibl
public final func append (value: Element) {
array.append (value: Variant(value))
}

/// Appends an element at the end of the array (alias of ``pushBack(value:)``).
public final func append (value: Element) where Element: ContentTypeStoring {
array.append (value: Variant(value))
}

/// 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.
///
Expand Down
13 changes: 10 additions & 3 deletions Sources/SwiftGodot/Variant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,23 @@ public class Variant: Hashable, Equatable, CustomDebugStringConvertible {
var content: ContentType = (0, 0, 0)
static var zero: ContentType = (0, 0, 0)

/// Initializes from the raw contents of another Variant
/// Initializes from the raw contents of another Variant, this will make a copy of the variant contents
init (fromContent: ContentType) {
content = fromContent
var copy = fromContent
gi.variant_new_copy (&content, &copy)
}


/// Initializes from the raw contents of another Variant, this will make a copy of the variant contents
init (fromContentPtr: inout ContentType) {
gi.variant_new_copy (&content, &fromContentPtr)
}

deinit {
if experimentalDisableVariantUnref { return }
gi.variant_destroy (&content)
}

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

0 comments on commit eea7e78

Please sign in to comment.