FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mrubyc/src/error.c at master · sxtek/mrubyc · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
sxtek
/
mrubyc
Public
forked from
mrubyc/mrubyc
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mrubyc
/
src
/
error.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
325 lines (255 loc) · 8.03 KB
Breadcrumbs
mrubyc
/
src
/
error.c
Copy path
File metadata and controls
325 lines (255 loc) · 8.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*! @file
@brief
exception classes
<pre>
Copyright (C) 2015-2023 Kyushu Institute of Technology.
Copyright (C) 2015-2023 Shimane IT Open-Innovation Center.
This file is distributed under BSD 3-Clause License.
</pre>
*/
/***** Feature test switches ************************************************/
/***** System headers *******************************************************/
//@cond
#include
"vm_config.h"
#include
<string.h>
#include
<stdarg.h>
//@endcond
/***** Local headers ********************************************************/
#include
"alloc.h"
#include
"value.h"
#include
"symbol.h"
#include
"error.h"
#include
"class.h"
#include
"c_string.h"
#include
"vm.h"
#include
"console.h"
/***** Constat values *******************************************************/
/***** Macros ***************************************************************/
/***** Typedefs *************************************************************/
/***** Function prototypes **************************************************/
/***** Local variables ******************************************************/
/***** Global variables *****************************************************/
/***** Functions ************************************************************/
//================================================================
/*! constructor
@param vm pointer to VM.
@param exc_cls pointer to Exception class.
@param message message.
@param len message length or zero.
@return exception object.
*/
mrbc_value
mrbc_exception_new
(
struct
VM
*
vm
,
struct
RClass
*
exc_cls
,
const
void
*
message
,
int
len
)
{
// allocate memory.
mrbc_exception
*
ex
=
mrbc_alloc
(
vm
,
sizeof
(
mrbc_exception
) );
if
( !
ex
) {
// ENOMEM
return
mrbc_nil_value
();
}
MRBC_INIT_OBJECT_HEADER
(
ex
,
"EX"
);
ex
->
cls
=
exc_cls
;
// in case of no message.
if
( !
message
) {
ex
->
message
=
0
;
ex
->
message_size
=
0
;
goto
RETURN
;
}
// in case of message is ""
if
(
*
(
const
char
*
)
message
==
0
) {
ex
->
message
=
(
const
uint8_t
*
)
""
;
ex
->
message_size
=
0
;
goto
RETURN
;
}
// in case of message in ROM.
if
(
len
==
0
) {
ex
->
message
=
message
;
ex
->
message_size
=
0
;
goto
RETURN
;
}
// else, copy the message.
uint8_t
*
buf
=
mrbc_alloc
(
vm
,
len
+
1
);
if
(
buf
) {
memcpy
(
buf
,
message
,
len
);
buf
[
len
]
=
0
;
ex
->
message_size
=
len
;
}
else
{
ex
->
message_size
=
0
;
}
ex
->
message
=
buf
;
RETURN
:
return
(
mrbc_value
){.
tt
=
MRBC_TT_EXCEPTION
, .
exception
=
ex
};
}
//================================================================
/*! constructor with allocated message buffer.
@param vm pointer to VM.
@param exc_cls pointer to Exception class.
@param message message buffer.
@param len message length.
@return exception object.
*/
mrbc_value
mrbc_exception_new_alloc
(
struct
VM
*
vm
,
struct
RClass
*
exc_cls
,
const
void
*
message
,
int
len
)
{
// allocate memory.
mrbc_exception
*
ex
=
mrbc_alloc
(
vm
,
sizeof
(
mrbc_exception
) );
if
( !
ex
) {
// ENOMEM
return
mrbc_nil_value
();
}
MRBC_INIT_OBJECT_HEADER
(
ex
,
"EX"
);
ex
->
cls
=
exc_cls
;
ex
->
message
=
message
;
ex
->
message_size
=
len
;
return
(
mrbc_value
){.
tt
=
MRBC_TT_EXCEPTION
, .
exception
=
ex
};
}
//================================================================
/*! destructor
@param value target.
*/
void
mrbc_exception_delete
(
mrbc_value
*
value
)
{
if
(
value
->
exception
->
message_size
) {
mrbc_raw_free
( (
void
*
)
value
->
exception
->
message
);
}
mrbc_raw_free
(
value
->
exception
);
}
//================================================================
/*! raise exception
@param vm pointer to VM.
@param exc_cls pointer to Exception class or NULL.
@param msg message or NULL.
@note (usage) mrbc_raise(vm, MRBC_CLASS(TypeError), "message here.");
*/
void
mrbc_raise
(
struct
VM
*
vm
,
struct
RClass
*
exc_cls
,
const
char
*
msg
)
{
if
(
vm
) {
struct
RClass
*
cls
=
exc_cls
?
exc_cls
:
MRBC_CLASS
(
RuntimeError
);
const
char
msg_len
=
msg
?
strlen
(
msg
) :
0
;
mrbc_decref
(
&
vm
->
exception
);
vm
->
exception
=
mrbc_exception_new
(
vm
,
cls
,
msg
,
msg_len
);
vm
->
flag_preemption
=
2
;
}
else
{
mrbc_printf
(
"Exception: %s (%s)\n"
,
msg
?
msg
:
mrbc_symid_to_str
(
exc_cls
->
sym_id
),
mrbc_symid_to_str
(
exc_cls
->
sym_id
));
}
}
//================================================================
/*! raise exception like printf
@param vm pointer to VM.
@param exc_cls pointer to Exception class.
@param fstr format string.
*/
void
mrbc_raisef
(
struct
VM
*
vm
,
struct
RClass
*
exc_cls
,
const
char
*
fstr
, ... )
{
static
const
int
MESSAGE_INI_LEN
=
32
;
va_list
ap
;
va_start
(
ap
,
fstr
);
char
*
buf
=
0
;
if
(
vm
)
buf
=
mrbc_alloc
(
vm
,
MESSAGE_INI_LEN
);
if
(
buf
) {
mrbc_vasprintf
(
&
buf
,
MESSAGE_INI_LEN
,
fstr
,
ap
);
mrbc_decref
(
&
vm
->
exception
);
vm
->
exception
=
mrbc_exception_new_alloc
(
vm
,
exc_cls
?
exc_cls
:
MRBC_CLASS
(
RuntimeError
),
buf
,
strlen
(
buf
) );
vm
->
flag_preemption
=
2
;
}
else
{
// VM == NULL or ENOMEM
mrbc_print
(
"Exception: "
);
mrbc_vprintf
(
fstr
,
ap
);
mrbc_printf
(
" (%s)\n"
,
exc_cls
?
mrbc_symid_to_str
(
exc_cls
->
sym_id
) :
"RuntimeError"
);
}
va_end
(
ap
);
}
//================================================================
/*! display exception
@param v pointer to Exception object.
*/
void
mrbc_print_exception
(
const
mrbc_value
*
v
)
{
if
(
mrbc_type
(
*
v
)
!=
MRBC_TT_EXCEPTION
)
return
;
const
mrbc_exception
*
exc
=
v
->
exception
;
const
char
*
clsname
=
mrbc_symid_to_str
(
exc
->
cls
->
sym_id
);
mrbc_printf
(
"Exception: %s (%s)\n"
,
exc
->
message
? (
const
char
*
)
exc
->
message
:
clsname
,
clsname
);
}
//================================================================
/*! display exception in vm.
@param vm pointer to VM
*/
void
mrbc_print_vm_exception
(
const
struct
VM
*
vm
)
{
if
(
mrbc_type
(
vm
->
exception
)
!=
MRBC_TT_EXCEPTION
)
return
;
const
mrbc_exception
*
exc
=
vm
->
exception
.
exception
;
const
char
*
clsname
=
mrbc_symid_to_str
(
exc
->
cls
->
sym_id
);
mrbc_printf
(
"Exception(vm_id=%d): %s (%s)\n"
,
vm
->
vm_id
,
exc
->
message
? (
const
char
*
)
exc
->
message
:
clsname
,
clsname
);
}
/***** Exception class ******************************************************/
//================================================================
/*! (method) new
*/
static
void
c_exception_new
(
struct
VM
*
vm
,
mrbc_value
v
[],
int
argc
)
{
assert
(
mrbc_type
(
v
[
0
])
==
MRBC_TT_CLASS
);
mrbc_value
value
;
if
(
argc
==
1
&&
mrbc_type
(
v
[
1
])
==
MRBC_TT_STRING
) {
value
=
mrbc_exception_new
(
vm
,
v
[
0
].
cls
,
mrbc_string_cstr
(
&
v
[
1
]),
mrbc_string_size
(
&
v
[
1
]));
}
else
{
value
=
mrbc_exception_new
(
vm
,
v
[
0
].
cls
,
NULL
,
0
);
}
SET_RETURN
(
value
);
}
//================================================================
/*! (method) message
*/
static
void
c_exception_message
(
struct
VM
*
vm
,
mrbc_value
v
[],
int
argc
)
{
mrbc_value
value
;
if
(
v
[
0
].
exception
->
message
) {
value
=
mrbc_string_new
(
vm
,
v
[
0
].
exception
->
message
,
v
[
0
].
exception
->
message_size
);
}
else
{
value
=
mrbc_string_new_cstr
(
vm
,
mrbc_symid_to_str
(
v
->
exception
->
cls
->
sym_id
));
}
mrbc_decref
(
&
v
[
0
] );
v
[
0
]
=
value
;
}
/* mruby/c Exception class hierarchy.
Exception
NoMemoryError
NotImplementedError
StandardError
ArgumentError
IndexError
NameError
NoMethodError
RangeError
RuntimeError
TypeError
ZeroDivisionError
*/
/* MRBC_AUTOGEN_METHOD_TABLE
FILE("_autogen_class_exception.h")
CLASS("Exception")
METHOD("new", c_exception_new )
METHOD("message", c_exception_message )
CLASS("NoMemoryError")
SUPER("Exception")
CLASS("NotImplementedError")
SUPER("Exception")
CLASS("StandardError")
SUPER("Exception")
CLASS("ArgumentError")
SUPER("StandardError")
CLASS("IndexError")
SUPER("StandardError")
CLASS("NameError")
SUPER("StandardError")
CLASS("NoMethodError")
SUPER("NameError")
CLASS("RangeError")
SUPER("StandardError")
CLASS("RuntimeError")
SUPER("StandardError")
CLASS("TypeError")
SUPER("StandardError")
CLASS("ZeroDivisionError")
SUPER("StandardError")
*/
#include
"_autogen_class_exception.h"
Back
|
FazBrowse Home
|
New Git URL