|
|
@ -39,7 +39,6 @@ using v8::Persistent; |
|
|
|
using v8::PropertyCallbackInfo; |
|
|
|
using v8::Value; |
|
|
|
using v8::String; |
|
|
|
using v8::ThrowException; |
|
|
|
using v8::TryCatch; |
|
|
|
|
|
|
|
typedef struct proxy_container { |
|
|
@ -84,6 +83,13 @@ Handle<Array> GetCallbacks(Handle<Object> proxy) { |
|
|
|
if (!dead) obj = Unwrap(info.This()); \ |
|
|
|
|
|
|
|
|
|
|
|
void ThrowTypeError(Isolate* isolate, const char* message) { |
|
|
|
HandleScope scope(isolate); |
|
|
|
Local<String> emessage = String::NewFromUtf8(isolate, message); |
|
|
|
isolate->ThrowException(v8::Exception::TypeError(emessage)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void WeakNamedPropertyGetter(Local<String> property, |
|
|
|
const PropertyCallbackInfo<Value>& info) { |
|
|
|
UNWRAP |
|
|
@ -146,19 +152,18 @@ void WeakPropertyEnumerator(const PropertyCallbackInfo<Array>& info) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void AddCallback(Handle<Object> proxy, Handle<Function> callback) { |
|
|
|
void AddCallback(Isolate* isolate, Handle<Object> proxy, Handle<Function> callback) { |
|
|
|
Handle<Array> callbacks = GetCallbacks(proxy); |
|
|
|
callbacks->Set(Integer::New(callbacks->Length()), callback); |
|
|
|
callbacks->Set(Integer::New(isolate, callbacks->Length()), callback); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void TargetCallback(Isolate* isolate, |
|
|
|
Persistent<Object>* ptarget, |
|
|
|
proxy_container* cont) { |
|
|
|
static void TargetCallback(const v8::WeakCallbackData<v8::Object, proxy_container>& data) { |
|
|
|
Isolate* isolate = data.GetIsolate(); |
|
|
|
HandleScope scope(isolate); |
|
|
|
|
|
|
|
Local<Object> target = Local<Object>::New(isolate, *ptarget); |
|
|
|
assert((*ptarget).IsNearDeath()); |
|
|
|
Local<Object> target = data.GetValue(); |
|
|
|
proxy_container* cont = data.GetParameter(); |
|
|
|
|
|
|
|
// invoke any listening callbacks
|
|
|
|
Local<Array> callbacks = Local<Array>::New(isolate, cont->callbacks); |
|
|
@ -168,7 +173,7 @@ void TargetCallback(Isolate* isolate, |
|
|
|
for (uint32_t i=0; i<len; i++) { |
|
|
|
|
|
|
|
Handle<Function> cb = Handle<Function>::Cast( |
|
|
|
callbacks->Get(Integer::New(i))); |
|
|
|
callbacks->Get(Integer::New(isolate, i))); |
|
|
|
|
|
|
|
TryCatch try_catch; |
|
|
|
|
|
|
@ -179,12 +184,9 @@ void TargetCallback(Isolate* isolate, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
cont->proxy.Dispose(); |
|
|
|
cont->proxy.Clear(); |
|
|
|
cont->target.Dispose(); |
|
|
|
cont->target.Clear(); |
|
|
|
cont->callbacks.Dispose(); |
|
|
|
cont->callbacks.Clear(); |
|
|
|
cont->proxy.Reset(); |
|
|
|
cont->target.Reset(); |
|
|
|
cont->callbacks.Reset(); |
|
|
|
delete cont; |
|
|
|
} |
|
|
|
|
|
|
@ -193,8 +195,7 @@ void Create(const FunctionCallbackInfo<Value>& args) { |
|
|
|
HandleScope scope(args.GetIsolate()); |
|
|
|
|
|
|
|
if (!args[0]->IsObject()) { |
|
|
|
Local<String> message = String::New("Object expected"); |
|
|
|
ThrowException(Exception::TypeError(message)); |
|
|
|
ThrowTypeError(args.GetIsolate(), "Object expected"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@ -207,11 +208,11 @@ void Create(const FunctionCallbackInfo<Value>& args) { |
|
|
|
|
|
|
|
cont->proxy.Reset(Isolate::GetCurrent(), proxy); |
|
|
|
cont->target.Reset(isolate, args[0].As<Object>()); |
|
|
|
cont->callbacks.Reset(isolate, Array::New()); |
|
|
|
cont->target.MakeWeak(cont, TargetCallback); |
|
|
|
cont->callbacks.Reset(isolate, Array::New(args.GetIsolate())); |
|
|
|
cont->target.SetWeak(cont, TargetCallback); |
|
|
|
|
|
|
|
if (args.Length() >= 2) { |
|
|
|
AddCallback(proxy, Handle<Function>::Cast(args[1])); |
|
|
|
AddCallback(args.GetIsolate(), proxy, Handle<Function>::Cast(args[1])); |
|
|
|
} |
|
|
|
|
|
|
|
args.GetReturnValue().Set(proxy); |
|
|
@ -233,8 +234,7 @@ void Get(const FunctionCallbackInfo<Value>& args) { |
|
|
|
HandleScope scope(args.GetIsolate()); |
|
|
|
|
|
|
|
if (!isWeakRef(args[0])) { |
|
|
|
Local<String> message = String::New("Weakref instance expected"); |
|
|
|
ThrowException(Exception::TypeError(message)); |
|
|
|
ThrowTypeError(args.GetIsolate(), "Weakref instance expected"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@ -249,8 +249,7 @@ void IsNearDeath(const FunctionCallbackInfo<Value>& args) { |
|
|
|
HandleScope scope(args.GetIsolate()); |
|
|
|
|
|
|
|
if (!isWeakRef(args[0])) { |
|
|
|
Local<String> message = String::New("Weakref instance expected"); |
|
|
|
ThrowException(Exception::TypeError(message)); |
|
|
|
ThrowTypeError(args.GetIsolate(), "Weakref instance expected"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@ -266,8 +265,7 @@ void IsDead(const FunctionCallbackInfo<Value>& args) { |
|
|
|
HandleScope scope(args.GetIsolate()); |
|
|
|
|
|
|
|
if (!isWeakRef(args[0])) { |
|
|
|
Local<String> message = String::New("Weakref instance expected"); |
|
|
|
ThrowException(Exception::TypeError(message)); |
|
|
|
ThrowTypeError(args.GetIsolate(), "Weakref instance expected"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@ -280,21 +278,19 @@ void AddCallback(const FunctionCallbackInfo<Value>& args) { |
|
|
|
HandleScope scope(args.GetIsolate()); |
|
|
|
|
|
|
|
if (!isWeakRef(args[0])) { |
|
|
|
Local<String> message = String::New("Weakref instance expected"); |
|
|
|
ThrowException(Exception::TypeError(message)); |
|
|
|
ThrowTypeError(args.GetIsolate(), "Weakref instance expected"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
Local<Object> proxy = args[0]->ToObject(); |
|
|
|
AddCallback(proxy, Handle<Function>::Cast(args[1])); |
|
|
|
AddCallback(args.GetIsolate(), proxy, Handle<Function>::Cast(args[1])); |
|
|
|
} |
|
|
|
|
|
|
|
void Callbacks(const FunctionCallbackInfo<Value>& args) { |
|
|
|
HandleScope scope(args.GetIsolate()); |
|
|
|
|
|
|
|
if (!isWeakRef(args[0])) { |
|
|
|
Local<String> message = String::New("Weakref instance expected"); |
|
|
|
ThrowException(Exception::TypeError(message)); |
|
|
|
ThrowTypeError(args.GetIsolate(), "Weakref instance expected"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|