|
1 | 1 | // RUN: %target-run-simple-swift | %FileCheck %s |
2 | 2 | // REQUIRES: executable_test |
3 | 3 |
|
4 | | -@_silgen_name("_swift_metadataAccessorCall") |
5 | | -func _metadataAccessorCall( |
6 | | - fn: UnsafeRawPointer, |
7 | | - request: UInt, |
8 | | - args: UnsafePointer<Any.Type>?, |
9 | | - count: Int |
10 | | -) -> MetadataResponse |
11 | | - |
12 | | -struct MetadataResponse { |
13 | | - let type: Any.Type |
14 | | - |
15 | | - let state: UInt8 |
16 | | -} |
| 4 | +import SwiftShims |
17 | 5 |
|
18 | 6 | struct MetadataAccessFunction { |
19 | | - let ptr: UnsafeRawPointer |
| 7 | + let ptr: UnsafeMutableRawPointer |
20 | 8 |
|
21 | | - func callAsFunction(request: UInt, args: [Any.Type]) -> MetadataResponse { |
| 9 | + func callAsFunction(request: Int, args: [Any.Type]) -> MetadataResponse { |
22 | 10 | args.withUnsafeBufferPointer { |
23 | | - _metadataAccessorCall( |
24 | | - fn: ptr, |
25 | | - request: request, |
26 | | - args: $0.baseAddress!, |
27 | | - count: $0.count |
28 | | - ) |
| 11 | + $0.baseAddress!.withMemoryRebound( |
| 12 | + to: UnsafeRawPointer?.self, |
| 13 | + capacity: args.count |
| 14 | + ) { |
| 15 | + _swift_metadataAccessorCall(ptr, request, $0, args.count) |
| 16 | + } |
29 | 17 | } |
30 | 18 | } |
31 | 19 | } |
32 | 20 |
|
33 | | -func callStructAccessor(for type: Any.Type, with generics: Any.Type...) { |
| 21 | +func callStructAccessor( |
| 22 | + for type: Any.Type, |
| 23 | + with generics: Any.Type... |
| 24 | +) -> MetadataResponse { |
34 | 25 | let metadata = unsafeBitCast(type, to: UnsafeRawPointer.self) |
35 | | - let descriptor = metadata.advanced(by: 8).load(as: UnsafeRawPointer.self) |
36 | | - let accessorLoc = descriptor.advanced(by: 12) |
| 26 | + let descriptor = metadata.advanced(by: MemoryLayout<Int>.size) |
| 27 | + .load(as: UnsafeMutableRawPointer.self) |
| 28 | + let accessorLoc = descriptor.advanced(by: MemoryLayout<Int32>.size * 3) |
37 | 29 | let accessor = accessorLoc.advanced(by: Int(accessorLoc.load(as: Int32.self))) |
38 | 30 |
|
39 | 31 | let accessFn = MetadataAccessFunction(ptr: accessor) |
40 | | - print(accessFn(request: 0, args: generics)) |
| 32 | + return accessFn(request: 0, args: generics) |
41 | 33 | } |
42 | 34 |
|
43 | | -// CHECK: MetadataResponse(type: Swift.Int, state: 0) |
44 | | -callStructAccessor(for: Int.self) |
45 | | - |
46 | | -// CHECK: MetadataResponse(type: Swift.Array<Swift.Double>, state: 0) |
47 | | -callStructAccessor(for: [Int].self, with: Double.self) |
48 | | - |
49 | | -// CHECK: MetadataResponse(type: Swift.Dictionary<Swift.Int, Swift.Array<Swift.Double>>, state: 0) |
50 | | -callStructAccessor(for: [String: [Int]].self, with: Int.self, [Double].self) |
| 35 | +let int = callStructAccessor(for: Int.self) |
| 36 | +// CHECK: Int |
| 37 | +print(unsafeBitCast(int.type!, to: Any.Type.self)) |
| 38 | +// CHECK: 0 |
| 39 | +print(int.state) |
| 40 | + |
| 41 | +let doubleArray = callStructAccessor(for: [Int].self, with: Double.self) |
| 42 | +// CHECK: Array<Double> |
| 43 | +print(unsafeBitCast(doubleArray.type!, to: Any.Type.self)) |
| 44 | +// CHECK: 0 |
| 45 | +print(doubleArray.state) |
| 46 | + |
| 47 | +let dictOfIntAndDoubleArray = callStructAccessor( |
| 48 | + for: [String: [Int]].self, |
| 49 | + with: Int.self, [Double].self |
| 50 | +) |
| 51 | +// CHECK: Dictionary<Int, Array<Double>> |
| 52 | +print(unsafeBitCast(dictOfIntAndDoubleArray.type!, to: Any.Type.self)) |
| 53 | +// CHECK: 0 |
| 54 | +print(dictOfIntAndDoubleArray.state) |
0 commit comments