Skip to content

Commit

Permalink
move to non-deprecated SetWeak API; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Apr 25, 2016
1 parent 7e2a08c commit d1e98e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 40 deletions.
14 changes: 7 additions & 7 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1157,10 +1157,10 @@ void OnPromiseGC(const FunctionCallbackInfo<Value>& args) {

CHECK(args[0]->IsObject());
Local<Object> promise = args[0].As<Object>();
Local<Value> err = GetPromiseReason(env, promise);

TrackPromise::New(env->isolate(), promise);
TrackPromise::New(env->isolate(), promise, err);

Local<Value> err = GetPromiseReason(env, promise);
Local<NativeWeakMap> unhandled_reject_map =
env->promise_unhandled_reject_map();
Local<Set> unhandled_reject_keys =
Expand Down Expand Up @@ -1639,12 +1639,10 @@ void AppendExceptionLine(Environment* env,
PrintErrorString("\n%s", arrow);
}

void ReportPromiseRejection(Isolate* isolate, Local<Value> promise) {
void ReportPromiseRejection(Isolate* isolate, Local<Value> reason) {
Environment* env = Environment::GetCurrent(isolate);

Local<Value> err = GetPromiseReason(env, promise);

ReportException(env, err, Exception::CreateMessage(isolate, err));
ReportException(env, reason, Exception::CreateMessage(isolate, reason));
}

void ReportException(Environment* env,
Expand Down Expand Up @@ -4416,7 +4414,9 @@ static void StartNodeInstance(void* arg) {
key_iter).ToLocalChecked();

if (unhandled_reject_map->Has(key)) {
ReportPromiseRejection(isolate, unhandled_reject_map->Get(key));
Local<Value> err = GetPromiseReason(env,
unhandled_reject_map->Get(key));
ReportPromiseRejection(isolate, err);
exit(1);
}
}
Expand Down
38 changes: 14 additions & 24 deletions src/track-promise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,41 @@

namespace node {

using v8::Function;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::Persistent;
using v8::Value;
using v8::WeakCallbackData;

typedef void (*FreeCallback)(Local<Object> object, Local<Function> fn);
using v8::WeakCallbackInfo;


TrackPromise* TrackPromise::New(Isolate* isolate,
Local<Object> object) {
return new TrackPromise(isolate, object);
}


Persistent<Object>* TrackPromise::persistent() {
return &persistent_;
Local<Object> promise, Local<Value> reason) {
return new TrackPromise(isolate, promise, reason);
}


TrackPromise::TrackPromise(Isolate* isolate,
Local<Object> object)
: persistent_(isolate, object) {
persistent_.SetWeak(this, WeakCallback);
persistent_.MarkIndependent();
Local<Object> promise, Local<Value> reason)
: promise_(isolate, promise), reason_(isolate, reason) {
promise_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter);
promise_.MarkIndependent();
}


TrackPromise::~TrackPromise() {
persistent_.Reset();
promise_.Reset();
reason_.Reset();
}


void TrackPromise::WeakCallback(
const WeakCallbackData<Object, TrackPromise>& data) {
data.GetParameter()->WeakCallback(data.GetIsolate(), data.GetValue());
const WeakCallbackInfo<TrackPromise>& data) {
TrackPromise* self = data.GetParameter();
node::ReportPromiseRejection(data.GetIsolate(),
PersistentToLocal(data.GetIsolate(), self->reason_));
delete self;
}


void TrackPromise::WeakCallback(Isolate* isolate, Local<Object> object) {
node::ReportPromiseRejection(isolate, object.As<Value>());
exit(1);
delete this;
}

} // namespace node
17 changes: 8 additions & 9 deletions src/track-promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ class Environment;

class TrackPromise {
public:
TrackPromise(v8::Isolate* isolate, v8::Local<v8::Object> object);
virtual ~TrackPromise();

static TrackPromise* New(v8::Isolate* isolate,
v8::Local<v8::Object> object);

inline v8::Persistent<v8::Object>* persistent();
v8::Local<v8::Object> promise,
v8::Local<v8::Value> reason);

static inline void WeakCallback(
const v8::WeakCallbackData<v8::Object, TrackPromise>& data);
const v8::WeakCallbackInfo<TrackPromise>& data);

private:
inline void WeakCallback(v8::Isolate* isolate, v8::Local<v8::Object> object);
TrackPromise(v8::Isolate* isolate, v8::Local<v8::Object> promise,
v8::Local<v8::Value> reason);
~TrackPromise();

v8::Persistent<v8::Object> persistent_;
v8::Persistent<v8::Object> promise_;
v8::Persistent<v8::Value> reason_;
};

} // namespace node
Expand Down

0 comments on commit d1e98e8

Please sign in to comment.