| 1 |
(* EXCEPTIONS.mod implement the ISO EXCEPTIONS specification. |
| 2 |
|
| 3 |
Copyright (C) 2003-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. *) |
| 26 |
|
| 27 |
IMPLEMENTATION MODULE EXCEPTIONS ; |
| 28 |
|
| 29 |
IMPORT RTExceptions ; |
| 30 |
IMPORT M2EXCEPTION ; |
| 31 |
IMPORT M2RTS ; |
| 32 |
IMPORT ASCII ; |
| 33 |
|
| 34 |
FROM SYSTEM IMPORT ADR ; |
| 35 |
FROM Storage IMPORT ALLOCATE ; |
| 36 |
|
| 37 |
|
| 38 |
TYPE |
| 39 |
ExceptionSource = POINTER TO RECORD |
| 40 |
eh: RTExceptions.EHBlock ; |
| 41 |
END ; |
| 42 |
(* values of this type are used within library modules to |
| 43 |
identify the source of raised exceptions *) |
| 44 |
|
| 45 |
|
| 46 |
PROCEDURE AllocateSource (VAR newSource: ExceptionSource) ; |
| 47 |
(* Allocates a unique value of type ExceptionSource *) |
| 48 |
BEGIN |
| 49 |
NEW(newSource) ; |
| 50 |
WITH newSource^ DO |
| 51 |
eh := RTExceptions.InitExceptionBlock() |
| 52 |
END |
| 53 |
END AllocateSource ; |
| 54 |
|
| 55 |
|
| 56 |
PROCEDURE RAISE (source: ExceptionSource; |
| 57 |
number: ExceptionNumber; |
| 58 |
message: ARRAY OF CHAR) ; |
| 59 |
(* Associates the given values of source, number and message with |
| 60 |
the current context and raises an exception. |
| 61 |
*) |
| 62 |
BEGIN |
| 63 |
RTExceptions.SetExceptionSource(source) ; |
| 64 |
RTExceptions.SetExceptionBlock(source^.eh) ; |
| 65 |
RTExceptions.Raise(number, ADR(__FILE__), __LINE__, __COLUMN__, ADR(__FUNCTION__), ADR(message)) ; |
| 66 |
(* we should never reach here as Raise should jump to the exception handler *) |
| 67 |
M2RTS.Halt('should never return from RTException.Raise', |
| 68 |
__FILE__, __FUNCTION__, __LINE__) |
| 69 |
END RAISE ; |
| 70 |
|
| 71 |
|
| 72 |
PROCEDURE CurrentNumber (source: ExceptionSource) : ExceptionNumber ; |
| 73 |
(* If the current coroutine is in the exceptional execution state |
| 74 |
because of the raising of an exception from source, returns the |
| 75 |
corresponding number, and otherwise raises an exception. |
| 76 |
*) |
| 77 |
BEGIN |
| 78 |
IF RTExceptions.IsInExceptionState() |
| 79 |
THEN |
| 80 |
RETURN( RTExceptions.GetNumber(source^.eh) ) |
| 81 |
ELSE |
| 82 |
RTExceptions.Raise(ORD(M2EXCEPTION.coException), |
| 83 |
ADR(__FILE__), __LINE__, __COLUMN__, ADR(__FUNCTION__), |
| 84 |
ADR('current coroutine is not in the exceptional execution state')) ; |
| 85 |
RETURN VAL (ExceptionNumber, M2EXCEPTION.exException) |
| 86 |
END |
| 87 |
END CurrentNumber ; |
| 88 |
|
| 89 |
|
| 90 |
PROCEDURE GetMessage (VAR text: ARRAY OF CHAR) ; |
| 91 |
(* If the current coroutine is in the exceptional execution state, |
| 92 |
returns the possibly truncated string associated with the |
| 93 |
current context. Otherwise, in normal execution state, |
| 94 |
returns the empty string. |
| 95 |
*) |
| 96 |
VAR |
| 97 |
p : POINTER TO CHAR ; |
| 98 |
i, h: CARDINAL ; |
| 99 |
BEGIN |
| 100 |
IF RTExceptions.IsInExceptionState() |
| 101 |
THEN |
| 102 |
h := HIGH(text) ; |
| 103 |
i := 0 ; |
| 104 |
p := RTExceptions.GetTextBuffer(RTExceptions.GetExceptionBlock()) ; |
| 105 |
WHILE (p#NIL) AND (p^#ASCII.nul) DO |
| 106 |
text[i] := p^ ; |
| 107 |
INC(i) ; |
| 108 |
INC(p) |
| 109 |
END ; |
| 110 |
IF i<=h |
| 111 |
THEN |
| 112 |
text[i] := ASCII.nul |
| 113 |
END |
| 114 |
ELSE |
| 115 |
text[0] := ASCII.nul |
| 116 |
END |
| 117 |
END GetMessage ; |
| 118 |
|
| 119 |
|
| 120 |
PROCEDURE IsCurrentSource (source: ExceptionSource) : BOOLEAN ; |
| 121 |
(* If the current coroutine is in the exceptional execution state |
| 122 |
because of the raising of an exception from source, returns TRUE, |
| 123 |
and otherwise returns FALSE. |
| 124 |
*) |
| 125 |
BEGIN |
| 126 |
RETURN( RTExceptions.IsInExceptionState() AND (source=RTExceptions.GetExceptionSource()) ) |
| 127 |
END IsCurrentSource ; |
| 128 |
|
| 129 |
|
| 130 |
PROCEDURE IsExceptionalExecution () : BOOLEAN ; |
| 131 |
(* If the current coroutine is in the exceptional execution state |
| 132 |
because of the raising of an exception, returns TRUE, |
| 133 |
and otherwise returns FALSE. |
| 134 |
*) |
| 135 |
BEGIN |
| 136 |
RETURN( RTExceptions.IsInExceptionState() ) |
| 137 |
END IsExceptionalExecution ; |
| 138 |
|
| 139 |
|
| 140 |
END EXCEPTIONS. |