1 /*
2  * Copyright (c) 2022 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_JS_PANDAFILE_EXECUTOR_H
17 #define ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_EXECUTOR_H
18 
19 #include "ecmascript/ecma_context.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/js_thread.h"
22 #include "ecmascript/jspandafile/js_pandafile.h"
23 #include "ecmascript/module/js_module_source_text.h"
24 
25 namespace panda::ecmascript {
26 class JSPandaFileExecutor {
27 public:
28     static constexpr int ROUTE_SUCCESS = 0;
29     static constexpr int ROUTE_INTERNAL_ERROR = 100001;
30     static constexpr int ROUTE_URI_ERROR = 100002;
31 
32     std::pair<CString, CString> ParseEntryPointWithUnifiedRules(EcmaVM *vm, const CString &filename,
33                                                                 std::string_view entryPoint);
34     static std::pair<CString, CString> ParseAbcEntryPoint(JSThread *thread, const CString &filename,
35                                                           [[maybe_unused]] std::string_view entryPoint);
36     static Expected<JSTaggedValue, bool> ExecuteFromFile(JSThread *thread, const CString &name,
37                                                          CString entry, bool needUpdate = false,
38                                                          bool executeFromJob = false);
39     static Expected<JSTaggedValue, bool> ExecuteFromAbsolutePathAbcFile(JSThread *thread, const CString &filename,
40                                                                         std::string_view entryPoint,
41                                                                         bool needUpdate = false,
42                                                                         bool executeFromJob = false);
43     static Expected<JSTaggedValue, bool> ExecuteFromAbcFile(JSThread *thread, const CString &filename,
44                                                             std::string_view entryPoint, bool needUpdate = false,
45                                                             bool executeFromJob = false);
46     static Expected<JSTaggedValue, bool> ExecuteFromBuffer(JSThread *thread, const void *buffer, size_t size,
47                                                            std::string_view entryPoint, const CString &filename = "",
48                                                            bool needUpdate = false, bool executeFromJob = false);
49     static Expected<JSTaggedValue, bool> ExecuteModuleBuffer(JSThread *thread, const void *buffer, size_t size,
50                                                              const CString &filename = "", bool needUpdate = false);
51     static Expected<JSTaggedValue, bool> CommonExecuteBuffer(JSThread *thread, bool isBundle, const CString &filename,
52                                                              const CString &entry, const void *buffer, size_t size,
53                                                              bool executeFromJob = false);
54     static Expected<JSTaggedValue, bool> Execute(JSThread *thread, const JSPandaFile *jsPandaFile,
55                                                  std::string_view entryPoint, bool executeFromJob = false);
56     static void BindPreloadedPandaFilesToAOT(EcmaVM *vm, const std::string &moduleName);
57     static void BindPandaFileToAot(JSPandaFile *jsPandaFile);
58     static Expected<JSTaggedValue, bool> ExecuteSpecialModule(JSThread *thread, const CString &recordName,
59                                                               const CString &filename, const JSPandaFile *jsPandaFile,
60                                                               const JSRecordInfo* recordInfo);
61     static Expected<JSTaggedValue, bool> LazyExecuteModule(JSThread *thread, CString &recordName,
62                                                            const CString &filename, bool isMergedAbc);
63     // Execute from secure mem
64     static Expected<JSTaggedValue, bool> ExecuteFromBufferSecure(JSThread *thread, uint8_t *buffer, size_t size,
65                                                                  std::string_view entryPoint,
66                                                                  const CString &filename = "", bool needUpdate = false);
67     static Expected<JSTaggedValue, bool> ExecuteModuleBufferSecure(JSThread *thread, uint8_t *buffer, size_t size,
68                                                                    const CString &filename = "",
69                                                                    bool needUpdate = false);
70     static Expected<JSTaggedValue, bool> CommonExecuteBuffer(JSThread *thread, const CString &filename,
71                                                              const CString &entry, const JSPandaFile *jsPandaFile);
72     static Expected<JSTaggedValue, bool> ExecuteSecureWithOhmUrl(JSThread *thread, uint8_t *buffer,
73                                                                  size_t size, const CString &fileName,
74                                                                  const CString &entryPoint);
75 
76     static int ExecuteAbcFileWithSingletonPatternFlag(JSThread *thread,
77         [[maybe_unused]] const CString &bundleName, const CString &moduleName, const CString &entry,
78         bool isSingletonPattern);
79 
80     static bool IsExecuteModuleInAbcFile(JSThread *thread, [[maybe_unused]] const CString &bundleName,
81         const CString &moduleName, const CString &entry);
82 };
83 }  // namespace panda::ecmascript
84 #endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_EXECUTOR_H
85