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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
(* EXCEPTIONS.mod implement the ISO EXCEPTIONS specification.
Copyright (C) 2003-2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Modula-2 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
General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. *)
IMPLEMENTATION MODULE EXCEPTIONS ;
IMPORT RTExceptions ;
IMPORT M2EXCEPTION ;
IMPORT M2RTS ;
IMPORT ASCII ;
FROM SYSTEM IMPORT ADR ;
FROM Storage IMPORT ALLOCATE ;
TYPE
ExceptionSource = POINTER TO RECORD
eh: RTExceptions.EHBlock ;
END ;
(* values of this type are used within library modules to
identify the source of raised exceptions *)
PROCEDURE AllocateSource (VAR newSource: ExceptionSource) ;
(* Allocates a unique value of type ExceptionSource *)
BEGIN
NEW(newSource) ;
WITH newSource^ DO
eh := RTExceptions.InitExceptionBlock()
END
END AllocateSource ;
PROCEDURE RAISE (source: ExceptionSource;
number: ExceptionNumber;
message: ARRAY OF CHAR) ;
(* Associates the given values of source, number and message with
the current context and raises an exception.
*)
BEGIN
RTExceptions.SetExceptionSource(source) ;
RTExceptions.SetExceptionBlock(source^.eh) ;
RTExceptions.Raise(number, ADR(__FILE__), __LINE__, __COLUMN__, ADR(__FUNCTION__), ADR(message)) ;
(* we should never reach here as Raise should jump to the exception handler *)
M2RTS.Halt('should never return from RTException.Raise',
__FILE__, __FUNCTION__, __LINE__)
END RAISE ;
PROCEDURE CurrentNumber (source: ExceptionSource) : ExceptionNumber ;
(* If the current coroutine is in the exceptional execution state
because of the raising of an exception from source, returns the
corresponding number, and otherwise raises an exception.
*)
BEGIN
IF RTExceptions.IsInExceptionState()
THEN
RETURN( RTExceptions.GetNumber(source^.eh) )
ELSE
RTExceptions.Raise(ORD(M2EXCEPTION.coException),
ADR(__FILE__), __LINE__, __COLUMN__, ADR(__FUNCTION__),
ADR('current coroutine is not in the exceptional execution state'))
END
END CurrentNumber ;
PROCEDURE GetMessage (VAR text: ARRAY OF CHAR) ;
(* If the current coroutine is in the exceptional execution state,
returns the possibly truncated string associated with the
current context. Otherwise, in normal execution state,
returns the empty string.
*)
VAR
p : POINTER TO CHAR ;
i, h: CARDINAL ;
BEGIN
IF RTExceptions.IsInExceptionState()
THEN
h := HIGH(text) ;
i := 0 ;
p := RTExceptions.GetTextBuffer(RTExceptions.GetExceptionBlock()) ;
WHILE (p#NIL) AND (p^#ASCII.nul) DO
text[i] := p^ ;
INC(i) ;
INC(p)
END ;
IF i<=h
THEN
text[i] := ASCII.nul
END
ELSE
text[0] := ASCII.nul
END
END GetMessage ;
PROCEDURE IsCurrentSource (source: ExceptionSource) : BOOLEAN ;
(* If the current coroutine is in the exceptional execution state
because of the raising of an exception from source, returns TRUE,
and otherwise returns FALSE.
*)
BEGIN
RETURN( RTExceptions.IsInExceptionState() AND (source=RTExceptions.GetExceptionSource()) )
END IsCurrentSource ;
PROCEDURE IsExceptionalExecution () : BOOLEAN ;
(* If the current coroutine is in the exceptional execution state
because of the raising of an exception, returns TRUE,
and otherwise returns FALSE.
*)
BEGIN
RETURN( RTExceptions.IsInExceptionState() )
END IsExceptionalExecution ;
END EXCEPTIONS.
|