• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef ECMASCRIPT_SERIALIZER_VALUE_SERIALIZER_H
17 #define ECMASCRIPT_SERIALIZER_VALUE_SERIALIZER_H
18 
19 #include "ecmascript/serializer/base_serializer-inl.h"
20 
21 namespace panda::ecmascript {
22 
23 class ValueSerializer : public BaseSerializer {
24 public:
25     explicit ValueSerializer(JSThread *thread, bool defaultTransfer = false, bool defaultCloneShared = false)
BaseSerializer(thread)26         : BaseSerializer(thread), defaultTransfer_(defaultTransfer), defaultCloneShared_(defaultCloneShared) {}
~ValueSerializer()27     ~ValueSerializer() override
28     {
29         // clear transfer obj set after serialization
30         transferDataSet_.clear();
31         cloneArrayBufferSet_.clear();
32         cloneSharedSet_.clear();
33     }
34     NO_COPY_SEMANTIC(ValueSerializer);
35     NO_MOVE_SEMANTIC(ValueSerializer);
36 
37     bool WriteValue(JSThread *thread, const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &transfer,
38                     const JSHandle<JSTaggedValue> &cloneList);
39 
40 protected:
41     virtual bool CheckObjectCanSerialize(TaggedObject *object, bool &findSharedObject);
GetMaxJSSerializerSize(EcmaVM * vm)42     virtual size_t GetMaxJSSerializerSize(EcmaVM *vm)
43     {
44         return vm->GetEcmaParamConfiguration().GetMaxJSSerializerSize();
45     }
46 
47 private:
48     virtual bool SerializeSharedObj([[maybe_unused]] TaggedObject *object);
49     void SerializeJSError(TaggedObject *object);
50     void SerializeNativeBindingObject(TaggedObject *object);
51     bool SerializeJSArrayBufferPrologue(TaggedObject *object);
52     void SerializeJSSharedArrayBufferPrologue(TaggedObject *object);
53     void SerializeJSSendableArrayBufferPrologue(TaggedObject *object);
54     void SerializeJSRegExpPrologue(JSRegExp *jsRegExp);
55     bool PrepareTransfer(JSThread *thread, const JSHandle<JSTaggedValue> &transfer);
56     bool PrepareClone(JSThread *thread, const JSHandle<JSTaggedValue> &cloneList);
IsInternalJSType(JSType type)57     bool IsInternalJSType(JSType type)
58     {
59         if ((type >= JSType::JS_RECORD_FIRST && type <= JSType::JS_RECORD_LAST) ||
60             (type == JSType::JS_NATIVE_POINTER && !supportJSNativePointer_)) {
61             return false;
62         }
63         return type >= JSType::HCLASS && type <= JSType::TYPE_LAST && type != JSType::SYMBOL;
64     }
65 
PrintAndRecordErrorMessage(const std::string & errorMessage)66     void PrintAndRecordErrorMessage(const std::string &errorMessage)
67     {
68         LOG_ECMA(ERROR) << errorMessage;
69         data_->SetErrorMessage(errorMessage);
70     }
71 
72     // process SourceTextModule fields
73     bool SerializeModuleCNativeObjects(TaggedObject *object);
74 protected:
75     void SerializeObjectImpl(TaggedObject *object, bool isWeak = false) override;
76     bool notSupport_ {false};
77 
78 private:
79     bool defaultTransfer_ {false};
80     bool defaultCloneShared_ {false};
81     bool supportJSNativePointer_ {false};
82     std::vector<std::pair<ssize_t, panda::JSNApi::NativeBindingInfo *>> detachCallbackInfo_;
83     CUnorderedSet<uintptr_t> transferDataSet_;
84     CUnorderedSet<uintptr_t> cloneArrayBufferSet_;
85     CUnorderedSet<uintptr_t> cloneSharedSet_;
86 };
87 }
88 
89 #endif  // ECMASCRIPT_SERIALIZER_BASE_SERIALIZER_H