Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Tests/GravityTests/GravityVirtualMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ struct GravityVirtualMachineTests {
#expect(result.toInteger == 2)
}

@Test("Bridges strings through null-terminated storage")
func bridgesNullTerminatedStrings() {
let delegate = TestVirtualMachineDelegate()
let virtualMachine = GravityVirtualMachine(
settings: .init(),
delegate: delegate
)
let value = GSValue(string: "Hello", in: virtualMachine)

#expect(value.toString == "Hello")
}

@Test("Collects declaration annotations without executing main")
func collectsDeclarationAnnotations() throws {
let delegate = TestVirtualMachineDelegate()
Expand Down
17 changes: 5 additions & 12 deletions binding/GravitySwift/GravityVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,11 @@ extension GravityVirtualMachine {

extension String {
func toPointer() -> UnsafePointer<CChar>? {
guard let data = self.data(using: .utf8) else { return nil }

let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: data.count)
let stream = OutputStream(toBuffer: buffer, capacity: data.count)

stream.open()
data.withUnsafeBytes { pointer in
let charPointer = pointer.bindMemory(to: CChar.self)
stream.write(charPointer.baseAddress!, maxLength: charPointer.count)
let capacity = utf8.count + 1
let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: capacity)
return withCString { source in
buffer.initialize(from: source, count: capacity)
return UnsafePointer(buffer)
}
stream.close()

return UnsafePointer<CChar>(buffer)
}
}
Loading