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
21namespace panda::ecmascript {
22
23class ValueSerializer : public BaseSerializer {
24public:
25    explicit ValueSerializer(JSThread *thread, bool defaultTransfer = false, bool defaultCloneShared = false)
26        : BaseSerializer(thread),  defaultTransfer_(defaultTransfer), defaultCloneShared_(defaultCloneShared) {}
27    ~ValueSerializer()
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
40private:
41    void SerializeObjectImpl(TaggedObject *object, bool isWeak = false) override;
42    void SerializeJSError(TaggedObject *object);
43    void SerializeNativeBindingObject(TaggedObject *object);
44    bool SerializeJSArrayBufferPrologue(TaggedObject *object);
45    void SerializeJSSharedArrayBufferPrologue(TaggedObject *object);
46    void SerializeJSSendableArrayBufferPrologue(TaggedObject *object);
47    void SerializeJSRegExpPrologue(JSRegExp *jsRegExp);
48    bool PrepareTransfer(JSThread *thread, const JSHandle<JSTaggedValue> &transfer);
49    bool PrepareClone(JSThread *thread, const JSHandle<JSTaggedValue> &cloneList);
50    bool CheckObjectCanSerialize(TaggedObject *object, bool &findSharedObject);
51    bool IsInternalJSType(JSType type)
52    {
53        if ((type >= JSType::JS_RECORD_FIRST && type <= JSType::JS_RECORD_LAST) ||
54            (type == JSType::JS_NATIVE_POINTER && !supportJSNativePointer_)) {
55            return false;
56        }
57        return type >= JSType::HCLASS && type <= JSType::TYPE_LAST && type != JSType::SYMBOL;
58    }
59
60private:
61    bool defaultTransfer_ {false};
62    bool defaultCloneShared_ {false};
63    bool notSupport_ {false};
64    bool supportJSNativePointer_ {false};
65    std::vector<std::pair<ssize_t, panda::JSNApi::NativeBindingInfo *>> detachCallbackInfo_;
66    CUnorderedSet<uintptr_t> transferDataSet_;
67    CUnorderedSet<uintptr_t> cloneArrayBufferSet_;
68    CUnorderedSet<uintptr_t> cloneSharedSet_;
69};
70}
71
72#endif  // ECMASCRIPT_SERIALIZER_BASE_SERIALIZER_H