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
|
//===--- BridgeStorage.swift ----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Bridged types are notionally single-word beasts that either store
// an objc class or a native Swift class. We'd like to be able to
// distinguish these cases efficiently.
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Swift
//===--- Code mimics the stdlib without using spare pointer bits ----------===//
import SwiftShims
protocol BridgeStorage {
associatedtype Native : AnyObject
associatedtype ObjC : AnyObject
init(native: Native, isFlagged: Bool)
init(native: Native)
init(objC: ObjC)
mutating func isUniquelyReferencedNative() -> Bool
mutating func isUniquelyReferencedUnflaggedNative() -> Bool
var isNative: Bool {get}
var isObjC: Bool {get}
var nativeInstance: Native {get}
var unflaggedNativeInstance: Native {get}
var objCInstance: ObjC {get}
}
extension _BridgeStorage : BridgeStorage {}
//===----------------------------------------------------------------------===//
//===--- Testing code -----------------------------------------------------===//
//===----------------------------------------------------------------------===//
import StdlibUnittest
var allTests = TestSuite("DiscriminatedBridgeObject")
class C {
deinit {
print("bye C!")
}
}
import Foundation
func isOSAtLeast(_ major: Int, _ minor: Int, patch: Int = 0) -> Bool {
// isOperatingSystemAtLeastVersion() is unavailable on some OS versions.
if #available(iOS 8.0, OSX 10.10, *) {
let procInfo: AnyObject = ProcessInfo.processInfo
return procInfo.isOperatingSystemAtLeast(
OperatingSystemVersion(majorVersion: major, minorVersion: minor,
patchVersion: patch))
}
return false
}
func expectTagged(_ s: NSString, _ expected: Bool) -> NSString {
#if arch(x86_64)
let mask: UInt = 0x8000000000000001
#elseif arch(arm64)
let mask: UInt = 0x8000000000000000
#else
let mask: UInt = 0
#endif
let taggedStringsSupported = mask != 0
let tagged = unsafeBitCast(s, to: UInt.self) & mask != 0
if taggedStringsSupported && expected == tagged {
// okay
} else if !taggedStringsSupported && !tagged {
// okay
} else {
let un = !tagged ? "un" : ""
fatalError("Unexpectedly \(un)tagged pointer for string \"\(s)\"")
}
return s
}
var taggedNSString : NSString {
return expectTagged(NSString(format: "foo"), true)
}
var unTaggedNSString : NSString {
return expectTagged("fûtbōl" as NSString, false)
}
allTests.test("_BridgeStorage") {
typealias B = _BridgeStorage<C>
let oy: NSString = "oy"
expectTrue(B(objC: oy).objCInstance === oy)
for flag in [false, true] {
do {
var b = B(native: C(), isFlagged: flag)
expectFalse(b.isObjC)
expectTrue(b.isNative)
expectEqual(!flag, b.isUnflaggedNative)
expectTrue(b.isUniquelyReferencedNative())
if !flag {
expectTrue(b.isUniquelyReferencedUnflaggedNative())
}
}
do {
let c = C()
var b = B(native: c, isFlagged: flag)
expectFalse(b.isObjC)
expectTrue(b.isNative)
expectFalse(b.isUniquelyReferencedNative())
expectEqual(!flag, b.isUnflaggedNative)
expectTrue(b.nativeInstance === c)
if !flag {
expectTrue(b.unflaggedNativeInstance === c)
expectFalse(b.isUniquelyReferencedUnflaggedNative())
}
// Keep 'c' alive for the isUniquelyReferenced check above.
_fixLifetime(c)
}
}
var b = B(native: C(), isFlagged: false)
expectTrue(b.isUniquelyReferencedNative())
// Add a reference and verify that it's still native but no longer unique
var c = b
expectFalse(b.isUniquelyReferencedNative())
_fixLifetime(b) // make sure b is not killed early
_fixLifetime(c) // make sure c is not killed early
let n = C()
var bb = B(native: n)
expectTrue(bb.nativeInstance === n)
expectTrue(bb.isNative)
expectTrue(bb.isUnflaggedNative)
expectFalse(bb.isObjC)
var d = B(objC: taggedNSString)
expectFalse(d.isUniquelyReferencedNative())
expectFalse(d.isNative)
expectFalse(d.isUnflaggedNative)
expectTrue(d.isObjC)
d = B(objC: unTaggedNSString)
expectFalse(d.isUniquelyReferencedNative())
expectFalse(d.isNative)
expectFalse(d.isUnflaggedNative)
expectTrue(d.isObjC)
}
runAllTests()
|