2012-06-08 15:13:33 -04:00
|
|
|
#include "rr.h"
|
|
|
|
|
|
|
|
namespace rr {
|
|
|
|
|
2012-06-20 04:17:34 -04:00
|
|
|
VALUE Backref::Storage;
|
2012-06-08 15:13:33 -04:00
|
|
|
ID Backref::_new;
|
2012-06-20 05:53:47 -04:00
|
|
|
ID Backref::object;
|
2012-06-08 15:13:33 -04:00
|
|
|
|
|
|
|
void Backref::Init() {
|
2012-06-20 05:53:47 -04:00
|
|
|
Storage = rb_eval_string("Ref::WeakReference");
|
2012-06-20 04:17:34 -04:00
|
|
|
rb_gc_register_address(&Storage);
|
2012-06-08 15:13:33 -04:00
|
|
|
_new = rb_intern("new");
|
2012-06-20 05:53:47 -04:00
|
|
|
object = rb_intern("object");
|
2012-06-08 15:13:33 -04:00
|
|
|
}
|
|
|
|
|
2012-06-20 05:53:47 -04:00
|
|
|
Backref::Backref(VALUE initial) {
|
|
|
|
allocate(initial);
|
2012-06-08 15:13:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Backref::~Backref() {
|
2012-06-20 05:53:47 -04:00
|
|
|
deallocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Backref::allocate(VALUE data) {
|
|
|
|
this->storage = rb_funcall(Storage, _new, 1, data);
|
|
|
|
rb_gc_register_address(&storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Backref::deallocate() {
|
2012-06-20 04:17:34 -04:00
|
|
|
rb_gc_unregister_address(&storage);
|
2012-06-08 15:13:33 -04:00
|
|
|
}
|
|
|
|
|
2012-06-20 05:53:47 -04:00
|
|
|
VALUE Backref::get() {
|
|
|
|
return rb_funcall(storage, object, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Backref::set(VALUE data) {
|
|
|
|
deallocate();
|
|
|
|
allocate(data);
|
|
|
|
return data;
|
2012-06-08 15:13:33 -04:00
|
|
|
}
|
|
|
|
|
2012-06-20 04:17:34 -04:00
|
|
|
v8::Handle<v8::Value> Backref::toExternal() {
|
2012-06-08 15:13:33 -04:00
|
|
|
v8::Local<v8::Value> wrapper = v8::External::Wrap(this);
|
|
|
|
v8::Persistent<v8::Value>::New(wrapper).MakeWeak(this, &release);
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Backref::release(v8::Persistent<v8::Value> handle, void* data) {
|
|
|
|
handle.Dispose();
|
|
|
|
Backref* backref = (Backref*)data;
|
|
|
|
delete backref;
|
|
|
|
}
|
|
|
|
}
|