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 "ecmascript/ecma_vm.h"
17 #include "ecmascript/js_runtime_options.h"
18 #include "ecmascript/tests/test_helper.h"
19
20 using namespace panda::ecmascript;
21
22 namespace panda::test {
23 class EcmaVMTest : public BaseTestWithOutScope {
24 };
25
26 /*
27 * @tc.name: CreateEcmaVMInTwoWays
28 * @tc.desc: Create EcmaVM in 2 ways, check the Options state
29 * @tc.type: FUNC
30 * @tc.require:
31 */
HWTEST_F_L0(EcmaVMTest,CreateEcmaVMInTwoWays)32 HWTEST_F_L0(EcmaVMTest, CreateEcmaVMInTwoWays)
33 {
34 RuntimeOption options;
35 options.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
36 EcmaVM::SetMultiThreadCheck(true);
37 EcmaVM *ecmaVm1 = JSNApi::CreateJSVM(options);
38 EXPECT_TRUE(ecmaVm1->GetMultiThreadCheck());
39 auto jsthread1 = ecmaVm1->GetJSThread();
40 EXPECT_TRUE(jsthread1 != nullptr);
41
42 std::thread t1([&]() {
43 JSRuntimeOptions options2;
44 options2.SetEnableArkTools(false);
45 options2.SetEnableForceGC(false);
46 options2.SetForceFullGC(false);
47 options2.SetArkProperties(ArkProperties::GC_STATS_PRINT);
48 options2.SetMemConfigProperty("jsHeap500");
49 // A non-production gc strategy. Prohibit stw-gc 10 times.
50 EcmaVM::SetMultiThreadCheck(false);
51 EcmaVM *ecmaVm2 = JSNApi::CreateEcmaVM(options2);
52 auto jsthread2 = ecmaVm2->GetJSThread();
53 EXPECT_FALSE(ecmaVm2->GetMultiThreadCheck());
54 EXPECT_TRUE(jsthread2 != nullptr);
55 EXPECT_TRUE(ecmaVm1 != ecmaVm2);
56
57 JSRuntimeOptions options1Out = ecmaVm1->GetJSOptions();
58 JSRuntimeOptions options2Out = ecmaVm2->GetJSOptions();
59
60 EXPECT_TRUE(&options1Out != &options2Out);
61
62 EXPECT_TRUE(options1Out.EnableArkTools() != options2Out.EnableArkTools());
63 EXPECT_TRUE(options1Out.EnableForceGC() != options2Out.EnableForceGC());
64 EXPECT_TRUE(options1Out.ForceFullGC() != options2Out.ForceFullGC());
65 EXPECT_TRUE(options1Out.GetArkProperties() != options2Out.GetArkProperties());
66 EXPECT_TRUE(options2Out.GetHeapSize() == 500_MB);
67
68 options2.SetAsmOpcodeDisableRange("1,10");
69 options2.ParseAsmInterOption();
70 EXPECT_TRUE(options2.GetAsmInterParsedOption().handleStart == 1); // 1 targer start
71 EXPECT_TRUE(options2.GetAsmInterParsedOption().handleEnd == 10); // 10 targer end
72
73 options2.SetAsmOpcodeDisableRange("0x1,0xa");
74 options2.ParseAsmInterOption();
75 EXPECT_TRUE(options2.GetAsmInterParsedOption().handleStart == 1); // 1 targer start
76 EXPECT_TRUE(options2.GetAsmInterParsedOption().handleEnd == 10); // 10 targer end
77
78 options2.SetAsmOpcodeDisableRange(",");
79 options2.ParseAsmInterOption();
80 EXPECT_TRUE(options2.GetAsmInterParsedOption().handleStart == 0);
81 EXPECT_TRUE(options2.GetAsmInterParsedOption().handleEnd == kungfu::BYTECODE_STUB_END_ID);
82
83 options2.SetAsmOpcodeDisableRange("@,@");
84 options2.ParseAsmInterOption();
85 EXPECT_TRUE(options2.GetAsmInterParsedOption().handleStart == 0);
86 EXPECT_TRUE(options2.GetAsmInterParsedOption().handleEnd == kungfu::BYTECODE_STUB_END_ID);
87
88 JSNApi::DestroyJSVM(ecmaVm2);
89 });
90 t1.join();
91 JSNApi::DestroyJSVM(ecmaVm1);
92 }
93
HWTEST_F_L0(EcmaVMTest,DumpExceptionObject)94 HWTEST_F_L0(EcmaVMTest, DumpExceptionObject)
95 {
96 RuntimeOption option;
97 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
98 EcmaVM *ecmaVm = JSNApi::CreateJSVM(option);
99 auto thread = ecmaVm->GetJSThread();
100 int arkProperties = thread->GetEcmaVM()->GetJSOptions().GetArkProperties();
101 ecmaVm->GetJSOptions().SetArkProperties(arkProperties | ArkProperties::EXCEPTION_BACKTRACE);
102 EXPECT_TRUE(ecmaVm->GetJSOptions().EnableExceptionBacktrace());
103 JSNApi::DestroyJSVM(ecmaVm);
104 }
105 } // namespace panda::ecmascript
106