forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_error.c
304 lines (276 loc) · 6.14 KB
/
eval_error.c
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
/* -*-c-*- */
/*
* included by eval.c
*/
static void
warn_printf(const char *fmt, ...)
{
VALUE str;
va_list args;
va_init_list(args, fmt);
str = rb_vsprintf(fmt, args);
va_end(args);
rb_write_error_str(str);
}
#define warn_print(x) rb_write_error(x)
#define warn_print2(x,l) rb_write_error2((x),(l))
#define warn_print_str(x) rb_write_error_str(x)
static void
error_pos(void)
{
const char *sourcefile = rb_sourcefile();
int sourceline = rb_sourceline();
if (sourcefile) {
ID caller_name;
if (sourceline == 0) {
warn_printf("%s", sourcefile);
}
else if ((caller_name = rb_frame_callee()) != 0) {
warn_printf("%s:%d:in `%s'", sourcefile, sourceline,
rb_id2name(caller_name));
}
else {
warn_printf("%s:%d", sourcefile, sourceline);
}
}
}
static VALUE
get_backtrace(VALUE info)
{
if (NIL_P(info))
return Qnil;
info = rb_funcall(info, rb_intern("backtrace"), 0);
if (NIL_P(info))
return Qnil;
return rb_check_backtrace(info);
}
VALUE
rb_get_backtrace(VALUE info)
{
return get_backtrace(info);
}
VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt);
static void
set_backtrace(VALUE info, VALUE bt)
{
ID set_backtrace = rb_intern("set_backtrace");
if (rb_backtrace_p(bt)) {
if (rb_method_basic_definition_p(CLASS_OF(info), set_backtrace)) {
rb_exc_set_backtrace(info, bt);
return;
}
else {
bt = rb_backtrace_to_str_ary(bt);
}
}
rb_funcall(info, rb_intern("set_backtrace"), 1, bt);
}
static void
error_print(void)
{
volatile VALUE errat = Qundef;
rb_thread_t *th = GET_THREAD();
VALUE errinfo = th->errinfo;
int raised_flag = th->raised_flag;
volatile VALUE eclass = Qundef, e = Qundef;
const char *volatile einfo;
volatile long elen;
if (NIL_P(errinfo))
return;
rb_thread_raised_clear(th);
TH_PUSH_TAG(th);
if (TH_EXEC_TAG() == 0) {
errat = get_backtrace(errinfo);
}
else if (errat == Qundef) {
errat = Qnil;
}
else if (eclass == Qundef || e != Qundef) {
goto error;
}
else {
goto no_message;
}
if (NIL_P(errat)) {
const char *file = rb_sourcefile();
int line = rb_sourceline();
if (!file)
warn_printf("%d", line);
else if (!line)
warn_printf("%s", file);
else
warn_printf("%s:%d", file, line);
}
else if (RARRAY_LEN(errat) == 0) {
error_pos();
}
else {
VALUE mesg = RARRAY_AREF(errat, 0);
if (NIL_P(mesg))
error_pos();
else {
warn_print_str(mesg);
}
}
eclass = CLASS_OF(errinfo);
if (eclass != Qundef &&
(e = rb_check_funcall(errinfo, rb_intern("message"), 0, 0)) != Qundef &&
(RB_TYPE_P(e, T_STRING) || !NIL_P(e = rb_check_string_type(e)))) {
einfo = RSTRING_PTR(e);
elen = RSTRING_LEN(e);
}
else {
no_message:
einfo = "";
elen = 0;
}
if (eclass == rb_eRuntimeError && elen == 0) {
warn_print(": unhandled exception\n");
}
else {
VALUE epath;
epath = rb_class_name(eclass);
if (elen == 0) {
warn_print(": ");
warn_print_str(epath);
warn_print("\n");
}
else {
char *tail = 0;
long len = elen;
if (RSTRING_PTR(epath)[0] == '#')
epath = 0;
if ((tail = memchr(einfo, '\n', elen)) != 0) {
len = tail - einfo;
tail++; /* skip newline */
}
warn_print(": ");
warn_print2(einfo, len);
if (epath) {
warn_print(" (");
warn_print_str(epath);
warn_print(")\n");
}
if (tail) {
warn_print2(tail, elen - len - 1);
if (einfo[elen-1] != '\n') warn_print2("\n", 1);
}
}
}
if (!NIL_P(errat)) {
long i;
long len = RARRAY_LEN(errat);
int skip = eclass == rb_eSysStackError;
#define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5)
#define TRACE_HEAD 8
#define TRACE_TAIL 5
for (i = 1; i < len; i++) {
VALUE line = RARRAY_AREF(errat, i);
if (RB_TYPE_P(line, T_STRING)) {
warn_printf("\tfrom %"PRIsVALUE"\n", line);
}
if (skip && i == TRACE_HEAD && len > TRACE_MAX) {
warn_printf("\t ... %ld levels...\n",
len - TRACE_HEAD - TRACE_TAIL);
i = len - TRACE_TAIL;
}
}
}
error:
TH_POP_TAG();
th->errinfo = errinfo;
rb_thread_raised_set(th, raised_flag);
}
void
ruby_error_print(void)
{
error_print();
}
void
rb_print_undef(VALUE klass, ID id, int scope)
{
const char *v;
switch (scope) {
default:
case NOEX_PUBLIC: v = ""; break;
case NOEX_PRIVATE: v = " private"; break;
case NOEX_PROTECTED: v = " protected"; break;
}
rb_name_error(id, "undefined%s method `%"PRIsVALUE"' for %s `%"PRIsVALUE"'", v,
QUOTE_ID(id),
(RB_TYPE_P(klass, T_MODULE)) ? "module" : "class",
rb_class_name(klass));
}
void
rb_print_undef_str(VALUE klass, VALUE name)
{
rb_name_error_str(name, "undefined method `%"PRIsVALUE"' for %s `%"PRIsVALUE"'",
QUOTE(name),
(RB_TYPE_P(klass, T_MODULE)) ? "module" : "class",
rb_class_name(klass));
}
static int
sysexit_status(VALUE err)
{
VALUE st = rb_iv_get(err, "status");
return NUM2INT(st);
}
static int
error_handle(int ex)
{
int status = EXIT_FAILURE;
rb_thread_t *th = GET_THREAD();
if (rb_threadptr_set_raised(th))
return EXIT_FAILURE;
switch (ex & TAG_MASK) {
case 0:
status = EXIT_SUCCESS;
break;
case TAG_RETURN:
error_pos();
warn_print(": unexpected return\n");
break;
case TAG_NEXT:
error_pos();
warn_print(": unexpected next\n");
break;
case TAG_BREAK:
error_pos();
warn_print(": unexpected break\n");
break;
case TAG_REDO:
error_pos();
warn_print(": unexpected redo\n");
break;
case TAG_RETRY:
error_pos();
warn_print(": retry outside of rescue clause\n");
break;
case TAG_THROW:
/* TODO: fix me */
error_pos();
warn_printf(": unexpected throw\n");
break;
case TAG_RAISE: {
VALUE errinfo = GET_THREAD()->errinfo;
if (rb_obj_is_kind_of(errinfo, rb_eSystemExit)) {
status = sysexit_status(errinfo);
}
else if (rb_obj_is_instance_of(errinfo, rb_eSignal)) {
/* no message when exiting by signal */
}
else {
error_print();
}
break;
}
case TAG_FATAL:
error_print();
break;
default:
rb_bug("Unknown longjmp status %d", ex);
break;
}
rb_threadptr_reset_raised(th);
return status;
}