Implement a hook that allows the compiler to register the aliases used with the

@compatibility_alias directive so that they can be resolved at runtime.
main
thebeing 15 years ago
parent ed9d0f33c9
commit ee7817cf57

@ -22,6 +22,7 @@ libobjc_OBJC_FILES = \
libobjc_C_FILES = \ libobjc_C_FILES = \
abi_version.c\ abi_version.c\
alias_table.c\
caps.c\ caps.c\
category_loader.c\ category_loader.c\
class_table.c\ class_table.c\

@ -18,6 +18,7 @@ OBJECTS = \
NSBlocks.o\ NSBlocks.o\
Protocol2.o\ Protocol2.o\
abi_version.o\ abi_version.o\
alias_table.o\
blocks_runtime.o\ blocks_runtime.o\
caps.o\ caps.o\
category_loader.o\ category_loader.o\

@ -18,6 +18,7 @@ OBJECTS = \
NSBlocks.bc\ NSBlocks.bc\
Protocol2.bc\ Protocol2.bc\
abi_version.bc\ abi_version.bc\
alias_table.bc\
blocks_runtime.bc\ blocks_runtime.bc\
caps.bc\ caps.bc\
category_loader.bc\ category_loader.bc\

@ -0,0 +1,27 @@
/** Declaration of a helper function for getting class references from aliases.
Copyright (c) 2011 Free Software Foundation, Inc.
Written by: Niels Grewe <niels.grewe@halbordnung.de>
Created: March 2011
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
#include "objc/runtime.h"
Class alias_getClass(const char *alias_name);

@ -0,0 +1,125 @@
/** A hash table for mapping compatibility aliases to classes.
Copyright (c) 2011 Free Software Foundation, Inc.
Written by: Niels Grewe <niels.grewe@halbordnung.de>
Created: March 2011
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
#include "objc/runtime.h"
#include "class.h"
#include "lock.h"
#include "string_hash.h"
#include <stdlib.h>
struct objc_alias
{
const char* name;
Class class;
};
typedef struct objc_alias *Alias;
static int alias_compare(const char *name, const Alias alias)
{
return string_compare(name, alias->name);
}
static int alias_hash(const Alias alias)
{
return string_hash(alias->name);
}
#define MAP_TABLE_NAME alias_table_internal
#define MAP_TABLE_COMPARE_FUNCTION alias_compare
#define MAP_TABLE_HASH_KEY string_hash
#define MAP_TABLE_HASH_VALUE alias_hash
#include "hash_table.h"
static alias_table_internal_table *alias_table;
#define POOL_NAME alias
#define POOL_TYPE struct objc_alias
#include "pool.h"
__attribute__((constructor)) static void alias_table_create_implicitly()
{
alias_table = alias_table_internal_create(128);
}
static Alias alias_table_get_safe(const char *alias_name)
{
return alias_table_internal_table_get(alias_table, alias_name);
}
Class alias_getClass(const char *alias_name)
{
if (NULL == alias_name)
{
return NULL;
}
Alias alias = alias_table_get_safe(alias_name);
if (NULL == alias)
{
return NULL;
}
return alias->class;
}
void alias_table_insert(Alias alias)
{
alias_table_internal_insert(alias_table, alias);
}
BOOL class_registerAlias_np(Class class, const char *alias)
{
if ((NULL == alias) || (NULL == class))
{
return 0;
}
/*
* If there already exists a matching alias, determine whether we the existing
* alias is the correct one. Please note that objc_getClass() goes through the
* alias lookup and will create the alias table if necessary.
*/
Class existingClass = (Class)objc_getClass(alias);
if (NULL != existingClass)
{
/*
* Return YES if the alias has already been registered for this very
* class, and NO if the alias is already used for another class.
*/
return (class == existingClass);
}
Alias newAlias = alias_pool_alloc();
newAlias->name = strdup(alias);
newAlias->class = class;
alias_table_insert(newAlias);
return 1;
}

@ -13,6 +13,7 @@ static const int32_t caps =
(1<<OBJC_CAP_OPTIONAL_PROTOCOLS) | (1<<OBJC_CAP_OPTIONAL_PROTOCOLS) |
(1<<OBJC_CAP_NONFRAGILE_IVARS) | (1<<OBJC_CAP_NONFRAGILE_IVARS) |
(1<<OBJC_DEVELOPER_MODE) | (1<<OBJC_DEVELOPER_MODE) |
(1<<OBJC_CAP_REGISTERED_COMPATIBILITY_ALIASES) |
#ifndef NO_OBJCXX #ifndef NO_OBJCXX
(1<<OBJC_UNIFIED_EXCEPTION_MODEL) | (1<<OBJC_UNIFIED_EXCEPTION_MODEL) |
#endif #endif

@ -1,6 +1,7 @@
#include "objc/runtime.h" #include "objc/runtime.h"
#include "objc/hooks.h" #include "objc/hooks.h"
#include "objc/developer.h" #include "objc/developer.h"
#include "alias.h"
#include "class.h" #include "class.h"
#include "method_list.h" #include "method_list.h"
#include "selector.h" #include "selector.h"
@ -452,6 +453,11 @@ id objc_getClass(const char *name)
if (nil != class) { return class; } if (nil != class) { return class; }
// Second chance lookup via @compatibilty_alias:
class = (id)alias_getClass(name);
if (nil != class) { return class; }
// Third chance lookup via the hook:
if (0 != _objc_lookup_class) if (0 != _objc_lookup_class)
{ {
class = (id)_objc_lookup_class(name); class = (id)_objc_lookup_class(name);

@ -73,6 +73,14 @@ extern "C" {
*/ */
#define OBJC_UNIFIED_EXCEPTION_MODEL 9 #define OBJC_UNIFIED_EXCEPTION_MODEL 9
/**
* The runtime provides a hook that allows the compiler to register class
* aliases declared with the @compatibility_alias keyword. This allows the
* runtime to resolve the alias, e.g. if objc_getClass() is called with an
* alias as the argument.
*/
#define OBJC_CAP_REGISTERED_COMPATIBILITY_ALIASES 10
/** /**
* Macro used to require the existence of a specific capability. This creates * Macro used to require the existence of a specific capability. This creates
* a function that is called by the loader and tests that the runtime supports * a function that is called by the loader and tests that the runtime supports

@ -314,6 +314,13 @@ const char *class_getWeakIvarLayout(Class cls);
*/ */
BOOL class_isMetaClass(Class cls); BOOL class_isMetaClass(Class cls);
/**
* Registers an alias for the class. Returns YES if the alias could be
* registered successfully.
*/
OBJC_NONPORTABLE
BOOL class_registerAlias_np(Class cls, const char *alias);
/** /**
* Replaces the named method with a new implementation. Note: the GNUstep * Replaces the named method with a new implementation. Note: the GNUstep
* Objective-C runtime uses typed selectors, however the types of the selector * Objective-C runtime uses typed selectors, however the types of the selector

Loading…
Cancel
Save