Skip to content

[+0-all] Update Reflection.mm for +0 normal args. #13672

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
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
55 changes: 53 additions & 2 deletions stdlib/public/runtime/Reflection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ static Mirror reflect(HeapObject *owner,
const OpaqueValue *mirrorValue;
std::tie(mirrorType, mirrorValue) = unwrapExistential(T, value);

#ifdef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_retain(owner);
#endif

// Use MagicMirror.
// Consumes 'owner'.
Mirror result;
Expand All @@ -346,7 +350,9 @@ intptr_t swift_TupleMirror_count(HeapObject *owner,
const OpaqueValue *value,
const Metadata *type) {
auto Tuple = static_cast<const TupleTypeMetadata *>(type);
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif
return Tuple->NumElements;
}

Expand Down Expand Up @@ -392,6 +398,10 @@ void swift_TupleMirror_subscript(String *outString,
auto bytes = reinterpret_cast<const char*>(value);
auto eltData = reinterpret_cast<const OpaqueValue *>(bytes + elt.Offset);

#ifdef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_retain(owner);
#endif

// 'owner' is consumed by this call.
new (outMirror) Mirror(reflect(owner, eltData, elt.Type));
}
Expand Down Expand Up @@ -460,13 +470,15 @@ static bool loadSpecialReferenceStorage(HeapObject *owner,

type->deallocateBufferIn(&temporaryBuffer);

#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
// swift_StructMirror_subscript and swift_ClassMirror_subscript
// requires that the owner be consumed. Since we have the new heap box as the
// owner now, we need to release the old owner to maintain the contract.
if (owner->metadata->isAnyClass())
swift_unknownRelease(owner);
else
swift_release(owner);
#endif

return true;
}
Expand All @@ -478,7 +490,9 @@ intptr_t swift_StructMirror_count(HeapObject *owner,
const OpaqueValue *value,
const Metadata *type) {
auto Struct = static_cast<const StructMetadata *>(type);
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif
return Struct->Description->Struct.NumFields;
}

Expand Down Expand Up @@ -563,7 +577,9 @@ static void getEnumMirrorInfo(const OpaqueValue *value,
const OpaqueValue *value,
const Metadata *type) {
if (!isEnumReflectable(type)) {
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif
return nullptr;
}

Expand All @@ -573,7 +589,9 @@ static void getEnumMirrorInfo(const OpaqueValue *value,
unsigned tag;
getEnumMirrorInfo(value, type, &tag, nullptr, nullptr);

#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif

return getFieldName(Description.CaseNames, tag);
}
Expand All @@ -588,7 +606,12 @@ static void getEnumMirrorInfo(const OpaqueValue *value,
OpaqueValue *mirrorValue = const_cast<OpaqueValue*>(cMirrorValue);
Mirror mirror;

bool take = mirrorValue == value;
bool take =
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
mirrorValue == value;
#else
false;
#endif
::new (&mirror) MagicMirror(mirrorValue, mirrorType, take);

MagicMirror *theMirror = reinterpret_cast<MagicMirror *>(&mirror);
Expand All @@ -602,13 +625,17 @@ intptr_t swift_EnumMirror_count(HeapObject *owner,
const OpaqueValue *value,
const Metadata *type) {
if (!isEnumReflectable(type)) {
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif
return 0;
}

const Metadata *payloadType;
getEnumMirrorInfo(value, type, nullptr, &payloadType, nullptr);
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif
return (payloadType != nullptr) ? 1 : 0;
}

Expand Down Expand Up @@ -637,7 +664,9 @@ void swift_EnumMirror_subscript(String *outString,
type->vw_destructiveInjectEnumTag(const_cast<OpaqueValue *>(value),
(int) (tag - Description.getNumPayloadCases()));

#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif

owner = pair.object;
value = pair.buffer;
Expand Down Expand Up @@ -665,7 +694,9 @@ intptr_t swift_ClassMirror_count(HeapObject *owner,
const OpaqueValue *value,
const Metadata *type) {
auto Clas = static_cast<const ClassMetadata*>(type);
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif
auto count = Clas->getDescription()->Class.NumFields;

// If the class has a superclass, the superclass instance is treated as the
Expand Down Expand Up @@ -771,7 +802,9 @@ intptr_t swift_ObjCMirror_count(HeapObject *owner,
if (isa->SuperClass)
count += 1;

#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif
return count;
}

Expand Down Expand Up @@ -810,7 +843,9 @@ void swift_ObjCMirror_subscript(String *outString,
swift_ClassMirror_quickLookObject(HeapObject *owner, const OpaqueValue *value,
const Metadata *type) {
id object = [*reinterpret_cast<const id *>(value) retain];
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_release(owner);
#endif
if ([object respondsToSelector:@selector(debugQuickLookObject)]) {
id quickLookObject = [object debugQuickLookObject];
[quickLookObject retain];
Expand Down Expand Up @@ -906,6 +941,10 @@ static Mirror getMirrorForSuperclass(const ClassMetadata *sup,
Mirror resultBuf;
MagicMirror *result = ::new (&resultBuf) MagicMirror;

#ifdef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_retain(owner);
#endif

result->Self = ClassSuperMirrorMetadata();
result->MirrorWitness = &ClassSuperMirrorWitnessTable;
result->Data.Owner = owner;
Expand All @@ -925,6 +964,10 @@ static Mirror ObjC_getMirrorForSuperclass(Class sup,
Mirror resultBuf;
MagicMirror *result = ::new (&resultBuf) MagicMirror;

#ifdef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
swift_retain(owner);
#endif

result->Self = ObjCSuperMirrorMetadata();
result->MirrorWitness = &ObjCSuperMirrorWitnessTable;
result->Data.Owner = owner;
Expand Down Expand Up @@ -1073,11 +1116,19 @@ static Mirror ObjC_getMirrorForSuperclass(Class sup,
Mirror result;
// Take the value, unless we projected a subvalue from it. We don't want to
// deal with partial value deinitialization.
bool take = mirrorValue == value;
bool take =
#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
mirrorValue == value;
#else
false;
#endif
::new (&result) MagicMirror(mirrorValue, mirrorType, take);

#ifndef SWIFT_RUNTIME_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
// Destroy the whole original value if we couldn't take it.
if (!take)
T->vw_destroy(value);
#endif
return MirrorReturn(result);
}

Expand Down