Skip to content

Commit

Permalink
Moved C++ exceptions classes to their own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford committed Aug 24, 2008
1 parent ce4957a commit 53f9ee1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 73 deletions.
2 changes: 1 addition & 1 deletion vm/object.cpp → vm/exception.cpp
@@ -1,5 +1,5 @@
#include "prelude.hpp"
#include "object.hpp"
#include "exception.hpp"
#include <vector>
#include <iostream>
#include <execinfo.h>
Expand Down
82 changes: 82 additions & 0 deletions vm/exception.hpp
@@ -0,0 +1,82 @@
#ifndef RBX_EXCEPTION_HPP
#define RBX_EXCEPTION_HPP

#include "prelude.hpp"
#include "builtin/object.hpp"

namespace rubinius {

void abort();
void print_backtrace();

class VMException {
public:
typedef std::vector<std::string> Backtrace;

Backtrace* backtrace;
VMException();
void print_backtrace();
};

class Assertion : public VMException {
public:
char *reason;

// Used instead of throwing an Assertion directly so that
// you can set a breakpoint on raise.
static void raise(const char* mesg);

Assertion(const char* reason) {
this->reason = strdup(reason);
}

~Assertion() {
free(reason);
}
};

class TypeError : public VMException {
public:
object_type type;
OBJECT object;
const char* reason;

// Used instead of throw'ning a TypeError directly,
// so that breakpoints can be set on this function and easily be
// debugged.
static void raise(object_type type, OBJECT obj, const char* reason = NULL);

TypeError(object_type type, OBJECT obj, const char* reason = NULL)
: type(type), object(obj), reason(reason) { };
};

class ObjectBoundsExceeded : public VMException {
public:
OBJECT obj;
size_t index;

static void raise(OBJECT o, size_t i);

ObjectBoundsExceeded(OBJECT o, size_t i) : obj(o), index(i) { }
};

class ZeroDivisionError : public VMException {
public:
INTEGER i;
const char* reason;

ZeroDivisionError(INTEGER i, const char* reason = NULL)
: i(i), reason(reason) { };
};

class FloatDomainError : public VMException {
public:
double d;
const char* reason;

FloatDomainError(double d, const char* reason = NULL)
: d(d), reason(reason) { };
};
};

#endif
73 changes: 1 addition & 72 deletions vm/object.hpp
Expand Up @@ -10,84 +10,13 @@

#include "prelude.hpp"
#include "builtin/object.hpp"
#include "exception.hpp"

/* Defines all the most common operations on for dealing with
* objects, such as type checking and casting. */

namespace rubinius {

void abort();
void print_backtrace();

class VMException {
public:
typedef std::vector<std::string> Backtrace;

Backtrace* backtrace;
VMException();
void print_backtrace();
};

class Assertion : public VMException {
public:
char *reason;

// Used instead of throwing an Assertion directly so that
// you can set a breakpoint on raise.
static void raise(const char* mesg);

Assertion(const char* reason) {
this->reason = strdup(reason);
}

~Assertion() {
free(reason);
}
};

class TypeError : public VMException {
public:
object_type type;
OBJECT object;
const char* reason;

// Used instead of throw'ning a TypeError directly,
// so that breakpoints can be set on this function and easily be
// debugged.
static void raise(object_type type, OBJECT obj, const char* reason = NULL);

TypeError(object_type type, OBJECT obj, const char* reason = NULL)
: type(type), object(obj), reason(reason) { };
};

class ObjectBoundsExceeded : public VMException {
public:
OBJECT obj;
size_t index;

static void raise(OBJECT o, size_t i);

ObjectBoundsExceeded(OBJECT o, size_t i) : obj(o), index(i) { }
};

class ZeroDivisionError : public VMException {
public:
INTEGER i;
const char* reason;

ZeroDivisionError(INTEGER i, const char* reason = NULL)
: i(i), reason(reason) { };
};

class FloatDomainError : public VMException {
public:
double d;
const char* reason;

FloatDomainError(double d, const char* reason = NULL)
: d(d), reason(reason) { };
};

void inspect(STATE, OBJECT);
void inspect(STATE, SYMBOL);

Expand Down

0 comments on commit 53f9ee1

Please sign in to comment.