1/*
2 * Copyright (c) 2021 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_JSPANDAFILE_LITERAL_DATA_EXTRACTOR_H
17#define ECMASCRIPT_JSPANDAFILE_LITERAL_DATA_EXTRACTOR_H
18
19#include "ecmascript/jspandafile/js_pandafile.h"
20#include "ecmascript/js_tagged_value-inl.h"
21#include "libpandafile/literal_data_accessor-inl.h"
22
23namespace panda::ecmascript {
24class LiteralDataExtractor {
25public:
26    LiteralDataExtractor() = default;
27    virtual ~LiteralDataExtractor() = default;
28
29    DEFAULT_NOEXCEPT_MOVE_SEMANTIC(LiteralDataExtractor);
30    DEFAULT_COPY_SEMANTIC(LiteralDataExtractor);
31
32    static constexpr uint32_t LITERALARRAY_VALUE_LOW_BOUNDARY = 100;
33
34    // Parameter 'size_t index' is for old isa, and 'EntityId id' for new isa.
35    static void ExtractObjectDatas(JSThread *thread, const JSPandaFile *jsPandaFile, size_t index,
36                                   JSMutableHandle<TaggedArray> elements, JSMutableHandle<TaggedArray> properties,
37                                   JSHandle<ConstantPool> constpool, const CString &entryPoint = "");
38    static PUBLIC_API void ExtractObjectDatas(JSThread *thread, const JSPandaFile *jsPandaFile, EntityId id,
39                                              JSMutableHandle<TaggedArray> elements,
40                                              JSMutableHandle<TaggedArray> properties,
41                                              JSHandle<ConstantPool> constpool, const CString &entryPoint = "",
42                                              bool isLoadedAOT = false,
43                                              JSHandle<AOTLiteralInfo> entryIndexes = JSHandle<AOTLiteralInfo>());
44
45    static JSHandle<TaggedArray> GetDatasIgnoreType(JSThread *thread, const JSPandaFile *jsPandaFile, size_t index,
46                                                    JSHandle<ConstantPool> constpool, const CString &entryPoint = "");
47    static PUBLIC_API JSHandle<TaggedArray> GetDatasIgnoreType(JSThread *thread, const JSPandaFile *jsPandaFile,
48        EntityId id, JSHandle<ConstantPool> constpool, const CString &entryPoint = "",
49        bool isLoadedAOT = false, JSHandle<AOTLiteralInfo> entryIndexes = JSHandle<AOTLiteralInfo>(),
50        ElementsKind *newKind = nullptr, JSHandle<JSTaggedValue> sendableEnv = JSHandle<JSTaggedValue>(),
51        ClassKind classKind = ClassKind::NON_SENDABLE);
52    static PUBLIC_API JSHandle<TaggedArray> GetDatasIgnoreTypeForClass(JSThread *thread, const JSPandaFile *jsPandaFile,
53        EntityId id, JSHandle<ConstantPool> constpool, const CString &entryPoint = "",
54        bool isLoadedAOT = false, JSHandle<AOTLiteralInfo> entryIndexes = JSHandle<AOTLiteralInfo>(),
55        ElementsKind *newKind = nullptr, JSHandle<JSTaggedValue> sendableEnv = JSHandle<JSTaggedValue>(),
56        ClassKind classKind = ClassKind::NON_SENDABLE);
57
58    static JSHandle<TaggedArray> GetDatasIgnoreTypeForClass(JSThread *thread, const JSPandaFile *jsPandaFile,
59                                                            size_t index, JSHandle<ConstantPool> constpool,
60                                                            const CString &entryPoint = "");
61
62    static JSHandle<JSFunction> DefineMethodInLiteral(JSThread *thread, const JSPandaFile *jsPandaFile,
63                                                      uint32_t offset, JSHandle<ConstantPool> constpool,
64                                                      FunctionKind kind, uint16_t length,
65                                                      const CString &entryPoint = "",
66                                                      bool isLoadedAOT = false, uint32_t entryIndex = 0,
67                                                      JSHandle<JSTaggedValue> sendableEnv = JSHandle<JSTaggedValue>(),
68                                                      ClassKind classKind = ClassKind::NON_SENDABLE);
69
70    static void PUBLIC_API GetMethodOffsets(const JSPandaFile *jsPandaFile, size_t index,
71                                            std::vector<uint32_t> &methodOffsets);
72
73    static void PUBLIC_API GetMethodOffsets(const JSPandaFile *jsPandaFile, EntityId id,
74                                            std::vector<uint32_t> &methodOffsets);
75private:
76    static JSHandle<FunctionTemplate> DefineFunctionTemplate(JSThread *thread, const JSPandaFile *jsPandaFile,
77        uint32_t offset, JSHandle<ConstantPool> constpool, FunctionKind kind, uint16_t length,
78        const CString &entryPoint = "", bool isLoadedAOT = false, uint32_t entryIndex = 0,
79        JSHandle<JSTaggedValue> sendableEnv = JSHandle<JSTaggedValue>(), ClassKind classKind = ClassKind::NON_SENDABLE);
80
81    static JSHandle<JSFunction> CreateJSFunctionInLiteral(EcmaVM *vm, const JSHandle<Method> &method,
82                                                          FunctionKind kind, ClassKind classKind);
83    static JSHandle<TaggedArray> EnumerateLiteralVals(JSThread *thread, panda_file::LiteralDataAccessor &lda,
84        const JSPandaFile *jsPandaFile, size_t index, JSHandle<ConstantPool> constpool,
85        const CString &entryPoint = "");
86};
87}  // namespace panda::ecmascript
88#endif  // ECMASCRIPT_JSPANDAFILE_LITERAL_DATA_EXTRACTOR_H
89