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:
parent
ca2bf2a91f
commit
dfb0a3c08f
2 changed files with 16 additions and 2 deletions
|
@ -13,17 +13,23 @@ namespace rr {
|
||||||
|
|
||||||
VALUE External::New(VALUE self, VALUE r_isolate, VALUE object) {
|
VALUE External::New(VALUE self, VALUE r_isolate, VALUE object) {
|
||||||
Isolate isolate(r_isolate);
|
Isolate isolate(r_isolate);
|
||||||
|
|
||||||
|
// as long as this external is alive within JavaScript, it should not be
|
||||||
|
// garbage collected by Ruby.
|
||||||
isolate.retainObject(object);
|
isolate.retainObject(object);
|
||||||
|
|
||||||
Locker lock(isolate);
|
Locker lock(isolate);
|
||||||
|
|
||||||
|
// create the external.
|
||||||
Container* container = new Container(object);
|
Container* container = new Container(object);
|
||||||
v8::Local<v8::External> external(v8::External::New(isolate, (void*)container));
|
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));
|
v8::Global<v8::External>* global(new v8::Global<v8::External>(isolate, external));
|
||||||
container->global = global;
|
|
||||||
|
|
||||||
global->SetWeak<Container>(container, &release, v8::WeakCallbackType::kParameter);
|
global->SetWeak<Container>(container, &release, v8::WeakCallbackType::kParameter);
|
||||||
|
container->global = global;
|
||||||
|
|
||||||
return External(isolate, external);
|
return External(isolate, external);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,14 @@ namespace rr {
|
||||||
VALUE object;
|
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) {
|
static void release(const v8::WeakCallbackInfo<Container>& info) {
|
||||||
Container* container(info.GetParameter());
|
Container* container(info.GetParameter());
|
||||||
if (info.IsFirstPass()) {
|
if (info.IsFirstPass()) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue