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 04:17:34 -04:00
|
|
|
ID Backref::access;
|
2012-06-08 15:13:33 -04:00
|
|
|
|
|
|
|
void Backref::Init() {
|
2012-06-20 04:17:34 -04:00
|
|
|
Storage = rb_eval_string("require 'v8/util/weakcell'; V8::Util::Weakcell::Storage");
|
|
|
|
rb_gc_register_address(&Storage);
|
2012-06-08 15:13:33 -04:00
|
|
|
_new = rb_intern("new");
|
2012-06-20 04:17:34 -04:00
|
|
|
access = rb_intern("access");
|
2012-06-08 15:13:33 -04:00
|
|
|
}
|
|
|
|
|
2012-06-20 04:17:34 -04:00
|
|
|
Backref::Backref() {
|
|
|
|
this->storage = rb_funcall(Storage, _new, 0);
|
|
|
|
rb_gc_register_address(&storage);
|
2012-06-08 15:13:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Backref::~Backref() {
|
2012-06-20 04:17:34 -04:00
|
|
|
rb_gc_unregister_address(&storage);
|
2012-06-08 15:13:33 -04:00
|
|
|
}
|
|
|
|
|
2012-06-20 04:17:34 -04:00
|
|
|
VALUE Backref::get(VALUE (*populate)(ANYARGS), VALUE data) {
|
|
|
|
VALUE args[0];
|
|
|
|
return rb_block_call(storage, access, 0, args, populate, 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;
|
|
|
|
}
|
|
|
|
}
|