@@ -25,7 +25,7 @@ class NodeCategorySet : public BaseObject { |
25 | 25 | static void Enable(const FunctionCallbackInfo<Value>& args); |
26 | 26 | static void Disable(const FunctionCallbackInfo<Value>& args); |
27 | 27 |
|
28 | | - const std::set<std::string>& GetCategories() { return categories_; } |
| 28 | + const std::set<std::string>& GetCategories() const { return categories_; } |
29 | 29 |
|
30 | 30 | void MemoryInfo(MemoryTracker* tracker) const override { |
31 | 31 | tracker->TrackThis(this); |
@@ -37,8 +37,8 @@ class NodeCategorySet : public BaseObject { |
37 | 37 | private: |
38 | 38 | NodeCategorySet(Environment* env, |
39 | 39 | Local<Object> wrap, |
40 | | - std::set<std::string> categories) : |
41 | | - BaseObject(env, wrap), categories_(categories) { |
| 40 | + std::set<std::string>&& categories) : |
| 41 | + BaseObject(env, wrap), categories_(std::move(categories)) { |
42 | 42 | MakeWeak(); |
43 | 43 | } |
44 | 44 |
|
@@ -52,12 +52,14 @@ void NodeCategorySet::New(const FunctionCallbackInfo<Value>& args) { |
52 | 52 | CHECK(args[0]->IsArray()); |
53 | 53 | Local<Array> cats = args[0].As<Array>(); |
54 | 54 | for (size_t n = 0; n < cats->Length(); n++) { |
55 | | - Local<Value> category = cats->Get(env->context(), n).ToLocalChecked(); |
| 55 | + Local<Value> category; |
| 56 | + if (!cats->Get(env->context(), n).ToLocal(&category)) return; |
56 | 57 | Utf8Value val(env->isolate(), category); |
| 58 | + if (!*val) return; |
57 | 59 | categories.emplace(*val); |
58 | 60 | } |
59 | 61 | CHECK_NOT_NULL(env->tracing_agent()); |
60 | | - new NodeCategorySet(env, args.This(), categories); |
| 62 | + new NodeCategorySet(env, args.This(), std::move(categories)); |
61 | 63 | } |
62 | 64 |
|
63 | 65 | void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) { |
@@ -91,13 +93,15 @@ void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) { |
91 | 93 | args.GetReturnValue().Set( |
92 | 94 | String::NewFromUtf8(env->isolate(), |
93 | 95 | categories.c_str(), |
94 | | - v8::NewStringType::kNormal).ToLocalChecked()); |
| 96 | + v8::NewStringType::kNormal, |
| 97 | + categories.size()).ToLocalChecked()); |
95 | 98 | } |
96 | 99 | } |
97 | 100 |
|
98 | 101 | // The tracing APIs require category groups to be pointers to long-lived |
99 | 102 | // strings. Those strings are stored here. |
100 | | -static std::unordered_set<std::string> categoryGroups; |
| 103 | +static std::unordered_set<std::string> category_groups; |
| 104 | +static Mutex category_groups_mutex; |
101 | 105 |
|
102 | 106 | // Gets a pointer to the category-enabled flags for a tracing category group, |
103 | 107 | // if tracing is enabled for it. |
@@ -107,14 +111,15 @@ static const uint8_t* GetCategoryGroupEnabled(const char* category_group) { |
107 | 111 | } |
108 | 112 |
|
109 | 113 | static const char* GetCategoryGroup(Environment* env, |
110 | | - const Local<Value> categoryValue) { |
111 | | - CHECK(categoryValue->IsString()); |
| 114 | + const Local<Value> category_value) { |
| 115 | + CHECK(category_value->IsString()); |
112 | 116 |
|
113 | | - Utf8Value category(env->isolate(), categoryValue); |
| 117 | + Utf8Value category(env->isolate(), category_value); |
| 118 | + Mutex::ScopedLock lock(category_groups_mutex); |
114 | 119 | // If the value already exists in the set, insertion.first will point |
115 | 120 | // to the existing value. Thus, this will maintain a long lived pointer |
116 | 121 | // to the category c-string. |
117 | | - auto insertion = categoryGroups.insert(category.out()); |
| 122 | + auto insertion = category_groups.insert(category.out()); |
118 | 123 |
|
119 | 124 | // The returned insertion is a pair whose first item is the object that was |
120 | 125 | // inserted or that blocked the insertion and second item is a boolean |
@@ -133,7 +138,7 @@ static void Emit(const FunctionCallbackInfo<Value>& args) { |
133 | 138 | // enabled. |
134 | 139 | const char* category_group = GetCategoryGroup(env, args[1]); |
135 | 140 | const uint8_t* category_group_enabled = |
136 | | - GetCategoryGroupEnabled(category_group); |
| 141 | + GetCategoryGroupEnabled(category_group); |
137 | 142 | if (*category_group_enabled == 0) return; |
138 | 143 |
|
139 | 144 | // get trace_event phase |
@@ -142,8 +147,8 @@ static void Emit(const FunctionCallbackInfo<Value>& args) { |
142 | 147 |
|
143 | 148 | // get trace_event name |
144 | 149 | CHECK(args[2]->IsString()); |
145 | | - Utf8Value nameValue(env->isolate(), args[2]); |
146 | | - const char* name = nameValue.out(); |
| 150 | + Utf8Value name_value(env->isolate(), args[2]); |
| 151 | + const char* name = name_value.out(); |
147 | 152 |
|
148 | 153 | // get trace_event id |
149 | 154 | int64_t id = 0; |
@@ -212,7 +217,7 @@ static void CategoryGroupEnabled(const FunctionCallbackInfo<Value>& args) { |
212 | 217 |
|
213 | 218 | const char* category_group = GetCategoryGroup(env, args[0]); |
214 | 219 | const uint8_t* category_group_enabled = |
215 | | - GetCategoryGroupEnabled(category_group); |
| 220 | + GetCategoryGroupEnabled(category_group); |
216 | 221 | args.GetReturnValue().Set(*category_group_enabled > 0); |
217 | 222 | } |
218 | 223 |
|
|