1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/ext/v8/external.cc
Charles Lowell 356b47a86f massively simplify reference creation.
make the template class define the RubyClass for
a given reference class. That way, it can define
the wrapping logic.
2012-05-15 11:46:14 -05:00

43 lines
No EOL
1,014 B
C++

#include "rr.h"
namespace rr {
void External::Init() {
ClassBuilder("External", "Value").
defineSingletonMethod("New", &New).
defineMethod("Value", &Value).
store(&Class);
}
VALUE External::New(VALUE self, VALUE data) {
return External::create(wrap(data));
}
v8::Handle<v8::External> External::wrap(VALUE data) {
Data* holder = new Data(data);
v8::Local<v8::External> ext = v8::External::New(holder);
v8::Persistent<v8::External>::New(ext).MakeWeak(holder, &release);
return ext;
}
VALUE External::unwrap(v8::Handle<v8::External> external) {
Data* data = (Data*)(external->Value());
return data->value;
}
VALUE External::Value(VALUE self) {
return unwrap(External(self));
}
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);
}
}