1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

more documentation on external

This commit is contained in:
Charles Lowell 2015-07-06 01:19:59 -05:00
parent ca2bf2a91f
commit dfb0a3c08f
2 changed files with 16 additions and 2 deletions

View file

@ -13,17 +13,23 @@ namespace rr {
VALUE External::New(VALUE self, VALUE r_isolate, VALUE object) {
Isolate isolate(r_isolate);
// as long as this external is alive within JavaScript, it should not be
// garbage collected by Ruby.
isolate.retainObject(object);
Locker lock(isolate);
// create the external.
Container* container = new Container(object);
v8::Local<v8::External> external(v8::External::New(isolate, (void*)container));
// next, we create a weak reference to this external so that we can be
// notified when V8 is done with it. At that point, we can let Ruby know
// that this external is done with it.
v8::Global<v8::External>* global(new v8::Global<v8::External>(isolate, external));
container->global = global;
global->SetWeak<Container>(container, &release, v8::WeakCallbackType::kParameter);
container->global = global;
return External(isolate, external);
}

View file

@ -21,6 +21,14 @@ namespace rr {
VALUE object;
};
/**
* Implements a v8::WeakCallbackInfo<Container>::Callback with all
* of its idiosyncracies. It happens in two passes. In the first
* pass, you are required to only reset the weak reference. In the
* second pass, you can actually do your cleanup. In this case, we
* schedule the referenced Ruby object to be released in the next
* Ruby gc pass.
*/
static void release(const v8::WeakCallbackInfo<Container>& info) {
Container* container(info.GetParameter());
if (info.IsFirstPass()) {