PageRenderTime 43ms CodeModel.GetById 18ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vm/tinyrb/error.c

http://github.com/feyeleanor/RubyGoLightly
C | 100 lines | 58 code | 13 blank | 29 comment | 5 complexity | 27d2599a14ccdfef732eb9119b1f5e43 MD5 | raw file
  1#include "tr.h"
  2
  3/* Error management stuff */
  4
  5/* Exception
  6 NoMemoryError
  7 ScriptError
  8   LoadError
  9   NotImplementedError
 10   SyntaxError
 11 SignalException
 12   Interrupt
 13 StandardError
 14   ArgumentError
 15   IOError
 16     EOFError
 17   IndexError
 18   LocalJumpError
 19   NameError
 20     NoMethodError
 21   RangeError
 22     FloatDomainError
 23   RegexpError
 24   RuntimeError
 25   SecurityError
 26   SystemCallError
 27   SystemStackError
 28   ThreadError
 29   TypeError
 30   ZeroDivisionError
 31 SystemExit
 32 fatal */
 33
 34OBJ TrException_new(VM, OBJ class, OBJ message) {
 35  OBJ e = TrObject_alloc(vm, class);
 36  tr_setivar(e, "@message", message);
 37  tr_setivar(e, "@backtrace", TR_NIL);
 38  return (OBJ)e;
 39}
 40
 41static OBJ TrException_cexception(VM, OBJ self, int argc, OBJ argv[]) {
 42  if (argc == 0) return TrException_new(vm, self, TR_CCLASS(self)->name);
 43  return TrException_new(vm, self, argv[0]);
 44}
 45
 46static OBJ TrException_iexception(VM, OBJ self, int argc, OBJ argv[]) {
 47  if (argc == 0) return self;
 48  return TrException_new(vm, TR_CLASS(self), argv[0]);
 49}
 50
 51static OBJ TrException_message(VM, OBJ self) {
 52  return tr_getivar(self, "@message");
 53}
 54
 55OBJ TrException_backtrace(VM, OBJ self) {
 56  return tr_getivar(self, "@backtrace");
 57}
 58
 59OBJ TrException_set_backtrace(VM, OBJ self, OBJ backtrace) {
 60  return tr_setivar(self, "@backtrace", backtrace);
 61}
 62
 63OBJ TrException_default_handler(VM, OBJ exception) {
 64  TrClass *c = TR_CCLASS(TR_CLASS(exception));
 65  OBJ msg = tr_getivar(exception, "@message");
 66  OBJ backtrace = tr_getivar(exception, "@backtrace");
 67  
 68  printf("%s: %s\n", TR_STR_PTR(c->name), TR_STR_PTR(msg));
 69  if (backtrace) {
 70    TR_ARRAY_EACH(backtrace, i, v, {
 71      printf("%s\n", TR_STR_PTR(v));
 72    });
 73  }
 74  
 75  TrVM_destroy(vm);
 76  exit(1);
 77}
 78
 79void TrError_init(VM) {
 80  OBJ c = vm->cException = tr_defclass("Exception", 0);
 81  tr_metadef(c, "exception", TrException_cexception, -1);
 82  tr_def(c, "exception", TrException_iexception, -1);
 83  tr_def(c, "backtrace", TrException_backtrace, 0);
 84  tr_def(c, "message", TrException_message, 0);
 85  tr_def(c, "to_s", TrException_message, 0);
 86  
 87  vm->cScriptError = tr_defclass("ScriptError", vm->cException);
 88  vm->cSyntaxError = tr_defclass("SyntaxError", vm->cScriptError);
 89  vm->cStandardError = tr_defclass("StandardError", vm->cException);
 90  vm->cArgumentError = tr_defclass("ArgumentError", vm->cStandardError);
 91  vm->cRegexpError = tr_defclass("RegexpError", vm->cStandardError);
 92  vm->cRuntimeError = tr_defclass("RuntimeError", vm->cStandardError);
 93  vm->cTypeError = tr_defclass("TypeError", vm->cStandardError);
 94  vm->cSystemCallError = tr_defclass("SystemCallError", vm->cStandardError);
 95  vm->cIndexError = tr_defclass("IndexError", vm->cStandardError);
 96  vm->cLocalJumpError = tr_defclass("LocalJumpError", vm->cStandardError);
 97  vm->cSystemStackError = tr_defclass("SystemStackError", vm->cStandardError);
 98  vm->cNameError = tr_defclass("NameError", vm->cStandardError);
 99  vm->cNoMethodError = tr_defclass("NoMethodError", vm->cNameError);
100}