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