@@ -164,20 +164,20 @@ static Class getSwiftNativeNSErrorClass() {
164
164
auto TheSwiftNativeNSError = getSwiftNativeNSErrorClass ();
165
165
assert (class_getInstanceSize (TheSwiftNativeNSError) == sizeof (SwiftErrorHeader)
166
166
&& " NSError layout changed!" );
167
-
167
+
168
168
// Determine the extra allocated space necessary to carry the value.
169
169
// TODO: If the error type is a simple enum with no associated values, we
170
170
// could emplace it in the "code" slot of the NSError and save ourselves
171
171
// some work.
172
-
172
+
173
173
unsigned size = type->getValueWitnesses ()->getSize ();
174
174
unsigned alignMask = type->getValueWitnesses ()->getAlignmentMask ();
175
175
176
176
size_t alignmentPadding = -sizeof (SwiftError) & alignMask;
177
177
size_t totalExtraSize = sizeof (SwiftError) - sizeof (SwiftErrorHeader)
178
178
+ alignmentPadding + size;
179
179
size_t valueOffset = alignmentPadding + sizeof (SwiftError);
180
-
180
+
181
181
// Allocate the instance as if it were a CFError. We won't really initialize
182
182
// the CFError parts until forced to though.
183
183
auto instance
@@ -194,15 +194,15 @@ static Class getSwiftNativeNSErrorClass() {
194
194
195
195
auto valueBytePtr = reinterpret_cast <char *>(instance) + valueOffset;
196
196
auto valuePtr = reinterpret_cast <OpaqueValue*>(valueBytePtr);
197
-
197
+
198
198
// If an initial value was given, copy or take it in.
199
199
if (initialValue) {
200
200
if (isTake)
201
201
type->vw_initializeWithTake (valuePtr, initialValue);
202
202
else
203
203
type->vw_initializeWithCopy (valuePtr, initialValue);
204
204
}
205
-
205
+
206
206
// Return the SwiftError reference and a pointer to the uninitialized value
207
207
// inside.
208
208
return BoxPair{reinterpret_cast <HeapObject*>(instance), valuePtr};
@@ -241,13 +241,13 @@ static Class getSwiftNativeNSErrorClass() {
241
241
// The witness table lives in the Foundation overlay, but it should be safe
242
242
// to assume that that's been linked in if a user is using NSError in their
243
243
// Swift source.
244
-
244
+
245
245
auto TheWitnessTable = SWIFT_LAZY_CONSTANT (dlsym (RTLD_DEFAULT,
246
246
" _TWPCSo7CFErrors5Error10Foundation" ));
247
247
assert (TheWitnessTable &&
248
248
" Foundation overlay not loaded, or 'CFError : Error' conformance "
249
249
" not available" );
250
-
250
+
251
251
return reinterpret_cast <const WitnessTable *>(TheWitnessTable);
252
252
}
253
253
@@ -342,15 +342,15 @@ static Class getSwiftNativeNSErrorClass() {
342
342
if (errorObject->isPureNSError ()) {
343
343
// Return a pointer to the scratch buffer.
344
344
auto asError = (NSError *)errorObject;
345
-
345
+
346
346
*scratch = (void *)errorObject;
347
347
out->value = (const OpaqueValue *)scratch;
348
348
out->type = swift_getObjCClassMetadata ((ClassMetadata*)[asError class ]);
349
349
350
350
out->errorConformance = getNSErrorConformanceToError ();
351
351
return ;
352
352
}
353
-
353
+
354
354
out->value = errorObject->getValue ();
355
355
out->type = errorObject->type ;
356
356
out->errorConformance = errorObject->errorConformance ;
@@ -424,13 +424,13 @@ static id _swift_bridgeErrorToNSError_(SwiftError *errorObject) {
424
424
if (errorObject->domain .load (std::memory_order_acquire) &&
425
425
errorObject->userInfo .load (std::memory_order_acquire))
426
426
return ns;
427
-
427
+
428
428
// Otherwise, calculate the domain and code (TODO: and user info), and
429
429
// initialize the NSError.
430
430
auto value = SwiftError::getIndirectValue (&errorObject);
431
431
auto type = errorObject->getType ();
432
432
auto witness = errorObject->getErrorConformance ();
433
-
433
+
434
434
NSString *domain = swift_stdlib_getErrorDomainNSString (value, type, witness);
435
435
NSInteger code = swift_stdlib_getErrorCode (value, type, witness);
436
436
NSDictionary *userInfo =
@@ -464,7 +464,7 @@ static id _swift_bridgeErrorToNSError_(SwiftError *errorObject) {
464
464
(CFStringRef)domain,
465
465
std::memory_order_acq_rel))
466
466
objc_release (domain);
467
-
467
+
468
468
return ns;
469
469
}
470
470
@@ -483,7 +483,7 @@ static id _swift_bridgeErrorToNSError_(SwiftError *errorObject) {
483
483
const Metadata *destType,
484
484
DynamicCastFlags flags) {
485
485
Class NSErrorClass = getNSErrorClass ();
486
-
486
+
487
487
auto CFErrorTypeID = SWIFT_LAZY_CONSTANT (CFErrorGetTypeID ());
488
488
// @_silgen_name("swift_stdlib_bridgeNSErrorToError")
489
489
// public func _stdlib_bridgeNSErrorToError<
@@ -503,7 +503,7 @@ bool BridgeFn(NSError *, OpaqueValue*, const Metadata *,
503
503
// If the Foundation overlay isn't loaded, then NSErrors can't be bridged.
504
504
if (!bridgeNSErrorToError || !TheObjectiveCBridgeableError)
505
505
return false ;
506
-
506
+
507
507
// Is the input type an NSError?
508
508
switch (srcType->getKind ()) {
509
509
case MetadataKind::Class :
@@ -541,14 +541,14 @@ bool BridgeFn(NSError *, OpaqueValue*, const Metadata *,
541
541
case MetadataKind::Tuple:
542
542
return false ;
543
543
}
544
-
544
+
545
545
// Is the target type a bridgeable error?
546
546
auto witness = swift_conformsToProtocol (destType,
547
547
TheObjectiveCBridgeableError);
548
-
548
+
549
549
if (!witness)
550
550
return false ;
551
-
551
+
552
552
// If so, attempt the bridge.
553
553
NSError *srcInstance = *reinterpret_cast <NSError * const *>(src);
554
554
objc_retain (srcInstance);
0 commit comments