14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_EXECUTOR_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_EXECUTOR_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_context.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
224514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/js_pandafile.h"
234514f5e3Sopenharmony_ci#include "ecmascript/module/js_module_source_text.h"
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda::ecmascript {
264514f5e3Sopenharmony_ciclass JSPandaFileExecutor {
274514f5e3Sopenharmony_cipublic:
284514f5e3Sopenharmony_ci    static constexpr int ROUTE_SUCCESS = 0;
294514f5e3Sopenharmony_ci    static constexpr int ROUTE_INTERNAL_ERROR = 100001;
304514f5e3Sopenharmony_ci    static constexpr int ROUTE_URI_ERROR = 100002;
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ci    std::pair<CString, CString> ParseEntryPointWithUnifiedRules(EcmaVM *vm, const CString &filename,
334514f5e3Sopenharmony_ci                                                                std::string_view entryPoint);
344514f5e3Sopenharmony_ci    static std::pair<CString, CString> ParseAbcEntryPoint(JSThread *thread, const CString &filename,
354514f5e3Sopenharmony_ci                                                          [[maybe_unused]] std::string_view entryPoint);
364514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteFromFile(JSThread *thread, const CString &name,
374514f5e3Sopenharmony_ci                                                         CString entry, bool needUpdate = false,
384514f5e3Sopenharmony_ci                                                         bool executeFromJob = false);
394514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteFromAbsolutePathAbcFile(JSThread *thread, const CString &filename,
404514f5e3Sopenharmony_ci                                                                        std::string_view entryPoint,
414514f5e3Sopenharmony_ci                                                                        bool needUpdate = false,
424514f5e3Sopenharmony_ci                                                                        bool executeFromJob = false);
434514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteFromAbcFile(JSThread *thread, const CString &filename,
444514f5e3Sopenharmony_ci                                                            std::string_view entryPoint, bool needUpdate = false,
454514f5e3Sopenharmony_ci                                                            bool executeFromJob = false);
464514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteFromBuffer(JSThread *thread, const void *buffer, size_t size,
474514f5e3Sopenharmony_ci                                                           std::string_view entryPoint, const CString &filename = "",
484514f5e3Sopenharmony_ci                                                           bool needUpdate = false, bool executeFromJob = false);
494514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteModuleBuffer(JSThread *thread, const void *buffer, size_t size,
504514f5e3Sopenharmony_ci                                                             const CString &filename = "", bool needUpdate = false);
514514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> CommonExecuteBuffer(JSThread *thread, bool isBundle, const CString &filename,
524514f5e3Sopenharmony_ci                                                             const CString &entry, const void *buffer, size_t size,
534514f5e3Sopenharmony_ci                                                             bool executeFromJob = false);
544514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> Execute(JSThread *thread, const JSPandaFile *jsPandaFile,
554514f5e3Sopenharmony_ci                                                 std::string_view entryPoint, bool executeFromJob = false);
564514f5e3Sopenharmony_ci    static void BindPreloadedPandaFilesToAOT(EcmaVM *vm, const std::string &moduleName);
574514f5e3Sopenharmony_ci    static void BindPandaFileToAot(JSPandaFile *jsPandaFile);
584514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteSpecialModule(JSThread *thread, const CString &recordName,
594514f5e3Sopenharmony_ci                                                              const CString &filename, const JSPandaFile *jsPandaFile,
604514f5e3Sopenharmony_ci                                                              const JSRecordInfo* recordInfo);
614514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> LazyExecuteModule(JSThread *thread, CString &recordName,
624514f5e3Sopenharmony_ci                                                           const CString &filename, bool isMergedAbc);
634514f5e3Sopenharmony_ci    // Execute from secure mem
644514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteFromBufferSecure(JSThread *thread, uint8_t *buffer, size_t size,
654514f5e3Sopenharmony_ci                                                                 std::string_view entryPoint,
664514f5e3Sopenharmony_ci                                                                 const CString &filename = "", bool needUpdate = false);
674514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteModuleBufferSecure(JSThread *thread, uint8_t *buffer, size_t size,
684514f5e3Sopenharmony_ci                                                                   const CString &filename = "",
694514f5e3Sopenharmony_ci                                                                   bool needUpdate = false);
704514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> CommonExecuteBuffer(JSThread *thread, const CString &filename,
714514f5e3Sopenharmony_ci                                                             const CString &entry, const JSPandaFile *jsPandaFile);
724514f5e3Sopenharmony_ci    static Expected<JSTaggedValue, bool> ExecuteSecureWithOhmUrl(JSThread *thread, uint8_t *buffer,
734514f5e3Sopenharmony_ci                                                                 size_t size, const CString &fileName,
744514f5e3Sopenharmony_ci                                                                 const CString &entryPoint);
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci    static int ExecuteAbcFileWithSingletonPatternFlag(JSThread *thread,
774514f5e3Sopenharmony_ci        [[maybe_unused]] const CString &bundleName, const CString &moduleName, const CString &entry,
784514f5e3Sopenharmony_ci        bool isSingletonPattern);
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    static bool IsExecuteModuleInAbcFile(JSThread *thread, [[maybe_unused]] const CString &bundleName,
814514f5e3Sopenharmony_ci        const CString &moduleName, const CString &entry);
824514f5e3Sopenharmony_ci};
834514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
844514f5e3Sopenharmony_ci#endif // ECMASCRIPT_JSPANDAFILE_JS_PANDAFILE_EXECUTOR_H
85