• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "napi_util.h"
16 
17 #include <unordered_map>
18 
19 #include "hiappevent_base.h"
20 #include "hilog/log.h"
21 #include "napi_error.h"
22 
23 #undef LOG_DOMAIN
24 #define LOG_DOMAIN 0xD002D07
25 
26 #undef LOG_TAG
27 #define LOG_TAG "NapiUtil"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace NapiUtil {
32 namespace {
33 const std::string DOMAIN_PROPERTY = "domain";
34 const std::string NAME_PROPERTY = "name";
35 const std::string EVENT_TYPE_PROPERTY = "eventType";
36 const std::string PARAM_PROPERTY = "params";
37 const std::string EVENT_INFOS_PROPERTY = "appEventInfos";
38 }
39 
IsNull(const napi_env env,const napi_value value)40 bool IsNull(const napi_env env, const napi_value value)
41 {
42     return GetType(env, value) == napi_null;
43 }
44 
IsBoolean(const napi_env env,const napi_value value)45 bool IsBoolean(const napi_env env, const napi_value value)
46 {
47     return GetType(env, value) == napi_boolean;
48 }
49 
IsNumber(const napi_env env,const napi_value value)50 bool IsNumber(const napi_env env, const napi_value value)
51 {
52     return GetType(env, value) == napi_number;
53 }
54 
IsString(const napi_env env,const napi_value value)55 bool IsString(const napi_env env, const napi_value value)
56 {
57     return GetType(env, value) == napi_string;
58 }
59 
IsObject(const napi_env env,const napi_value value)60 bool IsObject(const napi_env env, const napi_value value)
61 {
62     return GetType(env, value) == napi_object;
63 }
64 
IsFunction(const napi_env env,const napi_value value)65 bool IsFunction(const napi_env env, const napi_value value)
66 {
67     return GetType(env, value) == napi_function;
68 }
69 
IsArray(const napi_env env,const napi_value value)70 bool IsArray(const napi_env env, const napi_value value)
71 {
72     bool result = false;
73     if (napi_is_array(env, value, &result) != napi_ok) {
74         HILOG_ERROR(LOG_CORE, "failed to check array type");
75         return false;
76     }
77     return result;
78 }
79 
IsArrayType(const napi_env env,const napi_value value,napi_valuetype type)80 bool IsArrayType(const napi_env env, const napi_value value, napi_valuetype type)
81 {
82     if (!IsArray(env, value)) {
83         return false;
84     }
85     if (GetArrayLength(env, value) == 0) {
86         return true;
87     }
88     return GetArrayType(env, value) == type;
89 }
90 
GetType(const napi_env env,const napi_value value)91 napi_valuetype GetType(const napi_env env, const napi_value value)
92 {
93     napi_valuetype type;
94     if (napi_typeof(env, value, &type) != napi_ok) {
95         HILOG_ERROR(LOG_CORE, "failed to get value type");
96         return napi_undefined;
97     }
98     return type;
99 }
100 
GetArrayType(const napi_env env,const napi_value arr)101 napi_valuetype GetArrayType(const napi_env env, const napi_value arr)
102 {
103     uint32_t result = 0;
104     if (napi_get_array_length(env, arr, &result) != napi_ok) {
105         HILOG_ERROR(LOG_CORE, "failed to get the length of array");
106         return napi_undefined;
107     }
108 
109     napi_valuetype type = napi_null; // note: empty array returns null type
110     for (size_t i = 0; i < result; ++i) {
111         napi_value element = nullptr;
112         if (napi_get_element(env, arr, i, &element) != napi_ok) {
113             HILOG_ERROR(LOG_CORE, "failed to get the element of array");
114             return napi_undefined;
115         }
116         if (i == 0) {
117             type = GetType(env, element);
118             continue;
119         }
120         if (type != GetType(env, element)) {
121             HILOG_ERROR(LOG_CORE, "array has different element types");
122             return napi_undefined;
123         }
124     }
125     return type;
126 }
127 
GetArrayLength(const napi_env env,const napi_value arr)128 uint32_t GetArrayLength(const napi_env env, const napi_value arr)
129 {
130     uint32_t result = 0;
131     if (napi_get_array_length(env, arr, &result) != napi_ok) {
132         HILOG_ERROR(LOG_CORE, "failed to get the length of array");
133         return 0;
134     }
135     return result;
136 }
137 
GetElement(const napi_env env,const napi_value arr,uint32_t index)138 napi_value GetElement(const napi_env env, const napi_value arr, uint32_t index)
139 {
140     napi_value element = nullptr;
141     if (napi_get_element(env, arr, index, &element) != napi_ok) {
142         HILOG_ERROR(LOG_CORE, "failed to get the element of array.");
143         return nullptr;
144     }
145     return element;
146 }
147 
GetBoolean(const napi_env env,const napi_value value)148 bool GetBoolean(const napi_env env, const napi_value value)
149 {
150     bool bValue = false;
151     if (napi_get_value_bool(env, value, &bValue) != napi_ok) {
152         HILOG_ERROR(LOG_CORE, "failed to get bool value");
153         return false;
154     }
155     return bValue;
156 }
157 
GetBooleans(const napi_env env,const napi_value arr,std::vector<bool> & bools)158 void GetBooleans(const napi_env env, const napi_value arr, std::vector<bool>& bools)
159 {
160     uint32_t len = GetArrayLength(env, arr);
161     for (size_t i = 0; i < len; ++i) {
162         napi_value element = GetElement(env, arr, i);
163         if (element == nullptr) {
164             continue;
165         }
166         bools.push_back(GetBoolean(env, element));
167     }
168 }
169 
GetInt32(const napi_env env,const napi_value value)170 int32_t GetInt32(const napi_env env, const napi_value value)
171 {
172     int32_t iValue = 0;
173     if (napi_get_value_int32(env, value, &iValue) != napi_ok) {
174         HILOG_ERROR(LOG_CORE, "failed to get int32 value");
175         return 0;
176     }
177     return iValue;
178 }
179 
GetInt64(const napi_env env,const napi_value value)180 int64_t GetInt64(const napi_env env, const napi_value value)
181 {
182     int64_t iValue = 0;
183     if (napi_get_value_int64(env, value, &iValue) != napi_ok) {
184         HILOG_ERROR(LOG_CORE, "failed to get int64 value");
185         return 0;
186     }
187     return iValue;
188 }
189 
GetInt32s(const napi_env env,const napi_value arr,std::vector<int32_t> & ints)190 void GetInt32s(const napi_env env, const napi_value arr, std::vector<int32_t>& ints)
191 {
192     uint32_t len = GetArrayLength(env, arr);
193     for (size_t i = 0; i < len; ++i) {
194         napi_value element = GetElement(env, arr, i);
195         if (element == nullptr) {
196             continue;
197         }
198         ints.push_back(GetInt32(env, element));
199     }
200 }
201 
GetDouble(const napi_env env,const napi_value value)202 double GetDouble(const napi_env env, const napi_value value)
203 {
204     double dValue = 0;
205     if (napi_get_value_double(env, value, &dValue) != napi_ok) {
206         HILOG_ERROR(LOG_CORE, "failed to get double value");
207         return 0;
208     }
209     return dValue;
210 }
211 
GetDoubles(const napi_env env,const napi_value arr,std::vector<double> & doubles)212 void GetDoubles(const napi_env env, const napi_value arr, std::vector<double>& doubles)
213 {
214     uint32_t len = GetArrayLength(env, arr);
215     for (size_t i = 0; i < len; ++i) {
216         napi_value element = GetElement(env, arr, i);
217         if (element == nullptr) {
218             continue;
219         }
220         doubles.push_back(GetDouble(env, element));
221     }
222 }
223 
GetString(const napi_env env,const napi_value value)224 std::string GetString(const napi_env env, const napi_value value)
225 {
226     size_t bufsize = 0;
227     if (napi_get_value_string_utf8(env, value, nullptr, 0, &bufsize) != napi_ok) {
228         HILOG_ERROR(LOG_CORE, "failed to get string length");
229         return "";
230     }
231     std::vector<char> charVec(bufsize + 1); // 1 for '\0'
232     if (napi_get_value_string_utf8(env, value, charVec.data(), bufsize + 1, &bufsize) != napi_ok) {
233         HILOG_ERROR(LOG_CORE, "failed to get string value");
234         return "";
235     }
236     return std::string(charVec.data());
237 }
238 
GetStrings(const napi_env env,const napi_value arr,std::vector<std::string> & strs)239 void GetStrings(const napi_env env, const napi_value arr, std::vector<std::string>& strs)
240 {
241     uint32_t len = GetArrayLength(env, arr);
242     for (size_t i = 0; i < len; ++i) {
243         napi_value element = GetElement(env, arr, i);
244         if (element == nullptr) {
245             continue;
246         }
247         strs.push_back(GetString(env, element));
248     }
249 }
250 
GetStringsToSet(const napi_env env,const napi_value arr,std::unordered_set<std::string> & strs)251 void GetStringsToSet(const napi_env env, const napi_value arr, std::unordered_set<std::string>& strs)
252 {
253     uint32_t len = GetArrayLength(env, arr);
254     for (size_t i = 0; i < len; ++i) {
255         napi_value element = GetElement(env, arr, i);
256         if (element == nullptr) {
257             continue;
258         }
259         strs.insert(GetString(env, element));
260     }
261 }
262 
HasProperty(const napi_env env,const napi_value object,const std::string & name)263 bool HasProperty(const napi_env env, const napi_value object, const std::string& name)
264 {
265     bool result = false;
266     if (napi_has_named_property(env, object, name.c_str(), &result) != napi_ok) {
267         HILOG_ERROR(LOG_CORE, "failed to check whether the object has the named property");
268         return false;
269     }
270     return result;
271 }
272 
GetProperty(const napi_env env,const napi_value object,const std::string & name)273 napi_value GetProperty(const napi_env env, const napi_value object, const std::string& name)
274 {
275     if (!HasProperty(env, object, name)) {
276         return nullptr;
277     }
278     napi_value value = nullptr;
279     if (napi_get_named_property(env, object, name.c_str(), &value) != napi_ok) {
280         HILOG_ERROR(LOG_CORE, "failed to get property from object");
281         return nullptr;
282     }
283     return value;
284 }
285 
GetPropertyNames(const napi_env env,const napi_value object,std::vector<std::string> & names)286 void GetPropertyNames(const napi_env env, const napi_value object, std::vector<std::string>& names)
287 {
288     napi_value propertyNames = nullptr;
289     if (napi_get_property_names(env, object, &propertyNames) != napi_ok) {
290         HILOG_ERROR(LOG_CORE, "failed to get property names.");
291         return;
292     }
293     uint32_t len = 0;
294     if (napi_get_array_length(env, propertyNames, &len) != napi_ok) {
295         HILOG_ERROR(LOG_CORE, "failed to get array length");
296         return;
297     }
298     for (uint32_t i = 0; i < len; ++i) {
299         napi_value element = nullptr;
300         if (napi_get_element(env, propertyNames, i, &element) != napi_ok) {
301             HILOG_ERROR(LOG_CORE, "failed to get the element of array");
302             continue;
303         }
304         names.push_back(GetString(env, element));
305     }
306 }
307 
GetReferenceValue(const napi_env env,const napi_ref funcRef)308 napi_value GetReferenceValue(const napi_env env, const napi_ref funcRef)
309 {
310     napi_value refValue = nullptr;
311     if (napi_get_reference_value(env, funcRef, &refValue) != napi_ok) {
312         HILOG_ERROR(LOG_CORE, "failed to get reference value");
313         return nullptr;
314     }
315     return refValue;
316 }
317 
GetCbInfo(const napi_env env,napi_callback_info info,napi_value argv[],size_t argc)318 size_t GetCbInfo(const napi_env env, napi_callback_info info, napi_value argv[], size_t argc)
319 {
320     size_t paramNum = argc;
321     if (napi_get_cb_info(env, info, &paramNum, argv, nullptr, nullptr) != napi_ok) {
322         HILOG_ERROR(LOG_CORE, "failed to get callback info");
323         return 0;
324     }
325     return paramNum;
326 }
327 
CreateReference(const napi_env env,const napi_value func)328 napi_ref CreateReference(const napi_env env, const napi_value func)
329 {
330     napi_ref ref = nullptr;
331     if (napi_create_reference(env, func, 1, &ref) != napi_ok) { // 1 means initial reference count
332         HILOG_ERROR(LOG_CORE, "failed to create reference");
333         return nullptr;
334     }
335     return ref;
336 }
337 
CreateNull(const napi_env env)338 napi_value CreateNull(const napi_env env)
339 {
340     napi_value nullValue = nullptr;
341     if (napi_get_null(env, &nullValue) != napi_ok) {
342         HILOG_ERROR(LOG_CORE, "failed to create null");
343         return nullptr;
344     }
345     return nullValue;
346 }
347 
CreateUndefined(const napi_env env)348 napi_value CreateUndefined(const napi_env env)
349 {
350     napi_value undefinedValue = nullptr;
351     if (napi_get_undefined(env, &undefinedValue) != napi_ok) {
352         HILOG_ERROR(LOG_CORE, "failed to create undefined");
353         return nullptr;
354     }
355     return undefinedValue;
356 }
357 
CreateBoolean(const napi_env env,bool bValue)358 napi_value CreateBoolean(const napi_env env, bool bValue)
359 {
360     napi_value boolValue = nullptr;
361     if (napi_get_boolean(env, bValue, &boolValue) != napi_ok) {
362         HILOG_ERROR(LOG_CORE, "failed to create boolean");
363         return nullptr;
364     }
365     return boolValue;
366 }
367 
CreateInt32(const napi_env env,int32_t num)368 napi_value CreateInt32(const napi_env env, int32_t num)
369 {
370     napi_value intValue = nullptr;
371     if (napi_create_int32(env, num, &intValue) != napi_ok) {
372         HILOG_ERROR(LOG_CORE, "failed to create int32");
373         return nullptr;
374     }
375     return intValue;
376 }
377 
CreateInt64(const napi_env env,int64_t num)378 napi_value CreateInt64(const napi_env env, int64_t num)
379 {
380     napi_value intValue = nullptr;
381     if (napi_create_int64(env, num, &intValue) != napi_ok) {
382         HILOG_ERROR(LOG_CORE, "failed to create int64");
383         return nullptr;
384     }
385     return intValue;
386 }
387 
CreateString(const napi_env env,const std::string & str)388 napi_value CreateString(const napi_env env, const std::string& str)
389 {
390     napi_value strValue = nullptr;
391     if (napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &strValue) != napi_ok) {
392         HILOG_ERROR(LOG_CORE, "failed to create string");
393         return nullptr;
394     }
395     return strValue;
396 }
397 
CreateStrings(const napi_env env,const std::vector<std::string> & strs)398 napi_value CreateStrings(const napi_env env, const std::vector<std::string>& strs)
399 {
400     napi_value arr = CreateArray(env);
401     for (size_t i = 0; i < strs.size(); ++i) {
402         SetElement(env, arr, i, CreateString(env, strs[i]));
403     }
404     return arr;
405 }
406 
CreateObject(const napi_env env)407 napi_value CreateObject(const napi_env env)
408 {
409     napi_value obj = nullptr;
410     if (napi_create_object(env, &obj) != napi_ok) {
411         HILOG_ERROR(LOG_CORE, "failed to create object");
412         return nullptr;
413     }
414     return obj;
415 }
416 
CreateObject(const napi_env env,const std::string & key,const napi_value value)417 napi_value CreateObject(const napi_env env, const std::string& key, const napi_value value)
418 {
419     napi_value obj = nullptr;
420     if (napi_create_object(env, &obj) != napi_ok) {
421         HILOG_ERROR(LOG_CORE, "failed to create object");
422         return nullptr;
423     }
424     if (napi_set_named_property(env, obj, key.c_str(), value) != napi_ok) {
425         HILOG_ERROR(LOG_CORE, "failed to set property");
426         return nullptr;
427     }
428     return obj;
429 }
430 
CreateArray(const napi_env env)431 napi_value CreateArray(const napi_env env)
432 {
433     napi_value arr = nullptr;
434     if (napi_create_array(env, &arr) != napi_ok) {
435         HILOG_ERROR(LOG_CORE, "failed to create array");
436         return nullptr;
437     }
438     return arr;
439 }
440 
CreateDouble(const napi_env env,double dValue)441 napi_value CreateDouble(const napi_env env, double dValue)
442 {
443     napi_value doubleValue = nullptr;
444     if (napi_create_double(env, dValue, &doubleValue) != napi_ok) {
445         HILOG_ERROR(LOG_CORE, "failed to create double");
446         return nullptr;
447     }
448     return doubleValue;
449 }
450 
SetElement(const napi_env env,const napi_value obj,uint32_t index,const napi_value value)451 void SetElement(const napi_env env, const napi_value obj, uint32_t index, const napi_value value)
452 {
453     if (napi_set_element(env, obj, index, value) != napi_ok) {
454         HILOG_ERROR(LOG_CORE, "failed to set element");
455     }
456 }
457 
SetNamedProperty(const napi_env env,const napi_value obj,const std::string & key,const napi_value value)458 void SetNamedProperty(const napi_env env, const napi_value obj, const std::string& key, const napi_value value)
459 {
460     if (napi_set_named_property(env, obj, key.c_str(), value) != napi_ok) {
461         HILOG_ERROR(LOG_CORE, "failed to set property");
462     }
463 }
464 
ConvertToString(const napi_env env,const napi_value value)465 std::string ConvertToString(const napi_env env, const napi_value value)
466 {
467     napi_valuetype type = GetType(env, value);
468     if (type == napi_undefined) {
469         return "";
470     }
471 
472     std::string result = "";
473     switch (type) {
474         case napi_boolean:
475             result = GetBoolean(env, value) ? "true" : "false";
476             break;
477         case napi_number:
478             result = std::to_string(GetDouble(env, value));
479             break;
480         case napi_string:
481             result = GetString(env, value);
482             break;
483         default:
484             break;
485     }
486     return result;
487 }
488 
ThrowErrorMsg(napi_env env,int code,bool isThrow)489 void ThrowErrorMsg(napi_env env, int code, bool isThrow)
490 {
491     ThrowError(env, code, NapiError::GetErrorMsg(code), isThrow);
492 }
493 
ThrowError(napi_env env,int code,const std::string & msg,bool isThrow)494 void ThrowError(napi_env env, int code, const std::string& msg, bool isThrow)
495 {
496     // no error needs to be thrown before api 9
497     if (!isThrow) {
498         return;
499     }
500 
501     if (napi_throw_error(env, std::to_string(code).c_str(), msg.c_str()) != napi_ok) {
502         HILOG_ERROR(LOG_CORE, "failed to throw error, code=%{public}d, msg=%{public}s", code, msg.c_str());
503     }
504 }
505 
CreateError(napi_env env,int code,const std::string & msg)506 napi_value CreateError(napi_env env, int code, const std::string& msg)
507 {
508     napi_value err = nullptr;
509     if (napi_create_error(env, CreateString(env, std::to_string(code)), CreateString(env, msg), &err) != napi_ok) {
510         HILOG_ERROR(LOG_CORE, "failed to create error");
511         return nullptr;
512     }
513     return err;
514 }
515 
CreateErrMsg(const std::string & name)516 std::string CreateErrMsg(const std::string& name)
517 {
518     return "Parameter error. The " + name + " parameter is mandatory.";
519 }
520 
CreateErrMsg(const std::string & name,const std::string & type)521 std::string CreateErrMsg(const std::string& name, const std::string& type)
522 {
523     return "Parameter error. The type of " + name + " must be " + type + ".";
524 }
CreateErrMsg(const std::string & name,const napi_valuetype type)525 std::string CreateErrMsg(const std::string& name, const napi_valuetype type)
526 {
527     std::string typeStr = "";
528     switch (type) {
529         case napi_boolean:
530             typeStr = "boolean";
531             break;
532         case napi_number:
533             typeStr = "number";
534             break;
535         case napi_string:
536             typeStr = "string";
537             break;
538         case napi_object:
539             typeStr = "object";
540             break;
541         default:
542             break;
543     }
544     return CreateErrMsg(name, typeStr);
545 }
546 
CreateBaseValueByJson(const napi_env env,const Json::Value & jsonValue)547 napi_value CreateBaseValueByJson(const napi_env env, const Json::Value& jsonValue)
548 {
549     if (jsonValue.isBool()) {
550         return CreateBoolean(env, jsonValue.asBool());
551     }
552     if (jsonValue.isInt()) {
553         return CreateInt32(env, jsonValue.asInt());
554     }
555     if (jsonValue.isInt64() && jsonValue.type() != Json::ValueType::uintValue) {
556         return CreateInt64(env, jsonValue.asInt64());
557     }
558     if (jsonValue.isDouble()) {
559         return CreateDouble(env, jsonValue.asDouble());
560     }
561     if (jsonValue.isString()) {
562         return CreateString(env, jsonValue.asString());
563     }
564     return nullptr;
565 }
566 
CreateValueByJson(napi_env env,const Json::Value & jsonValue)567 napi_value CreateValueByJson(napi_env env, const Json::Value& jsonValue)
568 {
569     if (jsonValue.isArray()) {
570         napi_value array = CreateArray(env);
571         for (size_t i = 0; i < jsonValue.size(); ++i) {
572             SetElement(env, array, i, CreateValueByJson(env, jsonValue[static_cast<int>(i)]));
573         }
574         return array;
575     }
576     if (jsonValue.isObject()) {
577         napi_value obj = CreateObject(env);
578         auto eventNameList = jsonValue.getMemberNames();
579         for (auto it = eventNameList.cbegin(); it != eventNameList.cend(); ++it) {
580             auto propertyName = *it;
581             SetNamedProperty(env, obj, propertyName, CreateValueByJson(env, jsonValue[propertyName]));
582         }
583         return obj;
584     }
585     return CreateBaseValueByJson(env, jsonValue);
586 }
587 
CreateValueByJsonStr(napi_env env,const std::string & jsonStr)588 napi_value CreateValueByJsonStr(napi_env env, const std::string& jsonStr)
589 {
590     Json::Value jsonValue;
591     Json::Reader reader(Json::Features::strictMode());
592     if (!reader.parse(jsonStr, jsonValue)) {
593         HILOG_ERROR(LOG_CORE, "parse event detail info failed, please check the style of json");
594         return nullptr;
595     }
596     return CreateValueByJson(env, jsonValue);
597 }
598 
CreateEventInfo(napi_env env,std::shared_ptr<AppEventPack> event)599 napi_value CreateEventInfo(napi_env env, std::shared_ptr<AppEventPack> event)
600 {
601     napi_value obj = CreateObject(env);
602     SetNamedProperty(env, obj, DOMAIN_PROPERTY, CreateString(env, event->GetDomain()));
603     SetNamedProperty(env, obj, NAME_PROPERTY, CreateString(env, event->GetName()));
604     SetNamedProperty(env, obj, EVENT_TYPE_PROPERTY, CreateInt32(env, event->GetType()));
605     SetNamedProperty(env, obj, PARAM_PROPERTY, CreateValueByJsonStr(env, event->GetParamStr()));
606     return obj;
607 }
608 
CreateEventInfoArray(napi_env env,const std::vector<std::shared_ptr<AppEventPack>> & events)609 napi_value CreateEventInfoArray(napi_env env, const std::vector<std::shared_ptr<AppEventPack>>& events)
610 {
611     napi_value arr = CreateArray(env);
612     for (size_t i = 0; i < events.size(); ++i) {
613         SetElement(env, arr, i, CreateEventInfo(env, events[i]));
614     }
615     return arr;
616 }
617 
CreateEventGroups(napi_env env,const std::vector<std::shared_ptr<AppEventPack>> & events)618 napi_value CreateEventGroups(napi_env env, const std::vector<std::shared_ptr<AppEventPack>>& events)
619 {
620     std::unordered_map<std::string, std::vector<std::shared_ptr<AppEventPack>>> eventMap;
621     for (auto event : events) {
622         eventMap[event->GetName()].emplace_back(event);
623     }
624 
625     napi_value eventGroups = CreateArray(env);
626     size_t index = 0;
627     for (auto it = eventMap.begin(); it != eventMap.end(); ++it) {
628         napi_value eventInfos = CreateArray(env);
629         for (size_t i = 0; i < it->second.size(); ++i) {
630             SetElement(env, eventInfos, i, CreateEventInfo(env, it->second[i]));
631         }
632         napi_value obj = CreateObject(env);
633         SetNamedProperty(env, obj, NAME_PROPERTY, CreateString(env, it->first));
634         SetNamedProperty(env, obj, EVENT_INFOS_PROPERTY, eventInfos);
635         SetElement(env, eventGroups, index, obj);
636         ++index;
637     }
638     return eventGroups;
639 }
640 } // namespace NapiUtil
641 } // namespace HiviewDFX
642 } // namespace OHOS
643