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 #include <chrono>
17 #include <iostream>
18 #include <iterator>
19 #include <ostream>
20 #include <signal.h> // NOLINTNEXTLINE(modernize-deprecated-headers)
21 #include <vector>
22
23 #include "ecmascript/base/string_helper.h"
24 #include "ecmascript/ecma_string.h"
25 #include "ecmascript/ecma_vm.h"
26 #include "ecmascript/js_runtime_options.h"
27 #include "ecmascript/jspandafile/js_pandafile.h"
28 #include "ecmascript/log.h"
29 #include "ecmascript/mem/mem_controller.h"
30 #include "ecmascript/napi/include/jsnapi.h"
31
32 namespace panda::ecmascript {
33 using PatchErrorCode = panda::JSNApi::PatchErrorCode;
34
35 const std::string TEST_ENTRY_POINT = "test";
36 const std::string RETEST_ENTRY_POINT = "retest";
37
BlockSignals()38 void BlockSignals()
39 {
40 #if defined(PANDA_TARGET_UNIX)
41 sigset_t set;
42 if (sigemptyset(&set) == -1) {
43 LOG_ECMA(ERROR) << "sigemptyset failed";
44 return;
45 }
46 #endif // PANDA_TARGET_UNIX
47 }
48
GetHelper()49 std::string GetHelper()
50 {
51 std::string str;
52 str.append(COMMON_HELP_HEAD_MSG);
53 str.append(HELP_OPTION_MSG);
54 return str;
55 }
56
Main(const int argc,const char ** argv)57 int Main(const int argc, const char **argv)
58 {
59 auto startTime =
60 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
61 .count();
62
63 BlockSignals();
64
65 if (argc < 2) { // 2: at least have two arguments
66 std::cout << GetHelper();
67 return -1;
68 }
69
70 int newArgc = argc;
71 std::string files = argv[argc - 1];
72 if (!base::StringHelper::EndsWith(files, ".abc")) {
73 std::cout << "The last argument must be abc file" << std::endl;
74 std::cout << GetHelper();
75 return 1;
76 }
77
78 newArgc--;
79 JSRuntimeOptions runtimeOptions;
80 bool retOpt = runtimeOptions.ParseCommand(newArgc, argv);
81 if (!retOpt) {
82 std::cout << GetHelper();
83 return 1;
84 }
85
86 if (runtimeOptions.IsStartupTime()) {
87 std::cout << "\n"
88 << "Startup start time: " << startTime << std::endl;
89 }
90 EcmaVM *vm = JSNApi::CreateEcmaVM(runtimeOptions);
91 if (vm == nullptr) {
92 std::cout << "Cannot Create vm" << std::endl;
93 return -1;
94 }
95
96 {
97 std::cout << "QuickFix Execute start" << std::endl;
98 LocalScope scope(vm);
99 std::string entry = runtimeOptions.GetEntryPoint();
100
101 #if defined(PANDA_TARGET_WINDOWS)
102 arg_list_t fileNames = base::StringHelper::SplitString(files, ";");
103 #else
104 arg_list_t fileNames = base::StringHelper::SplitString(files, ":");
105 #endif
106 uint32_t len = fileNames.size();
107 if (len < 4) { // 4: four abc file
108 std::cout << "Must include base.abc, patch.abc, test.abc, retest.abc absolute path" << std::endl;
109 JSNApi::DestroyJSVM(vm);
110 return -1;
111 }
112 std::string baseFileName = fileNames[0];
113 bool isMergeAbc = runtimeOptions.GetMergeAbc();
114 JSNApi::SetBundle(vm, !isMergeAbc);
115
116 arg_list_t entryList = base::StringHelper::SplitString(entry, ":");
117 uint32_t size = entryList.size();
118 uint32_t entryNum = 1; // 1: one entry, excluding coldpatch testcases.
119 if (size < entryNum) { // 1: one entry
120 std::cout << "Must include 1 entry at least" << std::endl;
121 JSNApi::DestroyJSVM(vm);
122 return -1;
123 }
124
125 bool res = false;
126 if (size == entryNum) {
127 res = JSNApi::Execute(vm, baseFileName, entryList[0]);
128 if (!res) {
129 std::cout << "Cannot execute panda file '" << baseFileName <<
130 "' with entry '" << entry << "'" << std::endl;
131 JSNApi::DestroyJSVM(vm);
132 return -1;
133 }
134 }
135 JSNApi::EnableUserUncaughtErrorHandler(vm);
136
137 std::string testLoadFileName = fileNames[1];
138 std::string testUnloadFileName = fileNames[2]; // 2: third file, test unloadpatch abc file.
139 for (uint32_t i = 3; i < len; i++) { // 3: patch file, test unloadpatch abc file.
140 std::string patchFileName = fileNames[i];
141 std::cout << "QuickFix start load patch" << std::endl;
142 auto result = JSNApi::LoadPatch(vm, patchFileName, baseFileName);
143 if (result != PatchErrorCode::SUCCESS) {
144 std::cout << "LoadPatch failed"<< std::endl;
145 break;
146 }
147 std::cout << "QuickFix load patch success" << std::endl;
148
149 if (size > entryNum) {
150 res = JSNApi::Execute(vm, baseFileName, entryList[0]);
151 }
152
153 res = JSNApi::Execute(vm, testLoadFileName, TEST_ENTRY_POINT);
154 if (!res) {
155 std::cout << "Cannot execute panda file '" << testLoadFileName
156 << "' with entry '" << entry << "'" << std::endl;
157 break;
158 }
159
160 if (size == entryNum) {
161 std::cout << "QuickFix start check exception" << std::endl;
162 Local<ObjectRef> exception = JSNApi::GetAndClearUncaughtException(vm);
163 res = JSNApi::IsQuickFixCausedException(vm, exception, patchFileName);
164 if (res) {
165 std::cout << "QuickFix have exception" << std::endl;
166 } else {
167 std::cout << "QuickFix have no exception" << std::endl;
168 }
169
170 std::cout << "QuickFix start unload patch" << std::endl;
171 result = JSNApi::UnloadPatch(vm, patchFileName);
172 if (result != PatchErrorCode::SUCCESS) {
173 std::cout << "UnloadPatch failed!" << std::endl;
174 break;
175 }
176 std::cout << "QuickFix unload patch success" << std::endl;
177
178 res = JSNApi::Execute(vm, testUnloadFileName, RETEST_ENTRY_POINT);
179 if (!res) {
180 std::cout << "Cannot execute panda file '" << testUnloadFileName
181 << "' with entry '" << entry << "'" << std::endl;
182 break;
183 }
184 }
185 }
186 std::cout << "QuickFix Execute end" << std::endl;
187 }
188
189 JSNApi::DestroyJSVM(vm);
190 return 0;
191 }
192 } // namespace panda::ecmascript
193
main(int argc,const char ** argv)194 int main(int argc, const char **argv)
195 {
196 return panda::ecmascript::Main(argc, argv);
197 }
198