From 9a238cf6352875f244b259f0701b0300bdc1f052 Mon Sep 17 00:00:00 2001 From: y00576111 Date: Fri, 24 Dec 2021 18:02:19 +0800 Subject: [PATCH] sync to ohos-master Signed-off-by: y00576111 Change-Id: I9f3c1b8a34ab5b77f8fa9e62e96c74cc5e960876 --- jsapi/BUILD.gn | 6 +- .../interfaces/innerkits/worker_core/BUILD.gn | 39 ------- .../worker_core/include/worker_init.h | 40 ------- jsapi/worker/worker.cpp | 109 ++++++++++-------- jsapi/worker/worker.h | 11 +- jsapi/worker/worker_init.cpp | 43 ------- ohos.build | 14 +-- 7 files changed, 64 insertions(+), 198 deletions(-) delete mode 100644 jsapi/interfaces/innerkits/worker_core/BUILD.gn delete mode 100644 jsapi/interfaces/innerkits/worker_core/include/worker_init.h delete mode 100644 jsapi/worker/worker_init.cpp mode change 100755 => 100644 ohos.build diff --git a/jsapi/BUILD.gn b/jsapi/BUILD.gn index ff9a258..8061386 100644 --- a/jsapi/BUILD.gn +++ b/jsapi/BUILD.gn @@ -14,10 +14,7 @@ import("//build/ohos.gni") ohos_shared_library("worker") { - include_dirs = [ - "//base/compileruntime/js_worker_module/jsapi/worker", - "//base/compileruntime/js_worker_module/jsapi/interfaces/innerkits/worker_core/include", - ] + include_dirs = [ "//base/compileruntime/js_worker_module/jsapi/worker" ] sources = [ "worker/message_queue.cpp", @@ -29,7 +26,6 @@ ohos_shared_library("worker") { ] deps = [ - "//base/compileruntime/js_worker_module/jsapi/interfaces/innerkits/worker_core:worker_init", "//foundation/ace/napi:ace_napi", "//foundation/ace/napi:ace_napi_quickjs", ] diff --git a/jsapi/interfaces/innerkits/worker_core/BUILD.gn b/jsapi/interfaces/innerkits/worker_core/BUILD.gn deleted file mode 100644 index e4b5abd..0000000 --- a/jsapi/interfaces/innerkits/worker_core/BUILD.gn +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2021 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 -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//build/ohos.gni") - -config("worker_init_config") { - include_dirs = [ "//base/compileruntime/js_worker_module/jsapi/interfaces/innerkits/worker_core/include" ] -} - -ohos_shared_library("worker_init") { - include_dirs = [ "//base/compileruntime/js_worker_module/jsapi/interfaces/innerkits/worker_core/include" ] - - sources = - [ "//base/compileruntime/js_worker_module/jsapi/worker/worker_init.cpp" ] - - deps = [ - "//foundation/ace/napi:ace_napi", - "//foundation/ace/napi:ace_napi_quickjs", - ] - - public_configs = [ ":worker_init_config" ] - - subsystem_name = "ccruntime" - part_name = "jsapi_worker" -} - -group("jsapi_init_packages") { - deps = [ ":worker_init" ] -} diff --git a/jsapi/interfaces/innerkits/worker_core/include/worker_init.h b/jsapi/interfaces/innerkits/worker_core/include/worker_init.h deleted file mode 100644 index 2f33c99..0000000 --- a/jsapi/interfaces/innerkits/worker_core/include/worker_init.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2021 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FOUNDATION_CCRUNTIME_JSAPI_INTERFACES_INNERKITS_WORKER_CORE_INCLUDE_H -#define FOUNDATION_CCRUNTIME_JSAPI_INTERFACES_INNERKITS_WORKER_CORE_INCLUDE_H - -#include - -#include "native_engine/native_engine.h" - -using InitWorkerFunc = std::function; -using GetAssetFunc = std::function&)>; -using OffWorkerFunc = std::function; - -namespace OHOS::CCRuntime::Worker { -class WorkerCore { -public: - static InitWorkerFunc initWorkerFunc; - static void RegisterInitWorkerFunc(InitWorkerFunc func); - - static GetAssetFunc getAssertFunc; - static void RegisterAssetFunc(GetAssetFunc func); - - static OffWorkerFunc offWorkerFunc; - static void RegisterOffWorkerFunc(OffWorkerFunc func); -}; -} // namespace OHOS::CCRuntime::Worker -#endif // FOUNDATION_CCRUNTIME_JSAPI_INTERFACES_INNERKITS_WORKER_CORE_INCLUDE_H diff --git a/jsapi/worker/worker.cpp b/jsapi/worker/worker.cpp index 8993a0d..3d9cea5 100644 --- a/jsapi/worker/worker.cpp +++ b/jsapi/worker/worker.cpp @@ -73,47 +73,46 @@ void CallWorkCallback(napi_env env, napi_value recv, size_t argc, const napi_val } } -bool Worker::PrepareForWorkerInstance(const Worker* worker) +bool Worker::PrepareForWorkerInstance() { - napi_env env = worker->GetWorkerEnv(); - // 1. init worker environment - if (OHOS::CCRuntime::Worker::WorkerCore::initWorkerFunc != NULL) { - OHOS::CCRuntime::Worker::WorkerCore::initWorkerFunc(reinterpret_cast(env)); - } - // 2. Execute script - if (OHOS::CCRuntime::Worker::WorkerCore::getAssertFunc == NULL) { - HILOG_ERROR("worker::getAssertFunc is null"); - napi_throw_error(env, nullptr, "worker::getAssertFunc is null"); - return false; - } std::vector scriptContent; - OHOS::CCRuntime::Worker::WorkerCore::getAssertFunc(worker->GetScript(), scriptContent); - HILOG_INFO("worker:: script content size is %{public}d", (int)scriptContent.size()); + { + std::lock_guard lock(liveStatusLock_); + if (MainIsStop()) { + return false; + } + // 1. init worker async func + auto workerEngine = reinterpret_cast(workerEnv_); + auto mainEngine = reinterpret_cast(mainEnv_); + if (!mainEngine->CallWorkerAsyncWorkFunc(workerEngine)) { + HILOG_ERROR("worker:: CallWorkerAsyncWorkFunc error"); + } + // 2. init worker environment + if (!mainEngine->CallInitWorkerFunc(workerEngine)) { + HILOG_ERROR("worker:: CallInitWorkerFunc error"); + return false; + } + // 3. get uril content + if (!mainEngine->CallGetAssetFunc(script_, scriptContent)) { + HILOG_ERROR("worker:: CallGetAssetFunc error"); + return false; + } + } + HILOG_INFO("worker:: stringContent size is %{public}zu", scriptContent.size()); napi_value execScriptResult = nullptr; - napi_run_buffer_script(env, scriptContent, &execScriptResult); + napi_run_buffer_script(workerEnv_, scriptContent, &execScriptResult); if (execScriptResult == nullptr) { // An exception occurred when running the script. HILOG_ERROR("worker:: run script exception occurs, will handle exception"); - (const_cast(worker))->HandleException(); + HandleException(); return false; } - // 3. register postMessage in DedicatedWorkerGlobalScope - napi_value postFunctionObj = nullptr; - napi_create_function(env, "postMessage", NAPI_AUTO_LENGTH, Worker::PostMessageToMain, - const_cast(worker), &postFunctionObj); - NapiValueHelp::SetNamePropertyInGlobal(env, "postMessage", postFunctionObj); - // 4. register close in DedicatedWorkerGlobalScope - napi_value closeFuncObj = nullptr; - napi_create_function(env, "close", NAPI_AUTO_LENGTH, Worker::CloseWorker, - const_cast(worker), &closeFuncObj); - NapiValueHelp::SetNamePropertyInGlobal(env, "close", closeFuncObj); - // 5. register worker name in DedicatedWorkerGlobalScope - std::string workerName = worker->GetName(); - if (!workerName.empty()) { + // 4. register worker name in DedicatedWorkerGlobalScope + if (!name_.empty()) { napi_value nameValue = nullptr; - napi_create_string_utf8(env, workerName.c_str(), workerName.length(), &nameValue); - NapiValueHelp::SetNamePropertyInGlobal(env, "name", nameValue); + napi_create_string_utf8(workerEnv_, name_.c_str(), name_.length(), &nameValue); + NapiValueHelp::SetNamePropertyInGlobal(workerEnv_, "name", nameValue); } return true; } @@ -152,7 +151,6 @@ void Worker::PublishWorkerOverSignal() if (!MainIsStop()) { mainMessageQueue_.EnQueue(NULL); uv_async_send(&mainOnMessageSignal_); - TriggerPostTask(); } } @@ -185,7 +183,7 @@ void Worker::ExecuteInThread(const void* data) } // 2. add some preparation for the worker - if (PrepareForWorkerInstance(worker)) { + if (worker->PrepareForWorkerInstance()) { uv_async_init(loop, &worker->workerOnMessageSignal_, reinterpret_cast(Worker::WorkerOnMessage)); worker->UpdateWorkerState(RUNNING); // in order to invoke worker send before subThread start @@ -315,10 +313,7 @@ void Worker::MainOnMessageInner() // receive close signal. if (data == nullptr) { HILOG_INFO("worker:: worker received close signal"); - uv_unref((uv_handle_t*)&mainOnMessageSignal_); uv_close((uv_handle_t*)&mainOnMessageSignal_, nullptr); - - uv_unref((uv_handle_t*)&mainOnErrorSignal_); uv_close((uv_handle_t*)&mainOnErrorSignal_, nullptr); CloseMainCallback(); return; @@ -330,7 +325,11 @@ void Worker::MainOnMessageInner() } // handle data, call worker onMessage function to handle. napi_value result = nullptr; - napi_deserialize(mainEnv_, data, &result); + napi_status status = napi_deserialize(mainEnv_, data, &result); + if (status != napi_ok || result == nullptr) { + MainOnMessageErrorInner(); + return; + } napi_value event = nullptr; napi_create_object(mainEnv_, &event); napi_set_named_property(mainEnv_, event, "data", result); @@ -378,7 +377,6 @@ void Worker::HandleException() if (!MainIsStop()) { errorQueue_.EnQueue(data); uv_async_send(&mainOnErrorSignal_); - TriggerPostTask(); } } } else { @@ -506,14 +504,20 @@ napi_value Worker::PostMessageToMain(napi_env env, napi_callback_info cbinfo) } napi_value data = nullptr; + napi_status serializeStatus = napi_ok; if (argc >= WORKERPARAMNUM) { if (!NapiValueHelp::IsArray(argv[1])) { napi_throw_error(env, nullptr, "Transfer list must be an Array"); return nullptr; } - napi_serialize(env, argv[0], argv[1], &data); + serializeStatus = napi_serialize(env, argv[0], argv[1], &data); } else { - napi_serialize(env, argv[0], NapiValueHelp::GetUndefinedValue(env), &data); + serializeStatus = napi_serialize(env, argv[0], NapiValueHelp::GetUndefinedValue(env), &data); + } + + if (serializeStatus != napi_ok || data == nullptr) { + worker->WorkerOnMessageErrorInner(); + return nullptr; } if (data != nullptr) { @@ -529,7 +533,6 @@ void Worker::PostMessageToMainInner(MessageDataType data) if (mainEnv_ != nullptr && !MainIsStop()) { mainMessageQueue_.EnQueue(data); uv_async_send(&mainOnMessageSignal_); - TriggerPostTask(); } else { HILOG_ERROR("worker:: worker main engine is nullptr."); } @@ -720,11 +723,12 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) { std::lock_guard lock(worker->liveStatusLock_); if (worker->UpdateMainState(INACTIVE)) { - uv_unref((uv_handle_t*)&worker->mainOnMessageSignal_); - uv_close((uv_handle_t*)&worker->mainOnMessageSignal_, nullptr); - - uv_unref((uv_handle_t*)&worker->mainOnErrorSignal_); - uv_close((uv_handle_t*)&worker->mainOnErrorSignal_, nullptr); + if (!uv_is_closing((uv_handle_t*)&worker->mainOnMessageSignal_)) { + uv_close((uv_handle_t*)&worker->mainOnMessageSignal_, nullptr); + } + if (!uv_is_closing((uv_handle_t*)&worker->mainOnErrorSignal_)) { + uv_close((uv_handle_t*)&worker->mainOnErrorSignal_, nullptr); + } worker->ReleaseMainThreadContent(); } if (!worker->IsRunning()) { @@ -1093,8 +1097,15 @@ void Worker::CloseWorkerCallback() { CallWorkerFunction(0, nullptr, "onclose", true); // off worker inited environment - if (OHOS::CCRuntime::Worker::WorkerCore::offWorkerFunc != NULL) { - OHOS::CCRuntime::Worker::WorkerCore::offWorkerFunc(reinterpret_cast(workerEnv_)); + { + std::lock_guard lock(liveStatusLock_); + if (MainIsStop()) { + return; + } + auto mainEngine = reinterpret_cast(mainEnv_); + if (!mainEngine->CallOffWorkerFunc(reinterpret_cast(workerEnv_))) { + HILOG_ERROR("worker:: CallOffWorkerFunc error"); + } } } @@ -1137,6 +1148,8 @@ void Worker::ReleaseWorkerThreadContent() // 3. clear message send to worker thread workerMessageQueue_.Clear(workerEnv_); // 4. delete NativeEngine created in worker thread + auto workerEngine = reinterpret_cast(workerEnv_); + workerEngine->CloseAsyncWork(); CloseHelp::DeletePointer(reinterpret_cast(workerEnv_), false); workerEnv_ = nullptr; } diff --git a/jsapi/worker/worker.h b/jsapi/worker/worker.h index 9a60b60..578a423 100644 --- a/jsapi/worker/worker.h +++ b/jsapi/worker/worker.h @@ -25,7 +25,6 @@ #include "native_engine/native_engine.h" #include "utils/log.h" #include "worker_helper.h" -#include "worker_init.h" #include "worker_runner.h" namespace OHOS::CCRuntime::Worker { @@ -100,7 +99,6 @@ public: static void MainOnError(const uv_async_t* req); static void WorkerOnMessage(const uv_async_t* req); static void ExecuteInThread(const void* data); - static bool PrepareForWorkerInstance(const Worker* worker); static napi_value PostMessage(napi_env env, napi_callback_info cbinfo); static napi_value PostMessageToMain(napi_env env, napi_callback_info cbinfo); @@ -197,13 +195,6 @@ public: return false; } - void TriggerPostTask() - { - if (mainEnv_ != nullptr) { - return reinterpret_cast(mainEnv_)->TriggerPostTask(); - } - } - bool MainIsStop() const { return mainState_.load(std::memory_order_acquire) == INACTIVE; @@ -250,7 +241,7 @@ private: void ReleaseWorkerThreadContent(); void ReleaseMainThreadContent(); - + bool PrepareForWorkerInstance(); void ParentPortAddListenerInner(napi_env env, const char* type, const WorkerListener* listener); void ParentPortRemoveAllListenerInner(); void ParentPortRemoveListenerInner(napi_env env, const char* type, napi_ref callback); diff --git a/jsapi/worker/worker_init.cpp b/jsapi/worker/worker_init.cpp deleted file mode 100644 index dcb33ed..0000000 --- a/jsapi/worker/worker_init.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2021 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "worker_init.h" - -namespace OHOS::CCRuntime::Worker { -InitWorkerFunc WorkerCore::initWorkerFunc = NULL; -GetAssetFunc WorkerCore::getAssertFunc = NULL; -OffWorkerFunc WorkerCore::offWorkerFunc = NULL; - -void WorkerCore::RegisterInitWorkerFunc(InitWorkerFunc func) -{ - if (func != nullptr) { - WorkerCore::initWorkerFunc = func; - } -} - -void WorkerCore::RegisterAssetFunc(GetAssetFunc func) -{ - if (func != nullptr) { - WorkerCore::getAssertFunc = func; - } -} - -void WorkerCore::RegisterOffWorkerFunc(OffWorkerFunc func) -{ - if (func != nullptr) { - WorkerCore::offWorkerFunc = func; - } -} -} // namespace OHOS::CCRuntime::Worker \ No newline at end of file diff --git a/ohos.build b/ohos.build old mode 100755 new mode 100644 index eb78554..52b8760 --- a/ohos.build +++ b/ohos.build @@ -7,19 +7,7 @@ "phone" ], "module_list": [ - "//base/compileruntime/js_worker_module/jsapi:jsapi_packages", - "//base/compileruntime/js_worker_module/jsapi/interfaces/innerkits/worker_core:jsapi_init_packages" - ], - "inner_kits": [ - { - "header": { - "header_base": "//base/compileruntime/js_worker_module/jsapi/interfaces/innerkits/worker_core/include", - "header_files": [ - "worker_init.h" - ] - }, - "name": "//base/compileruntime/js_worker_module/jsapi/interfaces/innerkits/worker_core:worker_init" - } + "//base/compileruntime/js_worker_module/jsapi:jsapi_packages" ], "test_list": [ ] -- Gitee