Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created April 7, 2011 21:54
Show Gist options
  • Save cowboyd/908831 to your computer and use it in GitHub Desktop.
Save cowboyd/908831 to your computer and use it in GitHub Desktop.
An exploration into the different behaviors of handles.
#include "v8.h"
#include "stdio.h"
using namespace v8;
void PrintlnWeakReferenceCallback(Persistent<Value> value, void * parameters) {
printf("->invoking weak reference callback\n"); fflush(stdout);
value.Dispose();
}
int main(int argc, char **args) {
Persistent<Context> cxt(Context::New());
Persistent<Object> p;
cxt->Enter();
{
HandleScope hs;
printf("Creating a New Object\n");
p = Persistent<Object>::New(Object::New());
}
cxt->Exit();
cxt.Dispose();
while (!V8::IdleNotification()) {}
printf(" Empty? %d\n", p.IsEmpty());
printf(" NearDeath? %d\n", p.IsNearDeath());
printf(" IsWeak? %d\n", p.IsWeak());
printf("Making handle weak.\n");
p.MakeWeak(0, PrintlnWeakReferenceCallback);
while (!V8::IdleNotification()) {}
printf(" Empty? %d\n", p.IsEmpty());
printf(" NearDeath? %d\n", p.IsNearDeath());
printf(" IsWeak? %d\n", p.IsWeak());
printf("Disposing of the handle\n");
p.Dispose();
while (!V8::IdleNotification()) {}
printf(" Empty? %d\n", p.IsEmpty());
printf(" NearDeath? %d\n", p.IsNearDeath());
printf(" IsWeak? %d\n", p.IsWeak());
printf("Clearing the handle\n");
p.Clear();
while (!V8::IdleNotification()) {}
printf(" Empty? %d\n", p.IsEmpty());
printf(" NearDeath? %d\n", p.IsNearDeath());
printf(" IsWeak? %d\n", p.IsWeak());
return 0;
}
Creating a New Object
  Empty? 0
  NearDeath? 0
  IsWeak? 0
Making handle weak.
  Empty? 0
  NearDeath? 0
  IsWeak? 1
Disposing of the handle
  Empty? 0
  NearDeath? 0
  IsWeak? 0
Clearing the handle
  Empty? 1
  NearDeath? 0
  IsWeak? 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment