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 "test.h"
17 
18 #include "quickjs_native_engine.h"
19 
20 static NativeEngine* g_nativeEngine = nullptr;
21 
NativeEngineTest()22 NativeEngineTest::NativeEngineTest()
23 {
24     engine_ = g_nativeEngine;
25 }
26 
~NativeEngineTest()27 NativeEngineTest::~NativeEngineTest() {}
28 
LoopNewThread(void * data)29 static void LoopNewThread(void* data)
30 {
31     g_nativeEngine->Loop(LOOP_DEFAULT);
32 }
33 
main(int argc,char ** argv)34 int main(int argc, char** argv)
35 {
36     testing::GTEST_FLAG(output) = "xml:./";
37     testing::InitGoogleTest(&argc, argv);
38 
39     JSRuntime* rt = JS_NewRuntime();
40     if (rt == nullptr) {
41         return 0;
42     }
43 
44     JSContext* ctx = JS_NewContext(rt);
45     if (ctx == nullptr) {
46         return 0;
47     }
48 
49     js_std_add_helpers(ctx, 0, nullptr);
50 
51     g_nativeEngine = new QuickJSNativeEngine(rt, ctx, 0); // default instance id 0
52 
53     uv_thread_t tid;
54     uv_thread_create(&tid, LoopNewThread, nullptr);
55 
56     int ret = RUN_ALL_TESTS();
57 
58     uv_thread_join(&tid);
59 
60     delete g_nativeEngine;
61     g_nativeEngine = nullptr;
62 
63     js_std_free_handlers(rt);
64     JS_FreeContext(ctx);
65     JS_FreeRuntime(rt);
66 
67     return ret;
68 }
69