Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:
strategy:
matrix:
build-type: [ Release, Debug ]
llvm-version: [16, 17, 18]
llvm-version: [20, 21, 22]
arch:
- name: armhf
system-processor: arm
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
- name: Install cross-compile toolchain and QEMU
run: |
sudo apt update
sudo apt install libstdc++-9-dev-${{ matrix.arch.name }}-cross qemu-user ninja-build
sudo apt install libstdc++-11-dev-${{ matrix.arch.name }}-cross qemu-user ninja-build
- name: Configure CMake
run: |
export LDFLAGS="-L/usr/lib/llvm-${{ matrix.llvm-version }}/lib/ -fuse-ld=lld-${{ matrix.llvm-version}} -Wl,--dynamic-linker=/usr/${{ matrix.arch.triple }}/lib/${{ matrix.arch.rtld }},-rpath,/usr/${{ matrix.arch.triple }}/lib"
Expand Down
52 changes: 52 additions & 0 deletions Test/AssociateSetClass.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "Test.h"
#include <stdio.h>

BOOL deallocCalled = NO;
static char key;

@interface Observed : Test
@end

@implementation Observed
@end

@interface Associated : Test
@end

@implementation Associated
-(void) dealloc
{
deallocCalled = YES;
[super dealloc];
}
@end

int main(int argc, const char * argv[]) {
Class subclass = objc_allocateClassPair([Observed class], "ObservedSubclass", 0);
objc_registerClassPair(subclass);

@autoreleasepool
{
Observed *object = [Observed new];
Associated *value = [Associated new];
objc_setAssociatedObject(object, &key, value, OBJC_ASSOCIATION_RETAIN);
[value release];
assert(objc_getAssociatedObject(object, &key) == value);

object_setClass(object, subclass);
assert(object_getClass(object) == subclass);

fprintf(stderr, "Association after object_setClass: %p (expected %p)\n",
objc_getAssociatedObject(object, &key), value);
assert(objc_getAssociatedObject(object, &key) == value);

object_setClass(object, [Observed class]);
fprintf(stderr, "Association after object_setClass: %p (expected %p)\n",
objc_getAssociatedObject(object, &key), value);
assert(objc_getAssociatedObject(object, &key) == value);

[object release];
}
assert(deallocCalled);
return 0;
}
1 change: 1 addition & 0 deletions Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(TESTS
AllocatePair.m
AssociatedObject.m
AssociatedObject2.m
AssociateSetClass.m
BlockCapture_arc.m
BlockTest_arc.m
ConstantString.m
Expand Down
30 changes: 30 additions & 0 deletions associate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,33 @@ id object_getPrototype_np(id object)
{
return objc_getAssociatedObject(object, &prototypeKey);
}

Class object_setClass(id object, Class cls)
{
if (object == nil)
{
return Nil;
}
// If this is a small object, then don't set its class.
if (isSmallObject(object))
{
return classForObject(object);
}
auto guard = acquire_locks_for_pointers(object);
LOCK_RUNTIME_FOR_SCOPE();
Class oldClass = object->isa;
// If this object has a hidden class, then we are not going to change the
// isa pointer. Instead, we are going to pivot the superclass of that
// hidden class to the new superclass. This is more complex because it
// requires updating the list is subclasses of the parent and then
// recomputing our dtable.
if (Class hiddenClass = findHiddenClass(object))
{
oldClass = class_setSuperclass(hiddenClass, cls);
}
else
{
object->isa = cls;
}
return oldClass;
}
5 changes: 4 additions & 1 deletion objc/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,10 @@ void class_setIvarLayout(Class cls, const uint8_t *layout);
* because modifying the superclass of a class at run time is a very complex
* operation and this function is almost always used incorrectly.
*/
OBJC_PUBLIC __attribute__((deprecated))
OBJC_PUBLIC
#ifndef __OBJC_RUNTIME_INTERNAL__
__attribute__((deprecated))
#endif
Class class_setSuperclass(Class cls, Class newSuper);

OBJC_PUBLIC OBJC_GNUSTEP_RUNTIME_UNSUPPORTED("Weak instance variables")
Expand Down
35 changes: 15 additions & 20 deletions runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ void class_setIvarLayout(Class cls, const uint8_t *layout)
memcpy(cls->ivars, list, listsize);
}

__attribute__((deprecated))
Class class_setSuperclass(Class cls, Class newSuper)
{
CHECK_ARG(cls);
Expand Down Expand Up @@ -540,17 +539,23 @@ Class class_setSuperclass(Class cls, Class newSuper)
}
else
{
Class meta = cls->isa, newSuperMeta = newSuper->isa;
// Update the metaclass's superclass.
safe_remove_from_subclass_list(meta);
objc_resolve_class(newSuperMeta);
Class meta = cls->isa;
Class newSuperMeta = newSuper->isa;
// The old and new metaclasses can be the same. If we replace the
if (meta != newSuperMeta)
{
// Update the metaclass's superclass.
safe_remove_from_subclass_list(meta);
objc_resolve_class(newSuperMeta);

meta->super_class = newSuperMeta;
meta->isa = newSuperMeta->isa;
meta->super_class = newSuperMeta;
assert(meta != newSuperMeta);
meta->isa = newSuperMeta->isa;

// The super class's subclass list is used in certain method resolution scenarios.
meta->sibling_class = newSuperMeta->subclass_list;
newSuperMeta->subclass_list = meta;
// The super class's subclass list is used in certain method resolution scenarios.
meta->sibling_class = newSuperMeta->subclass_list;
newSuperMeta->subclass_list = meta;
}
}

LOCK_FOR_SCOPE(&initialize_lock);
Expand Down Expand Up @@ -815,16 +820,6 @@ Class object_getClass(id obj)
return isa;
}

Class object_setClass(id obj, Class cls)
{
CHECK_ARG(obj);
// If this is a small object, then don't set its class.
if (isSmallObject(obj)) { return classForObject(obj); }
Class oldClass = obj->isa;
obj->isa = cls;
return oldClass;
}

const char *object_getClassName(id obj)
{
CHECK_ARG(obj);
Expand Down
Loading