This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathfoundation-error.cpp
More file actions
598 lines (477 loc) · 14.9 KB
/
foundation-error.cpp
File metadata and controls
598 lines (477 loc) · 14.9 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode 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.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
#include "foundation-private.h"
#include <stdlib.h>
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF MCTypeInfoRef kMCOutOfMemoryErrorTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef kMCGenericErrorTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef kMCUnboundTypeErrorTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef kMCUnimplementedErrorTypeInfo;
static MCErrorRef s_last_error = nil;
static MCErrorRef s_out_of_memory_error = nil;
////////////////////////////////////////////////////////////////////////////////
static bool MCErrorFormatMessage(MCStringRef p_format, MCArrayRef p_info, MCStringRef& r_message)
{
MCAutoStringRef t_message;
if (!MCStringCreateMutable(0, &t_message))
return false;
uindex_t t_limit;
t_limit = MCStringGetLength(p_format);
uindex_t t_index;
t_index = 0;
while(t_index < t_limit)
{
unichar_t t_char;
t_char = MCStringGetCharAtIndex(p_format, t_index);
// If the sequence is '%{' and there is at least one more char after
// the '%{' then assume it is a key index.
if (t_char == '%' &&
t_index + 2 < t_limit &&
MCStringGetCharAtIndex(p_format, t_index + 1) == '{')
{
MCAutoStringRef t_key_string;
if (!MCStringCreateMutable(0, &t_key_string))
return false;
t_index += 2;
while(t_index < t_limit)
{
t_char = MCStringGetCharAtIndex(p_format, t_index);
if (t_char == '}')
break;
if (!MCStringAppendChar(*t_key_string, t_char))
return false;
t_index += 1;
}
// If it is a well-formed %{...} sequence then process it as a key
// of the info array. Otherwise just append the accumulated string.
if (t_char == '}')
{
MCNewAutoNameRef t_key;
if (!MCNameCreate(*t_key_string, &t_key))
return false;
MCValueRef t_value;
if (p_info != nil &&
MCArrayFetchValue(p_info, false, *t_key, t_value))
{
MCAutoStringRef t_formatted_value;
if (!MCStringFormat(&t_formatted_value, "%@", t_value))
return false;
if (!MCStringAppend(*t_message, *t_formatted_value))
return false;
}
}
else
{
if (!MCStringAppend(*t_message, *t_key_string))
return false;
}
}
else if (t_char == '%' &&
t_index + 1 < t_limit &&
MCStringGetCharAtIndex(p_format, t_index + 1) == '%')
{
if (!MCStringAppendChar(*t_message, '%'))
return false;
t_index += 1;
}
else
{
if (!MCStringAppendChar(*t_message, t_char))
return false;
}
t_index += 1;
}
r_message = MCValueRetain(*t_message);
return true;
}
MC_DLLEXPORT_DEF bool
MCErrorCreateWithMessage (MCTypeInfoRef p_typeinfo,
MCStringRef p_message,
MCArrayRef p_info,
MCErrorRef & r_error)
{
__MCAssertIsErrorTypeInfo(p_typeinfo);
__MCAssertIsString(p_message);
if (nil != p_info)
__MCAssertIsArray(p_info);
__MCError *self;
if (!__MCValueCreate(kMCValueTypeCodeError, self))
return false;
if (!MCErrorFormatMessage(p_message, p_info, self -> message))
{
MCValueRelease(self);
return false;
}
self -> typeinfo = MCValueRetain(p_typeinfo);
if (p_info != nil)
self -> info = MCValueRetain(p_info);
self -> backtrace = nil;
r_error = self;
return true;
}
MC_DLLEXPORT_DEF
bool MCErrorCreateWithMessageV(MCErrorRef& r_error,
MCTypeInfoRef p_error_type,
MCStringRef p_message,
va_list p_args)
{
__MCAssertIsErrorTypeInfo(p_error_type);
__MCAssertIsString(p_message);
MCAutoArrayRef t_info;
if (!MCArrayCreateMutable(&t_info))
return false;
for(;;)
{
const char *t_key;
t_key = va_arg(p_args, const char *);
if (t_key == nil)
break;
MCValueRef t_value;
t_value = va_arg(p_args, MCValueRef);
// If a value is nil, then it means don't include it.
if (t_value == nil)
continue;
MCNewAutoNameRef t_name;
if (!MCNameCreateWithNativeChars((const char_t *)t_key,
strlen(t_key),
&t_name))
return false;
if (!MCArrayStoreValue(*t_info,
true,
*t_name,
t_value))
{
return false;
}
}
if (!MCErrorCreateWithMessage(p_error_type,
p_message,
*t_info,
r_error))
{
return false;
}
return true;
}
MC_DLLEXPORT_DEF
bool MCErrorCreateWithMessageS(MCErrorRef& r_error,
MCTypeInfoRef p_error_type,
MCStringRef p_message,
...)
{
va_list t_args;
va_start(t_args, p_message);
bool t_result;
t_result = MCErrorCreateWithMessageV(r_error,
p_error_type,
p_message,
t_args);
va_end(t_args);
return t_result;
}
MC_DLLEXPORT_DEF
bool MCErrorCreate(MCTypeInfoRef p_typeinfo, MCArrayRef p_info, MCErrorRef& r_error)
{
return MCErrorCreateWithMessage (p_typeinfo,
MCErrorTypeInfoGetMessage (p_typeinfo),
p_info,
r_error);
}
MC_DLLEXPORT_DEF
bool MCErrorCreateV(MCErrorRef& r_error,
MCTypeInfoRef p_typeinfo,
va_list p_args)
{
return MCErrorCreateWithMessageV(r_error,
p_typeinfo,
MCErrorTypeInfoGetMessage (p_typeinfo),
p_args);
}
MC_DLLEXPORT_DEF
bool MCErrorCreateS(MCErrorRef& r_error,
MCTypeInfoRef p_typeinfo,
...)
{
va_list t_args;
va_start(t_args, p_typeinfo);
bool t_result;
t_result = MCErrorCreateV(r_error,
p_typeinfo,
t_args);
va_end(t_args);
return t_result;
}
MC_DLLEXPORT_DEF
bool MCErrorUnwind(MCErrorRef p_error, MCValueRef p_target, uindex_t p_row, uindex_t p_column)
{
__MCAssertIsError(p_error);
MCErrorFrame *t_frame;
if (!MCMemoryNew(t_frame))
return false;
t_frame -> caller = nil;
t_frame -> target = MCValueRetain(p_target);
t_frame -> row = p_row;
t_frame -> column = p_column;
if (p_error -> backtrace == nil)
p_error -> backtrace = t_frame;
else
{
MCErrorFrame *t_other_frame;
for(t_other_frame = p_error -> backtrace; t_other_frame -> caller != nil; t_other_frame = t_other_frame -> caller)
;
t_other_frame -> caller = t_frame;
}
return true;
}
MC_DLLEXPORT_DEF
MCNameRef MCErrorGetDomain(MCErrorRef self)
{
__MCAssertIsError(self);
return MCErrorTypeInfoGetDomain(self -> typeinfo);
}
MC_DLLEXPORT_DEF
MCArrayRef MCErrorGetInfo(MCErrorRef self)
{
__MCAssertIsError(self);
return self -> info;
}
MC_DLLEXPORT_DEF
MCStringRef MCErrorGetMessage(MCErrorRef self)
{
__MCAssertIsError(self);
return self -> message;
}
MC_DLLEXPORT_DEF
uindex_t MCErrorGetDepth(MCErrorRef self)
{
__MCAssertIsError(self);
if (self -> backtrace == nil)
return 0;
uindex_t t_depth;
t_depth = 0;
for(MCErrorFrame *t_frame = self -> backtrace; t_frame != nil; t_frame = t_frame -> caller)
t_depth += 1;
return t_depth;
}
////////////////////////////////////////////////////////////////////////////////
static MCErrorFrame *__MCErrorGetFrameAtLevel(MCErrorRef self, uindex_t p_level)
{
MCErrorFrame *t_frame;
for(t_frame = self -> backtrace; t_frame != nil && p_level != 0; t_frame = t_frame -> caller)
p_level -= 1;
if (p_level != 0)
return nil;
return t_frame;
}
MC_DLLEXPORT_DEF
MCValueRef MCErrorGetTargetAtLevel(MCErrorRef self, uindex_t p_level)
{
__MCAssertIsError(self);
MCErrorFrame *t_frame;
t_frame = __MCErrorGetFrameAtLevel(self, p_level);
if (t_frame == nil)
return nil;
return t_frame -> target;
}
MC_DLLEXPORT_DEF
uindex_t MCErrorGetRowAtLevel(MCErrorRef self, uindex_t p_level)
{
__MCAssertIsError(self);
MCErrorFrame *t_frame;
t_frame = __MCErrorGetFrameAtLevel(self, p_level);
if (t_frame == nil)
return 0;
return t_frame -> row;
}
MC_DLLEXPORT_DEF
uindex_t MCErrorGetColumnAtLevel(MCErrorRef self, uindex_t p_level)
{
__MCAssertIsError(self);
MCErrorFrame *t_frame;
t_frame = __MCErrorGetFrameAtLevel(self, p_level);
if (t_frame == nil)
return 0;
return t_frame -> column;
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCErrorThrow(MCErrorRef p_error)
{
__MCAssertIsError(p_error);
if (s_last_error != nil)
MCValueRelease(s_last_error);
s_last_error = MCValueRetain(p_error);
return false;
}
MC_DLLEXPORT_DEF
bool MCErrorCatch(MCErrorRef& r_error)
{
if (s_last_error == nil)
return false;
r_error = s_last_error;
s_last_error = nil;
return true;
}
MC_DLLEXPORT_DEF
void MCErrorReset(void)
{
MCAutoErrorRef t_error;
MCErrorCatch(&t_error);
}
MC_DLLEXPORT_DEF
MCErrorRef MCErrorPeek(void)
{
return s_last_error;
}
MC_DLLEXPORT_DEF
bool MCErrorIsPending(void)
{
return s_last_error != nil;
}
////////////////////////////////////////////////////////////////////////////////
static bool
MCErrorCreateAndThrowWithMessageV (MCTypeInfoRef p_error_type,
MCStringRef p_message,
va_list p_args)
{
MCAutoErrorRef t_error;
if (!MCErrorCreateWithMessageV(&t_error,
p_error_type,
p_message,
p_args))
{
return false;
}
return MCErrorThrow(*t_error);
}
MC_DLLEXPORT_DEF bool
MCErrorCreateAndThrowWithMessage (MCTypeInfoRef p_error_type,
MCStringRef p_message,
...)
{
va_list t_args;
va_start (t_args, p_message);
bool t_result;
t_result = MCErrorCreateAndThrowWithMessageV (p_error_type,
p_message,
t_args);
va_end (t_args);
return t_result;
}
MC_DLLEXPORT_DEF bool
MCErrorCreateAndThrow (MCTypeInfoRef p_error_type, ...)
{
va_list t_args;
va_start (t_args, p_error_type);
bool t_result;
t_result = MCErrorCreateAndThrowWithMessageV (p_error_type,
MCErrorTypeInfoGetMessage (p_error_type),
t_args);
va_end (t_args);
return t_result;
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool MCErrorThrowOutOfMemory(void)
{
if (s_out_of_memory_error == nil)
{
/* This function may be being called from within
* MCMemoryNew(). If there is no error structure already
* allocated, calling MCErrorCreate() to obtain one will call
* MCMemoryNew()... which will re-enter this function,
* probably recursively until the stack overflows. */
abort();
}
return MCErrorThrow(s_out_of_memory_error);
}
MC_DLLEXPORT_DEF
bool MCErrorThrowUnboundType(MCTypeInfoRef p_type)
{
__MCAssertIsTypeInfo(p_type);
return MCErrorCreateAndThrow(kMCUnboundTypeErrorTypeInfo, "type", MCNamedTypeInfoGetName(p_type), nil);
}
MC_DLLEXPORT_DEF
bool MCErrorThrowUnimplemented(MCStringRef p_reason)
{
__MCAssertIsString(p_reason);
return MCErrorCreateAndThrow(kMCUnimplementedErrorTypeInfo, "reason", p_reason, nil);
}
MC_DLLEXPORT_DEF
bool MCErrorThrowGeneric(MCStringRef p_reason)
{
__MCAssertIsString(p_reason);
return MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", p_reason, nil);
}
MC_DLLEXPORT_DEF
bool MCErrorThrowGenericWithMessage(MCStringRef p_message, ...)
{
va_list t_args;
va_start(t_args, p_message);
bool t_success;
t_success = MCErrorCreateAndThrowWithMessageV(kMCGenericErrorTypeInfo, p_message, t_args);
va_end(t_args);
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
void __MCErrorDestroy(__MCError *self)
{
MCValueRelease(self -> typeinfo);
MCValueRelease(self -> message);
MCValueRelease(self -> info);
while(self -> backtrace != nil)
{
MCErrorFrame *t_frame;
t_frame = self -> backtrace;
self -> backtrace = self -> backtrace -> caller;
MCValueRelease(t_frame -> target);
MCMemoryDelete(t_frame);
}
}
hash_t __MCErrorHash(__MCError *self)
{
return 0;
}
bool __MCErrorIsEqualTo(__MCError *self, __MCError *other_error)
{
return MCValueIsEqualTo(self -> typeinfo, other_error -> typeinfo);
}
bool __MCErrorCopyDescription(__MCError *self, MCStringRef& r_string)
{
return MCStringCopy (MCErrorGetMessage (self), r_string);
}
bool __MCErrorInitialize(void)
{
if (!MCNamedErrorTypeInfoCreate(MCNAME("livecode.lang.OutOfMemoryError"), MCNAME("runtime"), MCSTR("out of memory"), kMCOutOfMemoryErrorTypeInfo))
return false;
if (!MCNamedErrorTypeInfoCreate(MCNAME("livecode.lang.GenericError"), MCNAME("runtime"), MCSTR("%{reason}"), kMCGenericErrorTypeInfo))
return false;
if (!MCNamedErrorTypeInfoCreate(MCNAME("livecode.lang.UnboundTypeError"), MCNAME("runtime"), MCSTR("attempt to use unbound named type %{type}"), kMCUnboundTypeErrorTypeInfo))
return false;
if (!MCNamedErrorTypeInfoCreate(MCNAME("livecode.lang.UnimplementedError"), MCNAME("runtime"), MCSTR("%{reason}"), kMCUnimplementedErrorTypeInfo))
return false;
if (!MCErrorCreate(kMCOutOfMemoryErrorTypeInfo, nil, s_out_of_memory_error))
return false;
return true;
}
void __MCErrorFinalize(void)
{
MCValueRelease(s_last_error);
MCValueRelease(s_out_of_memory_error);
MCValueRelease(kMCOutOfMemoryErrorTypeInfo);
MCValueRelease(kMCGenericErrorTypeInfo);
MCValueRelease(kMCUnboundTypeErrorTypeInfo);
MCValueRelease(kMCUnimplementedErrorTypeInfo);
}
////////////////////////////////////////////////////////////////////////////////