diff --git a/interfaces/js/kits/napi/src/common/napi/n_val.cpp b/interfaces/js/kits/napi/src/common/napi/n_val.cpp index e97f317c6a255319ada5f52a7504cf3c971eaea0..4a982d2013c079c86385d1a499ac7b9c074831ad 100644 --- a/interfaces/js/kits/napi/src/common/napi/n_val.cpp +++ b/interfaces/js/kits/napi/src/common/napi/n_val.cpp @@ -30,13 +30,18 @@ NVal::operator bool() const } bool NVal::TypeIs(napi_valuetype expType) const +{ + return ValueTypeIs(val_, expType); +} + +bool NVal::ValueTypeIs(napi_value value, napi_valuetype expType) const { if (!*this) { return false; } napi_valuetype valueType; - napi_typeof(env_, val_, &valueType); + napi_typeof(env_, value, &valueType); if (expType != valueType) { return false; @@ -173,6 +178,47 @@ tuple NVal::ToTypedArrayInfo return make_tuple(status == napi_ok, data, length, byte_offset, type); } +string NVal::GetPrintString(napi_value value) const +{ + string str; + if (!ValueTypeIs(value, napi_string)) { + napi_value strValue = nullptr; + if (napi_coerce_to_string(env_, value, &strValue) != napi_ok) { + return str; + } + value = strValue; + } + napi_get_print_string(env_, value, str); + return str; +} + +tuple NVal::GetValObjectAsStr() const +{ + napi_value globalValue = nullptr; + auto fail = make_tuple(false, ""); + napi_status status = napi_get_global(env_, &globalValue); + if (status != napi_ok) { + return fail; + } + napi_value jsonValue = nullptr; + status = napi_get_named_property(env_, globalValue, "JSON", &jsonValue); + if (status != napi_ok) { + return fail; + } + napi_value stringifyValue = nullptr; + status = napi_get_named_property(env_, jsonValue, "stringify", &stringifyValue); + if (status != napi_ok) { + return fail; + } + napi_value transValue = nullptr; + status = napi_call_function(env_, jsonValue, stringifyValue, 1, &val_, &transValue); + if (status != napi_ok || transValue == nullptr) { + return fail; + } + string content = GetPrintString(transValue); + return make_tuple(true, content); +} + bool NVal::HasProp(string propName) const { bool res = false; diff --git a/interfaces/js/kits/napi/src/common/napi/n_val.h b/interfaces/js/kits/napi/src/common/napi/n_val.h index e2ed44dceb44b461742287e7b1a19856041f259d..2a0e1abe99e4f98a2b1aee93fde7ea852fbadd66 100644 --- a/interfaces/js/kits/napi/src/common/napi/n_val.h +++ b/interfaces/js/kits/napi/src/common/napi/n_val.h @@ -41,6 +41,8 @@ public: bool TypeIs(napi_valuetype expType) const; + bool ValueTypeIs(napi_value value, napi_valuetype expType) const; + /* SHOULD ONLY BE USED FOR EXPECTED TYPE */ std::tuple, size_t> ToUTF8String() const; @@ -68,6 +70,8 @@ public: std::tuple ToTypedArrayInfo() const; + std::tuple GetValObjectAsStr() const; + /* Static helpers to create js objects */ static NVal CreateUndefined(napi_env env); @@ -109,6 +113,8 @@ public: static inline napi_property_descriptor DeclareNapiGetterSetter(const char *name, napi_callback getter, napi_callback setter); +private: + std::string GetPrintString(napi_value value) const; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/interfaces/js/kits/napi/src/common/napi/uni_header.h b/interfaces/js/kits/napi/src/common/napi/uni_header.h index d000d78443d4388ff6bfe2e2eb5c94dbfba671ce..5de61d663fd45789f0791aceee036f7589fda8b0 100644 --- a/interfaces/js/kits/napi/src/common/napi/uni_header.h +++ b/interfaces/js/kits/napi/src/common/napi/uni_header.h @@ -15,10 +15,6 @@ #ifndef INTERFACES_JS_KITS_NAPI_SRC_COMMON_NAPI_UNI_HEADER_H #define INTERFACES_JS_KITS_NAPI_SRC_COMMON_NAPI_UNI_HEADER_H -#pragma once - -#include -#else #include "napi/native_api.h" #include "napi/native_node_api.h" diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp index 4e5ca26dc7d500b99036fbb29786792bca41c0af..4de4ba4ebb62a569f832449050eb28fedbde7ffb 100644 --- a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp @@ -95,7 +95,8 @@ void ParseLogContent(string& formatStr, vector& params, string& logCo break; case 'O': case 'o': - if (params[count].type == napi_object) { + if (params[count].type == napi_object || params[count].type == napi_function || + params[count].type == napi_undefined || params[count].type == napi_null) { ret += (priv && showPriv) ? PRIV_STR : params[count].val; } count++; @@ -223,8 +224,8 @@ napi_value HilogNapiBase::parseNapiValue(napi_env env, napi_callback_info info, if (typeStatus != napi_ok) { return nullptr; } - if (type == napi_number || type == napi_bigint || type == napi_object || - type == napi_undefined || type == napi_boolean || type == napi_null) { + if (type == napi_number || type == napi_bigint || type == napi_undefined || + type == napi_boolean || type == napi_null) { napi_value elmString; napi_status objectStatus = napi_coerce_to_string(env, element, &elmString); if (objectStatus != napi_ok) { @@ -234,6 +235,8 @@ napi_value HilogNapiBase::parseNapiValue(napi_env env, napi_callback_info info, if (!succ) { return nullptr; } + } else if (type == napi_object || type == napi_function) { + tie(succ, res.val) = NVal(env, element).GetValObjectAsStr(); } else if (type == napi_string) { tie(succ, name, ignore) = NVal(env, element).ToUTF8String(); if (!succ) {