!51 Fixed intrinsic issue

Merge pull request !51 from 小马奔腾/master
This commit is contained in:
openharmony_ci
2022-03-28 02:11:55 +00:00
committed by Gitee
8 changed files with 214 additions and 39 deletions
+1 -1
View File
@@ -57,4 +57,4 @@ void MessageQueue::Clear(napi_env env)
}
queueLock_.unlock();
}
} // namespace worker
} // namespace OHOS::CCRuntime::Worker
+3 -3
View File
@@ -13,8 +13,8 @@
* limitations under the License.
*/
#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORKER_MESSAGE_QUEUE_H
#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_MESSAGE_QUEUE_H
#ifndef JSAPI_WORKER_MESSAGE_QUEUE_H_
#define JSAPI_WORKER_MESSAGE_QUEUE_H_
#include <mutex>
#include <queue>
@@ -39,4 +39,4 @@ private:
std::queue<MessageDataType> queue_;
};
} // namespace OHOS::CCRuntime::Worker
#endif // FOUNDATION_CCRUNTIME_JSAPI_WORKER_MESSAGE_QUEUE_H
#endif // JSAPI_WORKER_MESSAGE_QUEUE_H_
+1 -1
View File
@@ -24,7 +24,7 @@ static napi_module g_workerModule = {
.nm_filename = nullptr,
.nm_register_func = OHOS::CCRuntime::Worker::Worker::InitWorker,
.nm_modname = "worker",
.nm_priv = ((void*)0),
.nm_priv = reinterpret_cast<void*>(0),
.reserved = { 0 },
};
/*
+3 -3
View File
@@ -13,8 +13,8 @@
* limitations under the License.
*/
#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORKER_THREAD_H
#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_THREAD_H
#ifndef JSAPI_WORKER_THREAD_H_
#define JSAPI_WORKER_THREAD_H_
#include <uv.h>
@@ -36,4 +36,4 @@ private:
};
} // namespace OHOS::CCRuntime::Worker
#endif // #define FOUNDATION_CCRUNTIME_JSAPI_WORKER_THREAD_H
#endif // #define JSAPI_WORKER_THREAD_H_
+22 -22
View File
@@ -41,7 +41,7 @@ void Worker::StartExecuteInThread(napi_env env, const char* script)
script_ = std::string(script);
CloseHelp::DeletePointer(script, true);
// 4. create WorkerRunner to Execute
// 3. create WorkerRunner to Execute
if (!runner_) {
runner_ = std::make_unique<WorkerRunner>(WorkerStartCallback(ExecuteInThread, this));
}
@@ -320,8 +320,8 @@ void Worker::HostOnMessageInner()
// receive close signal.
if (data == nullptr) {
HILOG_INFO("worker:: worker received close signal");
uv_close((uv_handle_t*)&hostOnMessageSignal_, nullptr);
uv_close((uv_handle_t*)&hostOnErrorSignal_, nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&hostOnMessageSignal_), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&hostOnErrorSignal_), nullptr);
CloseHostCallback();
return;
}
@@ -351,7 +351,7 @@ void Worker::HostOnMessageInner()
void Worker::TerminateWorker()
{
// when there is no active handle, worker loop will stop automatic.
uv_close((uv_handle_t*)&workerOnMessageSignal_, nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(&workerOnMessageSignal_), nullptr);
CloseWorkerCallback();
uv_loop_t* loop = GetWorkerLoop();
if (loop != nullptr) {
@@ -451,7 +451,7 @@ napi_value Worker::PostMessage(napi_env env, napi_callback_info cbinfo)
napi_value thisVar = nullptr;
napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr);
Worker* worker = nullptr;
napi_unwrap(env, thisVar, (void**)&worker);
napi_unwrap(env, thisVar, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: worker is nullptr when PostMessage, maybe worker is terminated");
@@ -492,7 +492,7 @@ napi_value Worker::PostMessageToHost(napi_env env, napi_callback_info cbinfo)
napi_value* argv = new napi_value[argc];
[[maybe_unused]] ObjectScope<napi_value> scope(argv, true);
Worker* worker = nullptr;
napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, (void**)&worker);
napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: when post message to host occur worker is nullptr");
@@ -553,7 +553,7 @@ napi_value Worker::Terminate(napi_env env, napi_callback_info cbinfo)
napi_value thisVar = nullptr;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr);
Worker* worker = nullptr;
napi_unwrap(env, thisVar, (void**)&worker);
napi_unwrap(env, thisVar, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: worker is nullptr when Terminate, maybe worker is terminated");
return nullptr;
@@ -591,7 +591,7 @@ napi_value Worker::CancelTask(napi_env env, napi_callback_info cbinfo)
napi_value thisVar = nullptr;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr);
Worker* worker = nullptr;
napi_unwrap(env, thisVar, (void**)&worker);
napi_unwrap(env, thisVar, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: worker is nullptr when CancelTask, maybe worker is terminated");
return nullptr;
@@ -611,7 +611,7 @@ napi_value Worker::CancelTask(napi_env env, napi_callback_info cbinfo)
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);
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: worker is nullptr when CancelTask, maybe worker is terminated");
return nullptr;
@@ -706,15 +706,15 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo)
napi_wrap(
env, thisVar, worker,
[](napi_env env, void* data, void* hint) {
Worker* worker = (Worker*)data;
Worker* worker = reinterpret_cast<Worker*>(data);
{
std::lock_guard<std::recursive_mutex> lock(worker->liveStatusLock_);
if (worker->UpdateHostState(INACTIVE)) {
if (!uv_is_closing((uv_handle_t*)&worker->hostOnMessageSignal_)) {
uv_close((uv_handle_t*)&worker->hostOnMessageSignal_, nullptr);
if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(&worker->hostOnMessageSignal_))) {
uv_close(reinterpret_cast<uv_handle_t*>(&worker->hostOnMessageSignal_), nullptr);
}
if (!uv_is_closing((uv_handle_t*)&worker->hostOnErrorSignal_)) {
uv_close((uv_handle_t*)&worker->hostOnErrorSignal_, nullptr);
if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(&worker->hostOnErrorSignal_))) {
uv_close(reinterpret_cast<uv_handle_t*>(&worker->hostOnErrorSignal_), nullptr);
}
worker->ReleaseHostThreadContent();
}
@@ -868,7 +868,7 @@ napi_value Worker::RemoveListener(napi_env env, napi_callback_info cbinfo)
}
Worker* worker = nullptr;
napi_unwrap(env, thisVar, (void**)&worker);
napi_unwrap(env, thisVar, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: worker is nullptr when RemoveListener, maybe worker is terminated");
return nullptr;
@@ -925,7 +925,7 @@ napi_value Worker::DispatchEvent(napi_env env, napi_callback_info cbinfo)
}
Worker* worker = nullptr;
napi_unwrap(env, thisVar, (void**)&worker);
napi_unwrap(env, thisVar, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: worker is nullptr when DispatchEvent, maybe worker is terminated");
return NapiValueHelp::GetBooleanValue(env, false);
@@ -983,7 +983,7 @@ napi_value Worker::RemoveAllListener(napi_env env, napi_callback_info cbinfo)
napi_value thisVar = nullptr;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr);
Worker* worker = nullptr;
napi_unwrap(env, thisVar, (void**)&worker);
napi_unwrap(env, thisVar, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: worker is nullptr when RemoveAllListener, maybe worker is terminated");
return nullptr;
@@ -1154,7 +1154,7 @@ void Worker::ReleaseHostThreadContent()
napi_value thisVar = nullptr;
napi_get_reference_value(hostEnv_, workerRef_, &thisVar);
Worker* worker = nullptr;
napi_remove_wrap(hostEnv_, thisVar, (void**)&worker);
napi_remove_wrap(hostEnv_, thisVar, reinterpret_cast<void**>(&worker));
// 4. set workerRef_ be null
napi_delete_reference(hostEnv_, workerRef_);
}
@@ -1173,7 +1173,7 @@ napi_value Worker::ParentPortAddEventListener(napi_env env, napi_callback_info c
napi_value* args = new napi_value[argc];
[[maybe_unused]] ObjectScope<napi_value> scope(args, true);
Worker* worker = nullptr;
napi_get_cb_info(env, cbinfo, &argc, args, nullptr, (void**)&worker);
napi_get_cb_info(env, cbinfo, &argc, args, nullptr, reinterpret_cast<void**>(&worker));
if (!NapiValueHelp::IsString(args[0])) {
napi_throw_error(env, nullptr, "Worker 1st param must be string with on");
@@ -1221,7 +1221,7 @@ napi_value Worker::ParentPortAddEventListener(napi_env env, napi_callback_info c
napi_value Worker::ParentPortRemoveAllListener(napi_env env, napi_callback_info cbinfo)
{
Worker* worker = nullptr;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void**)&worker);
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, reinterpret_cast<void**>(&worker));
if (worker == nullptr) {
HILOG_ERROR("worker:: when post message to host occur worker is nullptr");
@@ -1249,7 +1249,7 @@ napi_value Worker::ParentPortDispatchEvent(napi_env env, napi_callback_info cbin
napi_value* args = new napi_value[argc];
[[maybe_unused]] ObjectScope<napi_value> scope(args, true);
Worker* worker = nullptr;
napi_get_cb_info(env, cbinfo, &argc, args, nullptr, (void**)&worker);
napi_get_cb_info(env, cbinfo, &argc, args, nullptr, reinterpret_cast<void**>(&worker));
if (!NapiValueHelp::IsObject(args[0])) {
napi_throw_error(env, nullptr, "worker DispatchEvent 1st param must be Event");
@@ -1309,7 +1309,7 @@ napi_value Worker::ParentPortRemoveEventListener(napi_env env, napi_callback_inf
napi_value* args = new napi_value[argc];
[[maybe_unused]] ObjectScope<napi_value> scope(args, true);
Worker* worker = nullptr;
napi_get_cb_info(env, cbinfo, &argc, args, nullptr, (void**)&worker);
napi_get_cb_info(env, cbinfo, &argc, args, nullptr, reinterpret_cast<void**>(&worker));
if (!NapiValueHelp::IsString(args[0])) {
napi_throw_error(env, nullptr, "Worker 1st param must be string with on");
+178 -3
View File
@@ -13,8 +13,8 @@
* limitations under the License.
*/
#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORKER_H
#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_H
#ifndef JSAPI_WORKER_WORKER_H_
#define JSAPI_WORKER_WORKER_H_
#include <list>
#include <map>
@@ -93,38 +93,213 @@ public:
napi_ref ref_ {nullptr};
};
/**
* Creates a worker instance.
*
* @param env NAPI environment parameters.
* @param thisVar URL of the script to be executed by the worker.
*/
Worker(napi_env env, napi_ref thisVar);
/**
* The destructor of the Worker.
*/
~Worker();
/**
* The host thread receives the information.
*
* @param req The value of the object passed in by the js layer.
*/
static void HostOnMessage(const uv_async_t* req);
/**
* The host thread receives the information.
*
* @param req The value of the object passed in by the js layer.
*/
static void HostOnError(const uv_async_t* req);
/**
* The worker thread receives the information.
*
* @param req The value of the object passed in by the js layer.
*/
static void WorkerOnMessage(const uv_async_t* req);
/**
* ExecuteIn in thread.
*
* @param data The worker pointer.
*/
static void ExecuteInThread(const void* data);
/**
* Post a message.
*
* @param env NAPI environment parameters.
* @param thisVar The callback information of the js layer.
*/
static napi_value PostMessage(napi_env env, napi_callback_info cbinfo);
/**
* Add event listeners to host.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value PostMessageToHost(napi_env env, napi_callback_info cbinfo);
/**
* Terminates the worker thread to stop the worker from receiving messages.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value Terminate(napi_env env, napi_callback_info cbinfo);
/**
* Close the worker.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value CloseWorker(napi_env env, napi_callback_info cbinfo);
/**
* Adds an event listener to the worker.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value On(napi_env env, napi_callback_info cbinfo);
/**
* Adds an event listener to the worker and removes the event listener automatically after it is invoked once.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value Once(napi_env env, napi_callback_info cbinfo);
/**
* Removes an event listener to the worker.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value Off(napi_env env, napi_callback_info cbinfo);
/**
* Add event listeners.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value AddEventListener(napi_env env, napi_callback_info cbinfo);
/**
* Dispatch the event.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value DispatchEvent(napi_env env, napi_callback_info cbinfo);
/**
* Remove the event listener.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value RemoveEventListener(napi_env env, napi_callback_info cbinfo);
/**
* Remove all listener.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value RemoveAllListener(napi_env env, napi_callback_info cbinfo);
/**
* Add the listener.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value AddListener(napi_env env, napi_callback_info cbinfo, ListenerMode mode);
/**
* Remove the listener.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value RemoveListener(napi_env env, napi_callback_info cbinfo);
/**
* The constructor of worker.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value WorkerConstructor(napi_env env, napi_callback_info cbinfo);
/**
* Initialize the worker.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value InitWorker(napi_env env, napi_value exports);
/**
* Cancel the task.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value CancelTask(napi_env env, napi_callback_info cbinfo);
/**
* The parent port cancels the task.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value ParentPortCancelTask(napi_env env, napi_callback_info cbinfo);
/**
* The parent port adds an event listener.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value ParentPortAddEventListener(napi_env env, napi_callback_info cbinfo);
/**
* The parent port removes all event listener.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value ParentPortRemoveAllListener(napi_env env, napi_callback_info cbinfo);
/**
* The parent port dispatch the event listener.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value ParentPortDispatchEvent(napi_env env, napi_callback_info cbinfo);
/**
* The parent port removes the event listener.
*
* @param env NAPI environment parameters.
* @param cbinfo The callback information of the js layer.
*/
static napi_value ParentPortRemoveEventListener(napi_env env, napi_callback_info cbinfo);
void StartExecuteInThread(napi_env env, const char* script);
@@ -278,4 +453,4 @@ private:
std::recursive_mutex liveStatusLock_ {};
};
} // namespace OHOS::CCRuntime::Worker
#endif // FOUNDATION_CCRUNTIME_JSAPI_WORKER_H
#endif // JSAPI_WORKER_WORKER_H_
+3 -3
View File
@@ -13,8 +13,8 @@
* limitations under the License.
*/
#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORKER_HELP_H
#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_HELP_H
#ifndef JSAPI_WORKER_WORKER_HELPER_H_
#define JSAPI_WORKER_WORKER_HELPER_H_
#include "napi/native_api.h"
#include "napi/native_node_api.h"
@@ -87,4 +87,4 @@ private:
bool isArray_;
};
} // namespace OHOS::CCRuntime::Worker
#endif // FOUNDATION_CCRUNTIME_JSAPI_NAPI_VALUE_HELP_H
#endif // JSAPI_WORKER_WORKER_HELPER_H_
+3 -3
View File
@@ -13,8 +13,8 @@
* limitations under the License.
*/
#ifndef FOUNDATION_CCRUNTIME_JSAPI_WORK_RUNNER_H
#define FOUNDATION_CCRUNTIME_JSAPI_WORK_RUNNER_H
#ifndef JSAPI_WORKER_WORKER_RUNNER_H_
#define JSAPI_WORKER_WORKER_RUNNER_H_
#include <functional>
@@ -59,4 +59,4 @@ private:
uv_thread_t selfThreadId_ {0};
};
} // namespace OHOS::CCRuntime::Worker
#endif // FOUNDATION_CCRUNTIME_JSAPI_WORK_RUNNER_H
#endif // JSAPI_WORKER_WORKER_RUNNER_H_