133eb0b6dSopenharmony_ci/* 233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License. 533eb0b6dSopenharmony_ci * You may obtain a copy of the License at 633eb0b6dSopenharmony_ci * 733eb0b6dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 833eb0b6dSopenharmony_ci * 933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and 1333eb0b6dSopenharmony_ci * limitations under the License. 1433eb0b6dSopenharmony_ci */ 1533eb0b6dSopenharmony_ci 1633eb0b6dSopenharmony_ci#ifndef FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H 1733eb0b6dSopenharmony_ci#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H 1833eb0b6dSopenharmony_ci 1933eb0b6dSopenharmony_ci#include "ark_native_engine.h" 2033eb0b6dSopenharmony_ci#include "ecmascript/napi/include/jsnapi.h" 2133eb0b6dSopenharmony_ci#include "native_engine/native_reference.h" 2233eb0b6dSopenharmony_ci#include "native_engine/native_value.h" 2333eb0b6dSopenharmony_ci 2433eb0b6dSopenharmony_ciclass ArkNativeEngine; 2533eb0b6dSopenharmony_ci 2633eb0b6dSopenharmony_ciusing EcmaVM = panda::EcmaVM; 2733eb0b6dSopenharmony_ciusing panda::Global; 2833eb0b6dSopenharmony_ciusing JSValueRef = panda::JSValueRef; 2933eb0b6dSopenharmony_ciusing panda::Local; 3033eb0b6dSopenharmony_ciusing LocalScope = panda::LocalScope; 3133eb0b6dSopenharmony_ci 3233eb0b6dSopenharmony_cienum class FinalizerState { 3333eb0b6dSopenharmony_ci DESTRUCTION, 3433eb0b6dSopenharmony_ci COLLECTION, 3533eb0b6dSopenharmony_ci}; 3633eb0b6dSopenharmony_ci 3733eb0b6dSopenharmony_ciclass ArkNativeReference : public NativeReference { 3833eb0b6dSopenharmony_cipublic: 3933eb0b6dSopenharmony_ci ArkNativeReference(ArkNativeEngine* engine, 4033eb0b6dSopenharmony_ci napi_value value, 4133eb0b6dSopenharmony_ci uint32_t initialRefcount, 4233eb0b6dSopenharmony_ci bool deleteSelf = false, 4333eb0b6dSopenharmony_ci NapiNativeFinalize napiCallback = nullptr, 4433eb0b6dSopenharmony_ci void* data = nullptr, 4533eb0b6dSopenharmony_ci void* hint = nullptr, 4633eb0b6dSopenharmony_ci bool isAsyncCall = false, 4733eb0b6dSopenharmony_ci size_t nativeBindingSize = 0); 4833eb0b6dSopenharmony_ci ArkNativeReference(ArkNativeEngine* engine, 4933eb0b6dSopenharmony_ci Local<JSValueRef> value, 5033eb0b6dSopenharmony_ci uint32_t initialRefcount, 5133eb0b6dSopenharmony_ci bool deleteSelf, 5233eb0b6dSopenharmony_ci NapiNativeFinalize napiCallback, 5333eb0b6dSopenharmony_ci void* data, 5433eb0b6dSopenharmony_ci void* hint, 5533eb0b6dSopenharmony_ci bool isAsyncCall = false, 5633eb0b6dSopenharmony_ci size_t nativeBindingSize = 0); 5733eb0b6dSopenharmony_ci ~ArkNativeReference() override; 5833eb0b6dSopenharmony_ci 5933eb0b6dSopenharmony_ci uint32_t Ref() override; 6033eb0b6dSopenharmony_ci uint32_t Unref() override; 6133eb0b6dSopenharmony_ci napi_value Get() override; 6233eb0b6dSopenharmony_ci napi_value Get(NativeEngine* engine) override; 6333eb0b6dSopenharmony_ci void* GetData() override; 6433eb0b6dSopenharmony_ci operator napi_value() override; 6533eb0b6dSopenharmony_ci void SetDeleteSelf() override; 6633eb0b6dSopenharmony_ci bool GetDeleteSelf() const override; 6733eb0b6dSopenharmony_ci uint32_t GetRefCount() override; 6833eb0b6dSopenharmony_ci bool GetFinalRun() override; 6933eb0b6dSopenharmony_ci napi_value GetNapiValue() override; 7033eb0b6dSopenharmony_ci void ResetFinalizer() override; 7133eb0b6dSopenharmony_ci 7233eb0b6dSopenharmony_ciprivate: 7333eb0b6dSopenharmony_ci inline void ArkNativeReferenceConstructor(uint32_t initialRefCount, bool deleteSelf) 7433eb0b6dSopenharmony_ci { 7533eb0b6dSopenharmony_ci if (initialRefCount == 0) { 7633eb0b6dSopenharmony_ci value_.SetWeakCallback(reinterpret_cast<void*>(this), FreeGlobalCallBack, NativeFinalizeCallBack); 7733eb0b6dSopenharmony_ci } 7833eb0b6dSopenharmony_ci 7933eb0b6dSopenharmony_ci if (deleteSelf) { 8033eb0b6dSopenharmony_ci NativeReferenceManager* referenceManager = engine_->GetReferenceManager(); 8133eb0b6dSopenharmony_ci if (referenceManager != nullptr) { 8233eb0b6dSopenharmony_ci referenceManager->CreateHandler(this); 8333eb0b6dSopenharmony_ci } 8433eb0b6dSopenharmony_ci } 8533eb0b6dSopenharmony_ci 8633eb0b6dSopenharmony_ci engineId_ = engine_->GetId(); 8733eb0b6dSopenharmony_ci } 8833eb0b6dSopenharmony_ci 8933eb0b6dSopenharmony_ci ArkNativeEngine* engine_; 9033eb0b6dSopenharmony_ci uint64_t engineId_ {0}; 9133eb0b6dSopenharmony_ci 9233eb0b6dSopenharmony_ci Global<JSValueRef> value_; 9333eb0b6dSopenharmony_ci uint32_t refCount_ {0}; 9433eb0b6dSopenharmony_ci bool deleteSelf_ {false}; 9533eb0b6dSopenharmony_ci bool isAsyncCall_ {false}; 9633eb0b6dSopenharmony_ci 9733eb0b6dSopenharmony_ci bool hasDelete_ {false}; 9833eb0b6dSopenharmony_ci bool finalRun_ {false}; 9933eb0b6dSopenharmony_ci NapiNativeFinalize napiCallback_ {nullptr}; 10033eb0b6dSopenharmony_ci void* data_ {nullptr}; 10133eb0b6dSopenharmony_ci void* hint_ {nullptr}; 10233eb0b6dSopenharmony_ci size_t nativeBindingSize_ {0}; 10333eb0b6dSopenharmony_ci 10433eb0b6dSopenharmony_ci NativeReference* prev_ {nullptr}; 10533eb0b6dSopenharmony_ci NativeReference* next_ {nullptr}; 10633eb0b6dSopenharmony_ci 10733eb0b6dSopenharmony_ci void FinalizeCallback(FinalizerState state); 10833eb0b6dSopenharmony_ci 10933eb0b6dSopenharmony_ci static void FreeGlobalCallBack(void* ref); 11033eb0b6dSopenharmony_ci static void NativeFinalizeCallBack(void* ref); 11133eb0b6dSopenharmony_ci friend class NativeReferenceManager; 11233eb0b6dSopenharmony_ci}; 11333eb0b6dSopenharmony_ci 11433eb0b6dSopenharmony_ci#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H */ 115