Sha256: 59301f0ee1b6ef3eb25774b6bcdd4750a5c224e34e5b009f3accca62d1298d44

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

// -*- c++ -*-
#include "rucy/exception.h"


#include <ruby.h>
#include <rucy/rucy.h>
#include <rucy/string.h>


#define VA_STRING(format, result) \
	String result; \
	do \
	{ \
		if (format) \
		{ \
			va_list args; \
			va_start(args, format); \
			result = stringf(format, args); \
			va_end(args); \
		} \
	} \
	while (false)


namespace Rucy
{


	Class
	native_error_class ()
	{
		static Class c =
			rucy_module().define_class("NativeError", rb_eRuntimeError);
		return c;
	}


	void
	error (const char* format, ...)
	{
		VA_STRING(format, message);
		throw message;
	}

	void
	raise (Value type, const char* format, ...)
	{
		VA_STRING(format, message);
		throw RubyException(type, message.c_str());
	}


	void
	type_error (const char* format, ...)
	{
		VA_STRING(format, message);
		raise(rb_eTypeError, message.c_str());
	}

	void
	argument_error (const char* format, ...)
	{
		VA_STRING(format, message);
		raise(rb_eArgError, message.c_str());
	}

	void
	argument_error (
		const char* method, int nargs, int nargs_expected,
		int n1, int n2, int n3, int n4, int n5)
	{
		String message = stringf(
			"wrong number of arguments for %s: %d for %d",
			method, nargs, nargs_expected);

		int n[5] = {n1, n2, n3, n4, n5};
		for (int i = 0; i < 5 && n[i] >= 0; ++i)
			message += stringf(" or %d", n[i]);

		message += ".";
		argument_error(message.c_str());
	}

	void
	not_implemented_error (const char* format, ...)
	{
		VA_STRING(format, message);
		raise(rb_eNotImpError, message.c_str());
	}


	RubyException::RubyException (Value exception)
	:	Super(""), val(exception)
	{
	}

	RubyException::RubyException (Value type, const char* format, ...)
	:	Super(""), val(Qnil)
	{
		VA_STRING(format, message);
		val = rb_exc_new2(type, message.c_str());
	}

	const char*
	RubyException::what () const throw()
	{
		SYM(message);
		return value().call(message).c_str();
	}

	Value
	RubyException::value () const
	{
		return val;
	}


	RubyJumptag::RubyJumptag (int tag)
	:	tag(tag)
	{
	}


}// Rucy

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rucy-0.1.0 src/exception.cpp