Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
src: use std::map for the promise reject map
  • Loading branch information
Fishrock123 committed Feb 10, 2017
commit c083f6a6a910657a59afaa9389caa25d26772fbc
10 changes: 9 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <stdint.h>
#include <vector>
#include <map>

// Caveat emptor: we're going slightly crazy with macros here but the end
// hopefully justifies the means. We have a lot of per-context properties
Expand Down Expand Up @@ -253,7 +254,6 @@ namespace node {
V(process_object, v8::Object) \
V(promise_unhandled_rejection_function, v8::Function) \
V(promise_unhandled_rejection, v8::Function) \
V(promise_unhandled_reject_map, v8::NativeWeakMap) \
V(promise_unhandled_reject_keys, v8::Set) \
V(push_values_to_array_function, v8::Function) \
V(script_context_constructor_template, v8::FunctionTemplate) \
Expand Down Expand Up @@ -555,6 +555,14 @@ class Environment {

static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;

struct v8LocalCompare {
bool operator() (const v8::Local<v8::Value>& lhs, const v8::Local<v8::Value>& rhs) const {
return !lhs->StrictEquals(rhs);
}
};

std::map<v8::Local<v8::Value>, v8::Local<v8::Object>, v8LocalCompare> promise_unhandled_reject_map;

private:
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
const char* errmsg);
Expand Down
29 changes: 15 additions & 14 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

#include <string>
#include <vector>
#include <map>

#if defined(NODE_HAVE_I18N_SUPPORT)
#include <unicode/uvernum.h>
Expand Down Expand Up @@ -1206,21 +1207,19 @@ void TrackPromise(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsObject());
Local<Object> promise = args[0].As<Object>();

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

Local<Value> promise_value = GetPromiseReason(env, promise);
Local<NativeWeakMap> unhandled_reject_map =
env->promise_unhandled_reject_map();
std::map<Local<Value>, Local<Object>, Environment::v8LocalCompare> unhandled_reject_map =
env->promise_unhandled_reject_map;
Local<Set> unhandled_reject_keys =
env->promise_unhandled_reject_keys();

if (unhandled_reject_keys->Size() > 1000) {
return;
}

if (!unhandled_reject_map->Has(promise_value) &&
if (unhandled_reject_map.find(promise_value) != unhandled_reject_map.end() &&
!promise_value->IsUndefined()) {
unhandled_reject_map->Set(promise_value, promise);
unhandled_reject_map.insert(std::make_pair(promise_value, promise));
CHECK(!unhandled_reject_keys->Add(env->context(), promise_value).IsEmpty());
}
}
Expand All @@ -1232,22 +1231,23 @@ void UntrackPromise(const FunctionCallbackInfo<Value>& args) {
Local<Value> promise = args[0].As<Value>();

Local<Value> err = GetPromiseReason(env, promise);
Local<NativeWeakMap> unhandled_reject_map =
env->promise_unhandled_reject_map();
std::map<Local<Value>, Local<Object>, Environment::v8LocalCompare> unhandled_reject_map =
env->promise_unhandled_reject_map;
Local<Set> unhandled_reject_keys =
env->promise_unhandled_reject_keys();

if (unhandled_reject_keys->Has(env->context(), err).IsJust()) {
CHECK(unhandled_reject_keys->Delete(env->context(), err).IsJust());
unhandled_reject_map->Delete(err);
unhandled_reject_map.erase(err);
}
}

void SetupPromises(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();

env->set_promise_unhandled_reject_map(NativeWeakMap::New(isolate));
std::map<Local<Value>, Local<Object>, Environment::v8LocalCompare> promise_reject_map;
env->promise_unhandled_reject_map = promise_reject_map;
env->set_promise_unhandled_reject_keys(Set::New(isolate));

CHECK(args[0]->IsFunction());
Expand Down Expand Up @@ -4554,15 +4554,16 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
Null(env.isolate()), 1, &promise_keys_set).ToLocalChecked();
Local<Array> promise_keys = ret.As<Array>();
uint32_t key_count = promise_keys->Length();
Local<NativeWeakMap> unhandled_reject_map =
env.promise_unhandled_reject_map();
std::map<Local<Value>, Local<Object>, Environment::v8LocalCompare> unhandled_reject_map =
env.promise_unhandled_reject_map;

for (uint32_t key_iter = 0; key_iter < key_count; key_iter++) {
Local<Value> key = promise_keys->Get(env.context(),
key_iter).ToLocalChecked();

if (unhandled_reject_map->Has(key)) {
Local<Value> promise = unhandled_reject_map->Get(key);
if (unhandled_reject_map.find(key) != unhandled_reject_map.end()) {
Local<Value> promise =
unhandled_reject_map.find(key)->second;
Local<Value> err = GetPromiseReason(&env, promise);
Local<Message> message = Exception::CreateMessage(isolate, err);

Expand Down