| 1 | /* $Header: d:/cvsroot/tads/tads3/vmerr.h,v 1.3 1999/05/17 02:52:29 MJRoberts Exp $ */cxx . . $ `<file>/tads3/vmerr.h`/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmerr.h - VM exception handling |
| 12 | Function |
| 13 | Defines error macros for try/throw exception handling. |
| 14 | |
| 15 | To throw an exception, use err_throw(exception_object), where the |
| 16 | exception_object is an object describing the exception. This object |
| 17 | must be allocated with 'new'. Control will immediately transfer to |
| 18 | the nearest enclosing err_catch() block. |
| 19 | |
| 20 | Protected code is defined as shown below. The blocks must occur |
| 21 | in the order shown, but the err_catch and err_finally blocks are |
| 22 | optional (although it would be pointless to have an err_try that |
| 23 | omits both). |
| 24 | |
| 25 | err_try |
| 26 | { |
| 27 | // protected code |
| 28 | } |
| 29 | err_catch(exception_variable) // or just err_catch_disc |
| 30 | { |
| 31 | // Exception handler - this code is executed only if an |
| 32 | // exception occurs in the protected block. |
| 33 | // |
| 34 | // If an exception is thrown here (with no nested exception |
| 35 | // handler to catch it), the err_finally block will be executed |
| 36 | // and then the exception will be thrown to the enclosing block. |
| 37 | } |
| 38 | err_finally |
| 39 | { |
| 40 | // Code that is executed regardless whether exception occurs |
| 41 | // or not. If no exception occurs, this code is executed as |
| 42 | // soon as the protected block finishes. If an exception |
| 43 | // occurs, this code is executed, then the exception is |
| 44 | // re-thrown. |
| 45 | // |
| 46 | // If an exception is thrown here, the finally block will |
| 47 | // be aborted and the enclosing error handler will be activated. |
| 48 | // Care should be taken to ensure that code within this block |
| 49 | // is properly protected against exceptions if necessary. |
| 50 | } |
| 51 | err_end; |
| 52 | |
| 53 | err_catch automatically defines the given exception_variable as |
| 54 | a local variable of type (CVmException *) in the scope of the |
| 55 | err_catch block. The referenced CVmException object is valid |
| 56 | *only* within the err_catch block; after the err_catch block exits, |
| 57 | the CVmException object will no longer be present. |
| 58 | |
| 59 | If you don't need to access the CVmException information, use |
| 60 | err_catch_disc instead of err_catch - this has the same effect as |
| 61 | err_catch, but does not declare a local variable to point to the |
| 62 | exception object. |
| 63 | |
| 64 | To re-throw the exception being handled from an err_catch() block, |
| 65 | use err_rethrow(). |
| 66 | |
| 67 | IMPORTANT: control must NOT be transferred out of an err_try, err_catch, |
| 68 | or err_finally block via break, goto, or return. Actual execution of |
| 69 | the err_end is required in order to properly unwind the error stack. |
| 70 | The easiest way to leave one of these blocks prematurely, if necessary, |
| 71 | is with a goto: |
| 72 | |
| 73 | err_try |
| 74 | { |
| 75 | // some code |
| 76 | |
| 77 | if (done_with_this_block) |
| 78 | goto done; |
| 79 | |
| 80 | // more code |
| 81 | |
| 82 | done: ; |
| 83 | } |
| 84 | err_catch_disc |
| 85 | { |
| 86 | // etc... |
| 87 | } |
| 88 | err_end; |
| 89 | |
| 90 | Notes |
| 91 | |
| 92 | Modified |
| 93 | 10/20/98 MJRoberts - Creation |
| 94 | */ |
| 95 | |
| 96 | #ifndef VMERR_H |
| 97 | #define VMERR_Hcxx . . $ `tads3/vmerr.h:97:9`! |
| 98 | |
| 99 | #include <setjmp.h> |
| 100 | #include <stdarg.h> |
| 101 | |
| 102 | #include "t3std.h"cxx . . $ `<file>/tads3/t3std.h`/ |
| 103 | #include "vmerrnum.h"cxx . . $ `<file>/tads3/vmerrnum.h`/ |
| 104 | |
| 105 | |
| 106 | |
| 107 | /* ------------------------------------------------------------------------ */ |
| 108 | /* |
| 109 | * Error Message Definition structure |
| 110 | */ |
| 111 | struct err_msg_tcxx . . $ err_msg_t# |
| 112 | { |
| 113 | /* message number */ |
| 114 | int msgnumcxx . . $ err_msg_t#msgnum.; |
| 115 | |
| 116 | /* concise message text */ |
| 117 | const char *short_msgtxtcxx . . $ err_msg_t#short_msgtxt.; |
| 118 | |
| 119 | /* verbose message text */ |
| 120 | const char *long_msgtxtcxx . . $ err_msg_t#long_msgtxt.; |
| 121 | |
| 122 | #ifdef VMERR_BOOK_MSG |
| 123 | const char *book_msgtxt; |
| 124 | #endif |
| 125 | }; |
| 126 | |
| 127 | /* VM error message array */ |
| 128 | extern const err_msg_tcxx . . $ err_msg_t# *vm_messagescxx . . $ vm_messages.; |
| 129 | extern size_t vm_message_countcxx . . $ vm_message_count.; |
| 130 | |
| 131 | /* |
| 132 | * VM error message array - English version. This version of the |
| 133 | * messages is linked directly into the VM; at run time, we can attempt |
| 134 | * to replace this version with another language version obtained from |
| 135 | * an external file. We link in the English version so that we will |
| 136 | * always have a valid set of messages even if the user doesn't have a |
| 137 | * message file installed. |
| 138 | */ |
| 139 | extern const err_msg_tcxx . . $ err_msg_t# vm_messages_englishcxx . . $ vm_messages_english.[]; |
| 140 | extern size_t vm_message_count_englishcxx . . $ vm_message_count_english.; |
| 141 | |
| 142 | /* external message file signature */ |
| 143 | #define VM_MESSAGE_FILE_SIGNATUREcxx . . $ `tads3/vmerr.h:143:9`! "TADS3.Message.0001\n\r\032" |
| 144 | |
| 145 | |
| 146 | /* |
| 147 | * load an external message file - returns zero on success, non-zero on |
| 148 | * failure |
| 149 | */ |
| 150 | int err_load_message_filecxx . . $ err_load_message_file(f92481dec944a88b).(osfildef *fp, |
| 151 | const err_msg_tcxx . . $ err_msg_t# **arr, size_t *arr_size, |
| 152 | const err_msg_tcxx . . $ err_msg_t# *default_arr, |
| 153 | size_t default_arr_size); |
| 154 | |
| 155 | /* load default message file */ |
| 156 | #define err_load_vm_message_filecxx . . $ `tads3/vmerr.h:156:9`!(fp) \ |
| 157 | err_load_message_file((fp), &vm_messages, &vm_message_count, \ |
| 158 | vm_messages_english, vm_message_count_english) |
| 159 | |
| 160 | /* |
| 161 | * check to see if an external message file has been loaded for the |
| 162 | * default VM message set |
| 163 | */ |
| 164 | int err_is_message_file_loadedcxx . . $ err_is_message_file_loaded(b126dc7c1de90089).(); |
| 165 | |
| 166 | /* |
| 167 | * delete messages previously loaded with err_load_message_file (this is |
| 168 | * called automatically by err_terminate, so clients generally will not |
| 169 | * need to call this directly) |
| 170 | */ |
| 171 | void err_delete_message_arraycxx . . $ err_delete_message_array(872e136f420fb6f7).(const err_msg_tcxx . . $ err_msg_t# **arr, size_t *arr_size, |
| 172 | const err_msg_tcxx . . $ err_msg_t# *default_arr, |
| 173 | size_t default_arr_size); |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | * Search an array of messages for a given message number. The array |
| 178 | * must be sorted by message ID. |
| 179 | */ |
| 180 | const char *err_get_msgcxx . . $ err_get_msg(485e969cfb06687).(const err_msg_tcxx . . $ err_msg_t# *msg_array, size_t msg_count, |
| 181 | int msgnum, int verbose); |
| 182 | |
| 183 | /* |
| 184 | * Format a message with the parameters contained in an exception object. |
| 185 | * Returns the size in bytes of the formatted message. If the output |
| 186 | * buffer is null or too small, we'll fill it up as much as possible and |
| 187 | * return the actual length required for the full message text. |
| 188 | * |
| 189 | * Suports the following format codes: |
| 190 | * |
| 191 | * %s - String. Formats an ERR_TYPE_CHAR, ERR_TYPE_TEXTCHAR, or |
| 192 | * ERR_TYPE_TEXTCHAR_LEN value. |
| 193 | * |
| 194 | * %d, %u, %x - signed/unsigned decimal integer, hexadecimal integer. |
| 195 | * Formats an ERR_TYPE_INT value or an ERR_TYPE_ULONG value. Automatically |
| 196 | * uses the correct size for the argument. |
| 197 | * |
| 198 | * %% - Formats as a single percent sign. |
| 199 | */ |
| 200 | size_t err_format_msgcxx . . $ err_format_msg(f86bac8cf92f6ea5).(char *outbuf, size_t outbuflen, |
| 201 | const char *msg, const struct CVmExceptioncxx . . $ CVmException# *exc); |
| 202 | |
| 203 | /* |
| 204 | * Format a message, allocating a buffer to store the result. The caller |
| 205 | * must free the buffer with t3free(). |
| 206 | */ |
| 207 | char *err_format_msgcxx . . $ err_format_msg(41ae000119f27ca8).(const char *msg, const struct CVmExceptioncxx . . $ CVmException# *exc); |
| 208 | |
| 209 | /* ------------------------------------------------------------------------ */ |
| 210 | /* |
| 211 | * exception ID - this identifies an error |
| 212 | */ |
| 213 | typedef uintcxx . . $ uint# err_id_tcxx . . $ err_id_t#; |
| 214 | |
| 215 | /* |
| 216 | * Error parameter type codes |
| 217 | */ |
| 218 | enum err_param_typecxx . . $ err_param_type# |
| 219 | { |
| 220 | /* ---------------- Display Parameters ---------------- */ |
| 221 | /* |
| 222 | * The following parameter types are for display purposes. These are |
| 223 | * substituted into the message string in err_format_msg(). |
| 224 | */ |
| 225 | |
| 226 | /* parameter is a native 'int' value */ |
| 227 | ERR_TYPE_INTcxx . . $ ERR_TYPE_INT., |
| 228 | |
| 229 | /* parameter is a native 'unsigned long' value */ |
| 230 | ERR_TYPE_ULONGcxx . . $ ERR_TYPE_ULONG., |
| 231 | |
| 232 | /* parameter is a 'textchar_t *' value (null terminated) */ |
| 233 | ERR_TYPE_TEXTCHARcxx . . $ ERR_TYPE_TEXTCHAR., |
| 234 | |
| 235 | /* parameter is a 'char *' value (null terminated) */ |
| 236 | ERR_TYPE_CHARcxx . . $ ERR_TYPE_CHAR., |
| 237 | |
| 238 | /* |
| 239 | * parameter is a 'textchar_t *' value followed by a 'size_t' value |
| 240 | * giving the number of bytes in the string |
| 241 | */ |
| 242 | ERR_TYPE_TEXTCHAR_LENcxx . . $ ERR_TYPE_TEXTCHAR_LEN., |
| 243 | |
| 244 | /* parameter is a 'char *' value with a separate length */ |
| 245 | ERR_TYPE_CHAR_LENcxx . . $ ERR_TYPE_CHAR_LEN., |
| 246 | |
| 247 | /* ---------------- Non-Display Parameters ---------------- */ |
| 248 | /* |
| 249 | * The following parameter types are stored in the exception but are |
| 250 | * NOT used in formatting the error message. |
| 251 | */ |
| 252 | |
| 253 | /* |
| 254 | * a char* value giving the ID/Version string for the metaclass |
| 255 | * involved in the error (for example, "metaclass missing" or |
| 256 | * "metaclass version not available") |
| 257 | */ |
| 258 | ERR_TYPE_METACLASScxx . . $ ERR_TYPE_METACLASS., |
| 259 | |
| 260 | /* a char* value the ID/Version string for the function set involved */ |
| 261 | ERR_TYPE_FUNCSETcxx . . $ ERR_TYPE_FUNCSET., |
| 262 | |
| 263 | /* |
| 264 | * void (no parameter): this is a flag indicating that the error is a |
| 265 | * VM version error. This type of error can usually be solved by |
| 266 | * updating the interpreter to the latest version. |
| 267 | */ |
| 268 | ERR_TYPE_VERSION_FLAGcxx . . $ ERR_TYPE_VERSION_FLAG. |
| 269 | }; |
| 270 | |
| 271 | /* |
| 272 | * Exception parameter |
| 273 | */ |
| 274 | struct CVmExcParamcxx . . $ CVmExcParam# |
| 275 | { |
| 276 | /* type of this parameter */ |
| 277 | err_param_typecxx . . $ err_param_type# type_cxx . . $ CVmExcParam#type_.; |
| 278 | |
| 279 | /* value of the parameter */ |
| 280 | unioncxx . . $ CVmExcParam#$anonymous_type_0# |
| 281 | { |
| 282 | /* as an integer */ |
| 283 | int intval_cxx . . $ CVmExcParam#$anonymous_type_0#intval_.; |
| 284 | |
| 285 | /* as an unsigned long */ |
| 286 | unsigned long ulong_cxx . . $ CVmExcParam#$anonymous_type_0#ulong_.; |
| 287 | |
| 288 | /* as a text string */ |
| 289 | const textchar_tcxx . . $ textchar_t# *strval_cxx . . $ CVmExcParam#$anonymous_type_0#strval_.; |
| 290 | |
| 291 | /* as a char string */ |
| 292 | const char *charval_cxx . . $ CVmExcParam#$anonymous_type_0#charval_.; |
| 293 | |
| 294 | /* as char string with separate length counter */ |
| 295 | structcxx . . $ CVmExcParam#$anonymous_type_0#$anonymous_type_1# |
| 296 | { |
| 297 | const char *str_cxx . . $ CVmExcParam#$anonymous_type_0#$anonymous_type_1#str_.; |
| 298 | size_t len_cxx . . $ CVmExcParam#$anonymous_type_0#$anonymous_type_1#len_.; |
| 299 | } charlenval_cxx . . $ CVmExcParam#$anonymous_type_0#charlenval_.; |
| 300 | } val_cxx . . $ CVmExcParam#val_.; |
| 301 | }; |
| 302 | |
| 303 | /* |
| 304 | * Exception object |
| 305 | */ |
| 306 | struct CVmExceptioncxx . . $ CVmException# |
| 307 | { |
| 308 | /* get the error code */ |
| 309 | int get_error_codecxx . . $ CVmException#get_error_code(455f465bc33b4cdf).() const { return error_code_cxx . . $ CVmException#error_code_.; } |
| 310 | |
| 311 | /* get the number of parameters */ |
| 312 | int get_param_countcxx . . $ CVmException#get_param_count(455f465bc33b4cdf).() const { return param_count_cxx . . $ CVmException#param_count_.; } |
| 313 | |
| 314 | /* get the type of the nth parameter */ |
| 315 | err_param_typecxx . . $ err_param_type# get_param_typecxx . . $ CVmException#get_param_type(ec704a842aa5023a).(int n) const { return params_cxx . . $ CVmException#params_.[n].type_cxx . . $ CVmExcParam#type_.; } |
| 316 | |
| 317 | /* get the nth parameter as an integer */ |
| 318 | int get_param_intcxx . . $ CVmException#get_param_int(7ccb52b1bfe01045).(int n) const { return params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..intval_cxx . . $ CVmExcParam#$anonymous_type_0#intval_.; } |
| 319 | |
| 320 | /* get the nth parameter as an unsigned long */ |
| 321 | unsigned long get_param_ulongcxx . . $ CVmException#get_param_ulong(79dc3568750208f6).(int n) const |
| 322 | { return params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..ulong_cxx . . $ CVmExcParam#$anonymous_type_0#ulong_.; } |
| 323 | |
| 324 | /* get the nth parameter as a string */ |
| 325 | const textchar_tcxx . . $ textchar_t# *get_param_textcxx . . $ CVmException#get_param_text(afbf07f2bc1a9437).(int n) const |
| 326 | { return params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..strval_cxx . . $ CVmExcParam#$anonymous_type_0#strval_.; } |
| 327 | |
| 328 | /* get the nth parameter as a char string */ |
| 329 | const char *get_param_charcxx . . $ CVmException#get_param_char(afbf07f2bc1a9437).(int n) const |
| 330 | { return params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..charval_cxx . . $ CVmExcParam#$anonymous_type_0#charval_.; } |
| 331 | |
| 332 | /* get the nth parameter as a counted-length string */ |
| 333 | const char *get_param_char_lencxx . . $ CVmException#get_param_char_len(a3eb6b4b985f0690).(int n, size_t *len) const |
| 334 | { |
| 335 | /* set the length return */ |
| 336 | *len = params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..charlenval_cxx . . $ CVmExcParam#$anonymous_type_0#charlenval_..len_cxx . . $ CVmExcParam#$anonymous_type_0#$anonymous_type_1#len_.; |
| 337 | |
| 338 | /* return the string pointer */ |
| 339 | return params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..charlenval_cxx . . $ CVmExcParam#$anonymous_type_0#charlenval_..str_cxx . . $ CVmExcParam#$anonymous_type_0#$anonymous_type_1#str_.; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* set a parameter - null-terminated string value */ |
| 344 | void set_param_strcxx . . $ CVmException#set_param_str(66835f578de1bcb3).(int n, const char *str) |
| 345 | { |
| 346 | params_cxx . . $ CVmException#params_.[n].type_cxx . . $ CVmExcParam#type_. = ERR_TYPE_CHARcxx . . $ ERR_TYPE_CHAR.; |
| 347 | params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..charval_cxx . . $ CVmExcParam#$anonymous_type_0#charval_. = str; |
| 348 | } |
| 349 | |
| 350 | /* set a parameter - counted-length string value */ |
| 351 | void set_param_strcxx . . $ CVmException#set_param_str(197ab7b58a5ea4f1).(int n, const char *str, size_t len) |
| 352 | { |
| 353 | params_cxx . . $ CVmException#params_.[n].type_cxx . . $ CVmExcParam#type_. = ERR_TYPE_CHAR_LENcxx . . $ ERR_TYPE_CHAR_LEN.; |
| 354 | params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..charlenval_cxx . . $ CVmExcParam#$anonymous_type_0#charlenval_..str_cxx . . $ CVmExcParam#$anonymous_type_0#$anonymous_type_1#str_. = str; |
| 355 | params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..charlenval_cxx . . $ CVmExcParam#$anonymous_type_0#charlenval_..len_cxx . . $ CVmExcParam#$anonymous_type_0#$anonymous_type_1#len_. = len; |
| 356 | } |
| 357 | |
| 358 | /* set a parameter - integer value */ |
| 359 | void set_param_intcxx . . $ CVmException#set_param_int(3e454b8ccf442868).(int n, int val) |
| 360 | { |
| 361 | params_cxx . . $ CVmException#params_.[n].type_cxx . . $ CVmExcParam#type_. = ERR_TYPE_INTcxx . . $ ERR_TYPE_INT.; |
| 362 | params_cxx . . $ CVmException#params_.[n].val_cxx . . $ CVmExcParam#val_..intval_cxx . . $ CVmExcParam#$anonymous_type_0#intval_. = val; |
| 363 | } |
| 364 | |
| 365 | /* the error code */ |
| 366 | err_id_tcxx . . $ err_id_t# error_code_cxx . . $ CVmException#error_code_.; |
| 367 | |
| 368 | /* |
| 369 | * Is this a version-related error? If this is true, the error is due |
| 370 | * to an out-of-date interpreter, and can usually be solved by |
| 371 | * upgrading to the latest version. |
| 372 | */ |
| 373 | int version_flag_cxx . . $ CVmException#version_flag_.; |
| 374 | |
| 375 | /* |
| 376 | * Some version-related errors are due to component versions, namely |
| 377 | * metaclasses or function sets. For errors due to component versions, |
| 378 | * we set the version flag AND set the appropriate identifier here to |
| 379 | * indicate the required component version. (This is the component |
| 380 | * version that's set as a dependency in the byte-code program we're |
| 381 | * trying to run.) |
| 382 | */ |
| 383 | const char *metaclass_cxx . . $ CVmException#metaclass_.; |
| 384 | const char *funcset_cxx . . $ CVmException#funcset_.; |
| 385 | |
| 386 | /* number of parameters stored in the exception */ |
| 387 | int param_count_cxx . . $ CVmException#param_count_.; |
| 388 | |
| 389 | /* parameters (actual array size is given by param_cnt_) */ |
| 390 | CVmExcParamcxx . . $ CVmExcParam# params_cxx . . $ CVmException#params_.[1]; |
| 391 | }; |
| 392 | |
| 393 | |
| 394 | /* ------------------------------------------------------------------------ */ |
| 395 | /* |
| 396 | * Error states. The states are bit flags. The following combinations are |
| 397 | * possible: |
| 398 | * |
| 399 | * ERR_STATE_TRYING - the initial call to setjmp() in the err_try has just |
| 400 | * returned |
| 401 | * |
| 402 | * ERR_STATE_EXCEPTION - we've just longjmp()'d into the frame from a |
| 403 | * throw, and we haven't handled the exception yet |
| 404 | * |
| 405 | * ERR_STATE_EXCEPTION | ERR_STATE_FINALLY - we've longjmp()'d into the |
| 406 | * frame from a throw, and we've entered the err_finally without |
| 407 | * encountering an err_catch |
| 408 | * |
| 409 | * ERR_STATE_CAUGHT - we've longjmp()'d into the frame from a throw, and |
| 410 | * we've entered the err_catch clause |
| 411 | * |
| 412 | * ERR_STATE_CAUGHT | ERR_FINALLY - we've longjmp()'d into the frame from a |
| 413 | * throw, gone through the err_catch, and entered the err_finally |
| 414 | * |
| 415 | * ERR_STATE_RETHROWN - a new throw occurred within our err_catch or |
| 416 | * err_finally clause |
| 417 | * |
| 418 | * ERR_STATE_RETHROWN | ERR_STATE_FINALLY - a new error occurred within our |
| 419 | * err_catch or err_finally clause, and we've entered the err_finally |
| 420 | */ |
| 421 | typedef int err_state_tcxx . . $ err_state_t#; |
| 422 | |
| 423 | /* |
| 424 | * Trying - initial state, before any exception has been thrown. Note that |
| 425 | * this must have value 0, because we initialize to this state from the |
| 426 | * setjmp return going into the error frame, and setjmp always returns 0 on |
| 427 | * the initial call. |
| 428 | */ |
| 429 | const int ERR_STATE_TRYINGcxx . . $ ERR_STATE_TRYING. = 0; |
| 430 | |
| 431 | /* exception in progress, and has not been caught */ |
| 432 | const int ERR_STATE_EXCEPTIONcxx . . $ ERR_STATE_EXCEPTION. = 0x0001; |
| 433 | |
| 434 | /* exception has been caught */ |
| 435 | const int ERR_STATE_CAUGHTcxx . . $ ERR_STATE_CAUGHT. = 0x0002; |
| 436 | |
| 437 | /* error thrown while an err_catch or err_finally is in progress */ |
| 438 | const int ERR_STATE_RETHROWNcxx . . $ ERR_STATE_RETHROWN. = 0x4000; |
| 439 | |
| 440 | /* |
| 441 | * 'finally' block entered. This is OR'd into the other states when we |
| 442 | * first enter the 'finally', so that we'll know not to re-enter the block |
| 443 | * if another exception occurs within. |
| 444 | */ |
| 445 | const int ERR_STATE_FINALLYcxx . . $ ERR_STATE_FINALLY. = 0x8000; |
| 446 | |
| 447 | |
| 448 | /* ------------------------------------------------------------------------ */ |
| 449 | /* |
| 450 | * Error frame - allocated by err_try. This object is not manipulated |
| 451 | * directly by the client; this is handled automatically by the error |
| 452 | * macros. |
| 453 | */ |
| 454 | struct err_frame_tcxx . . $ err_frame_t# |
| 455 | { |
| 456 | /* current state */ |
| 457 | err_state_tcxx . . $ err_state_t# state_cxx . . $ err_frame_t#state_.; |
| 458 | |
| 459 | /* enclosing error frame */ |
| 460 | err_frame_tcxx . . $ err_frame_t# *prv_cxx . . $ err_frame_t#prv_.; |
| 461 | |
| 462 | /* exception being handled in this frame */ |
| 463 | CVmExceptioncxx . . $ CVmException# *exc_cxx . . $ err_frame_t#exc_.; |
| 464 | |
| 465 | /* jmpbuf for this handler */ |
| 466 | jmp_buf jmpbuf_cxx . . $ err_frame_t#jmpbuf_.; |
| 467 | }; |
| 468 | |
| 469 | /* |
| 470 | * The current error frame active in this thread. Each time we enter an |
| 471 | * err_try, we set up a new frame on the stack and set this thread-global |
| 472 | * to point to the new frame, and we store the old value (the enclosing |
| 473 | * frame) within the new frame, forming a stack via a linked list of |
| 474 | * frames. When we leave the stack at err_end or via a throw, we pop the |
| 475 | * frame stack by setting G_err_frame to the enclosing element. |
| 476 | */ |
| 477 | extern OS_DECL_TLScxx . . $ G_err_frame.(err_frame_t *, G_err_frame); |
| 478 | |
| 479 | /* |
| 480 | * Initialize the global error context. Should be called at program |
| 481 | * initialization. 'param_stack_size' is no longer used. |
| 482 | */ |
| 483 | void err_initcxx . . $ err_init(2c44cd906ae4e829).(size_t param_stack_size); |
| 484 | |
| 485 | /* |
| 486 | * Delete the global error context. Should be called at program |
| 487 | * termination. |
| 488 | */ |
| 489 | void err_terminatecxx . . $ err_terminate(49f6e7a06ebc5aa8).(); |
| 490 | |
| 491 | /* |
| 492 | * Throw an exception. The first form takes no parameters except the |
| 493 | * error code; the second form takes a parameter count followed by that |
| 494 | * number of parameters. Each parameter requires two arguments: the |
| 495 | * first is a type code of type err_param_type, and the second the |
| 496 | * value, whose interpretation depends on the type code. |
| 497 | */ |
| 498 | NORETURNcxx . . $ `tads2/osifc.h:175:15`! void err_throwcxx . . $ err_throw(cb2f890c2fdad230).(err_id_tcxx . . $ err_id_t# error_code); |
| 499 | NORETURNcxx . . $ `tads2/osifc.h:175:15`! void err_throw_acxx . . $ err_throw_a(947b9584d64bb411).(err_id_tcxx . . $ err_id_t# error_code, int param_count, ...); |
| 500 | |
| 501 | /* |
| 502 | * Rethrow the current exception. This is valid only in 'catch' blocks. |
| 503 | */ |
| 504 | void err_rethrowcxx . . $ err_rethrow(49f6e7a06ebc5aa8).(); |
| 505 | |
| 506 | /* |
| 507 | * Get the current exception being handled in the nearest enclosing |
| 508 | * err_catch block. This searches the error frame stack for a frame in the |
| 509 | * 'caught' state, and returns the exception object from that frame. This |
| 510 | * allows retrieving the current exception being handled even when we enter |
| 511 | * a new err_try block within an err_catch handler. |
| 512 | */ |
| 513 | CVmExceptioncxx . . $ CVmException# *err_get_cur_exccxx . . $ err_get_cur_exc(f56bc26ee4d91510).(); |
| 514 | |
| 515 | /* |
| 516 | * Fatal error - abort program |
| 517 | */ |
| 518 | void err_abortcxx . . $ err_abort(85c52e162fed56f9).(const char *message); |
| 519 | |
| 520 | |
| 521 | #define err_trycxx . . $ `tads3/vmerr.h:521:9`! \ |
| 522 | { \ |
| 523 | err_frame_t err_cur__; \ |
| 524 | err_cur__.prv_ = os_tls_get(err_frame_t *, G_err_frame); \ |
| 525 | os_tls_set(G_err_frame, &err_cur__); \ |
| 526 | if ((err_cur__.state_ = \ |
| 527 | (err_state_t)setjmp(err_cur__.jmpbuf_)) == ERR_STATE_TRYING) \ |
| 528 | { \ |
| 529 | |
| 530 | #define err_catchcxx . . $ `tads3/vmerr.h:530:9`!(exc) \ |
| 531 | } \ |
| 532 | if (err_cur__.state_ == ERR_STATE_EXCEPTION) \ |
| 533 | { \ |
| 534 | CVmException *exc; \ |
| 535 | exc = err_cur__.exc_; \ |
| 536 | err_cur__.state_ = ERR_STATE_CAUGHT; |
| 537 | |
| 538 | #define err_catch_disccxx . . $ `tads3/vmerr.h:538:9`! \ |
| 539 | } \ |
| 540 | if (err_cur__.state_ == ERR_STATE_EXCEPTION) \ |
| 541 | { \ |
| 542 | err_cur__.state_ = ERR_STATE_CAUGHT; |
| 543 | |
| 544 | |
| 545 | #define err_finallycxx . . $ `tads3/vmerr.h:545:9`! \ |
| 546 | } \ |
| 547 | if ((err_cur__.state_ & ERR_STATE_FINALLY) == 0) \ |
| 548 | { \ |
| 549 | err_cur__.state_ |= ERR_STATE_FINALLY; |
| 550 | |
| 551 | #define err_endcxx . . $ `tads3/vmerr.h:551:9`! \ |
| 552 | } \ |
| 553 | os_tls_set(G_err_frame, err_cur__.prv_); \ |
| 554 | if (err_cur__.state_ & (ERR_STATE_EXCEPTION | ERR_STATE_RETHROWN)) \ |
| 555 | { \ |
| 556 | if (os_tls_get(err_frame_t *, G_err_frame)->state_ \ |
| 557 | & ERR_STATE_CAUGHT) \ |
| 558 | t3free(os_tls_get(err_frame_t *, G_err_frame)->exc_); \ |
| 559 | os_tls_get(err_frame_t *, G_err_frame)->exc_ = err_cur__.exc_; \ |
| 560 | err_rethrow(); \ |
| 561 | } \ |
| 562 | if (err_cur__.state_ & ERR_STATE_CAUGHT) \ |
| 563 | t3free(err_cur__.exc_); \ |
| 564 | } |
| 565 | |
| 566 | #endif /* VMERR_H */ |
| 567 |