2011-04-07 12:49:47 -04:00
|
|
|
|
|
|
|
#include "rr.h"
|
2011-04-08 10:54:57 -04:00
|
|
|
#include "v8_handle.h"
|
2011-04-07 12:49:47 -04:00
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2011-06-15 09:58:33 -04:00
|
|
|
/**
|
|
|
|
* Creates a new Persistent storage cell for `handle`
|
|
|
|
* so that we can reference it from Ruby.
|
|
|
|
*/
|
2011-04-12 11:50:09 -04:00
|
|
|
v8_handle::v8_handle(Handle<void> handle) : handle(Persistent<void>::New(handle)) {
|
2011-05-02 22:54:03 -04:00
|
|
|
this->weakref_callback = Qnil;
|
|
|
|
this->weakref_callback_parameters = Qnil;
|
2011-04-12 11:50:09 -04:00
|
|
|
this->dead = false;
|
|
|
|
}
|
2011-04-08 10:54:57 -04:00
|
|
|
|
2011-06-15 09:37:58 -04:00
|
|
|
v8_handle::~v8_handle() {}
|
2011-04-12 11:50:09 -04:00
|
|
|
|
2011-04-07 12:49:47 -04:00
|
|
|
namespace {
|
2011-06-15 09:58:33 -04:00
|
|
|
/**
|
|
|
|
* Holds dead references, that are no longer being held in Ruby, so that they can be garbage collected
|
|
|
|
* inside of V8
|
|
|
|
*/
|
2011-06-15 09:37:58 -04:00
|
|
|
VALUE handle_queue;
|
|
|
|
|
2011-06-15 09:58:33 -04:00
|
|
|
/**
|
|
|
|
* Invoked by the Ruby garbage collector whenever it determines that this handle is
|
|
|
|
* still reachable. We in turn, mark our weak callback parameters, so that it knows
|
|
|
|
* they are reachable too.
|
|
|
|
*/
|
2011-05-02 22:54:03 -04:00
|
|
|
void v8_handle_mark(v8_handle* handle) {
|
|
|
|
rb_gc_mark(handle->weakref_callback);
|
|
|
|
rb_gc_mark(handle->weakref_callback_parameters);
|
2011-04-12 11:50:09 -04:00
|
|
|
}
|
2011-04-08 10:54:57 -04:00
|
|
|
|
2011-06-15 09:58:33 -04:00
|
|
|
/**
|
|
|
|
* Deallocates this handle. This function is invoked on Zombie handles after they have
|
|
|
|
* been released from V8 and finally
|
|
|
|
*/
|
2011-05-02 22:54:03 -04:00
|
|
|
void v8_handle_free(v8_handle* handle) {
|
2011-04-08 10:54:57 -04:00
|
|
|
delete handle;
|
|
|
|
}
|
2011-04-07 12:49:47 -04:00
|
|
|
|
2011-06-15 09:58:33 -04:00
|
|
|
/**
|
|
|
|
* Whenver a V8::C::Handle becomes garbage collected, we do not free it immediately.
|
|
|
|
* instead, we put them into a "zombie" queue, where its corresponding V8 storage cell
|
|
|
|
* can be released safely while the V8 engine is running. A zombie Ruby object is
|
|
|
|
* created to wrap it so that it can be stored in the queue.
|
|
|
|
*/
|
2011-06-15 09:37:58 -04:00
|
|
|
void v8_handle_enqueue(v8_handle* handle) {
|
|
|
|
handle->dead = true;
|
|
|
|
VALUE zombie = Data_Wrap_Struct(rr_v8_handle_class(), 0, v8_handle_free, handle);
|
|
|
|
rb_ary_unshift(handle_queue, zombie);
|
|
|
|
}
|
|
|
|
|
2011-06-15 09:58:33 -04:00
|
|
|
/**
|
|
|
|
* Drains the dead handle queue, and releases them from V8
|
|
|
|
*
|
|
|
|
* This implements the V8 `GCPrologueCallback` and is registered to run before
|
|
|
|
* each invocation of the V8 garbage collector. It empties the queue of dead handles
|
|
|
|
* and disposes of them. It is important to do this operations inside V8 so that
|
|
|
|
* Ruby garbage collection is never locked, and never touches V8.
|
|
|
|
*/
|
2011-06-15 09:37:58 -04:00
|
|
|
void v8_handle_dequeue(GCType type, GCCallbackFlags flags) {
|
|
|
|
for (VALUE handle = rb_ary_pop(handle_queue); RTEST(handle); handle = rb_ary_pop(handle_queue)) {
|
|
|
|
v8_handle* dead = NULL;
|
|
|
|
Data_Get_Struct(handle, struct v8_handle, dead);
|
|
|
|
dead->handle.Dispose();
|
|
|
|
dead->handle.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-07 12:49:47 -04:00
|
|
|
VALUE New(VALUE self, VALUE handle) {
|
2011-04-08 10:54:57 -04:00
|
|
|
if (RTEST(handle)) {
|
2011-04-11 00:19:26 -04:00
|
|
|
Persistent<void> that = rr_v8_handle<void>(handle);
|
|
|
|
return rr_v8_handle_new(self, that);
|
2011-04-08 10:54:57 -04:00
|
|
|
} else {
|
|
|
|
return rr_v8_handle_new(self, Handle<void>());
|
|
|
|
}
|
2011-04-07 12:49:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE IsEmpty(VALUE self) {
|
2011-04-08 10:54:57 -04:00
|
|
|
return rr_v82rb(rr_v8_handle<void>(self).IsEmpty());
|
2011-04-07 12:49:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Clear(VALUE self) {
|
2011-04-08 10:54:57 -04:00
|
|
|
rr_v8_handle<void>(self).Clear();
|
2011-04-07 12:49:47 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE Dispose(VALUE self) {
|
2011-04-08 10:54:57 -04:00
|
|
|
rr_v8_handle<void>(self).Dispose();
|
2011-04-07 12:49:47 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2011-04-12 11:50:09 -04:00
|
|
|
void RubyWeakReferenceCallback(Persistent<Value> value, void* parameter) {
|
2011-04-26 12:30:17 -04:00
|
|
|
VALUE self = (VALUE)parameter;
|
|
|
|
v8_handle* handle = rr_v8_handle_raw(self);
|
2011-05-02 22:54:03 -04:00
|
|
|
VALUE callback = handle->weakref_callback;
|
|
|
|
VALUE parameters = handle->weakref_callback_parameters;
|
2011-04-26 12:30:17 -04:00
|
|
|
if (RTEST(callback)) {
|
|
|
|
rb_funcall(callback, rb_intern("call"), 2, self, parameters);
|
|
|
|
}
|
2011-04-12 11:50:09 -04:00
|
|
|
value.Dispose();
|
|
|
|
handle->handle.Dispose();
|
|
|
|
handle->handle.Clear();
|
|
|
|
handle->dead = true;
|
|
|
|
|
2011-04-26 12:30:17 -04:00
|
|
|
}
|
2011-04-07 12:49:47 -04:00
|
|
|
|
2011-04-26 12:30:17 -04:00
|
|
|
VALUE MakeWeak(VALUE self, VALUE parameters, VALUE callback) {
|
2011-05-02 22:54:03 -04:00
|
|
|
v8_handle* handle = rr_v8_handle_raw(self);
|
|
|
|
handle->weakref_callback = callback;
|
|
|
|
handle->weakref_callback_parameters = parameters;
|
2011-04-12 11:50:09 -04:00
|
|
|
rr_v8_handle<void>(self).MakeWeak((void*)self, RubyWeakReferenceCallback);
|
2011-04-07 12:49:47 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE ClearWeak(VALUE self) {
|
2011-04-08 10:54:57 -04:00
|
|
|
rr_v8_handle<void>(self).ClearWeak();
|
2011-04-07 12:49:47 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE IsNearDeath(VALUE self) {
|
2011-04-08 10:54:57 -04:00
|
|
|
return rr_v82rb(rr_v8_handle<void>(self).IsNearDeath());
|
2011-04-07 12:49:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE IsWeak(VALUE self) {
|
2011-04-08 10:54:57 -04:00
|
|
|
return rr_v82rb(rr_v8_handle<void>(self).IsWeak());
|
2011-04-07 12:49:47 -04:00
|
|
|
}
|
2011-04-13 17:20:48 -04:00
|
|
|
|
|
|
|
VALUE dead_p(VALUE self) {
|
|
|
|
return rr_v8_handle_raw(self)->dead ? Qtrue : Qfalse;
|
|
|
|
}
|
2011-04-07 12:49:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void rr_init_handle() {
|
|
|
|
VALUE HandleClass = rr_define_class("Handle");
|
2011-04-13 17:20:48 -04:00
|
|
|
rr_define_method(HandleClass, "dead?", dead_p, 0);
|
2011-04-07 12:49:47 -04:00
|
|
|
rr_define_singleton_method(HandleClass, "New", New, 1);
|
|
|
|
rr_define_method(HandleClass, "IsEmpty", IsEmpty, 0);
|
|
|
|
rr_define_method(HandleClass, "Clear", Clear, 0);
|
|
|
|
rr_define_method(HandleClass, "Dispose", Dispose, 0);
|
2011-04-26 12:30:17 -04:00
|
|
|
rr_define_method(HandleClass, "MakeWeak", MakeWeak, 2);
|
2011-04-07 12:49:47 -04:00
|
|
|
rr_define_method(HandleClass, "ClearWeak", ClearWeak, 0);
|
|
|
|
rr_define_method(HandleClass, "IsNearDeath", IsNearDeath, 0);
|
|
|
|
rr_define_method(HandleClass, "IsWeak", IsWeak, 0);
|
2011-06-15 09:37:58 -04:00
|
|
|
|
|
|
|
handle_queue = rb_ary_new();
|
|
|
|
rb_gc_register_address(&handle_queue);
|
|
|
|
V8::AddGCPrologueCallback(v8_handle_dequeue);
|
2011-04-08 10:54:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rr_v8_handle_new(VALUE klass, v8::Handle<void> handle) {
|
2011-05-02 22:54:03 -04:00
|
|
|
v8_handle* new_handle = new v8_handle(handle);
|
2011-06-15 09:37:58 -04:00
|
|
|
return Data_Wrap_Struct(klass, v8_handle_mark, v8_handle_enqueue, new_handle);
|
2011-04-07 12:49:47 -04:00
|
|
|
}
|
|
|
|
|
2011-04-11 11:02:42 -04:00
|
|
|
VALUE rr_v8_handle_class() {
|
|
|
|
return rr_define_class("Handle");
|
|
|
|
}
|
|
|
|
|
2011-04-12 11:50:09 -04:00
|
|
|
v8_handle* rr_v8_handle_raw(VALUE value) {
|
|
|
|
v8_handle* handle = 0;
|
|
|
|
Data_Get_Struct(value, struct v8_handle, handle);
|
|
|
|
return handle;
|
|
|
|
}
|