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
wip move to unordered_map
does not work

compiles

segfaults
  • Loading branch information
Fishrock123 committed Feb 21, 2017
commit a6a36a508efc7044a89abcb768a7b02b31e879c4
22 changes: 17 additions & 5 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#endif
#include "handle_wrap.h"
#include "req-wrap.h"
#include "track-promise.h"
#include "tree.h"
#include "util.h"
#include "uv.h"
#include "v8.h"

#include <stdint.h>
#include <vector>
#include <map>
#include <unordered_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 @@ -556,12 +557,23 @@ 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);
}
bool operator() (const v8::Local<v8::Value>& lhs, const v8::Local<v8::Value>& rhs) const {
return !lhs->StrictEquals(rhs);
}
};

struct v8LocalHash {
size_t operator() (const v8::Local<v8::Value>& key) const {
if (!key.IsObject()) {

}
return (size_t) key.As<v8::Object>()->GetIdentityHash();
}
};

std::map<v8::Local<v8::Value>, v8::Local<v8::Object>, v8LocalCompare> promise_unhandled_reject_map;
std::unordered_map<v8::Local<v8::Value>, TrackPromise*, v8LocalHash, v8LocalCompare> promise_unhandled_reject_map;

// 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>),
Expand Down
33 changes: 18 additions & 15 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

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

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

class TrackPromise* tp = TrackPromise::New(env->isolate(), promise);

Local<Value> promise_value = GetPromiseReason(env, promise);
std::map<Local<Value>, Local<Object>, Environment::v8LocalCompare> unhandled_reject_map =
env->promise_unhandled_reject_map;
std::unordered_map<v8::Local<v8::Value>, class TrackPromise*, Environment::v8LocalHash, 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.find(promise_value) != unhandled_reject_map.end() &&
if (unhandled_reject_map->find(promise_value) != unhandled_reject_map->end() &&
!promise_value->IsUndefined()) {
unhandled_reject_map.insert(std::make_pair(promise_value, promise));
unhandled_reject_map->insert(std::make_pair(promise_value, tp));
CHECK(!unhandled_reject_keys->Add(env->context(), promise_value).IsEmpty());
}
}
Expand All @@ -1231,23 +1233,23 @@ void UntrackPromise(const FunctionCallbackInfo<Value>& args) {
Local<Value> promise = args[0].As<Value>();

Local<Value> err = GetPromiseReason(env, promise);
std::map<Local<Value>, Local<Object>, Environment::v8LocalCompare> unhandled_reject_map =
env->promise_unhandled_reject_map;
std::unordered_map<v8::Local<v8::Value>, class TrackPromise*, Environment::v8LocalHash, 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.erase(err);
unhandled_reject_map->erase(err);
}
}

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

std::map<Local<Value>, Local<Object>, Environment::v8LocalCompare> promise_reject_map;
env->promise_unhandled_reject_map = promise_reject_map;
// std::unordered_map<v8::Local<v8::Value>, class TrackPromise, Environment::v8LocalHash, 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,16 +4556,17 @@ 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();
std::map<Local<Value>, Local<Object>, Environment::v8LocalCompare> unhandled_reject_map =
env.promise_unhandled_reject_map;
std::unordered_map<v8::Local<v8::Value>, class TrackPromise*, Environment::v8LocalHash, 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.find(key) != unhandled_reject_map.end()) {
Local<Value> promise =
unhandled_reject_map.find(key)->second;
if (unhandled_reject_map->find(key) != unhandled_reject_map->end()) {
class TrackPromise* tp =
unhandled_reject_map->find(key)->second;
Local<Value> promise = tp->persistent()->Get(isolate);
Local<Value> err = GetPromiseReason(&env, promise);
Local<Message> message = Exception::CreateMessage(isolate, err);

Expand Down
4 changes: 2 additions & 2 deletions src/track-promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class TrackPromise {
virtual ~TrackPromise();

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

inline v8::Persistent<v8::Object>* persistent();
v8::Persistent<v8::Object>* persistent();

static inline void WeakCallback(
const v8::WeakCallbackInfo<TrackPromise>& data);
Expand Down