2010-05-18 13:27:23 +03:00
|
|
|
#include "rr.h"
|
|
|
|
#include "v8_try_catch.h"
|
2011-04-11 09:34:25 -05:00
|
|
|
#include "v8_message.h"
|
2010-05-18 13:27:23 +03:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
VALUE TryCatchClass;
|
|
|
|
|
|
|
|
TryCatch *unwrap(VALUE self) {
|
2011-05-10 15:19:34 -05:00
|
|
|
TryCatch *tc = 0;
|
|
|
|
Data_Get_Struct(self, class TryCatch, tc);
|
|
|
|
if (RTEST(rb_iv_get(self, "dead"))) {
|
2010-05-18 13:27:23 +03:00
|
|
|
rb_raise(rb_eScriptError, "out of scope access of %s", RSTRING_PTR(rb_inspect(self)));
|
|
|
|
return false;
|
2011-05-10 15:19:34 -05:00
|
|
|
} else {
|
|
|
|
return tc;
|
2010-05-18 13:27:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Try(int argc, VALUE *argv, VALUE self) {
|
|
|
|
if (rb_block_given_p()) {
|
|
|
|
HandleScope scope;
|
|
|
|
TryCatch tc;
|
2011-05-10 15:19:34 -05:00
|
|
|
VALUE try_catch = Data_Wrap_Struct(TryCatchClass, 0, 0, &tc);
|
2011-06-02 10:59:05 -05:00
|
|
|
rb_iv_set(try_catch, "dead", Qfalse);
|
2011-05-10 15:19:34 -05:00
|
|
|
VALUE result = rb_yield(try_catch);
|
|
|
|
rb_iv_set(try_catch, "dead", Qtrue);
|
2010-05-31 10:07:16 +03:00
|
|
|
tc.Reset();
|
2010-05-18 13:27:23 +03:00
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE HasCaught(VALUE self) {
|
|
|
|
TryCatch *tc = unwrap(self);
|
|
|
|
return tc ? rr_v82rb(tc->HasCaught()) : Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE _Exception(VALUE self) {
|
|
|
|
TryCatch *tc = unwrap(self);
|
|
|
|
return tc ? rr_v82rb(tc->Exception()) : Qnil;
|
|
|
|
}
|
|
|
|
|
2010-08-01 10:16:39 -05:00
|
|
|
VALUE _StackTrace(VALUE self) {
|
2010-05-18 13:27:23 +03:00
|
|
|
TryCatch *tc = unwrap(self);
|
|
|
|
return tc ? rr_v82rb(tc->StackTrace()) : Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE _Message(VALUE self) {
|
|
|
|
TryCatch *tc = unwrap(self);
|
|
|
|
return tc ? rr_v82rb(tc->Message()) : Qnil;
|
|
|
|
}
|
2010-05-18 16:15:51 +03:00
|
|
|
|
|
|
|
VALUE CanContinue(VALUE self) {
|
|
|
|
TryCatch *tc = unwrap(self);
|
|
|
|
return tc ? rr_v82rb(tc->CanContinue()) : Qnil;
|
|
|
|
}
|
2010-05-18 13:27:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void rr_init_v8_try_catch() {
|
|
|
|
TryCatchClass = rr_define_class("TryCatch");
|
|
|
|
rr_define_singleton_method(TryCatchClass, "try", Try, -1);
|
|
|
|
rr_define_method(TryCatchClass, "HasCaught", HasCaught, 0);
|
|
|
|
rr_define_method(TryCatchClass, "Exception", _Exception, 0);
|
2010-08-01 10:16:39 -05:00
|
|
|
rr_define_method(TryCatchClass, "StackTrace", _StackTrace, 0);
|
2010-05-18 13:27:23 +03:00
|
|
|
rr_define_method(TryCatchClass, "Message", _Message, 0);
|
2010-05-18 16:15:51 +03:00
|
|
|
rr_define_method(TryCatchClass, "CanContinue", CanContinue, 0);
|
2010-05-18 13:27:23 +03:00
|
|
|
}
|