|
10 | 10 |
|
11 | 11 | #include "Discovery.h"
|
12 | 12 |
|
13 |
| -#include <cstdlib> |
14 | 13 | #include <cstdint>
|
15 | 14 | #include <cstring>
|
16 | 15 | #include <type_traits>
|
17 |
| -#include <vector> |
18 | 16 |
|
19 | 17 | #if defined(SWT_NO_DYNAMIC_LINKING)
|
20 | 18 | #pragma mark - Statically-linked section bounds
|
@@ -189,46 +187,32 @@ struct SWTTypeMetadataRecord {
|
189 | 187 |
|
190 | 188 | #pragma mark - Legacy test discovery
|
191 | 189 |
|
192 |
| -void **swt_copyTypesWithNamesContaining(const void *sectionBegin, size_t sectionSize, const char *nameSubstring, size_t *outCount) { |
193 |
| - void **result = nullptr; |
194 |
| - size_t resultCount = 0; |
195 |
| - |
196 |
| - auto records = reinterpret_cast<const SWTTypeMetadataRecord *>(sectionBegin); |
197 |
| - size_t recordCount = sectionSize / sizeof(SWTTypeMetadataRecord); |
198 |
| - for (size_t i = 0; i < recordCount; i++) { |
199 |
| - auto contextDescriptor = records[i].getContextDescriptor(); |
200 |
| - if (!contextDescriptor) { |
201 |
| - // This type metadata record is invalid (or we don't understand how to |
202 |
| - // get its context descriptor), so skip it. |
203 |
| - continue; |
204 |
| - } else if (contextDescriptor->isGeneric()) { |
205 |
| - // Generic types cannot be fully instantiated without generic |
206 |
| - // parameters, which is not something we can know abstractly. |
207 |
| - continue; |
208 |
| - } |
| 190 | +const size_t SWTTypeMetadataRecordByteCount = sizeof(SWTTypeMetadataRecord); |
209 | 191 |
|
210 |
| - // Check that the type's name passes. This will be more expensive than the |
211 |
| - // checks above, but should be cheaper than realizing the metadata. |
212 |
| - const char *typeName = contextDescriptor->getName(); |
213 |
| - bool nameOK = typeName && nullptr != std::strstr(typeName, nameSubstring); |
214 |
| - if (!nameOK) { |
215 |
| - continue; |
216 |
| - } |
| 192 | +const void *swt_getTypeFromTypeMetadataRecord(const void *recordAddress, const char *nameSubstring) { |
| 193 | + auto record = reinterpret_cast<const SWTTypeMetadataRecord *>(recordAddress); |
| 194 | + auto contextDescriptor = record->getContextDescriptor(); |
| 195 | + if (!contextDescriptor) { |
| 196 | + // This type metadata record is invalid (or we don't understand how to |
| 197 | + // get its context descriptor), so skip it. |
| 198 | + return nullptr; |
| 199 | + } else if (contextDescriptor->isGeneric()) { |
| 200 | + // Generic types cannot be fully instantiated without generic |
| 201 | + // parameters, which is not something we can know abstractly. |
| 202 | + return nullptr; |
| 203 | + } |
217 | 204 |
|
218 |
| - if (void *typeMetadata = contextDescriptor->getMetadata()) { |
219 |
| - if (!result) { |
220 |
| - // This is the first matching type we've found. That presumably means |
221 |
| - // we'll find more, so allocate enough space for all remaining types in |
222 |
| - // the section. Is this necessarily space-efficient? No, but this |
223 |
| - // allocation is short-lived and is immediately copied and freed in the |
224 |
| - // Swift caller. |
225 |
| - result = reinterpret_cast<void **>(std::calloc(recordCount - i, sizeof(void *))); |
226 |
| - } |
227 |
| - result[resultCount] = typeMetadata; |
228 |
| - resultCount += 1; |
229 |
| - } |
| 205 | + // Check that the type's name passes. This will be more expensive than the |
| 206 | + // checks above, but should be cheaper than realizing the metadata. |
| 207 | + const char *typeName = contextDescriptor->getName(); |
| 208 | + bool nameOK = typeName && nullptr != std::strstr(typeName, nameSubstring); |
| 209 | + if (!nameOK) { |
| 210 | + return nullptr; |
| 211 | + } |
| 212 | + |
| 213 | + if (void *typeMetadata = contextDescriptor->getMetadata()) { |
| 214 | + return typeMetadata; |
230 | 215 | }
|
231 | 216 |
|
232 |
| - *outCount = resultCount; |
233 |
| - return result; |
| 217 | + return nullptr; |
234 | 218 | }
|
0 commit comments