Skip to content

Commit 51dbbed

Browse files
committed
Fix Swift mock object deinit and add a test for them (#2717)
1 parent 0a2c703 commit 51dbbed

File tree

7 files changed

+81
-0
lines changed

7 files changed

+81
-0
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ members = [
6767
"fixtures/simple-iface",
6868
"fixtures/struct-default-values",
6969
"fixtures/swift-codable",
70+
"fixtures/swift-mock-objects",
7071
"fixtures/swift-omit-labels",
7172
"fixtures/futures",
7273
"fixtures/swift-bridging-header-compile",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "uniffi-fixture-swift-mock-objects"
3+
version = "0.22.0"
4+
edition = "2021"
5+
license = "MPL-2.0"
6+
publish = false
7+
8+
[lib]
9+
crate-type = ["lib", "cdylib"]
10+
name = "uniffi_swift_mock_objects"
11+
12+
[dependencies]
13+
uniffi = { workspace = true }
14+
15+
[dev-dependencies]
16+
uniffi = { workspace = true, features = ["bindgen-tests"] }
17+
18+
[features]
19+
ffi-trace = ["uniffi/ffi-trace"]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#[derive(uniffi::Object, Debug)]
6+
pub struct TestObject;
7+
8+
#[uniffi::export]
9+
impl TestObject {
10+
pub fn is_mock(&self) -> bool {
11+
false
12+
}
13+
}
14+
15+
uniffi::setup_scaffolding!("swift_mock_objects");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import Foundation
6+
import swift_mock_objects
7+
8+
// Swift version 6 gives a warning that we must restate `@unchecked Sendable`, however earlier
9+
// versions of Swift fail if we add that. Let's leave it out and accept the warning for now.
10+
public class TestObjectMock: TestObject {
11+
12+
required public init(unsafeFromHandle handle: UInt64) {
13+
fatalError("Not supported")
14+
}
15+
16+
public init() {
17+
super.init(noHandle: NoHandle.init())
18+
}
19+
20+
// Override the "is_mock()" function
21+
22+
override public func isMock() -> Bool {
23+
return true;
24+
}
25+
}
26+
27+
var mocked: TestObjectMock? = TestObjectMock()
28+
29+
// Test that our mock object can override the functions of the real object
30+
assert(mocked!.isMock())
31+
32+
// Test that we don't crash when deinitializing the mock object
33+
mocked = nil
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uniffi::build_foreign_language_testcases!("tests/bindings/test_mock_objects.swift",);

uniffi_bindgen/src/bindings/swift/templates/ObjectTemplate.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ open class {{ impl_class_name }}: {{ protocol_name }}, {{ config.conformance_lis
5656
{%- endmatch %}
5757

5858
deinit {
59+
if handle == 0 {
60+
// Mock objects have handle=0 don't try to free them
61+
return
62+
}
63+
5964
try! rustCall { {{ obj.ffi_object_free().name() }}(handle, $0) }
6065
}
6166

0 commit comments

Comments
 (0)