Files
js_worker_module/jsapi/worker/worker_helper.h
T
wupengyong 9dfe2c186f add worker core
Signed-off-by: wupengyong <wupengyong@huawei.com>
2021-08-22 09:48:16 +08:00

91 lines
2.9 KiB
C++

/*
* 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_WORKER_HELP_H
#define FOUNDATION_CCRUNTIME_JSAPI_WORKER_HELP_H
#include "napi/native_api.h"
#include "napi/native_node_api.h"
namespace OHOS::CCRuntime::Worker {
class DereferenceHelp {
public:
template<typename Inner, typename Outer>
static Outer* DereferenceOf(const Inner Outer::*field, const Inner* pointer)
{
if (field != nullptr && pointer != nullptr) {
uintptr_t fieldOffset = (uintptr_t)&(static_cast<Outer*>(0)->*field);
Outer* outPointer = reinterpret_cast<Outer*>((uintptr_t)(pointer) - fieldOffset);
return outPointer;
}
return nullptr;
}
};
class NapiValueHelp {
public:
static bool IsString(napi_value value);
static bool IsArray(napi_value value);
static bool IsConstructor(napi_env env, napi_callback_info cbInfo);
static bool IsCallable(napi_env env, napi_value value);
static bool IsCallable(napi_env env, napi_ref value);
static size_t GetCallbackInfoArgc(napi_env env, napi_callback_info cbInfo);
static napi_value GetNamePropertyInParentPort(napi_env env, napi_ref parentPort, const char* name);
static void SetNamePropertyInGlobal(napi_env env, const char* name, napi_value value);
static napi_value GetUndefinedValue(napi_env env);
static bool IsObject(napi_value value);
static char* GetString(napi_env env, napi_value value);
static napi_value GetBooleanValue(napi_env env, bool value);
};
class CloseHelp {
public:
template<typename T>
static void DeletePointer(const T* value, bool isArray)
{
if (value == nullptr) {
return;
}
if (isArray) {
delete[] value;
} else {
delete value;
}
}
};
template<typename T>
class ObjectScope {
public:
ObjectScope(T* data, bool isArray) : data_(data), isArray_(isArray) {}
~ObjectScope()
{
if (data_ == nullptr) {
return;
}
if (isArray_) {
delete[] data_;
} else {
delete data_;
}
}
private:
T* data_;
bool isArray_;
};
} // namespace OHOS::CCRuntime::Worker
#endif // FOUNDATION_CCRUNTIME_JSAPI_NAPI_VALUE_HELP_H