forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjc_mangling.swift
79 lines (62 loc) · 2.24 KB
/
objc_mangling.swift
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
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -module-name MangleTest %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
// rdar://problem/56959761
// UNSUPPORTED: OS=watchos
import Foundation
/* FIXME: SwiftObject doesn't support -description
class Foo { }
var anyFoo: AnyObject = Foo()
print(anyFoo.description())
@objc class Bar { }
var anyBar: AnyObject = Bar()
print(anyBar.description())
*/
func checkClassName(_ cls: AnyClass, _ name: String, _ mangled: String)
{
// Class's name should appear unmangled.
assert(NSStringFromClass(cls) == name)
assert(NSStringFromClass(object_getClass(cls)!) == name)
// Look up by unmangled name should work.
// Look up by mangled name should also work.
for query in [name, mangled] {
let cls2 = NSClassFromString(query)!
assert(cls === cls2)
assert(object_getClass(cls) === object_getClass(cls2))
}
}
func checkProtocolName(_ proto: Protocol, _ name: String, _ mangled: String)
{
// Protocol's name should appear unmangled.
assert(NSStringFromProtocol(proto) == name)
// Look up by unmangled name should work.
// Look up by mangled name should also work.
for query in [name, mangled] {
let proto2 = NSProtocolFromString(query)
assert(protocol_isEqual(proto, proto2))
}
}
func checkIvarName(_ cls: AnyClass, _ name: String)
{
let ivarName = ivar_getName(class_getInstanceVariable(cls, name)!)
let s = ivarName != nil ? String(cString: ivarName!) : Optional.none
assert(name == s)
}
@objc class Wibble : NSObject { }
checkClassName(Wibble.self, "MangleTest.Wibble", "_TtC10MangleTest6Wibble")
// Check whether the class name comes out properly in the instance description
var anyWibble: AnyObject = Wibble()
print(anyWibble.description)
// CHECK: MangleTest.Wibble
// ObjC metadata is not punycoded.
@objc protocol RadicalHeart⺖ { }
checkProtocolName(RadicalHeart⺖.self, "MangleTest.RadicalHeart⺖", "_TtP10MangleTest15RadicalHeart⺖_")
@objc class RadicalSheep⽺ : NSObject, RadicalHeart⺖ {
var ⽺x: Int = 0
}
checkClassName(RadicalSheep⽺.self,
"MangleTest.RadicalSheep⽺", "_TtC10MangleTest15RadicalSheep⽺")
checkIvarName(RadicalSheep⽺.self, "⽺x")