From be47db7eec5239095bfec5715ac1b81ff9779094 Mon Sep 17 00:00:00 2001 From: xdmal Date: Thu, 10 Mar 2022 19:57:16 +0800 Subject: [PATCH 1/3] Solve the checked issue Signed-off-by: xdmal --- README.md | 33 +++----- README.zh.md | 11 ++- jsapi/BUILD.gn | 2 +- jsapi/worker/message_queue.cpp | 13 ++- jsapi/worker/message_queue.h | 2 +- jsapi/worker/native_module_worker.cpp | 2 +- jsapi/worker/thread.cpp | 2 +- jsapi/worker/thread.h | 2 +- jsapi/worker/worker.cpp | 114 ++++++++++++++------------ jsapi/worker/worker.h | 2 +- jsapi/worker/worker_helper.cpp | 15 ++-- jsapi/worker/worker_helper.h | 2 +- jsapi/worker/worker_runner.cpp | 6 +- jsapi/worker/worker_runner.h | 2 +- mozilla_docs.txt | 2 +- 15 files changed, 112 insertions(+), 98 deletions(-) diff --git a/README.md b/README.md index ef33b38..eaab4b3 100644 --- a/README.md +++ b/README.md @@ -71,8 +71,7 @@ worker.on("alert", (e)=>{ - name -| once(type:string, listener:EventListener) | Adds an event listener to the worker and - removes the event listener automically after it is invoked once | +| once(type:string, listener:EventListener) | Adds an event listener to the worker and removes the event listener automically after it is invoked once | |---|---| - example @@ -166,8 +165,7 @@ worker.removeAllListener(); - name -| onexit?:(code:number)=>void | The onexit attribute of the worker specifies the event handler to be called - when the worker exits. The handler is executed in the host thread | +| onexit?:(code:number)=>void | The onexit attribute of the worker specifies the event handler to be called when the worker exits. The handler is executed in the host thread | |---|---| - example @@ -184,8 +182,7 @@ worker.onexit = function(e) { - name -| onerror?:(ev:ErrorEvent)=>void | The onerror attribute of the worker specifies the event handler to be called - when an exception occurs during worker execution. The event handler is executed in the host thread | +| onerror?:(ev:ErrorEvent)=>void | The onerror attribute of the worker specifies the event handler to be called when an exception occurs during worker execution. The event handler is executed in the host thread | |---|---| - example @@ -202,9 +199,7 @@ worker.onerror = function(e) { - name -| onmessage?:(ev:MessageEvent)=>void | The onmessage attribute of the worker specifies the event handler to be called - then the host thread receives a message created by itself and sent by the worker through the parentPort.postMessage. - The event handler is executed in the host thread | +| onmessage?:(ev:MessageEvent)=>void | The onmessage attribute of the worker specifies the event handler to be called then the host thread receives a message created by itself and sent by the worker through the parentPort.postMessage. The event handler is executed in the host thread | |---|---| - example @@ -221,8 +216,7 @@ worker.onmessage = function(e) { - name -| onmessageerror?:(event:MessageEvent)=>void | The onmessage attribute of the worker specifies the event handler - when the worker receives a message that cannot be serialized. The event handler is executed in the host thread | +| onmessageerror?:(event:MessageEvent)=>void | The onmessage attribute of the worker specifies the event handler when the worker receives a message that cannot be serialized. The event handler is executed in the host thread | |---|---| - example @@ -294,9 +288,7 @@ parentPort.onmessage = function(e) { - name -| onmessage?:(event:MessageEvent)=>void | The onmessage attribute of parentPort specifies the event handler to be called - then the worker thread receives a message sent by the host thread through worker postMessage. - The event handler is executed in the worker thread | +| onmessage?:(event:MessageEvent)=>void | The onmessage attribute of parentPort specifies the event handler to be called then the worker thread receives a message sent by the host thread through worker postMessage. The event handler is executed in the worker thread | |---|---| - example @@ -319,8 +311,7 @@ parentPort.onmessage = function(e) { - name -| onerror?:(ev: ErrorEvent)=>void | The onerror attribute of parentPort specifies the event handler to be called - when an exception occurs during worker execution. The event handler is executed in the worker thread | +| onerror?:(ev: ErrorEvent)=>void | The onerror attribute of parentPort specifies the event handler to be called when an exception occurs during worker execution. The event handler is executed in the worker thread | |---|---| - example @@ -344,8 +335,7 @@ parentPort.onerror = function(e) { - name -| onmessageerror?:(event: MessageEvent)=>void | The onmessage attribute of parentPort specifies the event handler to be called - then the worker receives a message that cannot be deserialized. The event handler is executed in the worker thread. | +| onmessageerror?:(event: MessageEvent)=>void | The onmessage attribute of parentPort specifies the event handler to be called then the worker receives a message that cannot be deserialized. The event handler is executed in the worker thread. | |---|---| - example @@ -366,8 +356,11 @@ parentPort.onmessageerror = function(e) { ### Repositories Involved -- ace_ace_engine -- ace_napi +- ace_ace_engine(foundation/ace/ace_engine-readme.md) +- ace_napi(foundation/ace/napi-readme.md) + +## Related warehouse +[js_worker_module Subsystem](base/compileruntime/js_worker_module-readme.md) ### License diff --git a/README.zh.md b/README.zh.md index 30cc5ac..fd02915 100644 --- a/README.zh.md +++ b/README.zh.md @@ -288,8 +288,7 @@ parentPort.onmessage = function(e) { - 属性名 -| onmessage?:(event:MessageEvent)=>void | 宿主线程收到来自其创建的worker通过worker.postMessage接口发送的消息时被调用的事件处理程序, - 处理程序在worker线程中执行 | +| onmessage?:(event:MessageEvent)=>void | 宿主线程收到来自其创建的worker通过worker.postMessage接口发送的消息时被调用的事件处理程序,处理程序在worker线程中执行 | |---|---| - 使用示例 @@ -357,8 +356,12 @@ parentPort.onmessageerror = function(e) { ### 涉及仓 -- ace_ace_engine -- ace_napi +- ace_ace_engine(foundation/ace/ace_engine-readme_zh.md) +- ace_napi(foundation/ace/napi-readme_zh.md) + +## 相关仓 +[js_worker_module 子系统](base/compileruntime/js_worker_module-readme_zh.md) + ### 许可证 diff --git a/jsapi/BUILD.gn b/jsapi/BUILD.gn index d6ef1a0..67d4ff5 100644 --- a/jsapi/BUILD.gn +++ b/jsapi/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/jsapi/worker/message_queue.cpp b/jsapi/worker/message_queue.cpp index 1ef3de0..ecb0b75 100644 --- a/jsapi/worker/message_queue.cpp +++ b/jsapi/worker/message_queue.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,6 +14,7 @@ */ #include "message_queue.h" +#include "utils/log.h" namespace OHOS::CCRuntime::Worker { void MessageQueue::EnQueue(MessageDataType data) @@ -30,9 +31,13 @@ bool MessageQueue::DeQueue(MessageDataType *data) queueLock_.unlock(); return false; } - *data = queue_.front(); - queue_.pop(); - queueLock_.unlock(); + if (data) { + *data = queue_.front(); + queue_.pop(); + queueLock_.unlock(); + } else { + HILOG_ERROR("worker:: data is nullptr."); + } return true; } diff --git a/jsapi/worker/message_queue.h b/jsapi/worker/message_queue.h index 67d4a48..eeee987 100644 --- a/jsapi/worker/message_queue.h +++ b/jsapi/worker/message_queue.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/jsapi/worker/native_module_worker.cpp b/jsapi/worker/native_module_worker.cpp index c7386e2..b304375 100644 --- a/jsapi/worker/native_module_worker.cpp +++ b/jsapi/worker/native_module_worker.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/jsapi/worker/thread.cpp b/jsapi/worker/thread.cpp index 79dacaa..b13906f 100644 --- a/jsapi/worker/thread.cpp +++ b/jsapi/worker/thread.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/jsapi/worker/thread.h b/jsapi/worker/thread.h index c0e0470..73a8361 100644 --- a/jsapi/worker/thread.h +++ b/jsapi/worker/thread.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/jsapi/worker/worker.cpp b/jsapi/worker/worker.cpp index b256edb..d89c94c 100644 --- a/jsapi/worker/worker.cpp +++ b/jsapi/worker/worker.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -29,7 +29,7 @@ void Worker::StartExecuteInThread(napi_env env, const char* script) // 1. init hostOnMessageSignal_ in host loop auto engine = reinterpret_cast(env); uv_loop_t* loop = engine->GetUVLoop(); - if (loop == nullptr) { + if (!loop) { napi_throw_error(env, nullptr, "worker::engine loop is null"); CloseHelp::DeletePointer(script, true); return; @@ -58,7 +58,7 @@ napi_value Worker::CloseWorker(napi_env env, napi_callback_info cbinfo) { Worker* worker = nullptr; napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void**)&worker); - if (worker != nullptr) { + if (worker) { worker->CloseInner(); } return NapiValueHelp::GetUndefinedValue(env); @@ -104,7 +104,7 @@ bool Worker::PrepareForWorkerInstance() HILOG_INFO("worker:: stringContent size is %{public}zu", scriptContent.size()); napi_value execScriptResult = nullptr; napi_run_actor(workerEnv_, scriptContent, workerAmi.c_str(), &execScriptResult); - if (execScriptResult == nullptr) { + if (!execScriptResult) { // An exception occurred when running the script. HILOG_ERROR("worker:: run script exception occurs, will handle exception"); HandleException(); @@ -170,7 +170,7 @@ void Worker::ExecuteInThread(const void* data) } napi_env env = worker->GetHostEnv(); napi_create_runtime(env, &workerEnv); - if (workerEnv == nullptr) { + if (!workerEnv) { napi_throw_error(env, nullptr, "Worker create runtime error"); return; } @@ -180,7 +180,7 @@ void Worker::ExecuteInThread(const void* data) } uv_loop_t* loop = worker->GetWorkerLoop(); - if (loop == nullptr) { + if (!loop) { HILOG_ERROR("worker:: Worker loop is nullptr"); return; } @@ -209,7 +209,7 @@ void Worker::ExecuteInThread(const void* data) void Worker::HostOnMessage(const uv_async_t* req) { Worker* worker = DereferenceHelp::DereferenceOf(&Worker::hostOnMessageSignal_, req); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker::worker is null"); return; } @@ -218,7 +218,7 @@ void Worker::HostOnMessage(const uv_async_t* req) void Worker::HostOnErrorInner() { - if (hostEnv_ == nullptr || HostIsStop()) { + if (!hostEnv_ || HostIsStop()) { HILOG_ERROR("worker:: host thread maybe is over"); return; } @@ -248,7 +248,7 @@ void Worker::HostOnErrorInner() void Worker::HostOnError(const uv_async_t* req) { Worker* worker = DereferenceHelp::DereferenceOf(&Worker::hostOnErrorSignal_, req); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker::worker is null"); return; } @@ -259,7 +259,7 @@ void Worker::HostOnError(const uv_async_t* req) void Worker::WorkerOnMessage(const uv_async_t* req) { Worker* worker = DereferenceHelp::DereferenceOf(&Worker::workerOnMessageSignal_, req); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker::worker is null"); return; } @@ -301,7 +301,7 @@ void Worker::HandleEventListeners(napi_env env, napi_value recv, size_t argc, co void Worker::HostOnMessageInner() { - if (hostEnv_ == nullptr || HostIsStop()) { + if (!hostEnv_ || HostIsStop()) { HILOG_ERROR("worker:: host thread maybe is over"); return; } @@ -314,7 +314,7 @@ void Worker::HostOnMessageInner() MessageDataType data = nullptr; while (hostMessageQueue_.DeQueue(&data)) { // receive close signal. - if (data == nullptr) { + if (!data) { HILOG_INFO("worker:: worker received close signal"); uv_close((uv_handle_t*)&hostOnMessageSignal_, nullptr); uv_close((uv_handle_t*)&hostOnErrorSignal_, nullptr); @@ -329,7 +329,7 @@ void Worker::HostOnMessageInner() // handle data, call worker onMessage function to handle. napi_value result = nullptr; napi_status status = napi_deserialize(hostEnv_, data, &result); - if (status != napi_ok || result == nullptr) { + if (status != napi_ok || !result) { HostOnMessageErrorInner(); return; } @@ -350,7 +350,7 @@ void Worker::TerminateWorker() uv_close((uv_handle_t*)&workerOnMessageSignal_, nullptr); CloseWorkerCallback(); uv_loop_t* loop = GetWorkerLoop(); - if (loop != nullptr) { + if (loop) { uv_stop(loop); } UpdateWorkerState(TERMINATED); @@ -372,7 +372,7 @@ void Worker::HandleException() // WorkerGlobalScope onerror WorkerOnErrorInner(exception); - if (hostEnv_ != nullptr) { + if (hostEnv_) { napi_value data = nullptr; napi_serialize(workerEnv_, exception, NapiValueHelp::GetUndefinedValue(workerEnv_), &data); { @@ -394,14 +394,14 @@ void Worker::WorkerOnMessageInner() } MessageDataType data = nullptr; while (workerMessageQueue_.DeQueue(&data)) { - if (data == NULL || IsTerminating()) { + if (!data || IsTerminating()) { HILOG_INFO("worker:: worker reveive terminate signal"); TerminateWorker(); return; } napi_value result = nullptr; napi_status status = napi_deserialize(workerEnv_, data, &result); - if (status != napi_ok || result == nullptr) { + if (status != napi_ok || !result) { WorkerOnMessageErrorInner(); return; } @@ -420,7 +420,7 @@ void Worker::WorkerOnMessageInner() void Worker::HostOnMessageErrorInner() { - if (hostEnv_ == nullptr || HostIsStop()) { + if (!hostEnv_ || HostIsStop()) { HILOG_ERROR("worker:: host thread maybe is over"); return; } @@ -450,7 +450,7 @@ napi_value Worker::PostMessage(napi_env env, napi_callback_info cbinfo) Worker* worker = nullptr; napi_unwrap(env, thisVar, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: worker is nullptr when PostMessage, maybe worker is terminated"); return nullptr; } @@ -471,11 +471,15 @@ napi_value Worker::PostMessage(napi_env env, napi_callback_info cbinfo) } else { serializeStatus = napi_serialize(env, argv[0], NapiValueHelp::GetUndefinedValue(env), &data); } - if (serializeStatus != napi_ok || data == nullptr) { + if (serializeStatus != napi_ok || !data) { worker->HostOnMessageErrorInner(); return nullptr; } - worker->PostMessageInner(data); + + if (data) { + worker->PostMessageInner(data); + } + return NapiValueHelp::GetUndefinedValue(env); } @@ -491,7 +495,7 @@ napi_value Worker::PostMessageToHost(napi_env env, napi_callback_info cbinfo) Worker* worker = nullptr; napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: when post message to host occur worker is nullptr"); return nullptr; } @@ -514,18 +518,22 @@ napi_value Worker::PostMessageToHost(napi_env env, napi_callback_info cbinfo) serializeStatus = napi_serialize(env, argv[0], NapiValueHelp::GetUndefinedValue(env), &data); } - if (serializeStatus != napi_ok || data == nullptr) { + if (serializeStatus != napi_ok || !data) { worker->WorkerOnMessageErrorInner(); return nullptr; } - worker->PostMessageToHostInner(data); + + if (data) { + worker->PostMessageToHostInner(data); + } + return NapiValueHelp::GetUndefinedValue(env); } void Worker::PostMessageToHostInner(MessageDataType data) { std::lock_guard lock(liveStatusLock_); - if (hostEnv_ != nullptr && !HostIsStop()) { + if (hostEnv_ && !HostIsStop()) { hostMessageQueue_.EnQueue(data); uv_async_send(&hostOnMessageSignal_); } else { @@ -555,7 +563,7 @@ napi_value Worker::Terminate(napi_env env, napi_callback_info cbinfo) napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr); Worker* worker = nullptr; napi_unwrap(env, thisVar, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: worker is nullptr when Terminate, maybe worker is terminated"); return nullptr; } @@ -592,7 +600,7 @@ napi_value Worker::CancelTask(napi_env env, napi_callback_info cbinfo) napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr); Worker* worker = nullptr; napi_unwrap(env, thisVar, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: worker is nullptr when CancelTask, maybe worker is terminated"); return nullptr; } @@ -612,7 +620,7 @@ napi_value Worker::ParentPortCancelTask(napi_env env, napi_callback_info cbinfo) { Worker* worker = nullptr; napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: worker is nullptr when CancelTask, maybe worker is terminated"); return nullptr; } @@ -657,7 +665,7 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) // 2. new worker instance worker = new Worker(env, nullptr); - if (worker == nullptr) { + if (!worker) { napi_throw_error(env, nullptr, "create worker error"); return nullptr; } @@ -669,7 +677,7 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) napi_get_named_property(env, args[1], "name", &nameValue); if (NapiValueHelp::IsString(nameValue)) { char* nameStr = NapiValueHelp::GetString(env, nameValue); - if (nameStr == nullptr) { + if (!nameStr) { napi_throw_error(env, nullptr, "worker name create error, please check."); return nullptr; } @@ -681,7 +689,7 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) napi_get_named_property(env, args[1], "type", &typeValue); if (NapiValueHelp::IsString(typeValue)) { char* typeStr = NapiValueHelp::GetString(env, typeValue); - if (typeStr == nullptr) { + if (!typeStr) { napi_throw_error(env, nullptr, "worker type create error, please check."); return nullptr; } @@ -706,7 +714,7 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) // 3. execute in thread char* script = NapiValueHelp::GetString(env, args[0]); - if (script == nullptr) { + if (!script) { napi_throw_error(env, nullptr, "worker script create error, please check."); return nullptr; } @@ -761,7 +769,7 @@ napi_value Worker::AddListener(napi_env env, napi_callback_info cbinfo, Listener } Worker* worker = nullptr; napi_unwrap(env, thisVar, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: worker is nullptr when addListener, maybe worker is terminated"); return nullptr; } @@ -780,7 +788,7 @@ napi_value Worker::AddListener(napi_env env, napi_callback_info cbinfo, Listener } listener->SetCallable(env, args[1]); char* typeStr = NapiValueHelp::GetString(env, args[0]); - if (typeStr == nullptr) { + if (!typeStr) { CloseHelp::DeletePointer(listener, false); napi_throw_error(env, nullptr, "worker listener type create error, please check."); return nullptr; @@ -792,7 +800,7 @@ napi_value Worker::AddListener(napi_env env, napi_callback_info cbinfo, Listener bool Worker::WorkerListener::operator==(const WorkerListener& listener) const { - if (listener.worker_ == nullptr) { + if (!listener.worker_) { return false; } napi_env env = listener.worker_->GetHostEnv(); @@ -831,7 +839,7 @@ void Worker::RemoveListenerInner(napi_env env, const char* type, napi_ref callba return; } std::list& listenerList = iter->second; - if (callback != nullptr) { + if (callback) { std::list::iterator it = std::find_if(listenerList.begin(), listenerList.end(), Worker::FindWorkerListener(env, callback)); if (it != listenerList.end()) { @@ -876,7 +884,7 @@ napi_value Worker::RemoveListener(napi_env env, napi_callback_info cbinfo) Worker* worker = nullptr; napi_unwrap(env, thisVar, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: worker is nullptr when RemoveListener, maybe worker is terminated"); return nullptr; } @@ -891,7 +899,7 @@ napi_value Worker::RemoveListener(napi_env env, napi_callback_info cbinfo) } char* typeStr = NapiValueHelp::GetString(env, args[0]); - if (typeStr == nullptr) { + if (!typeStr) { napi_throw_error(env, nullptr, "worker listener type create error, please check."); return nullptr; } @@ -933,7 +941,7 @@ napi_value Worker::DispatchEvent(napi_env env, napi_callback_info cbinfo) Worker* worker = nullptr; napi_unwrap(env, thisVar, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: worker is nullptr when DispatchEvent, maybe worker is terminated"); return NapiValueHelp::GetBooleanValue(env, false); } @@ -950,7 +958,7 @@ napi_value Worker::DispatchEvent(napi_env env, napi_callback_info cbinfo) napi_value argv[1] = { args[0] }; char* typeStr = NapiValueHelp::GetString(env, typeValue); - if (typeStr == nullptr) { + if (!typeStr) { napi_throw_error(env, nullptr, "worker listener type create error, please check."); return NapiValueHelp::GetBooleanValue(env, false); } @@ -991,7 +999,7 @@ napi_value Worker::RemoveAllListener(napi_env env, napi_callback_info cbinfo) napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr); Worker* worker = nullptr; napi_unwrap(env, thisVar, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: worker is nullptr when RemoveAllListener, maybe worker is terminated"); return nullptr; } @@ -1028,7 +1036,7 @@ napi_value Worker::InitWorker(napi_env env, napi_value exports) worker = *item; } } - if (worker == nullptr) { + if (!worker) { napi_throw_error(env, nullptr, "worker:: worker is null"); return exports; } @@ -1069,7 +1077,7 @@ void Worker::WorkerOnErrorInner(napi_value error) bool Worker::CallWorkerFunction(size_t argc, const napi_value* argv, const char* methodName, bool tryCatch) { - if (workerEnv_ == nullptr) { + if (!workerEnv_) { return false; } napi_value callback = NapiValueHelp::GetNamePropertyInParentPort(workerEnv_, parentPort_, methodName); @@ -1081,7 +1089,7 @@ bool Worker::CallWorkerFunction(size_t argc, const napi_value* argv, const char* napi_value undefinedValue = NapiValueHelp::GetUndefinedValue(workerEnv_); napi_value callbackResult = nullptr; napi_call_function(workerEnv_, undefinedValue, callback, argc, argv, &callbackResult); - if (tryCatch && callbackResult == nullptr) { + if (tryCatch && !callbackResult) { // handle exception HandleException(); } @@ -1106,7 +1114,7 @@ void Worker::CloseWorkerCallback() void Worker::CallHostFunction(size_t argc, const napi_value* argv, const char* methodName) const { - if (hostEnv_ == nullptr || HostIsStop()) { + if (!hostEnv_ || HostIsStop()) { HILOG_ERROR("worker:: host thread maybe is over"); return; } @@ -1191,7 +1199,7 @@ napi_value Worker::ParentPortAddEventListener(napi_env env, napi_callback_info c return nullptr; } - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: when post message to host occur worker is nullptr"); return nullptr; } @@ -1214,7 +1222,7 @@ napi_value Worker::ParentPortAddEventListener(napi_env env, napi_callback_info c } listener->SetCallable(env, args[1]); char* typeStr = NapiValueHelp::GetString(env, args[0]); - if (typeStr == nullptr) { + if (!typeStr) { CloseHelp::DeletePointer(listener, false); napi_throw_error(env, nullptr, "worker listener type create error, please check."); return nullptr; @@ -1229,7 +1237,7 @@ napi_value Worker::ParentPortRemoveAllListener(napi_env env, napi_callback_info Worker* worker = nullptr; napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void**)&worker); - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: when post message to host occur worker is nullptr"); return nullptr; } @@ -1269,7 +1277,7 @@ napi_value Worker::ParentPortDispatchEvent(napi_env env, napi_callback_info cbin return NapiValueHelp::GetBooleanValue(env, false); } - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: when post message to host occur worker is nullptr"); return NapiValueHelp::GetBooleanValue(env, false); } @@ -1282,7 +1290,7 @@ napi_value Worker::ParentPortDispatchEvent(napi_env env, napi_callback_info cbin napi_value argv[1] = { args[0] }; char* typeStr = NapiValueHelp::GetString(env, typeValue); - if (typeStr == nullptr) { + if (!typeStr) { napi_throw_error(env, nullptr, "worker listener type create error, please check."); return NapiValueHelp::GetBooleanValue(env, false); } @@ -1327,7 +1335,7 @@ napi_value Worker::ParentPortRemoveEventListener(napi_env env, napi_callback_inf return nullptr; } - if (worker == nullptr) { + if (!worker) { HILOG_ERROR("worker:: when post message to host occur worker is nullptr"); return nullptr; } @@ -1344,7 +1352,7 @@ napi_value Worker::ParentPortRemoveEventListener(napi_env env, napi_callback_inf } char* typeStr = NapiValueHelp::GetString(env, args[0]); - if (typeStr == nullptr) { + if (!typeStr) { napi_throw_error(env, nullptr, "worker listener type create error, please check."); return nullptr; } @@ -1393,7 +1401,7 @@ void Worker::ParentPortRemoveListenerInner(napi_env env, const char* type, napi_ return; } std::list& listenerList = iter->second; - if (callback != nullptr) { + if (callback) { std::list::iterator it = std::find_if(listenerList.begin(), listenerList.end(), Worker::FindWorkerListener(env, callback)); if (it != listenerList.end()) { diff --git a/jsapi/worker/worker.h b/jsapi/worker/worker.h index 83c86ea..4b51900 100644 --- a/jsapi/worker/worker.h +++ b/jsapi/worker/worker.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/jsapi/worker/worker_helper.cpp b/jsapi/worker/worker_helper.cpp index 71f45a0..5ad4551 100644 --- a/jsapi/worker/worker_helper.cpp +++ b/jsapi/worker/worker_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,7 @@ #include "worker_helper.h" #include "native_engine/native_value.h" +#include "utils/log.h" namespace OHOS::CCRuntime::Worker { const static int32_t MAXCHARLENGTH = 200; @@ -23,13 +24,13 @@ const static int32_t MAXCHARLENGTH = 200; bool NapiValueHelp::IsString(napi_value value) { auto valNative = reinterpret_cast(value); - return valNative == nullptr ? false : valNative->TypeOf() == NATIVE_STRING; + return !valNative ? false : valNative->TypeOf() == NATIVE_STRING; } bool NapiValueHelp::IsArray(napi_value value) { auto valNative = reinterpret_cast(value); - return valNative == nullptr ? false : valNative->IsArray(); + return !valNative ? false : valNative->IsArray(); } bool NapiValueHelp::IsConstructor(napi_env env, napi_callback_info cbInfo) @@ -75,7 +76,7 @@ bool NapiValueHelp::IsCallable(napi_env env, napi_ref value) { napi_value obj = nullptr; napi_get_reference_value(env, value, &obj); - if (obj == nullptr) { + if (!obj) { return false; } return IsCallable(env, obj); @@ -103,7 +104,11 @@ char* NapiValueHelp::GetString(napi_env env, napi_value value) return nullptr; } char* buffer = new char[bufferSize + 1] { 0 }; - napi_get_value_string_utf8(env, value, buffer, bufferSize + 1, &strLength); + if (buffer) { + napi_get_value_string_utf8(env, value, buffer, bufferSize + 1, &strLength); + } else { + HILOG_ERROR("worker:: buffer is nullptr."); + } return buffer; } diff --git a/jsapi/worker/worker_helper.h b/jsapi/worker/worker_helper.h index 873e0ae..59d0988 100644 --- a/jsapi/worker/worker_helper.h +++ b/jsapi/worker/worker_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/jsapi/worker/worker_runner.cpp b/jsapi/worker/worker_runner.cpp index dc8e4ae..61a8106 100644 --- a/jsapi/worker/worker_runner.cpp +++ b/jsapi/worker/worker_runner.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -29,7 +29,7 @@ WorkerRunner::~WorkerRunner() void WorkerRunner::WorkerInnerRunner::Run() { - if (runner_ != nullptr) { + if (runner_) { runner_->Run(); } } @@ -38,7 +38,7 @@ WorkerRunner::WorkerInnerRunner::WorkerInnerRunner(const WorkerRunner* runner) : void WorkerRunner::Run() const { - if (callback_.callback != nullptr) { + if (callback_.callback) { callback_.callback(callback_.data); } } diff --git a/jsapi/worker/worker_runner.h b/jsapi/worker/worker_runner.h index 1835ce4..bf70fe7 100644 --- a/jsapi/worker/worker_runner.h +++ b/jsapi/worker/worker_runner.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/mozilla_docs.txt b/mozilla_docs.txt index 21a26f3..c7b3787 100644 --- a/mozilla_docs.txt +++ b/mozilla_docs.txt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at -- Gitee From 1d9004f4c71eb0f0493cd1f62390f5b1ae3da411 Mon Sep 17 00:00:00 2001 From: xdmal Date: Fri, 11 Mar 2022 11:47:09 +0800 Subject: [PATCH 2/3] Solve the checked issue Signed-off-by: xdmal --- jsapi/worker/worker.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/jsapi/worker/worker.cpp b/jsapi/worker/worker.cpp index d89c94c..9e7a97a 100644 --- a/jsapi/worker/worker.cpp +++ b/jsapi/worker/worker.cpp @@ -45,7 +45,11 @@ void Worker::StartExecuteInThread(napi_env env, const char* script) if (!runner_) { runner_ = std::make_unique(WorkerStartCallback(ExecuteInThread, this)); } - runner_->Execute(); // start a new thread + if (runner_) { + runner_->Execute(); // start a new thread + } else { + HILOG_ERROR("runner_ is nullptr"); + } } void Worker::CloseInner() -- Gitee From ba6388a4470301d6a3e5eea20ea43076554919d6 Mon Sep 17 00:00:00 2001 From: xmdal Date: Sat, 12 Mar 2022 21:39:56 +0800 Subject: [PATCH 3/3] try Signed-off-by: xmdal --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eaab4b3..4e9d7ec 100644 --- a/README.md +++ b/README.md @@ -364,4 +364,4 @@ parentPort.onmessageerror = function(e) { ### License -Worker is available under [Mozilla license](https://www.mozilla.org/en-US/MPL/), and the documentation is detailed in [documentation](https://gitee.com/openharmony/js_worker_module/blob/master/mozilla_docs.txt). See [LICENSE](https://gitee.com/openharmony/js_worker_module/blob/master/LICENSE) for the full license text. \ No newline at end of file +Worker is available under [Mozilla license](https://www.mozilla.org/en-US/MPL/), and the documentation is detailed in [documentation](https://gitee.com/openharmony/js_worker_module/blob/master/mozilla_docs.txt). See [LICENSE](https://gitee.com/openharmony/js_worker_module/blob/master/LICENSE) for the full license text. -- Gitee