• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits>
20 #include <ostream>
21 #include <signal.h>  // NOLINTNEXTLINE(modernize-deprecated-headers)
22 #include <vector>
23 
24 #include "ecmascript/base/string_helper.h"
25 #include "ecmascript/ecma_string.h"
26 #include "ecmascript/ecma_vm.h"
27 #include "ecmascript/js_runtime_options.h"
28 #include "ecmascript/jspandafile/js_pandafile.h"
29 #include "ecmascript/log.h"
30 #include "ecmascript/mem/mem_controller.h"
31 #include "ecmascript/napi/include/jsnapi.h"
32 
33 namespace panda::ecmascript {
34 const std::string TEST_ENTRY_POINT = "test";
35 const std::string RETEST_ENTRY_POINT = "retest";
36 
BlockSignals()37 void BlockSignals()
38 {
39 #if defined(PANDA_TARGET_UNIX)
40     sigset_t set;
41     if (sigemptyset(&set) == -1) {
42         LOG_ECMA(ERROR) << "sigemptyset failed";
43         return;
44     }
45 #endif  // PANDA_TARGET_UNIX
46 }
47 
GetHelper()48 std::string GetHelper()
49 {
50     std::string str;
51     str.append(COMMON_HELP_HEAD_MSG);
52     str.append(HELP_OPTION_MSG);
53     str.append(HELP_TAIL_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, test1.abc, test2.abc absolute path" << std::endl;
109             return -1;
110         }
111         std::string baseFileName = fileNames[0];
112         bool isMergeAbc = runtimeOptions.GetMergeAbc();
113         JSNApi::SetBundle(vm, !isMergeAbc);
114         auto res = JSNApi::Execute(vm, baseFileName, entry);
115         if (!res) {
116             std::cout << "Cannot execute panda file '" << baseFileName << "' with entry '" << entry << "'" << std::endl;
117             return -1;
118         }
119         JSNApi::EnableUserUncaughtErrorHandler(vm);
120 
121         std::string testLoadFileName = fileNames[1];
122         std::string testUnloadFileName = fileNames[2]; // 2: third file, test unloadpatch abc file.
123         for (uint32_t i = 3; i < len; i++) { // 3: patch file, test unloadpatch abc file.
124             std::string patchFileName = fileNames[i];
125             std::cout << "QuickFix start load patch" << std::endl;
126             res = JSNApi::LoadPatch(vm, patchFileName, baseFileName);
127             if (!res) {
128                 std::cout << "LoadPatch failed"<< std::endl;
129                 return -1;
130             }
131             std::cout << "QuickFix load patch success" << std::endl;
132 
133             res = JSNApi::Execute(vm, testLoadFileName, TEST_ENTRY_POINT);
134             if (!res) {
135                 std::cout << "Cannot execute panda file '" << testLoadFileName
136                         << "' with entry '" << entry << "'" << std::endl;
137                 return -1;
138             }
139 
140             std::cout << "QuickFix start check exception" << std::endl;
141             Local<ObjectRef> exception = JSNApi::GetAndClearUncaughtException(vm);
142             res = JSNApi::IsQuickFixCausedException(vm, exception, patchFileName);
143             if (res) {
144                 std::cout << "QuickFix have exception." << std::endl;
145             } else {
146                 std::cout << "QuickFix have no exception" << std::endl;
147             }
148 
149             std::cout << "QuickFix start unload patch" << std::endl;
150             res = JSNApi::UnloadPatch(vm, patchFileName);
151             if (!res) {
152                 std::cout << "UnloadPatch failed!" << std::endl;
153                 return -1;
154             }
155             std::cout << "QuickFix unload patch success" << std::endl;
156 
157             res = JSNApi::Execute(vm, testUnloadFileName, RETEST_ENTRY_POINT);
158             if (!res) {
159                 std::cout << "Cannot execute panda file '" << testUnloadFileName
160                         << "' with entry '" << entry << "'" << std::endl;
161                 return -1;
162             }
163         }
164         std::cout << "QuickFix Execute end" << std::endl;
165     }
166 
167     JSNApi::DestroyJSVM(vm);
168     return 0;
169 }
170 }  // namespace panda::ecmascript
171 
main(int argc,const char ** argv)172 int main(int argc, const char **argv)
173 {
174     return panda::ecmascript::Main(argc, argv);
175 }
176