From: Jay Freeman (saurik) Date: Wed, 6 Jan 2016 08:03:19 +0000 (-0800) Subject: The value from +alloc can be a permanent (NSDate). X-Git-Tag: v0.9.590~50 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/78b24692d45f090f846e4c327ca7fa2ee6d302eb The value from +alloc can be a permanent (NSDate). --- diff --git a/ObjectiveC/Internal.hpp b/ObjectiveC/Internal.hpp index 8606d55..deb5e5d 100644 --- a/ObjectiveC/Internal.hpp +++ b/ObjectiveC/Internal.hpp @@ -38,11 +38,10 @@ struct Selector_privateData : struct Instance : CYValue { - enum Flags { - None = 0, - Permanent = (1 << 0), - Uninitialized = (1 << 1), - }; + typedef unsigned Flags; + static const Flags None = 0; + static const Flags Permanent = 1 << 0; + static const Flags Uninitialized = 1 << 1; Flags flags_; @@ -53,6 +52,10 @@ struct Instance : static JSClassRef GetClass(id value, Flags flags); + _finline bool IsPermanent() const { + return (flags_ & Permanent) != 0; + } + _finline bool IsUninitialized() const { return (flags_ & Uninitialized) != 0; } diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 2714912..140b759 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -353,12 +353,15 @@ Instance::Instance(id value, Flags flags) : CYValue(value), flags_(flags) { - if ((flags & Instance::Permanent) == 0) + if (IsPermanent()); + /*else if ([value retainCount] == NSUInteger(-1)) + flags_ |= Instance::Permanent;*/ + else value_ = [value_ retain]; } Instance::~Instance() { - if ((flags_ & Permanent) == 0) + if (!IsPermanent()) [value_ release]; } @@ -1558,10 +1561,12 @@ JSValueRef Object::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool if (initialize) { Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); - if ((internal->flags_ & Instance::Uninitialized) != 0) { - internal->flags_ = static_cast(internal->flags_ & ~Instance::Uninitialized); - _assert(internal->value_ == nil); - internal->value_ = value; + if (internal->IsUninitialized()) { + internal->flags_ &= ~Instance::Uninitialized; + if (internal->value_ == nil) + internal->value_ = value; + else + _assert(internal->value_ == value); } [value release]; @@ -2449,7 +2454,7 @@ static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObje self = internal->value_; _class = nil; uninitialized = internal->IsUninitialized(); - if (uninitialized) + if (uninitialized && [internal->value_ retainCount] != NSUInteger(-1)) internal->value_ = nil; } else { self = CYCastNSObject(&pool, context, arguments[0]);