Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
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
85 changes: 57 additions & 28 deletions Frameworks/Foundation/NSArray.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@
//
//******************************************************************************

#include "Starboard.h"
#include "StubReturn.h"
#include "CFHelpers.h"
#include "Foundation/NSMutableArray.h"
#include "Foundation/NSMutableData.h"
#include "Foundation/NSEnumerator.h"
#include "Foundation/NSKeyedArchiver.h"
#include "Foundation/NSArray.h"
#include "NSEnumeratorInternal.h"
#include "CoreFoundation/CFArray.h"
#include "Foundation/NSMutableString.h"
#include "NSKeyedArchiverInternal.h"
#include "Foundation/NSIndexSet.h"
#include "Foundation/NSNull.h"
#include "VAListHelper.h"
#include "LoggingNative.h"
#include "NSRaise.h"
#include "NSCFArray.h"
#include "BridgeHelpers.h"
#import <_NSKeyValueCodingAggregateFunctions.h>
#import "Starboard.h"
#import "StubReturn.h"
#import "CFHelpers.h"
#import "Foundation/NSMutableArray.h"
#import "Foundation/NSMutableData.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSKeyedArchiver.h"
#import "Foundation/NSArray.h"
#import "NSEnumeratorInternal.h"
#import "CoreFoundation/CFArray.h"
#import "Foundation/NSMutableString.h"
#import "NSKeyedArchiverInternal.h"
#import "Foundation/NSIndexSet.h"
#import "Foundation/NSNull.h"
#import "VAListHelper.h"
#import "LoggingNative.h"
#import "NSRaise.h"
#import "NSCFArray.h"
#import "BridgeHelpers.h"
#import "NSKeyValueObserving-Internal.h"
#import "_NSKeyValueCodingAggregateFunctions.h"

static const wchar_t* TAG = L"NSArray";

Expand Down Expand Up @@ -863,28 +864,56 @@ - (NSArray*)objectsAtIndexes:(NSIndexSet*)indexes {
}

/**
@Status Stub
@Status Interoperable
*/
- (void)addObserver:(id)observer forKeyPath:(NSString*)keyPath options:(NSKeyValueObservingOptions)options context:(void*)context {
NS_COLLECTION_THROW_ILLEGAL_KVO(keyPath);
}

/**
@Status Interoperable
*/
- (void)removeObserver:(id)observer forKeyPath:(NSString*)keyPath context:(void*)context {
NS_COLLECTION_THROW_ILLEGAL_KVO(keyPath);
}

/**
@Status Interoperable
*/
- (void)addObserver:(NSObject*)anObserver
- (void)removeObserver:(id)observer forKeyPath:(NSString*)keyPath {
NS_COLLECTION_THROW_ILLEGAL_KVO(keyPath);
}

/**
@Status Caveat
@Notes This is a convenience method, no performance gains for using this over individual calls to addObserver to affected objects
*/
- (void)addObserver:(NSObject*)observer
toObjectsAtIndexes:(NSIndexSet*)indexes
forKeyPath:(NSString*)keyPath
options:(NSKeyValueObservingOptions)options
context:(void*)context {
UNIMPLEMENTED();
[indexes enumerateIndexesUsingBlock:^(NSUInteger index, BOOL* stop) {
[[self objectAtIndex:index] addObserver:observer forKeyPath:keyPath options:options context:context];
}];
}

/**
@Status Stub
@Status Caveat
@Notes This is a convenience method, no performance gains for using this over individual calls to removeObserver to affected objects
*/
- (void)removeObserver:(NSObject*)anObserver fromObjectsAtIndexes:(NSIndexSet*)indexes forKeyPath:(NSString*)keyPath {
UNIMPLEMENTED();
- (void)removeObserver:(NSObject*)observer fromObjectsAtIndexes:(NSIndexSet*)indexes forKeyPath:(NSString*)keyPath {
[self removeObserver:observer fromObjectsAtIndexes:indexes forKeyPath:keyPath context:nullptr];
}

/**
@Status Stub
@Status Caveat
@Notes This is a convenience method, no performance gains for using this over individual calls to removeObserver to affected objects
*/
- (void)removeObserver:(NSObject*)observer fromObjectsAtIndexes:(NSIndexSet*)indexes forKeyPath:(NSString*)keyPath context:(void*)context {
UNIMPLEMENTED();
[indexes enumerateIndexesUsingBlock:^(NSUInteger index, BOOL* stop) {
[[self objectAtIndex:index] removeObserver:observer forKeyPath:keyPath context:context];
}];
}

/**
Expand Down
6 changes: 0 additions & 6 deletions Frameworks/Foundation/NSCFArray.mm
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(NSObject*)obj {
- (void)insertObject:(NSObject*)objAddr atIndex:(NSUInteger)index {
BRIDGED_THROW_IF_IMMUTABLE(_CFArrayIsMutable, CFArrayRef);
NS_COLLECTION_THROW_IF_NULL_REASON(objAddr, [NSString stringWithFormat:@"*** %@ object cannot be nil", NSStringFromSelector(_cmd)]);
if (objAddr == nil) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:[NSString stringWithFormat:@"*** %@ object cannot be nil", NSStringFromSelector(_cmd)]
userInfo:nil];
}

CFArrayInsertValueAtIndex(static_cast<CFMutableArrayRef>(self), index, reinterpret_cast<const void*>(objAddr));
}

Expand Down
8 changes: 3 additions & 5 deletions Frameworks/include/NSEnumeratorInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ __inline void _enumerateWithBlock(id<NSFastEnumeration> enumerator, NSEnumeratio
__block BOOL stop = FALSE;
for (id key in enumerator) {
if (options & NSEnumerationConcurrent) {
dispatch_group_async(group,
queue,
^() {
block(key, &stop);
});
dispatch_group_async(group, queue, ^() {
block(key, &stop);
});
} else {
block(key, &stop);
}
Expand Down
24 changes: 19 additions & 5 deletions Frameworks/include/NSKeyValueObserving-Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@
#import <Foundation/Foundation.h>
#include <Starboard.h>

#define NS_COLLECTION_THROW_ILLEGAL_KVO(keyPath) \
do { \
[NSException raise:NSInvalidArgumentException \
format:@"-[%hs %hs] is not supported. Key path: %@", object_getClassName(self), sel_getName(_cmd), keyPath]; \
} while (false)

@class _NSKVOKeypathObserver;

@interface _NSKVOKeyObserver: NSObject
- (instancetype)initWithObject:(id)object keypathObserver:(_NSKVOKeypathObserver*)keypathObserver key:(NSString*)key restOfKeypath:(NSString*)restOfKeypath affectedObservers:(NSArray*)affectedObservers;
@interface _NSKVOKeyObserver : NSObject
- (instancetype)initWithObject:(id)object
keypathObserver:(_NSKVOKeypathObserver*)keypathObserver
key:(NSString*)key
restOfKeypath:(NSString*)restOfKeypath
affectedObservers:(NSArray*)affectedObservers;
@property (nonatomic, retain) _NSKVOKeypathObserver* keypathObserver;
@property (nonatomic, retain) _NSKVOKeyObserver* restOfKeypathObserver;
@property (nonatomic, retain) NSArray* dependentObservers;
Expand All @@ -31,8 +41,12 @@
@property (nonatomic, assign) bool root;
@end

@interface _NSKVOKeypathObserver: NSObject
- (instancetype)initWithObject:(id)object observer:(id)observer keyPath:(NSString*)keypath options:(NSKeyValueObservingOptions)options context:(void*)context;
@interface _NSKVOKeypathObserver : NSObject
- (instancetype)initWithObject:(id)object
observer:(id)observer
keyPath:(NSString*)keypath
options:(NSKeyValueObservingOptions)options
context:(void*)context;
@property (nonatomic, assign) id object;
@property (nonatomic, assign) id observer;
@property (nonatomic, copy) NSString* keypath;
Expand All @@ -42,7 +56,7 @@
@property (atomic, retain) NSMutableDictionary* pendingChange;
@end

@interface _NSKVOObservationInfo: NSObject {
@interface _NSKVOObservationInfo : NSObject {
NSMutableDictionary<NSString*, NSArray<_NSKVOKeyObserver*>*>* _keyObserverMap;
NSInteger _dependencyDepth;
NSMutableSet<NSString*>* _existingDependentKeys;
Expand Down
9 changes: 3 additions & 6 deletions include/Foundation/NSArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,13 @@ FOUNDATION_EXPORT_CLASS
- (BOOL)writeToFile:(NSString*)path atomically:(BOOL)flag;
- (BOOL)writeToURL:(NSURL*)aURL atomically:(BOOL)flag;
- (NSArray<NSString*>*)pathsMatchingExtensions:(NSArray<NSString*>*)filterTypes;
- (void)removeObserver:(NSObject*)observer
fromObjectsAtIndexes:(NSIndexSet*)indexes
forKeyPath:(NSString*)keyPath
context:(void*)context STUB_METHOD;
- (void)removeObserver:(NSObject*)observer fromObjectsAtIndexes:(NSIndexSet*)indexes forKeyPath:(NSString*)keyPath context:(void*)context;
- (void)addObserver:(NSObject*)anObserver
toObjectsAtIndexes:(NSIndexSet*)indexes
forKeyPath:(NSString*)keyPath
options:(NSKeyValueObservingOptions)options
context:(void*)context STUB_METHOD;
- (void)removeObserver:(NSObject*)anObserver fromObjectsAtIndexes:(NSIndexSet*)indexes forKeyPath:(NSString*)keyPath STUB_METHOD;
context:(void*)context;
- (void)removeObserver:(NSObject*)anObserver fromObjectsAtIndexes:(NSIndexSet*)indexes forKeyPath:(NSString*)keyPath;
- (void)setValue:(id)value forKey:(NSString*)key;
- (id)valueForKey:(NSString*)key;
@end
59 changes: 59 additions & 0 deletions tests/unittests/Foundation/NSObject_KeyValueObservationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,63 @@ - (NSUInteger)hits {
}
andExpectChangeCallbacks:@[ insertCallbackPost, illegalChangeNotification ]];
EXPECT_EQ(1, facade.hits);
}

TEST(KVO, NSArrayShouldNotBeObservable) {
NSArray* test = @[ @1, @2, @3 ];
_NSFoundationTestKVOObserver* observer = [[_NSFoundationTestKVOObserver new] autorelease];
EXPECT_ANY_THROW([test addObserver:observer forKeyPath:@"count" options:0 context:nullptr]);

// These would throw anyways because there should be no observer for the key path, but test anyways
EXPECT_ANY_THROW([test removeObserver:observer forKeyPath:@"count"]);
EXPECT_ANY_THROW([test removeObserver:observer forKeyPath:@"count" context:nullptr]);
}

TEST(KVO, NSArrayShouldThrowWhenTryingToObserveIndexesOutOfRange) {
NSArray* test = @[ [[TEST_IDENT(Observee) new] autorelease], [[TEST_IDENT(Observee) new] autorelease] ];
_NSFoundationTestKVOObserver* observer = [[_NSFoundationTestKVOObserver new] autorelease];
EXPECT_ANY_THROW(
[test addObserver:observer toObjectsAtIndexes:[NSIndexSet indexSetWithIndex:4] forKeyPath:@"bareArray" options:0 context:nullptr]);
}

TEST(KVO, NSArrayObserveElements) {
NSArray* observeeArray =
@[ [[TEST_IDENT(Observee) new] autorelease], [[TEST_IDENT(Observee) new] autorelease], [[TEST_IDENT(Observee) new] autorelease] ];
_NSFoundationTestKVOObserver* observer = [[_NSFoundationTestKVOObserver new] autorelease];
EXPECT_NO_THROW([observeeArray addObserver:observer
toObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]
forKeyPath:@"manualNotificationArray"
options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew)
context:nullptr]);

// First two elements in range for observation so observer will receive changes
[observeeArray[0] addObjectToManualArray:@"object1"];
[observeeArray[0] addObjectToManualArray:@"object2"];
EXPECT_EQ(2, observer.hits);

[observeeArray[1] addObjectToManualArray:@"object1"];
EXPECT_EQ(3, observer.hits);

// But the third element is not so observer will not receive changes
[observeeArray[2] addObjectToManualArray:@"object1"];
EXPECT_EQ(3, observer.hits);

EXPECT_NO_THROW([observeeArray removeObserver:observer
fromObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)]
forKeyPath:@"manualNotificationArray"]);

// Removed observer from first element, so modifying it will not report a change
[observeeArray[0] addObjectToManualArray:@"object3"];
EXPECT_EQ(3, observer.hits);

// But the second element is still being observed
[observeeArray[1] addObjectToManualArray:@"object2"];
EXPECT_EQ(4, observer.hits);

EXPECT_NO_THROW([observeeArray removeObserver:observer
fromObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 1)]
forKeyPath:@"manualNotificationArray"]);

[observeeArray[1] addObjectToManualArray:@"object3"];
EXPECT_EQ(4, observer.hits);
}