• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <csignal>
17 
18 #include "ecmascript/base/string_helper.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/mem/clock_scope.h"
21 #include "ecmascript/platform/os.h"
22 
23 
24 namespace panda::ecmascript {
BlockSignals()25 void BlockSignals()
26 {
27 #if defined(PANDA_TARGET_UNIX)
28     sigset_t set;
29     if (sigemptyset(&set) == -1) {
30         LOG_ECMA(ERROR) << "sigemptyset failed";
31         return;
32     }
33 #endif  // PANDA_TARGET_UNIX
34 }
35 
GetHelper()36 std::string GetHelper()
37 {
38     std::string str;
39     str.append(COMMON_HELP_HEAD_MSG);
40     str.append(HELP_OPTION_MSG);
41     return str;
42 }
43 
44 static bool g_testEnd = false;
45 
IsEqual(EcmaVM * vm,Local<JSValueRef> jsArg0,Local<JSValueRef> jsArg1)46 bool IsEqual(EcmaVM *vm, Local<JSValueRef> jsArg0, Local<JSValueRef> jsArg1)
47 {
48     if (jsArg0->IsStrictEquals(vm, jsArg1)) {
49         return true;
50     } else if ((jsArg0->IsJSArray(vm) && jsArg1->IsJSArray(vm))) {
51         Local<ArrayRef> arr0(jsArg0);
52         Local<ArrayRef> arr1(jsArg1);
53         uint32_t length = arr0->Length(vm);
54         if (length != arr1->Length(vm)) {
55             return false;
56         }
57         for (uint32_t i = 0; i < length; i++) {
58             Local<JSValueRef> arg0 = ArrayRef::GetValueAt(vm, arr0, i);
59             Local<JSValueRef> arg1 = ArrayRef::GetValueAt(vm, arr1, i);
60             if (!IsEqual(vm, arg0, arg1)) {
61                 return false;
62             }
63         }
64         return true;
65     }
66     return false;
67 }
68 
TestEnd(JsiRuntimeCallInfo * runtimeInfo)69 Local<JSValueRef> TestEnd(JsiRuntimeCallInfo *runtimeInfo)
70 {
71     EcmaVM *vm = runtimeInfo->GetVM();
72     g_testEnd = true;
73     return JSValueRef::Undefined(vm);
74 }
75 
AssertEqual(JsiRuntimeCallInfo * runtimeInfo)76 Local<JSValueRef> AssertEqual(JsiRuntimeCallInfo *runtimeInfo)
77 {
78     EcmaVM *vm = runtimeInfo->GetVM();
79 
80     uint32_t argsCount = runtimeInfo->GetArgsNumber();
81     if (argsCount < 2) { // 2: at least have two arguments
82         std::string errStr = "Assertion failed: At least have two arguments.";
83         auto error = panda::Exception::TypeError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
84         panda::JSNApi::ThrowException(vm, error);
85         return JSValueRef::Undefined(vm);
86     }
87 
88     Local<JSValueRef> jsArg0 = runtimeInfo->GetCallArgRef(0);
89     Local<JSValueRef> jsArg1 = runtimeInfo->GetCallArgRef(1);
90 
91     if (!IsEqual(vm, jsArg0, jsArg1)) {
92         std::string errStr = std::string("Assertion failed: ").append(jsArg0->ToString(vm)->ToString(vm))
93             .append(" != ").append(jsArg1->ToString(vm)->ToString(vm));
94         auto error = panda::Exception::TypeError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
95         panda::JSNApi::ThrowException(vm, error);
96     }
97 
98     return JSValueRef::Undefined(vm);
99 }
100 
AssertTrue(JsiRuntimeCallInfo * runtimeInfo)101 Local<JSValueRef> AssertTrue(JsiRuntimeCallInfo *runtimeInfo)
102 {
103     EcmaVM *vm = runtimeInfo->GetVM();
104 
105     uint32_t argsCount = runtimeInfo->GetArgsNumber();
106     if (argsCount < 1) {
107         std::string errStr = "Assertion failed: At least have one argument.";
108         auto error = panda::Exception::TypeError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
109         panda::JSNApi::ThrowException(vm, error);
110         return JSValueRef::Undefined(vm);
111     }
112 
113     Local<JSValueRef> jsArg0 = runtimeInfo->GetCallArgRef(0);
114 
115     if (!jsArg0->IsTrue()) {
116         std::string errStr = std::string("Assertion failed: Expect ").append(jsArg0->ToString(vm)->ToString(vm))
117             .append(" equals True.");
118         auto error = panda::Exception::TypeError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
119         panda::JSNApi::ThrowException(vm, error);
120     }
121 
122     return JSValueRef::Undefined(vm);
123 }
124 
AssertUnreachable(JsiRuntimeCallInfo * runtimeInfo)125 Local<JSValueRef> AssertUnreachable(JsiRuntimeCallInfo *runtimeInfo)
126 {
127     EcmaVM *vm = runtimeInfo->GetVM();
128 
129     LOG_ECMA(FATAL) << "this test is unreachable";
130     return JSValueRef::Undefined(vm);
131 }
132 
ExecutePandaFile(EcmaVM * vm,JSRuntimeOptions & runtimeOptions,std::string & files)133 bool ExecutePandaFile(EcmaVM *vm, JSRuntimeOptions &runtimeOptions, std::string &files)
134 {
135     bool ret = true;
136     LocalScope scope(vm);
137     std::string entry = runtimeOptions.GetEntryPoint();
138 #if defined(PANDA_TARGET_WINDOWS)
139     arg_list_t fileNames = base::StringHelper::SplitString(files, ";");
140 #else
141     arg_list_t fileNames = base::StringHelper::SplitString(files, ":");
142 #endif
143     if (runtimeOptions.EnableMultiContext()) {
144         // Simulate creating a new context and switch to the new context.
145         Local<JSValueRef> context = JSNApi::CreateContext(vm);
146         JSNApi::SwitchContext(vm, context);
147     }
148     if (runtimeOptions.GetTestAssert()) {
149         Local<ObjectRef> globalObj = JSNApi::GetGlobalObject(vm);
150         Local<FunctionRef> assertEqual = FunctionRef::New(vm, AssertEqual);
151         globalObj->Set(vm, StringRef::NewFromUtf8(vm, "assert_equal"), assertEqual);
152         Local<FunctionRef> assertTrue = FunctionRef::New(vm, AssertTrue);
153         globalObj->Set(vm, StringRef::NewFromUtf8(vm, "assert_true"), assertTrue);
154         Local<FunctionRef> assertUnreachable = FunctionRef::New(vm, AssertUnreachable);
155         globalObj->Set(vm, StringRef::NewFromUtf8(vm, "assert_unreachable"), assertUnreachable);
156         Local<FunctionRef> testEnd = FunctionRef::New(vm, TestEnd);
157         globalObj->Set(vm, StringRef::NewFromUtf8(vm, "test_end"), testEnd);
158     }
159     if (runtimeOptions.WasAOTOutputFileSet()) {
160         JSNApi::LoadAotFile(vm, "");
161     }
162     ClockScope execute;
163     for (const auto &fileName : fileNames) {
164         auto res = JSNApi::Execute(vm, fileName, entry);
165         if (!res) {
166             std::cerr << "Cannot execute panda file '" << fileName << "' with entry '" << entry << "'" << std::endl;
167             ret = false;
168             break;
169         }
170     }
171     if (runtimeOptions.GetTestAssert() && !g_testEnd) {
172         LOG_ECMA(FATAL) << "this test didn't run to the end normally.";
173     }
174     auto totalTime = execute.TotalSpentTime();
175 
176     if (runtimeOptions.IsEnablePrintExecuteTime()) {
177         std::cout << "execute pandafile spent time " << totalTime << "ms" << std::endl;
178     }
179     return ret;
180 }
181 
Main(const int argc,const char ** argv)182 int Main(const int argc, const char **argv)
183 {
184     InitializeMallocConfig();
185     auto startTime =
186         std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
187             .count();
188 
189     BlockSignals();
190 
191     if (argc < 2) { // 2: at least have two arguments
192         std::cerr << GetHelper();
193         return -1;
194     }
195 
196     int newArgc = argc;
197     std::string files = argv[argc - 1];
198     if (!base::StringHelper::EndsWith(files, ".abc")) {
199         std::cerr << "The last argument must be abc file" << std::endl;
200         std::cerr << GetHelper();
201         return 1;
202     }
203 
204     newArgc--;
205     JSRuntimeOptions runtimeOptions;
206     bool retOpt = runtimeOptions.ParseCommand(newArgc, argv);
207     if (!runtimeOptions.IsMegaICInitialized()) {
208         runtimeOptions.SetEnableMegaIC(true);
209     }
210     if (!retOpt) {
211         std::cerr << GetHelper();
212         return 1;
213     }
214 
215     if (runtimeOptions.IsStartupTime()) {
216         std::cout << "\n"
217                   << "Startup start time: " << startTime << std::endl;
218     }
219     bool ret = true;
220     EcmaVM *vm = JSNApi::CreateEcmaVM(runtimeOptions);
221     if (vm == nullptr) {
222         std::cerr << "Cannot Create vm" << std::endl;
223         return -1;
224     }
225 
226     bool isMergeAbc = runtimeOptions.GetMergeAbc();
227     JSNApi::SetBundle(vm, !isMergeAbc);
228     ret = ExecutePandaFile(vm, runtimeOptions, files);
229 
230     JSNApi::DestroyJSVM(vm);
231     return ret ? 0 : -1;
232 }
233 }  // namespace panda::ecmascript
234 
main(int argc,const char ** argv)235 int main(int argc, const char **argv)
236 {
237     return panda::ecmascript::Main(argc, argv);
238 }
239