diff --git a/README.md b/README.md index ef33b3898c98ddbe56d1d71d3cac8917de9e1259..4e9d7ecad79c7332f38339a31e2a1cf382963df7 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,9 +356,12 @@ 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 -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. diff --git a/README.zh.md b/README.zh.md index 30cc5ac2a399493eb6f53ea362e214501170d44b..fd02915c2a9b9ffcb28cf987deb69e4b12726214 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 d6ef1a03f69833129d040f3e80a6dc69ae77cc59..67d4ff544f8266408cdc95fe21f04260a8ed8aae 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 1ef3de08f23840ce0da12fc1944c5859f5bff944..ecb0b7506afa28827ce77b85f4be044d91866865 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 67d4a4803394964d5486b10b0bf50d09b805bbd5..eeee987d58bde850e56c38ac67e160ee35f43347 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 c7386e2a959553c3a79ef4e8a0da8614a6a733de..b30437511843780172e21fa12a3d04d9e330e938 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 79dacaa44772ac6f06cc1aa70a828ecf2005de8c..b13906f9d90df9b6f602cb57e24fe25d7fedbb42 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 c0e047047620d621652a6f824e2a05aa7b2d9e21..73a83614d0dce4da8c58c1ac6a8e3b032a9d441a 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 b256edb06985f75a9479a991a3717a4e14209058..9e7a97abff2e58391c5716d700e747595008ed3f 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; @@ -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() @@ -58,7 +62,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 +108,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 +174,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 +184,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 +213,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 +222,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 +252,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 +263,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 +305,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 +318,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 +333,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 +354,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 +376,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 +398,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 +424,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 +454,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 +475,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 +499,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 +522,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 +567,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 +604,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 +624,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 +669,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 +681,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 +693,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 +718,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 +773,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 +792,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 +804,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 +843,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 +888,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 +903,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 +945,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 +962,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 +1003,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 +1040,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 +1081,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 +1093,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 +1118,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 +1203,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 +1226,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 +1241,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 +1281,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 +1294,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 +1339,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 +1356,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 +1405,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 83c86eae7fb4fbcdcb8d857622bacd90c56525f4..4b5190047b17ea147d6293da88bfd1738eb62812 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 71f45a0a264bb08126f05ecc9dbb0cc4b497cd2c..5ad45516f67ffe781466f4f793797ca139f08a1b 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 873e0ae85c2af411d76e2509181e47e7556aef48..59d0988c344bae5164f3ccd2f61a3e28712a2546 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 dc8e4aeb9cdd2f779a646931f135af49fcc94d1d..61a8106d5b48c8b6ec381c9610321e4c1fb753fa 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 1835ce42957ca5aef1f8dc57939f50a72855c370..bf70fe7059c9bbec86975b961e994b1a338c521a 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 21a26f3acb2b7b1fb88c6ce41e0f5412e685b361..c7b3787573f4c7427f8e5f201d61f0e2a739deea 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