1 /**
2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "plugins/ets/runtime/interop_js/ets_proxy/shared_reference.h"
17 #include "plugins/ets/runtime/interop_js/interop_context.h"
18 #include "plugins/ets/runtime/interop_js/js_convert.h"
19 #include "plugins/ets/runtime/types/ets_box_primitive.h"
20 #include "runtime/mem/local_object_handle.h"
21
22 namespace ark::ets::interop::js::ets_proxy {
23
InitRef(InteropCtx * ctx,EtsObject * etsObject,napi_ref jsRef,uint32_t refIdx)24 void SharedReference::InitRef(InteropCtx *ctx, EtsObject *etsObject, napi_ref jsRef, uint32_t refIdx)
25 {
26 ASSERT(etsObject != nullptr);
27 if (!etsObject->HasInteropIndex()) {
28 etsObject->SetInteropIndex(refIdx);
29 }
30 SetETSObject(etsObject);
31 jsRef_ = jsRef;
32 ctx_ = ctx;
33 }
34
InitETSObject(InteropCtx * ctx,EtsObject * etsObject,napi_ref jsRef,uint32_t refIdx)35 void SharedReference::InitETSObject(InteropCtx *ctx, EtsObject *etsObject, napi_ref jsRef, uint32_t refIdx)
36 {
37 flags_.SetBit<SharedReferenceFlags::Bit::ETS>();
38 InitRef(ctx, etsObject, jsRef, refIdx);
39 }
40
InitJSObject(InteropCtx * ctx,EtsObject * etsObject,napi_ref jsRef,uint32_t refIdx)41 void SharedReference::InitJSObject(InteropCtx *ctx, EtsObject *etsObject, napi_ref jsRef, uint32_t refIdx)
42 {
43 flags_.SetBit<SharedReferenceFlags::Bit::JS>();
44 InitRef(ctx, etsObject, jsRef, refIdx);
45 }
46
InitHybridObject(InteropCtx * ctx,EtsObject * etsObject,napi_ref jsRef,uint32_t refIdx)47 void SharedReference::InitHybridObject(InteropCtx *ctx, EtsObject *etsObject, napi_ref jsRef, uint32_t refIdx)
48 {
49 flags_.SetBit<SharedReferenceFlags::Bit::ETS>();
50 flags_.SetBit<SharedReferenceFlags::Bit::JS>();
51 InitRef(ctx, etsObject, jsRef, refIdx);
52 }
53
MarkIfNotMarked()54 bool SharedReference::MarkIfNotMarked()
55 {
56 return flags_.SetBit<SharedReferenceFlags::Bit::MARK>();
57 }
58
operator ++()59 SharedReference::Iterator &SharedReference::Iterator::operator++()
60 {
61 uint32_t index = ref_->flags_.GetNextIndex();
62 if (index == 0U) {
63 ref_ = nullptr;
64 } else {
65 ref_ = SharedReferenceStorage::GetCurrent()->GetItemByIndex(index);
66 }
67 return *this;
68 }
69
operator ++(int)70 SharedReference::Iterator SharedReference::Iterator::operator++(int) // NOLINT(cert-dcl21-cpp)
71 {
72 auto result = *this;
73 auto index = ref_->flags_.GetNextIndex();
74 if (index == 0U) {
75 ref_ = nullptr;
76 } else {
77 ref_ = SharedReferenceStorage::GetCurrent()->GetItemByIndex(index);
78 }
79 return result;
80 }
81
82 } // namespace ark::ets::interop::js::ets_proxy
83