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

simplify TryCatch wrappers.

This commit is contained in:
Charles Lowell 2011-05-10 15:19:34 -05:00
parent 76f2f76cc4
commit 158717bc60
2 changed files with 11 additions and 38 deletions

View file

@ -7,43 +7,14 @@ using namespace v8;
namespace {
VALUE TryCatchClass;
struct try_catch {
TryCatch *tc;
bool alive;
try_catch(TryCatch *ptr);
};
try_catch::try_catch(TryCatch *ptr) :tc(ptr) {
this->alive = true;
}
struct kill_try_catch {
try_catch *tc;
kill_try_catch(try_catch *ptr);
~kill_try_catch();
};
kill_try_catch::kill_try_catch(try_catch *ptr) : tc(ptr) {}
kill_try_catch::~kill_try_catch() {
tc->alive = false;
}
void mark_try_catch(try_catch *tc) {
//nothing to mark since the TryCatch object is stack allocated.
}
void free_try_catch(try_catch *ptr) {
delete ptr;
}
TryCatch *unwrap(VALUE self) {
try_catch *ref = 0;
Data_Get_Struct(self, struct try_catch, ref);
if (ref->alive) {
return ref->tc;
} else {
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;
} else {
return tc;
}
}
@ -51,10 +22,9 @@ namespace {
if (rb_block_given_p()) {
HandleScope scope;
TryCatch tc;
try_catch *wrapper = new try_catch(&tc);
kill_try_catch kill_on_return(wrapper);
VALUE rb_ref = Data_Wrap_Struct(TryCatchClass, mark_try_catch, free_try_catch, wrapper);
VALUE result = rb_yield(rb_ref);
VALUE try_catch = Data_Wrap_Struct(TryCatchClass, 0, 0, &tc);
VALUE result = rb_yield(try_catch);
rb_iv_set(try_catch, "dead", Qtrue);
tc.Reset();
return result;
} else {

View file

@ -4,6 +4,9 @@ include V8
describe C::TryCatch do
before {@cxt = C::Context::New()}
after {@cxt.Dispose()}
it "does not allow instance creation by default" do
lambda {
C::TryCatch.new