2001-12-17 14:31:42 +00:00
|
|
|
/** Implementation of ObjC runtime for GNUStep
|
1995-08-24 22:35:25 +00:00
|
|
|
Copyright (C) 1995 Free Software Foundation, Inc.
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1995-08-24 22:35:25 +00:00
|
|
|
Date: Aug 1995
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-08-24 22:35:25 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1995-08-24 22:35:25 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1995-08-24 22:35:25 +00:00
|
|
|
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
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1995-08-24 22:35:25 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-10-09 14:00:01 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2019-12-09 23:36:00 +00:00
|
|
|
Boston, MA 02110 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSObjCRuntime class reference</title>
|
2005-02-22 11:22:44 +00:00
|
|
|
*/
|
1995-08-24 22:35:25 +00:00
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2011-05-19 08:19:24 +00:00
|
|
|
|
|
|
|
#if !defined (__GNU_LIBOBJC__)
|
|
|
|
# include <objc/encoding.h>
|
|
|
|
#endif
|
|
|
|
|
2010-02-17 11:47:06 +00:00
|
|
|
#import "Foundation/NSException.h"
|
1995-08-24 22:35:25 +00:00
|
|
|
|
2002-12-31 10:52:54 +00:00
|
|
|
/**
|
2008-06-06 13:57:06 +00:00
|
|
|
* Returns a string object containing the name for
|
|
|
|
* aProtocol. If aProtocol is 0, returns nil.
|
|
|
|
*/
|
2021-03-26 15:06:49 +00:00
|
|
|
GS_DECLARE NSString *
|
2008-06-06 13:57:06 +00:00
|
|
|
NSStringFromProtocol(Protocol *aProtocol)
|
|
|
|
{
|
|
|
|
if (aProtocol != (Protocol*)0)
|
A huge blob of changes to make -base build with libobjc2, without using the old libobjc2 headers.
Most of these changes involve simply removing direct manipulation of any runtime structures and replacing them with runtime function calls. For example class->name becomes class_getName(class) and so on.
libobjc2, like the Apple runtime, the NeXT runtime, and every version of the Objective-C spec, calls the class pointer in id isa. A few files now have #define class_pointer isa at the top. This line replaces class_pointer in the old GNU libobjc headers with isa so either class_pointer or isa can be used for accessing the class of an object. Note: object_getClass() should be used in most cases because, in some future version of the runtime, this will skip things like lock classes and other hidden classes (e.g. KVO classes).
All of the old forwarding stuff has been removed. Most of this stuff followed convoluted code paths that ended with an exception. A few simply broke in exciting ways. Hopefully no one has used them for the last ten years or so, but we can bring them back with some #ifndef stuff if they're really needed by anyone.
There is currently a bug in configure, which prevents dladdr() from being detected, so you need to manually tweak config.h to build - I have not fixed the fall-back code in objc-load.m to work with libobjc2, I just added a new version that uses the loader's functionality directly.
Although -base now builds, it builds with a lot of warnings. <string.h> is missing from a lot of files, so memcpy() and strlen() generate implicit function declaration warnings.
Additionally, libobjc2 does still provide the sel_{get,register}_*() functions, but they're wrappers around the newer API ones. These are deprecated and are not exposed in the headers. Although they work, we should be replacing them with the libobjc2 versions as soon as possible.
This incorporates a patch by Eric.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@31265 72102866-910b-0410-8b05-ffd578937521
2010-09-09 15:06:09 +00:00
|
|
|
return [NSString stringWithUTF8String: protocol_getName(aProtocol)];
|
2008-06-06 13:57:06 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the protocol whose name is supplied in the
|
|
|
|
* aProtocolName argument, or 0 if a nil string is supplied.
|
|
|
|
*/
|
2021-03-26 15:06:49 +00:00
|
|
|
GS_DECLARE Protocol *
|
2008-06-06 13:57:06 +00:00
|
|
|
NSProtocolFromString(NSString *aProtocolName)
|
|
|
|
{
|
|
|
|
if (aProtocolName != nil)
|
|
|
|
{
|
|
|
|
int len = [aProtocolName length];
|
|
|
|
char buf[len+1];
|
|
|
|
|
|
|
|
[aProtocolName getCString: buf
|
|
|
|
maxLength: len + 1
|
|
|
|
encoding: NSASCIIStringEncoding];
|
|
|
|
return GSProtocolFromName (buf);
|
|
|
|
}
|
|
|
|
return (Protocol*)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string object containing the name for
|
2002-12-31 10:52:54 +00:00
|
|
|
* aSelector. If aSelector is 0, returns nil.
|
|
|
|
*/
|
2021-03-26 15:06:49 +00:00
|
|
|
GS_DECLARE NSString *
|
1995-08-24 22:35:25 +00:00
|
|
|
NSStringFromSelector(SEL aSelector)
|
|
|
|
{
|
1998-07-15 12:47:38 +00:00
|
|
|
if (aSelector != (SEL)0)
|
2010-02-22 10:13:20 +00:00
|
|
|
return [NSString stringWithUTF8String: sel_getName(aSelector)];
|
1998-07-15 12:47:38 +00:00
|
|
|
return nil;
|
1995-08-24 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2002-12-31 10:52:54 +00:00
|
|
|
/**
|
2010-08-17 18:38:00 +00:00
|
|
|
* Returns (creating if necessary) the selector whose name is supplied in the
|
2002-12-31 10:52:54 +00:00
|
|
|
* aSelectorName argument, or 0 if a nil string is supplied.
|
|
|
|
*/
|
2021-03-26 15:06:49 +00:00
|
|
|
GS_DECLARE SEL
|
1995-08-24 22:35:25 +00:00
|
|
|
NSSelectorFromString(NSString *aSelectorName)
|
|
|
|
{
|
1998-07-15 12:47:38 +00:00
|
|
|
if (aSelectorName != nil)
|
2003-04-14 07:19:28 +00:00
|
|
|
{
|
2006-02-27 09:35:19 +00:00
|
|
|
int len = [aSelectorName length];
|
2003-04-14 07:19:28 +00:00
|
|
|
char buf[len+1];
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
[aSelectorName getCString: buf
|
|
|
|
maxLength: len + 1
|
|
|
|
encoding: NSASCIIStringEncoding];
|
2010-08-17 18:38:00 +00:00
|
|
|
return sel_registerName (buf);
|
2003-04-14 07:19:28 +00:00
|
|
|
}
|
1998-07-15 12:47:38 +00:00
|
|
|
return (SEL)0;
|
1995-08-24 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2002-12-31 10:52:54 +00:00
|
|
|
/**
|
|
|
|
* Returns the class whose name is supplied in the
|
2011-02-07 22:53:35 +00:00
|
|
|
* aClassName argument, or Nil if a nil string is supplied.
|
|
|
|
* If no such class has been loaded, the function returns Nil.
|
2002-12-31 10:52:54 +00:00
|
|
|
*/
|
2021-03-26 15:06:49 +00:00
|
|
|
GS_DECLARE Class
|
1995-08-24 22:35:25 +00:00
|
|
|
NSClassFromString(NSString *aClassName)
|
|
|
|
{
|
1998-07-15 12:47:38 +00:00
|
|
|
if (aClassName != nil)
|
2003-04-14 07:19:28 +00:00
|
|
|
{
|
2006-02-27 09:35:19 +00:00
|
|
|
int len = [aClassName length];
|
2003-04-14 07:19:28 +00:00
|
|
|
char buf[len+1];
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
[aClassName getCString: buf
|
|
|
|
maxLength: len + 1
|
|
|
|
encoding: NSASCIIStringEncoding];
|
2010-02-22 10:13:20 +00:00
|
|
|
return objc_lookUpClass (buf);
|
2003-04-14 07:19:28 +00:00
|
|
|
}
|
1998-07-15 12:47:38 +00:00
|
|
|
return (Class)0;
|
1995-08-24 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2002-12-31 10:52:54 +00:00
|
|
|
/**
|
|
|
|
* Returns an [NSString] object containing the class name for
|
|
|
|
* aClass. If aClass is 0, returns nil.
|
|
|
|
*/
|
2021-03-26 15:06:49 +00:00
|
|
|
GS_DECLARE NSString *
|
1998-07-15 12:47:38 +00:00
|
|
|
NSStringFromClass(Class aClass)
|
1995-08-24 22:35:25 +00:00
|
|
|
{
|
1998-07-15 12:47:38 +00:00
|
|
|
if (aClass != (Class)0)
|
2010-02-22 10:13:20 +00:00
|
|
|
return [NSString stringWithUTF8String: (char*)class_getName(aClass)];
|
1998-07-15 12:47:38 +00:00
|
|
|
return nil;
|
1995-08-24 22:35:25 +00:00
|
|
|
}
|
1998-07-15 12:47:38 +00:00
|
|
|
|
2002-12-31 10:52:54 +00:00
|
|
|
/**
|
|
|
|
* When provided with a C string containing encoded type information,
|
|
|
|
* this method extracts size and alignment information for the specified
|
|
|
|
* type into the buffers pointed to by sizep and alignp.<br />
|
2010-04-14 11:52:21 +00:00
|
|
|
* If either sizep or alignp is a null pointer, the corresponding data is
|
2002-12-31 10:52:54 +00:00
|
|
|
* not extracted.<br />
|
2010-04-14 11:52:21 +00:00
|
|
|
* The function returns a pointer into the type information C string
|
|
|
|
* immediately after the decoded information.
|
2002-12-31 10:52:54 +00:00
|
|
|
*/
|
2021-03-26 15:06:49 +00:00
|
|
|
GS_DECLARE const char *
|
2010-03-01 05:43:08 +00:00
|
|
|
NSGetSizeAndAlignment(const char *typePtr,
|
|
|
|
NSUInteger *sizep, NSUInteger *alignp)
|
1998-07-15 12:47:38 +00:00
|
|
|
{
|
2010-03-01 10:34:35 +00:00
|
|
|
if (typePtr != NULL)
|
|
|
|
{
|
|
|
|
/* Skip any offset, but don't call objc_skip_offset() as that's buggy.
|
|
|
|
*/
|
|
|
|
if (*typePtr == '+' || *typePtr == '-')
|
|
|
|
{
|
|
|
|
typePtr++;
|
|
|
|
}
|
|
|
|
while (isdigit(*typePtr))
|
|
|
|
{
|
|
|
|
typePtr++;
|
|
|
|
}
|
|
|
|
typePtr = objc_skip_type_qualifiers (typePtr);
|
|
|
|
if (sizep)
|
|
|
|
{
|
|
|
|
*sizep = objc_sizeof_type (typePtr);
|
|
|
|
}
|
|
|
|
if (alignp)
|
|
|
|
{
|
|
|
|
*alignp = objc_alignof_type (typePtr);
|
|
|
|
}
|
2010-04-14 11:52:21 +00:00
|
|
|
typePtr = objc_skip_typespec (typePtr);
|
2010-03-01 10:34:35 +00:00
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
return typePtr;
|
1998-07-15 12:47:38 +00:00
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|