2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 // Support minimal stand-alone implementation plus hooks for swapping
28 // in a richer implementation.
30 // Created by Blaine Garst on Fri Nov 01 2002.
31 // Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
36 #import "objc-exception.h"
38 static objc_exception_functions_t xtab;
40 // forward declaration
41 static void set_default_handlers();
48 // get table; version tells how many
49 void objc_exception_get_functions(objc_exception_functions_t *table) {
50 // only version 0 supported at this point
51 if (table && table->version == 0)
56 void objc_exception_set_functions(objc_exception_functions_t *table) {
57 // only version 0 supported at this point
58 if (table && table->version == 0)
63 * The following functions are
64 * synthesized by the compiler upon encountering language constructs
67 void objc_exception_throw(id exception) {
68 if (!xtab.throw_exc) {
69 set_default_handlers();
71 xtab.throw_exc(exception);
74 void objc_exception_try_enter(void *localExceptionData) {
75 if (!xtab.throw_exc) {
76 set_default_handlers();
78 xtab.try_enter(localExceptionData);
82 void objc_exception_try_exit(void *localExceptionData) {
83 if (!xtab.throw_exc) {
84 set_default_handlers();
86 xtab.try_exit(localExceptionData);
90 id objc_exception_extract(void *localExceptionData) {
91 if (!xtab.throw_exc) {
92 set_default_handlers();
94 return xtab.extract(localExceptionData);
98 int objc_exception_match(Class exceptionClass, id exception) {
99 if (!xtab.throw_exc) {
100 set_default_handlers();
102 return xtab.match(exceptionClass, exception);
106 // quick and dirty exception handling code
107 // default implementation - mostly a toy for use outside/before Foundation
108 // provides its implementation
109 // Perhaps the default implementation should just complain loudly and quit
116 extern void _objc_inform(const char *fmt, ...);
118 typedef struct { jmp_buf buf; void *pointers[4]; } LocalData_t;
120 typedef struct _threadChain {
121 LocalData_t *topHandler;
123 struct _threadChain *next;
127 #define _ExceptionDebug 0
129 static ThreadChainLink_t ThreadChainLink;
131 static ThreadChainLink_t *getChainLink() {
132 // follow links until thread_self() found (someday) XXX
133 pthread_t self = pthread_self();
134 ThreadChainLink_t *walker = &ThreadChainLink;
135 while (walker->perThreadID != (void *)self) {
136 if (walker->next != NULL) {
137 walker = walker->next;
141 // XXX not thread safe (!)
142 // XXX Also, we don't register to deallocate on thread death
143 walker->next = (ThreadChainLink_t *)malloc(sizeof(ThreadChainLink_t));
144 walker = walker->next;
146 walker->topHandler = NULL;
147 walker->perThreadID = self;
152 static void default_try_enter(void *localExceptionData) {
153 ThreadChainLink_t *chainLink = getChainLink();
154 ((LocalData_t *)localExceptionData)->pointers[1] = chainLink->topHandler;
155 chainLink->topHandler = localExceptionData;
156 if (_ExceptionDebug) _objc_inform("entered try block %x\n", chainLink->topHandler);
159 static void default_throw(id value) {
160 ThreadChainLink_t *chainLink = getChainLink();
162 if (_ExceptionDebug) _objc_inform("objc_exception_throw with nil value\n");
165 if (chainLink == NULL) {
166 if (_ExceptionDebug) _objc_inform("No handler in place!\n");
169 if (_ExceptionDebug) _objc_inform("exception thrown, going to handler block %x\n", chainLink->topHandler);
170 LocalData_t *led = chainLink->topHandler;
171 chainLink->topHandler = led->pointers[1]; // pop top handler
172 led->pointers[0] = value; // store exception that is thrown
173 _longjmp(led->buf, 1);
176 static void default_try_exit(void *led) {
177 ThreadChainLink_t *chainLink = getChainLink();
178 if (!chainLink || led != chainLink->topHandler) {
179 if (_ExceptionDebug) _objc_inform("!!! mismatched try block exit handlers !!!\n");
182 if (_ExceptionDebug) _objc_inform("removing try block handler %x\n", chainLink->topHandler);
183 chainLink->topHandler = chainLink->topHandler->pointers[1]; // pop top handler
186 static id default_extract(void *localExceptionData) {
187 LocalData_t *led = (LocalData_t *)localExceptionData;
188 return (id)led->pointers[0];
191 static int default_match(Class exceptionClass, id exception) {
192 //return [exception isKindOfClass:exceptionClass];
194 for (cls = exception->isa; nil != cls; cls = cls->super_class)
195 if (cls == exceptionClass) return 1;
199 static void set_default_handlers() {
200 objc_exception_functions_t default_functions = {
201 0, default_throw, default_try_enter, default_try_exit, default_extract, default_match };
203 // should this always print?
204 if (_ExceptionDebug) _objc_inform("*** Setting default (non-Foundation) exception mechanism\n");
205 objc_exception_set_functions(&default_functions);