Skip to content

Runtime: Forward SwiftValue -description and -debugDescription to the boxed value. #3581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions stdlib/public/runtime/SwiftObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ SWIFT_RUNTIME_EXPORT @interface SwiftObject<NSObject> {

namespace swift {

struct String { void *x, *y, *z; };

/// Helper from the standard library for stringizing an arbitrary object.
extern "C" SWIFT_CC(swift)
void swift_getSummary(String *out, OpaqueValue *value, const Metadata *T);

// Convert a Swift String to an NSString.
NSString *convertStringToNSString(String *swiftString);

#endif

}
Expand Down
44 changes: 16 additions & 28 deletions stdlib/public/runtime/SwiftObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <unordered_map>
#if SWIFT_OBJC_INTEROP
# import <CoreFoundation/CFBase.h> // for CFTypeID
# import <Foundation/Foundation.h>
# include <malloc/malloc.h>
# include <dispatch/dispatch.h>
#endif
Expand Down Expand Up @@ -91,20 +92,13 @@ static uintptr_t computeISAMask() {
class_getInstanceSize(cls), mask));
}

// Helper from the standard library for stringizing an arbitrary object.
namespace {
struct String { void *x, *y, *z; };
}

extern "C" void swift_getSummary(String *out, OpaqueValue *value,
const Metadata *T);

static NSString *_getDescription(SwiftObject *obj) {
NSString *swift::convertStringToNSString(String *swiftString) {
typedef SWIFT_CC(swift) NSString *ConversionFn(void *sx, void *sy, void *sz);

// Cached lookup of swift_convertStringToNSString, which is in Foundation.
static ConversionFn *convertStringToNSString = nullptr;

static std::atomic<ConversionFn *> TheConvertStringToNSString(nullptr);
auto convertStringToNSString =
TheConvertStringToNSString.load(std::memory_order_relaxed);
if (!convertStringToNSString) {
convertStringToNSString = (ConversionFn *)(uintptr_t)
dlsym(RTLD_DEFAULT, "swift_convertStringToNSString");
Expand All @@ -113,31 +107,25 @@ static uintptr_t computeISAMask() {
// ObjC interop is low.
if (!convertStringToNSString)
return @"SwiftObject";

TheConvertStringToNSString.store(convertStringToNSString,
std::memory_order_relaxed);
}

return convertStringToNSString(swiftString->x,
swiftString->y,
swiftString->z);
}

static NSString *_getDescription(SwiftObject *obj) {
String tmp;
swift_retain((HeapObject*)obj);
swift_getSummary(&tmp, (OpaqueValue*)&obj, _swift_getClassOfAllocated(obj));
return [convertStringToNSString(tmp.x, tmp.y, tmp.z) autorelease];
return [convertStringToNSString(&tmp) autorelease];
}

static NSString *_getClassDescription(Class cls) {
typedef SWIFT_CC(swift) NSString *ConversionFn(Class cls);

// Cached lookup of NSStringFromClass, which is in Foundation.
static ConversionFn *NSStringFromClass_fn = nullptr;

if (!NSStringFromClass_fn) {
NSStringFromClass_fn = (ConversionFn *)(uintptr_t)
dlsym(RTLD_DEFAULT, "NSStringFromClass");
// If Foundation hasn't loaded yet, fall back to returning the static string
// "SwiftObject". The likelihood of someone invoking +description without
// ObjC interop is low.
if (!NSStringFromClass_fn)
return @"SwiftObject";
}

return NSStringFromClass_fn(cls);
return NSStringFromClass(cls);
}


Expand Down
24 changes: 23 additions & 1 deletion stdlib/public/runtime/SwiftValue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ - (void)dealloc {
}
#pragma clang diagnostic pop

static NSString *getValueDescription(SwiftValue *self) {
String tmp;
const Metadata *type;
const OpaqueValue *value;
std::tie(type, value) = getValueFromSwiftValue(self);

// Copy the value, since it will be consumed by getSummary.
ValueBuffer copyBuf;
auto copy = type->vw_initializeBufferWithCopy(&copyBuf,
const_cast<OpaqueValue*>(value));
swift_getSummary(&tmp, copy, type);
type->vw_deallocateBuffer(&copyBuf);
return convertStringToNSString(&tmp);
}

- (NSString *)description {
return getValueDescription(self);
}
- (NSString *)debugDescription {
return getValueDescription(self);
}

// Private methods for debugging purposes.

- (const Metadata *)_swiftTypeMetadata {
Expand All @@ -189,7 +211,7 @@ - (const OpaqueValue *)_swiftValue {
return getValueFromSwiftValue(self).second;
}

// TODO: Forward description, debugDescription, isEqual:, hash, etc. to
// TODO: Forward isEqual: and hash to
// corresponding operations on the boxed Swift type.

@end
Expand Down
28 changes: 28 additions & 0 deletions test/1_stdlib/BridgeIdAsAny.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ func ==(a: KnownUnbridged, b: KnownUnbridged) -> Bool {
return a.x === b.x && a.y === b.y
}

struct KnownUnbridgedWithDescription: CustomStringConvertible,
CustomDebugStringConvertible {
var x, y: LifetimeTracked

init() {
x = LifetimeTracked(17)
y = LifetimeTracked(38)
}

var description: String {
return "\(x)\(y), baby, hey"
}

var debugDescription: String {
return "KnownUnbridgedWithDescription(\"\(x)\(y)\" /* baby, hey */)"
}
}

func bridgedObjectPreservesIdentity(original: LifetimeTracked,
bridged: AnyObject) {
expectTrue(original === bridged)
Expand Down Expand Up @@ -115,4 +133,14 @@ BridgeAnything.test("${testName}") {
}
% end

BridgeAnything.test("description of boxed values") {
for x in [KnownUnbridged(), KnownUnbridgedWithDescription()] as [Any] {
let summary = String(reflecting: x)
expectEqual(summary, _bridgeAnythingToObjectiveC(x).description)
expectEqual(summary, _bridgeAnythingToObjectiveC(x).debugDescription)
}

expectEqual(0, LifetimeTracked.instances)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test harness does the leak check for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, thanks @gribozavr !

}

runAllTests()