ladybird/Tests/LibGC/TestGCBindings.swift
Andrew Kaster fca6fd0b85 LibGC: Add Swift bindings to the GC heap
This includes a protocol for creating LibGC Heap allocated Swift
objects. Pay no attention to the Unmanaged shenanigans, they are
all behind the curtain.
2024-11-19 14:32:11 -07:00

39 lines
1.2 KiB
Swift

/*
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
import AK
import GC
import GCTesting
import Testing
// FIXME: We want a type declared *here* for HeapString, but it gives a compiler warning:
// error: type 'GCString' cannot conform to protocol 'HeapAllocatable' because it has requirements that cannot be satisfied
// Even using the same exact code from LibGC/Heap+Swift.swift
// This is likely because one of the required types for HeapAllocatable is not fully imported from C++ and thus can't
// be re-exported by the GC module.
@Suite(.serialized)
struct TestGCSwiftBindings {
@Test func createBoundString() {
let heap = test_gc_heap()
let string = heap.withDeferredGC {
return HeapString.allocate(on: heap)
}
#expect(string.pointee.string == "")
heap.collect_garbage(GC.Heap.CollectionType.CollectGarbage)
string.pointee.string = "Hello, World!"
heap.collect_garbage(GC.Heap.CollectionType.CollectGarbage)
#expect(string.pointee.string == "Hello, World!")
heap.collect_garbage(GC.Heap.CollectionType.CollectEverything)
}
@Test func testInterop() {
test_interop()
}
}