• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef ECMASCRIPT_PANDAFILE_JS_PANDAFILE_SNAPSHOT_H
16 #define ECMASCRIPT_PANDAFILE_JS_PANDAFILE_SNAPSHOT_H
17 
18 #include "common_components/taskpool/task.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/jspandafile/js_pandafile.h"
21 
22 namespace panda::ecmascript {
23 class JSPandaFileSnapshot {
24 public:
25     static constexpr std::string_view JSPANDAFILE_FILE_NAME = "_Pandafile.ams";
26     static constexpr std::string_view SNAPSHOT_FILE_SUFFIX = ".ams"; // ark module snapshot
27 
28     static void PostWriteDataToFileJob(const EcmaVM *vm, const CString &path, const CString &version);
29     static bool ReadData(JSThread *thread, JSPandaFile *jsPandaFile, const CString &path, const CString &version);
30 
31 protected:
32     static bool IsJSPandaFileSnapshotFileExist(const CString &fileName, const CString &path);
33     static CString GetJSPandaFileFileName(const CString &fileName, const CString &path);
34     static void RemoveSnapshotFiles(const CString &path);
35 
36 // JSPandaFile snapshot layout
37 // +---------------------------------+<-------- BaseInfo
38 // |    Application Version Code     |
39 // +---------------------------------+
40 // |   System Version Code Length    |
41 // |      System Version Code        |
42 // +---------------------------------+
43 // |        JSPandaFile Size         |
44 // +---------------------------------+
45 // |           moduleName            |
46 // +---------------------------------+<-------- MethodLiterals
47 // |           numMethods            |
48 // +---------------------------------+
49 // |          MethodLiteral          |
50 // |               ...               |
51 // +---------------------------------+<-------- MainMethodIndex
52 // |      MainMethodIndex size       |
53 // +---------------------------------+
54 // |         MainMethodIndex         |
55 // |        RecordName Lenth         |
56 // |         RecordName ptr          |
57 // |               ...               |
58 // +---------------------------------+<-------- CheckSum
59 // |             CheckSum            |
60 // +---------------------------------+
61     static bool WriteDataToFile(JSThread *thread, JSPandaFile *jsPandaFile, const CString &path,
62         const CString &version);
63     static bool ReadDataFromFile(JSThread *thread, JSPandaFile *jsPandaFile, const CString &path,
64         const CString &version);
65 
66     class JSPandaFileSnapshotTask : public common::Task {
67     public:
JSPandaFileSnapshotTask(int32_t id,JSThread * thread,JSPandaFile * jsPandaFile,const CString & path,const CString & version)68         JSPandaFileSnapshotTask(int32_t id, JSThread *thread, JSPandaFile *jsPandaFile, const CString &path,
69             const CString &version) : Task(id), thread_(thread), jsPandaFile_(jsPandaFile), path_(path),
70             version_(version) {}
71         ~JSPandaFileSnapshotTask() override = default;
72         bool Run(uint32_t threadIndex) override;
73 
74         NO_COPY_SEMANTIC(JSPandaFileSnapshotTask);
75         NO_MOVE_SEMANTIC(JSPandaFileSnapshotTask);
76 
77     private:
78         JSThread *thread_ {nullptr};
79         JSPandaFile *jsPandaFile_ {nullptr};
80         CString path_ {};
81         CString version_ {};
82     };
83 };
84 }  // namespace panda::ecmascript
85 #endif // ECMASCRIPT_PANDAFILE_JS_PANDAFILE_SNAPSHOT_H