File: objc_implementation.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (227 lines) | stat: -rw-r--r-- 7,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// RUN: %target-run-simple-swift(-import-objc-header %S/Inputs/objc_implementation.h -D TOP_LEVEL_CODE -swift-version 5 -enable-experimental-feature CImplementation -target %target-stable-abi-triple) %s | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
// REQUIRES: swift_feature_CImplementation

import Foundation

// A class whose deallocation is detectable, so we can verify that deinits
// are correctly synthesized.
class LastWords {
  var text: String
  
  init(text: String) {
    self.text = text
  }
  
  deinit {
    print(text, "It's better to burn out than to fade away.")
  }
}

@_objcImplementation extension ImplClass {
  @objc override init() {
    print("ImplClass.init()")
    self.implProperty = 0
    self.object = LastWords(text: String(describing: type(of: self)))
    super.init()
  }

  @objc var implProperty: Int
  final var object: LastWords
  final weak var defaultNilProperty: AnyObject?
  @objc var defaultIntProperty: Int = 17

  @objc class func runTests() {
    implFunc(2023 - 34)

    do {
      print("*** ImplClass init ***")
      let impl = ImplClass()
      impl.testSelf()
      print("*** ImplClass end ***")
    }

    do {
      print("*** SwiftSubclass init ***")
      let swiftSub = SwiftSubclass()
      swiftSub.testSelf()
      print("*** SwiftSubclass end ***")
    }
  }

  @objc func testSelf() {
#if RESILIENCE
    let resilientImpl = Self.makeResilientImpl()
    resilientImpl.printSelf(withLabel: 1)
    resilientImpl.mutate()
    resilientImpl.printSelf(withLabel: 2)
#endif

    self.printSelf(withLabel: 1)
    self.implProperty = 42
    self.printSelf(withLabel: 2)
  }

  @objc func printSelf(withLabel label: CInt) {
    let type = type(of: self)
    print("*** \(type) #\(label) ***")
    print("\(type).someMethod() =", self.someMethod())
    print("\(type).implProperty =", self.implProperty)
    print("\(type).defaultIntProperty =", self.defaultIntProperty)
    print("\(type).description =", self.description)
  }

  @objc func someMethod() -> String { "ImplClass" }

  @objc class func makeResilientImpl() -> ImplClassWithResilientStoredProperty {
#if RESILIENCE
    ImplClassWithResilientStoredProperty()
#else
    fatalError()
#endif
  }

  open override var description: String {
    "ImplClass(implProperty: \(implProperty), object: \(object))"
  }
}

class SwiftSubclass: ImplClass {
  @objc var otherProperty: Int = 1

  override init() {
    print("SwiftSubclass.init()")
    super.init()
  }

  override func someMethod() -> String { "SwiftSubclass" }

  override func testSelf() {
    super.testSelf()
    self.otherProperty = 13
    self.printSelf(withLabel: 3)
  }

  override func printSelf(withLabel label: CInt) {
    super.printSelf(withLabel: label)
    let type = type(of: self)
    print("\(type).otherProperty =", self.otherProperty)
  }

#if RESILIENCE
  override class func makeResilientImpl() -> ImplClassWithResilientStoredProperty {
    SwiftResilientStoredSubclass()
  }
#endif
}

#if RESILIENCE
@_objcImplementation extension ImplClassWithResilientStoredProperty {
  final var mirror: Mirror?
  final var afterMirrorProperty: Int

  public override init() {
    self.mirror = nil
    self.afterMirrorProperty = 0
  }

  @objc func printSelf(withLabel label: CInt) {
    let type = type(of: self)
    print("*** \(type) #\(label) ***")
    print("\(type).mirror =", self.mirror as Any)
    print("\(type).afterMirrorProperty =", self.afterMirrorProperty)
  }

  @objc func mutate() {
    self.afterMirrorProperty = 42
  }
}

extension SwiftSubclass {
  class SwiftResilientStoredSubclass: ImplClassWithResilientStoredProperty {
    final var mirror2: Mirror?
    final var afterMirrorProperty2: Int

    public override init() {
      self.mirror2 = nil
      self.afterMirrorProperty2 = 1
    }

    override func printSelf(withLabel label: CInt) {
      super.printSelf(withLabel: label)
      let type = type(of: self)
      print("\(type).mirror2 =", self.mirror2 as Any)
      print("\(type).afterMirrorProperty2 =", self.afterMirrorProperty2)
    }

    override func mutate() {
      super.mutate()
      self.afterMirrorProperty2 = 43
    }
  }
}
#endif

@_objcImplementation @_cdecl("implFunc") public func implFunc(_ param: Int32) {
  print("implFunc(\(param))")
}

// `#if swift` to ignore the inactive branch's contents
#if swift(>=5.0) && TOP_LEVEL_CODE
ImplClass.runTests()
// CHECK: implFunc(1989)
// CHECK-LABEL: *** ImplClass init ***
// CHECK: ImplClass.init()
// CHECK-RESILIENCE-LABEL: *** ImplClassWithResilientStoredProperty #1 ***
// CHECK-RESILIENCE: ImplClassWithResilientStoredProperty.mirror = nil
// CHECK-RESILIENCE: ImplClassWithResilientStoredProperty.afterMirrorProperty = 0
// CHECK-RESILIENCE-LABEL: *** ImplClassWithResilientStoredProperty #2 ***
// CHECK-RESILIENCE: ImplClassWithResilientStoredProperty.mirror = nil
// CHECK-RESILIENCE: ImplClassWithResilientStoredProperty.afterMirrorProperty = 42
// CHECK-LABEL: *** ImplClass #1 ***
// CHECK: ImplClass.someMethod() = ImplClass
// CHECK: ImplClass.implProperty = 0
// CHECK: ImplClass.defaultIntProperty = 17
// CHECK: ImplClass.description = ImplClass(implProperty: 0, object: main.LastWords)
// CHECK-LABEL: *** ImplClass #2 ***
// CHECK: ImplClass.someMethod() = ImplClass
// CHECK: ImplClass.implProperty = 42
// CHECK: ImplClass.defaultIntProperty = 17
// CHECK: ImplClass.description = ImplClass(implProperty: 42, object: main.LastWords)
// CHECK-LABEL: *** ImplClass end ***
// CHECK: ImplClass It's better to burn out than to fade away.
// CHECK-LABEL: *** SwiftSubclass init ***
// CHECK: SwiftSubclass.init()
// CHECK: ImplClass.init()
// CHECK-RESILIENCE-LABEL: *** SwiftResilientStoredSubclass #1 ***
// CHECK-RESILIENCE: SwiftResilientStoredSubclass.mirror = nil
// CHECK-RESILIENCE: SwiftResilientStoredSubclass.afterMirrorProperty = 0
// CHECK-RESILIENCE: SwiftResilientStoredSubclass.mirror2 = nil
// CHECK-RESILIENCE: SwiftResilientStoredSubclass.afterMirrorProperty2 = 1
// CHECK-RESILIENCE-LABEL: *** SwiftResilientStoredSubclass #2 ***
// CHECK-RESILIENCE: SwiftResilientStoredSubclass.mirror = nil
// CHECK-RESILIENCE: SwiftResilientStoredSubclass.afterMirrorProperty = 42
// CHECK-RESILIENCE: SwiftResilientStoredSubclass.mirror2 = nil
// CHECK-RESILIENCE: SwiftResilientStoredSubclass.afterMirrorProperty2 = 43
// CHECK-LABEL: *** SwiftSubclass #1 ***
// CHECK: SwiftSubclass.someMethod() = SwiftSubclass
// CHECK: SwiftSubclass.implProperty = 0
// CHECK: SwiftSubclass.defaultIntProperty = 17
// CHECK: SwiftSubclass.description = ImplClass(implProperty: 0, object: main.LastWords)
// CHECK: SwiftSubclass.otherProperty = 1
// CHECK-LABEL: *** SwiftSubclass #2 ***
// CHECK: SwiftSubclass.someMethod() = SwiftSubclass
// CHECK: SwiftSubclass.implProperty = 42
// CHECK: SwiftSubclass.defaultIntProperty = 17
// CHECK: SwiftSubclass.description = ImplClass(implProperty: 42, object: main.LastWords)
// CHECK: SwiftSubclass.otherProperty = 1
// CHECK-LABEL: *** SwiftSubclass #3 ***
// CHECK: SwiftSubclass.someMethod() = SwiftSubclass
// CHECK: SwiftSubclass.implProperty = 42
// CHECK: SwiftSubclass.defaultIntProperty = 17
// CHECK: SwiftSubclass.description = ImplClass(implProperty: 42, object: main.LastWords)
// CHECK: SwiftSubclass.otherProperty = 13
// CHECK-LABEL: *** SwiftSubclass end ***
// CHECK: SwiftSubclass It's better to burn out than to fade away.
#endif