test: fix up weakref.cc after v8 api change
This commit is contained in:
parent
d2d07421ca
commit
7684e0b554
206
test/gc/node_modules/weak/src/weakref.cc
generated
vendored
206
test/gc/node_modules/weak/src/weakref.cc
generated
vendored
@ -18,11 +18,29 @@
|
||||
#include "v8.h"
|
||||
#include "node.h"
|
||||
|
||||
using namespace v8;
|
||||
using namespace node;
|
||||
|
||||
namespace {
|
||||
|
||||
using node::FatalException;
|
||||
using v8::Array;
|
||||
using v8::Boolean;
|
||||
using v8::Exception;
|
||||
using v8::Function;
|
||||
using v8::FunctionTemplate;
|
||||
using v8::FunctionCallbackInfo;
|
||||
using v8::Handle;
|
||||
using v8::HandleScope;
|
||||
using v8::Integer;
|
||||
using v8::Isolate;
|
||||
using v8::Local;
|
||||
using v8::None;
|
||||
using v8::Object;
|
||||
using v8::ObjectTemplate;
|
||||
using v8::Persistent;
|
||||
using v8::PropertyCallbackInfo;
|
||||
using v8::Value;
|
||||
using v8::String;
|
||||
using v8::ThrowException;
|
||||
using v8::TryCatch;
|
||||
|
||||
typedef struct proxy_container {
|
||||
Persistent<Object> proxy;
|
||||
@ -46,14 +64,16 @@ Handle<Object> Unwrap(Handle<Object> proxy) {
|
||||
assert(!IsDead(proxy));
|
||||
proxy_container *cont = reinterpret_cast<proxy_container*>(
|
||||
proxy->GetAlignedPointerFromInternalField(0));
|
||||
return cont->target;
|
||||
Isolate* isolate = Isolate::GetCurrent();
|
||||
return Local<Object>::New(isolate, cont->target);
|
||||
}
|
||||
|
||||
Handle<Array> GetCallbacks(Handle<Object> proxy) {
|
||||
proxy_container *cont = reinterpret_cast<proxy_container*>(
|
||||
proxy->GetAlignedPointerFromInternalField(0));
|
||||
assert(cont != NULL);
|
||||
return cont->callbacks;
|
||||
Isolate* isolate = Isolate::GetCurrent();
|
||||
return Local<Array>::New(isolate, cont->callbacks);
|
||||
}
|
||||
|
||||
|
||||
@ -64,67 +84,65 @@ Handle<Array> GetCallbacks(Handle<Object> proxy) {
|
||||
if (!dead) obj = Unwrap(info.This()); \
|
||||
|
||||
|
||||
Handle<Value> WeakNamedPropertyGetter(Local<String> property,
|
||||
const AccessorInfo& info) {
|
||||
void WeakNamedPropertyGetter(Local<String> property,
|
||||
const PropertyCallbackInfo<Value>& info) {
|
||||
UNWRAP
|
||||
return dead ? Local<Value>() : obj->Get(property);
|
||||
if (!dead) info.GetReturnValue().Set(obj->Get(property));
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> WeakNamedPropertySetter(Local<String> property,
|
||||
Local<Value> value,
|
||||
const AccessorInfo& info) {
|
||||
void WeakNamedPropertySetter(Local<String> property,
|
||||
Local<Value> value,
|
||||
const PropertyCallbackInfo<Value>& info) {
|
||||
UNWRAP
|
||||
if (!dead) obj->Set(property, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
Handle<Integer> WeakNamedPropertyQuery(Local<String> property,
|
||||
const AccessorInfo& info) {
|
||||
return HandleScope().Close(Integer::New(None));
|
||||
void WeakNamedPropertyQuery(Local<String> property,
|
||||
const PropertyCallbackInfo<Integer>& info) {
|
||||
info.GetReturnValue().Set(None);
|
||||
}
|
||||
|
||||
|
||||
Handle<Boolean> WeakNamedPropertyDeleter(Local<String> property,
|
||||
const AccessorInfo& info) {
|
||||
void WeakNamedPropertyDeleter(Local<String> property,
|
||||
const PropertyCallbackInfo<Boolean>& info) {
|
||||
UNWRAP
|
||||
return Boolean::New(!dead && obj->Delete(property));
|
||||
info.GetReturnValue().Set(!dead && obj->Delete(property));
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> WeakIndexedPropertyGetter(uint32_t index,
|
||||
const AccessorInfo& info) {
|
||||
void WeakIndexedPropertyGetter(uint32_t index,
|
||||
const PropertyCallbackInfo<Value>& info) {
|
||||
UNWRAP
|
||||
return dead ? Local<Value>() : obj->Get(index);
|
||||
if (!dead) info.GetReturnValue().Set(obj->Get(index));
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> WeakIndexedPropertySetter(uint32_t index,
|
||||
Local<Value> value,
|
||||
const AccessorInfo& info) {
|
||||
void WeakIndexedPropertySetter(uint32_t index,
|
||||
Local<Value> value,
|
||||
const PropertyCallbackInfo<Value>& info) {
|
||||
UNWRAP
|
||||
if (!dead) obj->Set(index, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
Handle<Integer> WeakIndexedPropertyQuery(uint32_t index,
|
||||
const AccessorInfo& info) {
|
||||
return HandleScope().Close(Integer::New(None));
|
||||
void WeakIndexedPropertyQuery(uint32_t index,
|
||||
const PropertyCallbackInfo<Integer>& info) {
|
||||
info.GetReturnValue().Set(None);
|
||||
}
|
||||
|
||||
|
||||
Handle<Boolean> WeakIndexedPropertyDeleter(uint32_t index,
|
||||
const AccessorInfo& info) {
|
||||
void WeakIndexedPropertyDeleter(uint32_t index,
|
||||
const PropertyCallbackInfo<Boolean>& info) {
|
||||
UNWRAP
|
||||
return Boolean::New(!dead && obj->Delete(index));
|
||||
info.GetReturnValue().Set(!dead && obj->Delete(index));
|
||||
}
|
||||
|
||||
|
||||
Handle<Array> WeakPropertyEnumerator(const AccessorInfo& info) {
|
||||
void WeakPropertyEnumerator(const PropertyCallbackInfo<Array>& info) {
|
||||
UNWRAP
|
||||
return HandleScope().Close(dead ? Array::New(0) : obj->GetPropertyNames());
|
||||
info.GetReturnValue().Set(dead ? Array::New(0) : obj->GetPropertyNames());
|
||||
}
|
||||
|
||||
|
||||
@ -137,19 +155,20 @@ void AddCallback(Handle<Object> proxy, Handle<Function> callback) {
|
||||
void TargetCallback(Isolate* isolate, Persistent<Object>* ptarget, void* arg) {
|
||||
HandleScope scope(isolate);
|
||||
|
||||
Persistent<Object> target = *ptarget;
|
||||
assert(target.IsNearDeath());
|
||||
Local<Object> target = Local<Object>::New(isolate, *ptarget);
|
||||
assert((*ptarget).IsNearDeath());
|
||||
|
||||
proxy_container *cont = reinterpret_cast<proxy_container*>(arg);
|
||||
|
||||
// invoke any listening callbacks
|
||||
uint32_t len = cont->callbacks->Length();
|
||||
Local<Array> callbacks = Local<Array>::New(isolate, cont->callbacks);
|
||||
uint32_t len = callbacks->Length();
|
||||
Handle<Value> argv[1];
|
||||
argv[0] = target;
|
||||
for (uint32_t i=0; i<len; i++) {
|
||||
|
||||
Handle<Function> cb = Handle<Function>::Cast(
|
||||
cont->callbacks->Get(Integer::New(i)));
|
||||
callbacks->Get(Integer::New(i)));
|
||||
|
||||
TryCatch try_catch;
|
||||
|
||||
@ -160,46 +179,45 @@ void TargetCallback(Isolate* isolate, Persistent<Object>* ptarget, void* arg) {
|
||||
}
|
||||
}
|
||||
|
||||
cont->proxy->SetAlignedPointerInInternalField(0, NULL);
|
||||
cont->proxy.Dispose();
|
||||
cont->proxy.Clear();
|
||||
cont->target.Dispose();
|
||||
cont->target.Clear();
|
||||
cont->callbacks.Dispose();
|
||||
cont->callbacks.Clear();
|
||||
free(cont);
|
||||
delete cont;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Create(const Arguments& args) {
|
||||
void Create(const FunctionCallbackInfo<Value>& args) {
|
||||
HandleScope scope;
|
||||
|
||||
if (!args[0]->IsObject()) {
|
||||
Local<String> message = String::New("Object expected");
|
||||
return ThrowException(Exception::TypeError(message));
|
||||
ThrowException(Exception::TypeError(message));
|
||||
return;
|
||||
}
|
||||
|
||||
proxy_container *cont = (proxy_container *)
|
||||
malloc(sizeof(proxy_container));
|
||||
proxy_container *cont = new proxy_container;
|
||||
|
||||
cont->target = Persistent<Object>::New(Isolate::GetCurrent(),
|
||||
args[0]->ToObject());
|
||||
cont->callbacks = Persistent<Array>::New(Isolate::GetCurrent(),
|
||||
Array::New());
|
||||
Isolate* isolate = args.GetIsolate();
|
||||
Local<ObjectTemplate> tmpl = Local<ObjectTemplate>::New(isolate, proxyClass);
|
||||
Local<Object> proxy = tmpl->NewInstance();
|
||||
proxy->SetAlignedPointerInInternalField(0, cont);
|
||||
|
||||
cont->proxy = Persistent<Object>::New(Isolate::GetCurrent(),
|
||||
proxyClass->NewInstance());
|
||||
cont->proxy->SetAlignedPointerInInternalField(0, cont);
|
||||
cont->proxy.Reset(Isolate::GetCurrent(), proxy);
|
||||
cont->target.Reset(isolate, args[0].As<Object>());
|
||||
cont->callbacks.Reset(isolate, Array::New());
|
||||
|
||||
cont->target.MakeWeak(Isolate::GetCurrent(),
|
||||
static_cast<void*>(cont),
|
||||
TargetCallback);
|
||||
|
||||
if (args.Length() >= 2) {
|
||||
AddCallback(cont->proxy, Handle<Function>::Cast(args[1]));
|
||||
AddCallback(proxy, Handle<Function>::Cast(args[1]));
|
||||
}
|
||||
|
||||
return cont->proxy;
|
||||
args.GetReturnValue().Set(proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -210,102 +228,100 @@ bool isWeakRef (Handle<Value> val) {
|
||||
return val->IsObject() && val->ToObject()->InternalFieldCount() == 1;
|
||||
}
|
||||
|
||||
Handle<Value> IsWeakRef (const Arguments& args) {
|
||||
HandleScope scope;
|
||||
return Boolean::New(isWeakRef(args[0]));
|
||||
void IsWeakRef (const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(isWeakRef(args[0]));
|
||||
}
|
||||
|
||||
Handle<Value> Get(const Arguments& args) {
|
||||
void Get(const FunctionCallbackInfo<Value>& args) {
|
||||
HandleScope scope;
|
||||
|
||||
if (!isWeakRef(args[0])) {
|
||||
Local<String> message = String::New("Weakref instance expected");
|
||||
return ThrowException(Exception::TypeError(message));
|
||||
ThrowException(Exception::TypeError(message));
|
||||
return;
|
||||
}
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
|
||||
const bool dead = IsDead(proxy);
|
||||
if (dead) return Undefined();
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
if (IsDead(proxy)) return;
|
||||
|
||||
Handle<Object> obj = Unwrap(proxy);
|
||||
return scope.Close(obj);
|
||||
args.GetReturnValue().Set(obj);
|
||||
}
|
||||
|
||||
Handle<Value> IsNearDeath(const Arguments& args) {
|
||||
void IsNearDeath(const FunctionCallbackInfo<Value>& args) {
|
||||
HandleScope scope;
|
||||
|
||||
if (!isWeakRef(args[0])) {
|
||||
Local<String> message = String::New("Weakref instance expected");
|
||||
return ThrowException(Exception::TypeError(message));
|
||||
ThrowException(Exception::TypeError(message));
|
||||
return;
|
||||
}
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
|
||||
proxy_container *cont = reinterpret_cast<proxy_container*>(
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
proxy_container *cont = static_cast<proxy_container*>(
|
||||
proxy->GetAlignedPointerFromInternalField(0));
|
||||
assert(cont != NULL);
|
||||
|
||||
Handle<Boolean> rtn = Boolean::New(cont->target.IsNearDeath());
|
||||
|
||||
return scope.Close(rtn);
|
||||
args.GetReturnValue().Set(cont->target.IsNearDeath());
|
||||
}
|
||||
|
||||
Handle<Value> IsDead(const Arguments& args) {
|
||||
void IsDead(const FunctionCallbackInfo<Value>& args) {
|
||||
HandleScope scope;
|
||||
|
||||
if (!isWeakRef(args[0])) {
|
||||
Local<String> message = String::New("Weakref instance expected");
|
||||
return ThrowException(Exception::TypeError(message));
|
||||
ThrowException(Exception::TypeError(message));
|
||||
return;
|
||||
}
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
|
||||
const bool dead = IsDead(proxy);
|
||||
return Boolean::New(dead);
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
args.GetReturnValue().Set(IsDead(proxy));
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> AddCallback(const Arguments& args) {
|
||||
void AddCallback(const FunctionCallbackInfo<Value>& args) {
|
||||
HandleScope scope;
|
||||
|
||||
if (!isWeakRef(args[0])) {
|
||||
Local<String> message = String::New("Weakref instance expected");
|
||||
return ThrowException(Exception::TypeError(message));
|
||||
ThrowException(Exception::TypeError(message));
|
||||
return;
|
||||
}
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
AddCallback(proxy, Handle<Function>::Cast(args[1]));
|
||||
|
||||
return Undefined();
|
||||
}
|
||||
|
||||
Handle<Value> Callbacks(const Arguments& args) {
|
||||
void Callbacks(const FunctionCallbackInfo<Value>& args) {
|
||||
HandleScope scope;
|
||||
|
||||
if (!isWeakRef(args[0])) {
|
||||
Local<String> message = String::New("Weakref instance expected");
|
||||
return ThrowException(Exception::TypeError(message));
|
||||
ThrowException(Exception::TypeError(message));
|
||||
return;
|
||||
}
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
|
||||
return scope.Close(GetCallbacks(proxy));
|
||||
Local<Object> proxy = args[0]->ToObject();
|
||||
args.GetReturnValue().Set(GetCallbacks(proxy));
|
||||
}
|
||||
|
||||
|
||||
void Initialize(Handle<Object> target) {
|
||||
HandleScope scope;
|
||||
|
||||
proxyClass = Persistent<ObjectTemplate>::New(Isolate::GetCurrent(),
|
||||
ObjectTemplate::New());
|
||||
proxyClass->SetNamedPropertyHandler(WeakNamedPropertyGetter,
|
||||
WeakNamedPropertySetter,
|
||||
WeakNamedPropertyQuery,
|
||||
WeakNamedPropertyDeleter,
|
||||
WeakPropertyEnumerator);
|
||||
proxyClass->SetIndexedPropertyHandler(WeakIndexedPropertyGetter,
|
||||
WeakIndexedPropertySetter,
|
||||
WeakIndexedPropertyQuery,
|
||||
WeakIndexedPropertyDeleter,
|
||||
WeakPropertyEnumerator);
|
||||
proxyClass->SetInternalFieldCount(1);
|
||||
Local<ObjectTemplate> tmpl = ObjectTemplate::New();
|
||||
tmpl->SetNamedPropertyHandler(WeakNamedPropertyGetter,
|
||||
WeakNamedPropertySetter,
|
||||
WeakNamedPropertyQuery,
|
||||
WeakNamedPropertyDeleter,
|
||||
WeakPropertyEnumerator);
|
||||
tmpl->SetIndexedPropertyHandler(WeakIndexedPropertyGetter,
|
||||
WeakIndexedPropertySetter,
|
||||
WeakIndexedPropertyQuery,
|
||||
WeakIndexedPropertyDeleter,
|
||||
WeakPropertyEnumerator);
|
||||
tmpl->SetInternalFieldCount(1);
|
||||
proxyClass.Reset(Isolate::GetCurrent(), tmpl);
|
||||
|
||||
NODE_SET_METHOD(target, "get", Get);
|
||||
NODE_SET_METHOD(target, "create", Create);
|
||||
|
Loading…
x
Reference in New Issue
Block a user