!1637 Modify napi_wrap usage specifications

Merge pull request !1637 from yuqing_han/master
This commit is contained in:
openharmony_ci 2024-11-14 08:37:50 +00:00 committed by Gitee
commit 974dc0885c
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 94 additions and 53 deletions

View File

@ -281,10 +281,11 @@ static napi_value GetBufferWrapValue(napi_env env, napi_value thisVar, Buffer *b
{
napi_status status = napi_wrap(env, thisVar, buffer, FinalizeBufferCallback, nullptr, nullptr);
if (status != napi_ok) {
HILOG_ERROR("Buffer:: can not wrap buffer");
if (buffer != nullptr) {
delete buffer;
buffer = nullptr;
}
HILOG_ERROR("Buffer:: can not wrap buffer");
return nullptr;
}
return thisVar;

View File

@ -33,15 +33,19 @@ namespace OHOS::Xml {
HILOG_ERROR("ConvertXmlConstructor:: memory allocation failed, objectInfo is nullptr");
return nullptr;
}
napi_wrap(
env, thisVar, objectInfo,
napi_status status = napi_wrap(env, thisVar, objectInfo,
[](napi_env environment, void *data, void *hint) {
auto obj = reinterpret_cast<ConvertXml*>(data);
if (obj != nullptr) {
delete obj;
obj = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok) {
HILOG_ERROR("ConvertXmlConstructor:: napi_wrap failed");
delete objectInfo;
objectInfo = nullptr;
}
return thisVar;
}

View File

@ -130,16 +130,18 @@ namespace OHOS::Url {
} else if (argc == 2) { // 2:When the input parameter is set to 2
UrlStructor(env, info, object);
}
napi_wrap(
env, thisVar, object,
napi_status status = napi_wrap(env, thisVar, object,
[](napi_env environment, void *data, void *hint) {
auto obj = reinterpret_cast<URL*>(data);
if (obj != nullptr) {
delete obj;
obj = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && object != nullptr) {
delete object;
object = nullptr;
}
return thisVar;
}
@ -513,16 +515,19 @@ namespace OHOS::Url {
HILOG_ERROR("SeachParamsConstructor:: memory allocation failed, object is nullptr");
return nullptr;
}
napi_wrap(
env, thisVar, object,
napi_status status = napi_wrap(env, thisVar, object,
[](napi_env environment, void *data, void *hint) {
auto obj = reinterpret_cast<URLSearchParams*>(data);
if (obj != nullptr) {
delete obj;
obj = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && object != nullptr) {
HILOG_ERROR("SeachParamsConstructor:: napi_wrap failed");
delete object;
object = nullptr;
}
return thisVar;
}

View File

@ -74,15 +74,19 @@ static const int32_t ERROR_CODE = 401; // 401 : the parameter type is incorrect
}
}
}
napi_wrap(
env, thisVar, object,
napi_status status = napi_wrap(env, thisVar, object,
[](napi_env environment, void *data, void *hint) {
auto obj = reinterpret_cast<XmlSerializer*>(data);
if (obj != nullptr) {
delete obj;
obj = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && object != nullptr) {
HILOG_ERROR("XmlPullParserConstructor:: napi_wrap failed");
delete object;
object = nullptr;
}
return thisVar;
}
@ -134,15 +138,19 @@ static const int32_t ERROR_CODE = 401; // 401 : the parameter type is incorrect
}
}
}
napi_wrap(
env, thisVar, object,
napi_status status = napi_wrap(env, thisVar, object,
[](napi_env env, void *data, void *hint) {
auto obj = reinterpret_cast<XmlPullParser*>(data);
if (obj != nullptr) {
delete obj;
obj = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && object != nullptr) {
HILOG_ERROR("XmlPullParserConstructor:: napi_wrap failed");
delete object;
object = nullptr;
}
return thisVar;
}

View File

@ -641,15 +641,19 @@ namespace OHOS::JsSysModule::Process {
void *data = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data));
auto objectInfo = new ProcessManager();
napi_wrap(
env, thisVar, objectInfo,
napi_status status = napi_wrap(env, thisVar, objectInfo,
[](napi_env environment, void *data, void *hint) {
auto objInfo = reinterpret_cast<ProcessManager*>(data);
if (objInfo != nullptr) {
delete objInfo;
objInfo = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && objectInfo != nullptr) {
HILOG_ERROR("ProcessManager:: napi_wrap failed");
delete objectInfo;
objectInfo = nullptr;
}
return thisVar;
}

View File

@ -528,6 +528,29 @@ namespace OHOS::Util {
return false;
}
static napi_value InitTextEncoder(napi_env env, napi_value thisVar, std::string encoding, std::string orgEncoding)
{
auto object = new (std::nothrow) TextEncoder(encoding);
if (object == nullptr) {
HILOG_ERROR("TextEncoder:: memory allocation failed, object is nullptr");
return nullptr;
}
object->SetOrgEncoding(orgEncoding);
napi_status status = napi_wrap(env, thisVar, object,
[](napi_env environment, void *data, void *hint) {
auto obj = reinterpret_cast<TextEncoder*>(data);
if (obj != nullptr) {
delete obj;
obj = nullptr;
}
}, nullptr, nullptr);
if (status != napi_ok) {
delete object;
object = nullptr;
}
return thisVar;
}
// Encoder
static napi_value TextEncoderConstructor(napi_env env, napi_callback_info info)
{
@ -564,22 +587,7 @@ namespace OHOS::Util {
encoding = buffer;
}
}
auto object = new (std::nothrow) TextEncoder(encoding);
if (object == nullptr) {
HILOG_ERROR("TextEncoder:: memory allocation failed, object is nullptr");
return nullptr;
}
object->SetOrgEncoding(orgEncoding);
napi_wrap(
env, thisVar, object,
[](napi_env environment, void *data, void *hint) {
auto obj = reinterpret_cast<TextEncoder*>(data);
if (obj != nullptr) {
delete obj;
}
},
nullptr, nullptr);
return thisVar;
return InitTextEncoder(env, thisVar, encoding, orgEncoding);
}
static napi_value GetEncoding(napi_env env, napi_callback_info info)
@ -778,15 +786,19 @@ namespace OHOS::Util {
void *data = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data));
auto objectInfo = new Base64();
napi_wrap(
env, thisVar, objectInfo,
napi_status status = napi_wrap(env, thisVar, objectInfo,
[](napi_env environment, void *data, void *hint) {
auto objInfo = reinterpret_cast<Base64*>(data);
if (objInfo != nullptr) {
delete objInfo;
objInfo = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && objectInfo != nullptr) {
HILOG_ERROR("Base64Constructor:: napi_wrap failed");
delete objectInfo;
objectInfo = nullptr;
}
return thisVar;
}
@ -1034,15 +1046,18 @@ namespace OHOS::Util {
void* data = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data));
auto objectInfo = new Types();
napi_wrap(
env, thisVar, objectInfo,
napi_status status = napi_wrap(env, thisVar, objectInfo,
[](napi_env environment, void* data, void* hint) {
auto objectInformation = reinterpret_cast<Types*>(data);
if (objectInformation != nullptr) {
delete objectInformation;
objectInformation = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && objectInfo != nullptr) {
delete objectInfo;
objectInfo = nullptr;
}
return thisVar;
}
@ -1595,15 +1610,19 @@ namespace OHOS::Util {
}
}
auto objectInfo = new StringDecoder(enconding);
napi_wrap(
env, thisVar, objectInfo,
napi_status status = napi_wrap(env, thisVar, objectInfo,
[](napi_env environment, void* data, void* hint) {
auto obj = reinterpret_cast<StringDecoder*>(data);
if (obj != nullptr) {
delete obj;
obj = nullptr;
}
},
nullptr, nullptr);
}, nullptr, nullptr);
if (status != napi_ok && objectInfo != nullptr) {
HILOG_ERROR("StringDecoderConstructor:: napi_wrap failed.");
delete objectInfo;
objectInfo = nullptr;
}
return thisVar;
}