• 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/js_runtime_options.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     EcmaContext *context1 = nullptr;
144     if (runtimeOptions.IsEnableContext()) {
145         context1 = JSNApi::CreateJSContext(vm);
146         JSNApi::SwitchCurrentContext(vm, context1);
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     if (runtimeOptions.IsEnableContext()) {
176         JSNApi::DestroyJSContext(vm, context1);
177     }
178 
179     if (runtimeOptions.IsEnablePrintExecuteTime()) {
180         std::cout << "execute pandafile spent time " << totalTime << "ms" << std::endl;
181     }
182     return ret;
183 }
184 
Main(const int argc,const char ** argv)185 int Main(const int argc, const char **argv)
186 {
187     InitializeMallocConfig();
188     auto startTime =
189         std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
190             .count();
191 
192     BlockSignals();
193 
194     if (argc < 2) { // 2: at least have two arguments
195         std::cerr << GetHelper();
196         return -1;
197     }
198 
199     int newArgc = argc;
200     std::string files = argv[argc - 1];
201     if (!base::StringHelper::EndsWith(files, ".abc")) {
202         std::cerr << "The last argument must be abc file" << std::endl;
203         std::cerr << GetHelper();
204         return 1;
205     }
206 
207     newArgc--;
208     JSRuntimeOptions runtimeOptions;
209     bool retOpt = runtimeOptions.ParseCommand(newArgc, argv);
210     if (!runtimeOptions.IsMegaICInitialized()) {
211         runtimeOptions.SetEnableMegaIC(true);
212     }
213     if (!retOpt) {
214         std::cerr << GetHelper();
215         return 1;
216     }
217 
218     if (runtimeOptions.IsStartupTime()) {
219         std::cout << "\n"
220                   << "Startup start time: " << startTime << std::endl;
221     }
222     bool ret = true;
223     EcmaVM *vm = JSNApi::CreateEcmaVM(runtimeOptions);
224     if (vm == nullptr) {
225         std::cerr << "Cannot Create vm" << std::endl;
226         return -1;
227     }
228 
229     bool isMergeAbc = runtimeOptions.GetMergeAbc();
230     JSNApi::SetBundle(vm, !isMergeAbc);
231     ret = ExecutePandaFile(vm, runtimeOptions, files);
232 
233     JSNApi::DestroyJSVM(vm);
234     return ret ? 0 : -1;
235 }
236 }  // namespace panda::ecmascript
237 
main(int argc,const char ** argv)238 int main(int argc, const char **argv)
239 {
240     return panda::ecmascript::Main(argc, argv);
241 }
242