File: CXXException.m

package info (click to toggle)
gnustep-base 1.28.1%2Breally1.28.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,008 kB
  • sloc: objc: 223,137; ansic: 35,562; sh: 184; makefile: 128; cpp: 122; xml: 32
file content (106 lines) | stat: -rw-r--r-- 2,482 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#if defined(__has_include)
#if __has_include(<objc/hooks.h>)
#import "Foundation/NSObject.h"
#import "GNUstepBase/CXXException.h"
#include <objc/runtime.h>
#include <objc/hooks.h>

/** From the CodeSourcery ABI Spec, with C++ pointers turned to void*, and
 * other parts abridged. */

typedef enum
{
  _URC_FOREIGN_EXCEPTION_CAUGHT = 1
} _Unwind_Reason_Code;

struct _Unwind_Exception;

typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code,
                                              struct _Unwind_Exception *);
struct _Unwind_Exception
{
  uint64_t exception_class;
  _Unwind_Exception_Cleanup_Fn exception_cleanup;
  unsigned long private_1;
  unsigned long private_2;
} __attribute__((__aligned__));

_Unwind_Reason_Code _Unwind_Resume_or_Rethrow(struct _Unwind_Exception *);


struct __cxa_exception
{
  void *exceptionType;
  void (*exceptionDestructor) (void *); 
  void (*unexpectedHandler) (void *); 
  void (*terminateHandler) (void *); 
  void *nextException;

  int			handlerCount;
  int			handlerSwitchValue;
  const char *		actionRecord;
  const char *		languageSpecificData;
  void *			catchTemp;
  void *			adjustedPtr;
  struct _Unwind_Exception	unwindHeader;
};

@implementation CXXException
static Class CXXExceptionClass;
// TODO: Add an API for registering other classes for other exception types
static Class boxClass(int64_t foo)
{
  // Big endian platforms
  if (foo == *(int64_t*)(void*)"GNUCC++\0")
    {
      return CXXExceptionClass;
    }
  // Little endian platforms
  if (foo == *(int64_t*)(void*)"\0++CCUNG")
    {
      return CXXExceptionClass;
    }
  return Nil;
}
+ (void) load
{
  CXXExceptionClass = self;
  _objc_class_for_boxing_foreign_exception = boxClass;
}
+ (id) exceptionWithForeignException: (struct _Unwind_Exception*)ex
{
  CXXException *box = [self new];
  box->ex = ex;
  return [box autorelease];
}
- (void*) thrownValue
{
  return ex + 1;
}
- (void*) cxx_type_info
{
  char *ptr = (char*)ex;
  ptr -= __builtin_offsetof(struct __cxa_exception, unwindHeader);
  return ((struct __cxa_exception*)(void*)ptr)->exceptionType;
}
- (void) rethrow
{
#if defined(WITH_UNWIND)
  struct _Unwind_Exception *re = ex;
  // We aren't allowed to hold onto the exception if it's been rethrown.
  ex = 0;
  _Unwind_Resume_or_Rethrow(re);
#endif
}
- (void) dealloc
{
  if (0 != ex && 0 != ex->exception_cleanup)
    {
      ex->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, ex);
    }
  [super dealloc];
}
@end

#endif
#endif