2012-06-08 14:13:33 -05:00
|
|
|
#include "rr.h"
|
|
|
|
|
|
|
|
namespace rr {
|
|
|
|
|
2012-06-20 03:17:34 -05:00
|
|
|
VALUE Backref::Storage;
|
2012-06-08 14:13:33 -05:00
|
|
|
ID Backref::_new;
|
2012-06-20 04:53:47 -05:00
|
|
|
ID Backref::object;
|
2012-06-08 14:13:33 -05:00
|
|
|
|
|
|
|
void Backref::Init() {
|
2012-08-11 12:26:14 -05:00
|
|
|
Storage = rb_eval_string("V8::Weak::Ref");
|
2012-06-20 03:17:34 -05:00
|
|
|
rb_gc_register_address(&Storage);
|
2012-06-08 14:13:33 -05:00
|
|
|
_new = rb_intern("new");
|
2012-06-20 04:53:47 -05:00
|
|
|
object = rb_intern("object");
|
2012-06-08 14:13:33 -05:00
|
|
|
}
|
|
|
|
|
2012-06-20 04:53:47 -05:00
|
|
|
Backref::Backref(VALUE initial) {
|
2012-09-19 15:38:59 +03:00
|
|
|
set(initial);
|
|
|
|
rb_gc_register_address(&storage);
|
2012-06-08 14:13:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Backref::~Backref() {
|
2012-09-19 15:38:59 +03:00
|
|
|
rb_gc_unregister_address(&storage);
|
2012-06-20 04:53:47 -05:00
|
|
|
}
|
|
|
|
|
2012-09-19 15:38:59 +03:00
|
|
|
VALUE Backref::set(VALUE data) {
|
2012-06-20 04:53:47 -05:00
|
|
|
this->storage = rb_funcall(Storage, _new, 1, data);
|
2012-09-19 15:38:59 +03:00
|
|
|
return data;
|
2012-06-08 14:13:33 -05:00
|
|
|
}
|
|
|
|
|
2012-06-20 04:53:47 -05:00
|
|
|
VALUE Backref::get() {
|
|
|
|
return rb_funcall(storage, object, 0);
|
|
|
|
}
|
|
|
|
|
2012-06-20 03:17:34 -05:00
|
|
|
v8::Handle<v8::Value> Backref::toExternal() {
|
2013-01-08 01:45:14 -06:00
|
|
|
v8::Local<v8::Value> wrapper = v8::External::New(this);
|
2012-06-08 14:13:33 -05:00
|
|
|
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;
|
|
|
|
}
|
2013-01-08 01:45:14 -06:00
|
|
|
}
|