1 /*
2 * Copyright (c) 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 "ecmascript/serializer/inter_op_value_deserializer.h"
17
18 #include "ecmascript/checkpoint/thread_state_transition.h"
19
20 namespace panda::ecmascript {
DerivedExtraReadSingleEncodeData(uint8_t encodeFlag,uintptr_t objAddr,size_t fieldOffset)21 size_t InterOpValueDeserializer::DerivedExtraReadSingleEncodeData(uint8_t encodeFlag, uintptr_t objAddr,
22 size_t fieldOffset)
23 {
24 size_t handledFieldSize = sizeof(JSTaggedType);
25 ObjectSlot slot(objAddr + fieldOffset);
26 switch (encodeFlag) {
27 case (uint8_t)EncodeFlag::XREF_BINDING_OBJECT: {
28 slot.Update(JSTaggedValue::Undefined().GetRawData());
29 AttachXRefFunc af = reinterpret_cast<AttachXRefFunc>(data_->ReadJSTaggedType(position_));
30 void *attachXRefData = reinterpret_cast<void *>(data_->ReadJSTaggedType(position_));
31 JSHandle<JSTaggedValue> obj(thread_, JSTaggedValue(static_cast<JSTaggedType>(objAddr)));
32 // defer new xref binding object until deserialize finish
33 xRefBindingAttachInfos_.emplace_back(af, attachXRefData, obj, fieldOffset);
34 break;
35 }
36 default:
37 LOG_ECMA(FATAL) << "this branch is unreachable " << static_cast<int>(encodeFlag);
38 UNREACHABLE();
39 }
40 return handledFieldSize;
41 }
42
DeserializeSpecialRecordedObjects()43 void InterOpValueDeserializer::DeserializeSpecialRecordedObjects()
44 {
45 for (auto &info : xRefBindingAttachInfos_) {
46 DeserializeXRefBindingObject(&info);
47 }
48 }
49
DeserializeXRefBindingObject(XRefBindingAttachInfo * info)50 void InterOpValueDeserializer::DeserializeXRefBindingObject(XRefBindingAttachInfo *info)
51 {
52 [[maybe_unused]] EcmaHandleScope scope(thread_);
53 AttachXRefFunc af = info->af_;
54 void *attachXRefData = info->attachXRefData_;
55 Local<JSValueRef> attachVal;
56 {
57 ThreadNativeScope nativeScope(thread_);
58 attachVal = af(engine_, attachXRefData);
59 }
60 if (attachVal.IsEmpty()) {
61 LOG_ECMA(ERROR) << "NativeBindingObject is empty";
62 attachVal = JSValueRef::Undefined(thread_->GetEcmaVM());
63 }
64 JSTaggedType res = JSNApiHelper::ToJSHandle(attachVal).GetTaggedType();
65 ObjectSlot slot = info->GetSlot();
66 slot.Update(res);
67 if (!JSTaggedValue(res).IsInvalidValue()) {
68 WriteBarrier(thread_, reinterpret_cast<void *>(info->GetObjAddr()), info->GetFieldOffset(), res);
69 }
70 }
71 } // namespace panda::ecmascript
72
73