2012-05-08 16:11:50 -04:00
|
|
|
#include "rr.h"
|
|
|
|
|
|
|
|
namespace rr {
|
|
|
|
|
|
|
|
void External::Init() {
|
|
|
|
ClassBuilder("External", "Value").
|
|
|
|
defineSingletonMethod("New", &New).
|
2012-05-09 14:12:44 -04:00
|
|
|
defineMethod("Value", &Value).
|
|
|
|
store(&Class);
|
2012-05-08 16:11:50 -04:00
|
|
|
}
|
|
|
|
VALUE External::New(VALUE self, VALUE data) {
|
2012-05-15 13:23:06 -04:00
|
|
|
return External(wrap(data));
|
2012-05-09 14:12:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<v8::External> External::wrap(VALUE data) {
|
2012-05-08 16:11:50 -04:00
|
|
|
Data* holder = new Data(data);
|
|
|
|
v8::Local<v8::External> ext = v8::External::New(holder);
|
|
|
|
v8::Persistent<v8::External>::New(ext).MakeWeak(holder, &release);
|
2012-05-09 14:12:44 -04:00
|
|
|
return ext;
|
2012-05-08 16:11:50 -04:00
|
|
|
}
|
2012-05-09 14:12:44 -04:00
|
|
|
|
|
|
|
VALUE External::unwrap(v8::Handle<v8::External> external) {
|
|
|
|
Data* data = (Data*)(external->Value());
|
2012-05-08 16:11:50 -04:00
|
|
|
return data->value;
|
|
|
|
}
|
2012-05-09 14:12:44 -04:00
|
|
|
|
|
|
|
VALUE External::Value(VALUE self) {
|
|
|
|
return unwrap(External(self));
|
|
|
|
}
|
|
|
|
|
2012-05-08 16:11:50 -04:00
|
|
|
void External::release(v8::Persistent<v8::Value> handle, void* parameter) {
|
|
|
|
handle.Dispose();
|
|
|
|
Data* data = (Data*)parameter;
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
External::Data::Data(VALUE data) {
|
|
|
|
this->value = data;
|
|
|
|
rb_gc_register_address(&value);
|
|
|
|
}
|
|
|
|
External::Data::~Data() {
|
|
|
|
rb_gc_unregister_address(&value);
|
|
|
|
}
|
|
|
|
}
|