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
|
|
|
|
|
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
|
|
|
|
modify it under the terms of the GNU Library 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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSObjCRuntime class reference</title>
|
|
|
|
$Date$ $Revision$
|
1995-08-24 22:35:25 +00:00
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/preface.h>
|
1995-08-24 22:35:25 +00:00
|
|
|
#include <Foundation/NSObjCRuntime.h>
|
|
|
|
#include <Foundation/NSString.h>
|
1998-12-14 06:17:12 +00:00
|
|
|
#include <mframe.h>
|
2001-05-31 22:39:16 +00:00
|
|
|
#include <string.h>
|
1995-08-24 22:35:25 +00:00
|
|
|
|
|
|
|
NSString *
|
|
|
|
NSStringFromSelector(SEL aSelector)
|
|
|
|
{
|
1998-07-15 12:47:38 +00:00
|
|
|
if (aSelector != (SEL)0)
|
2000-10-31 16:17:33 +00:00
|
|
|
return [NSString stringWithCString: GSObjCSelectorName(aSelector)];
|
1998-07-15 12:47:38 +00:00
|
|
|
return nil;
|
1995-08-24 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SEL
|
|
|
|
NSSelectorFromString(NSString *aSelectorName)
|
|
|
|
{
|
1998-07-15 12:47:38 +00:00
|
|
|
if (aSelectorName != nil)
|
|
|
|
return sel_get_any_uid ([aSelectorName cString]);
|
|
|
|
return (SEL)0;
|
1995-08-24 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Class
|
|
|
|
NSClassFromString(NSString *aClassName)
|
|
|
|
{
|
1998-07-15 12:47:38 +00:00
|
|
|
if (aClassName != nil)
|
|
|
|
return objc_lookup_class ([aClassName cString]);
|
|
|
|
return (Class)0;
|
1995-08-24 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
1998-07-15 12:47:38 +00:00
|
|
|
NSString *
|
|
|
|
NSStringFromClass(Class aClass)
|
1995-08-24 22:35:25 +00:00
|
|
|
{
|
1998-07-15 12:47:38 +00:00
|
|
|
if (aClass != (Class)0)
|
2000-10-31 16:17:33 +00:00
|
|
|
return [NSString stringWithCString: (char*)GSObjCName(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
|
|
|
|
|
|
|
const char *
|
|
|
|
NSGetSizeAndAlignment(const char *typePtr, unsigned *sizep, unsigned *alignp)
|
|
|
|
{
|
1998-08-13 20:45:32 +00:00
|
|
|
NSArgumentInfo info;
|
|
|
|
typePtr = mframe_next_arg(typePtr, &info);
|
1998-07-15 12:47:38 +00:00
|
|
|
if (sizep)
|
1998-08-13 20:45:32 +00:00
|
|
|
*sizep = info.size;
|
|
|
|
if (alignp)
|
|
|
|
*alignp = info.align;
|
1999-06-24 19:30:29 +00:00
|
|
|
return typePtr;
|
1998-07-15 12:47:38 +00:00
|
|
|
}
|
2000-12-02 21:36:56 +00:00
|
|
|
|
1999-01-15 11:24:03 +00:00
|
|
|
BOOL
|
2000-12-02 21:36:56 +00:00
|
|
|
GSInstanceVariableInfo(id obj, NSString *iVarName,
|
|
|
|
const char **type, unsigned *size, unsigned *offset)
|
1999-01-15 11:24:03 +00:00
|
|
|
{
|
2000-11-28 09:29:21 +00:00
|
|
|
const char *name = [iVarName cString];
|
|
|
|
Class class;
|
1999-01-15 11:24:03 +00:00
|
|
|
struct objc_ivar_list *ivars;
|
|
|
|
struct objc_ivar *ivar = 0;
|
|
|
|
|
|
|
|
class = [obj class];
|
|
|
|
while (class != nil && ivar == 0)
|
|
|
|
{
|
|
|
|
ivars = class->ivars;
|
|
|
|
class = class->super_class;
|
2000-11-28 09:29:21 +00:00
|
|
|
if (ivars != 0)
|
1999-01-15 11:24:03 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ivars->ivar_count; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(ivars->ivar_list[i].ivar_name, name) == 0)
|
|
|
|
{
|
|
|
|
ivar = &ivars->ivar_list[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ivar == 0)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2001-01-03 11:22:59 +00:00
|
|
|
if (type)
|
2000-12-02 21:36:56 +00:00
|
|
|
*type = ivar->ivar_type;
|
2001-01-03 11:22:59 +00:00
|
|
|
if (size)
|
2000-12-02 21:36:56 +00:00
|
|
|
*size = objc_sizeof_type(ivar->ivar_type);
|
2001-01-03 11:22:59 +00:00
|
|
|
if (offset)
|
2000-12-02 21:36:56 +00:00
|
|
|
*offset = ivar->ivar_offset;
|
1999-01-15 11:24:03 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2000-12-02 21:36:56 +00:00
|
|
|
BOOL
|
|
|
|
GSGetInstanceVariable(id obj, NSString *iVarName, void *data)
|
2000-11-28 09:29:21 +00:00
|
|
|
{
|
2000-12-02 21:36:56 +00:00
|
|
|
int offset;
|
|
|
|
const char *type;
|
|
|
|
unsigned int size;
|
2000-11-28 09:29:21 +00:00
|
|
|
|
2000-12-02 21:36:56 +00:00
|
|
|
if (GSInstanceVariableInfo(obj, iVarName, &type, &size, &offset) == NO)
|
2000-11-28 09:29:21 +00:00
|
|
|
{
|
2000-12-02 21:36:56 +00:00
|
|
|
return NO;
|
2000-11-28 09:29:21 +00:00
|
|
|
}
|
2002-02-10 12:54:27 +00:00
|
|
|
//This very highly unprobable value can be used as a marker
|
|
|
|
NSAssert(offset!=UINT_MAX,@"Bad Offset");
|
2000-12-02 21:36:56 +00:00
|
|
|
memcpy(data, ((void*)obj) + offset, size);
|
|
|
|
return YES;
|
2000-11-28 09:29:21 +00:00
|
|
|
}
|
|
|
|
|
1999-01-15 11:24:03 +00:00
|
|
|
BOOL
|
1999-05-25 06:41:48 +00:00
|
|
|
GSSetInstanceVariable(id obj, NSString *iVarName, const void *data)
|
1999-01-15 11:24:03 +00:00
|
|
|
{
|
2000-11-28 09:29:21 +00:00
|
|
|
int offset;
|
|
|
|
const char *type;
|
|
|
|
unsigned int size;
|
1999-01-15 11:24:03 +00:00
|
|
|
|
2000-12-02 21:36:56 +00:00
|
|
|
if (GSInstanceVariableInfo(obj, iVarName, &type, &size, &offset) == NO)
|
1999-01-15 11:24:03 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2002-02-10 12:54:27 +00:00
|
|
|
//This very highly unprobable value can be used as a marker
|
|
|
|
NSAssert(offset!=UINT_MAX,@"Bad Offset");
|
1999-01-15 11:24:03 +00:00
|
|
|
memcpy(((void*)obj) + offset, data, size);
|
|
|
|
return YES;
|
|
|
|
}
|
2001-05-31 22:39:16 +00:00
|
|
|
|
|
|
|
/* Getting a system error message on a variety of systems */
|
|
|
|
#ifdef __MINGW__
|
|
|
|
LPTSTR GetErrorMsg(DWORD msgId)
|
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, msgId,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
|
|
(LPTSTR)&lpMsgBuf, 0, NULL);
|
|
|
|
|
|
|
|
return (LPTSTR)lpMsgBuf;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifndef HAVE_STRERROR
|
|
|
|
const char*
|
|
|
|
strerror(int eno)
|
|
|
|
{
|
|
|
|
extern char* sys_errlist[];
|
|
|
|
extern int sys_nerr;
|
|
|
|
|
|
|
|
if (eno < 0 || eno >= sys_nerr)
|
|
|
|
{
|
|
|
|
return("unknown error number");
|
|
|
|
}
|
|
|
|
return(sys_errlist[eno]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* __MINGW__ */
|
|
|
|
|
|
|
|
const char *GSLastErrorStr(long error_id)
|
|
|
|
{
|
|
|
|
#ifdef __MINGW__
|
|
|
|
return GetErrorMsg(GetLastError());
|
|
|
|
#else
|
|
|
|
return strerror(error_id);
|
|
|
|
#endif
|
|
|
|
}
|