Skip to content

Commit

Permalink
Initial port of WinObjC's KVO implementation to GNUstep (#420)
Browse files Browse the repository at this point in the history
* GSAtomic: Add prefix to macro definitions

* NSKVOSupport: Import

* NSKVOSupport: Add test cases

* NSKVOSwizzling: Ugly C Rewrite

* NSKeyValueObserving: Use old implementation as fallback

* NSKeyValueObserving: Rename TypeEncodingCases header

* NSKVOSupport: Fix new objects not being added to NSKeyValueChangeNew set on set mutation

* NSKeyValueMutableSet: Fix will and didChange notifications for set operations

* NSKeyValueMutableSet: Document Accessor Search Patterns

* NSKVOSupport: Add toMany test

* NSKeyValueCoding: Change notifications when changing value via setValue:forKey:

* NSKVOSupport: Add more tests

* NSKVOSupport: Do not wrap block in try/finally to avoid crash in windows

* NSKVOSwizzling: use _alloca on Windows

* NSKVOSupport: Do not autorelease newWithObservee:

* NSKVOSupport: Do not leak Observee and TestFacade objects

* Improve runtime detection in makefile

* Add file extension of source file in GNUMakefile

* NSKVOSupport: Remove @status comments

* NSKVOSupport: Implement private notify method

* NSUserDefaults: KVO Support and fix macOS incompatibilities

* NSKeyValueObserving: Set old to null if nil

* NSKeyValueObserving: Remove cached new value

* NSMethodSignature: Add signature cache

* NSKVOSupport: Remove ObjC2 features and mark tests failing on GCC as hopeful

* Call class method instead of private _keyPathsForValuesAffectingValueForKey

* Move _keyPathsForValuesAffectingValueForKey body into class method and statically construct empty NSSet

* NSUserDefaults: Change notification should contain old value from other domains aswell

* NSUserDefaults: Fetch new value from all domains

* NSKVOInternal: Fixup filename in header

* NSUserDefaults: Go through search list instead of only one domain in KVO change

* Making indentation a bit less worse

* Add NSUserDefaults KVO tests

* NSKVOSupport: NSUserDefaults test small fixes

* Add autoreleasepool

* NSUserDefaults: Only emit change notifications if value changed

* Avoid compiler warnings and tidy some of the whitespace/formatting

---------

Co-authored-by: Frederik Seiffert <frederik@algoriddim.com>
Co-authored-by: rfm <richardfrithmacdonald@gmail.com>
Co-authored-by: rfm <rfm@gnu.org>
  • Loading branch information
4 people authored Nov 10, 2024
1 parent 544dcce commit 6681a3d
Show file tree
Hide file tree
Showing 20 changed files with 6,710 additions and 116 deletions.
13 changes: 12 additions & 1 deletion Source/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ NSJSONSerialization.m \
NSKeyedArchiver.m \
NSKeyedUnarchiver.m \
NSKeyValueCoding.m \
NSKeyValueObserving.m \
NSLengthFormatter.m \
NSLinguisticTagger.m \
NSLocale.m \
Expand Down Expand Up @@ -358,6 +357,18 @@ NSZone.m \
externs.m \
objc-load.m

# We have two implementations for Key Value Observing.
# One highly-optimised one that depends on libobjc2
# and the original implementation.
ifeq ($(OBJC_RUNTIME_LIB), ng)
BASE_MFILES += \
NSKVOSupport.m \
NSKVOSwizzling.m
else
BASE_MFILES += \
NSKeyValueObserving.m
endif

ifeq ($(OBJC_RUNTIME_LIB), ng)
BASE_MFILES += \
NSKeyValueCoding+Caching.m
Expand Down
22 changes: 11 additions & 11 deletions Source/GSAtomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* Use native C11 atomic operations. _Atomic() should be defined by the
* compiler.
*/
#define atomic_load_explicit(object, order) \
#define gs_atomic_load_explicit(object, order) \
__c11_atomic_load(object, order)
#define atomic_store_explicit(object, desired, order) \
#define gs_atomic_store_explicit(object, desired, order) \
__c11_atomic_store(object, desired, order)

#else
Expand All @@ -33,15 +33,15 @@
#define _Atomic(T) struct { T volatile __val; }
#if __has_builtin(__sync_swap)
/* Clang provides a full-barrier atomic exchange - use it if available. */
#define atomic_exchange_explicit(object, desired, order) \
#define gs_atomic_exchange_explicit(object, desired, order) \
((void)(order), __sync_swap(&(object)->__val, desired))
#else
/*
* __sync_lock_test_and_set() is only an acquire barrier in theory (although in
* practice it is usually a full barrier) so we need an explicit barrier before
* it.
*/
#define atomic_exchange_explicit(object, desired, order) \
#define gs_atomic_exchange_explicit(object, desired, order) \
__extension__ ({ \
__typeof__(object) __o = (object); \
__typeof__(desired) __d = (desired); \
Expand All @@ -50,10 +50,10 @@ __extension__ ({ \
__sync_lock_test_and_set(&(__o)->__val, __d); \
})
#endif
#define atomic_load_explicit(object, order) \
#define gs_atomic_load_explicit(object, order) \
((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
#define atomic_store_explicit(object, desired, order) \
((void)atomic_exchange_explicit(object, desired, order))
#define gs_atomic_store_explicit(object, desired, order) \
((void)gs_atomic_exchange_explicit(object, desired, order))

#endif

Expand All @@ -64,9 +64,9 @@ __extension__ ({ \
/*
* Convenience functions.
*/
#define atomic_load(object) \
atomic_load_explicit(object, __ATOMIC_SEQ_CST)
#define atomic_store(object, desired) \
atomic_store_explicit(object, desired, __ATOMIC_SEQ_CST)
#define gs_atomic_load(object) \
gs_atomic_load_explicit(object, __ATOMIC_SEQ_CST)
#define gs_atomic_store(object, desired) \
gs_atomic_store_explicit(object, desired, __ATOMIC_SEQ_CST)

#endif // _GSAtomic_h_
6 changes: 6 additions & 0 deletions Source/GSPThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ typedef CONDITION_VARIABLE gs_cond_t;
#define GS_COND_BROADCAST(cond) WakeAllConditionVariable(&(cond))

/* Pthread-like locking primitives defined in NSLock.m */
#ifdef __cplusplus
extern "C" {
#endif
void gs_mutex_init(gs_mutex_t *l, gs_mutex_attr_t attr);
int gs_mutex_lock(gs_mutex_t *l);
int gs_mutex_trylock(gs_mutex_t *l);
int gs_mutex_unlock(gs_mutex_t *l);
int gs_cond_wait(gs_cond_t *cond, gs_mutex_t *mutex);
int gs_cond_timedwait(gs_cond_t *cond, gs_mutex_t *mutex, DWORD millisecs);
#ifdef __cplusplus
}
#endif

/*
* Threading primitives.
Expand Down
129 changes: 129 additions & 0 deletions Source/NSKVOInternal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
NSKVOInternal.h
Copyright (C) 2024 Free Software Foundation, Inc.
Written by: Hugo Melder <hugo@algoriddim.com>
Date: June 2024
Based on WinObjC KVO tests by Microsoft Corporation.
This file is part of GNUStep-base
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
If you are interested in a warranty or support for this source code,
contact Scott Christley <scottc@net-community.com> for more information.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
/**
Copyright (c) Microsoft. All rights reserved.
This code is licensed under the MIT License (MIT).
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/* This Key Value Observing Implementation is tied to libobjc2 */

#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSKeyValueObserving.h>
#import <Foundation/NSException.h>

#if defined(__OBJC2__)

#import "GSPThread.h"

#define NS_COLLECTION_THROW_ILLEGAL_KVO(keyPath) \
do \
{ \
[NSException \
raise: NSInvalidArgumentException \
format: @"-[%s %s] 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;
@property (nonatomic, retain) _NSKVOKeypathObserver *keypathObserver;
@property (nonatomic, retain) _NSKVOKeyObserver *restOfKeypathObserver;
@property (nonatomic, retain) NSArray *dependentObservers;
@property (nonatomic, assign) id object;
@property (nonatomic, copy) NSString *key;
@property (nonatomic, copy) NSString *restOfKeypath;
@property (nonatomic, retain) NSArray *affectedObservers;
@property (nonatomic, assign) BOOL root;
@property (nonatomic, readonly) BOOL isRemoved;
@end

@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;
@property (nonatomic, assign) NSKeyValueObservingOptions options;
@property (nonatomic, assign) void *context;

@property (atomic, retain) NSMutableDictionary *pendingChange;
@end

@interface _NSKVOObservationInfo : NSObject
{
NSMutableDictionary<NSString *, NSMutableArray<_NSKVOKeyObserver *> *>
*_keyObserverMap;
NSInteger _dependencyDepth;
NSMutableSet<NSString *> *_existingDependentKeys;
gs_mutex_t _lock;
}
- (instancetype)init;
- (NSArray *)observersForKey:(NSString *)key;
- (void)addObserver:(_NSKVOKeyObserver *)observer;
@end

// From NSKVOSwizzling
void
_NSKVOEnsureKeyWillNotify(id object, NSString *key);

#endif

/* Implementation in NSKVOSupport.m for ObjC2 and NSKeyValueObserving
* respectively
*/
@interface
NSObject (NSKeyValueObservingPrivate)
- (void)_notifyObserversOfChangeForKey:(NSString *)key
oldValue:(id)oldValue
newValue:(id)newValue;
@end
Loading

0 comments on commit 6681a3d

Please sign in to comment.