diff --git a/v8_cxt.cpp b/v8_cxt.cpp index ad67cac..20597bb 100644 --- a/v8_cxt.cpp +++ b/v8_cxt.cpp @@ -5,12 +5,8 @@ using namespace v8; -v8_cxt::v8_cxt() { - // v8_cxt::v8_cxt() { - handle = Context::New(); - printf("Allocate Native V8 Context\n"); - } - +v8_cxt::v8_cxt() : v8_ref(Context::New()) { + printf("Allocate Native V8 Context\n"); } VALUE v8_cxt_allocate(VALUE clazz) { diff --git a/v8_ref.cpp b/v8_ref.cpp index 77f1745..048106f 100644 --- a/v8_ref.cpp +++ b/v8_ref.cpp @@ -2,13 +2,16 @@ #include "stdio.h" using namespace v8; + template v8_ref::v8_ref(Handle object) : handle(*object) { - printf("Allocated v8 reference\n"); + } template v8_ref::~v8_ref() { printf("Disposing of v8 reference\n"); - handle.Dispose(); + if (!handle.IsEmpty()) { + handle.Dispose(); + } } void v8_ref_mark(v8_ref_data* ref) { diff --git a/v8_ref.h b/v8_ref.h index 895889d..ce4b25e 100644 --- a/v8_ref.h +++ b/v8_ref.h @@ -3,12 +3,20 @@ #include +//this is a non-template superclass so that the mark() and free() functions +//don't need to be templatized. Not sure if this is valid... are +// class destructors virtual? + class v8_ref_data { }; +//the v8_ref wraps a v8 handle so that ruby can hold a reference to it. + template class v8_ref : v8_ref_data { public: + //takes a handle object and adds a new persistent handle for + //the referenced object. v8_ref(v8::Handle object); ~v8_ref(); private: