From 137617ca19036beae0164086669c3b9e892d1812 Mon Sep 17 00:00:00 2001 From: y00576111 Date: Mon, 21 Mar 2022 10:53:11 +0800 Subject: [PATCH] Modify potential risks Call napi_wrap before opening the child thread issue: https://gitee.com/openharmony/js_worker_module/issues/I4YQ0G Signed-off-by: y00576111 Change-Id: Ia2b8d9dff8579b47f29b6c5c31a2d19b4cc4f045 --- jsapi/worker/worker.cpp | 16 ++++++++-------- jsapi/worker/worker.h | 2 -- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/jsapi/worker/worker.cpp b/jsapi/worker/worker.cpp index 6186e56..525508a 100644 --- a/jsapi/worker/worker.cpp +++ b/jsapi/worker/worker.cpp @@ -328,14 +328,14 @@ void Worker::HostOnMessageInner() if (!isCallable) { // onmessage is not func, no need to continue HILOG_ERROR("worker:: worker onmessage is not a callable"); - return; + continue; } // 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) { HostOnMessageErrorInner(); - return; + continue; } napi_value event = nullptr; napi_create_object(hostEnv_, &event); @@ -397,8 +397,8 @@ void Worker::WorkerOnMessageInner() return; } MessageDataType data = nullptr; - while (workerMessageQueue_.DeQueue(&data)) { - if (data == nullptr || IsTerminating()) { + while (!IsTerminated() && workerMessageQueue_.DeQueue(&data)) { + if (data == nullptr) { HILOG_INFO("worker:: worker reveive terminate signal"); TerminateWorker(); return; @@ -407,7 +407,7 @@ void Worker::WorkerOnMessageInner() napi_status status = napi_deserialize(workerEnv_, data, &result); if (status != napi_ok || result == nullptr) { WorkerOnMessageErrorInner(); - return; + continue; } napi_value event = nullptr; @@ -416,8 +416,7 @@ void Worker::WorkerOnMessageInner() napi_value argv[1] = { event }; bool callFeedback = CallWorkerFunction(1, argv, "onmessage", true); if (!callFeedback) { - // onmessage is not function, exit the loop directly. - return; + HILOG_ERROR("worker:: call WorkerGlobalScope onmessage error"); } } } @@ -704,7 +703,6 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) napi_throw_error(env, nullptr, "worker script create error, please check."); return nullptr; } - worker->StartExecuteInThread(env, script); napi_wrap( env, thisVar, worker, [](napi_env env, void* data, void* hint) { @@ -729,6 +727,7 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) }, nullptr, nullptr); napi_create_reference(env, thisVar, 1, &worker->workerRef_); + worker->StartExecuteInThread(env, script); return thisVar; } @@ -1078,6 +1077,7 @@ bool Worker::CallWorkerFunction(size_t argc, const napi_value* argv, const char* if (tryCatch && callbackResult == nullptr) { // handle exception HandleException(); + return false; } return true; } diff --git a/jsapi/worker/worker.h b/jsapi/worker/worker.h index 66c7bd4..a8ffc09 100644 --- a/jsapi/worker/worker.h +++ b/jsapi/worker/worker.h @@ -29,8 +29,6 @@ #include "worker_runner.h" namespace OHOS::CCRuntime::Worker { -class Worker; - class Worker { public: static const int8_t WORKERPARAMNUM = 2;