1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/ext/v8/v8_try_catch.cpp

71 lines
1.9 KiB
C++
Raw Normal View History

#include "rr.h"
#include "v8_try_catch.h"
2011-04-11 09:34:25 -05:00
#include "v8_message.h"
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"))) {
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;
}
}
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);
tc.Reset();
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) {
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;
}
}
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);
rr_define_method(TryCatchClass, "Message", _Message, 0);
2010-05-18 16:15:51 +03:00
rr_define_method(TryCatchClass, "CanContinue", CanContinue, 0);
}