@@ -25,7 +25,7 @@ class NodeCategorySet : public BaseObject {
2525 static void Enable (const FunctionCallbackInfo<Value>& args);
2626 static void Disable (const FunctionCallbackInfo<Value>& args);
2727
28- const std::set<std::string>& GetCategories () { return categories_; }
28+ const std::set<std::string>& GetCategories () const { return categories_; }
2929
3030 void MemoryInfo (MemoryTracker* tracker) const override {
3131 tracker->TrackThis (this );
@@ -37,8 +37,8 @@ class NodeCategorySet : public BaseObject {
3737 private:
3838 NodeCategorySet (Environment* env,
3939 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) ) {
4242 MakeWeak ();
4343 }
4444
@@ -52,12 +52,14 @@ void NodeCategorySet::New(const FunctionCallbackInfo<Value>& args) {
5252 CHECK (args[0 ]->IsArray ());
5353 Local<Array> cats = args[0 ].As <Array>();
5454 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 ;
5657 Utf8Value val (env->isolate (), category);
58+ if (!*val) return ;
5759 categories.emplace (*val);
5860 }
5961 CHECK_NOT_NULL (env->tracing_agent ());
60- new NodeCategorySet (env, args.This (), categories);
62+ new NodeCategorySet (env, args.This (), std::move ( categories) );
6163}
6264
6365void NodeCategorySet::Enable (const FunctionCallbackInfo<Value>& args) {
@@ -91,13 +93,15 @@ void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
9193 args.GetReturnValue ().Set (
9294 String::NewFromUtf8 (env->isolate (),
9395 categories.c_str (),
94- v8::NewStringType::kNormal ).ToLocalChecked ());
96+ v8::NewStringType::kNormal ,
97+ categories.size ()).ToLocalChecked ());
9598 }
9699}
97100
98101// The tracing APIs require category groups to be pointers to long-lived
99102// 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;
101105
102106// Gets a pointer to the category-enabled flags for a tracing category group,
103107// if tracing is enabled for it.
@@ -107,14 +111,15 @@ static const uint8_t* GetCategoryGroupEnabled(const char* category_group) {
107111}
108112
109113static 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 ());
112116
113- Utf8Value category (env->isolate (), categoryValue);
117+ Utf8Value category (env->isolate (), category_value);
118+ Mutex::ScopedLock lock (category_groups_mutex);
114119 // If the value already exists in the set, insertion.first will point
115120 // to the existing value. Thus, this will maintain a long lived pointer
116121 // to the category c-string.
117- auto insertion = categoryGroups .insert (category.out ());
122+ auto insertion = category_groups .insert (category.out ());
118123
119124 // The returned insertion is a pair whose first item is the object that was
120125 // inserted or that blocked the insertion and second item is a boolean
@@ -133,7 +138,7 @@ static void Emit(const FunctionCallbackInfo<Value>& args) {
133138 // enabled.
134139 const char * category_group = GetCategoryGroup (env, args[1 ]);
135140 const uint8_t * category_group_enabled =
136- GetCategoryGroupEnabled (category_group);
141+ GetCategoryGroupEnabled (category_group);
137142 if (*category_group_enabled == 0 ) return ;
138143
139144 // get trace_event phase
@@ -142,8 +147,8 @@ static void Emit(const FunctionCallbackInfo<Value>& args) {
142147
143148 // get trace_event name
144149 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 ();
147152
148153 // get trace_event id
149154 int64_t id = 0 ;
@@ -212,7 +217,7 @@ static void CategoryGroupEnabled(const FunctionCallbackInfo<Value>& args) {
212217
213218 const char * category_group = GetCategoryGroup (env, args[0 ]);
214219 const uint8_t * category_group_enabled =
215- GetCategoryGroupEnabled (category_group);
220+ GetCategoryGroupEnabled (category_group);
216221 args.GetReturnValue ().Set (*category_group_enabled > 0 );
217222}
218223
0 commit comments