• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 <gtest/gtest.h>
17 
18 #include "types/ets_error.h"
19 #include "tests/runtime/types/ets_test_mirror_classes.h"
20 
21 namespace ark::ets::test {
22 
23 class EtsErrorTest : public testing::Test {
24 public:
EtsErrorTest()25     EtsErrorTest()
26     {
27         RuntimeOptions options;
28         options.SetShouldLoadBootPandaFiles(true);
29         options.SetShouldInitializeIntrinsics(false);
30         options.SetCompilerEnableJit(false);
31         options.SetGcType("epsilon");
32         options.SetLoadRuntimes({"ets"});
33 
34         auto stdlib = std::getenv("PANDA_STD_LIB");
35         if (stdlib == nullptr) {
36             std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
37             std::abort();
38         }
39         options.SetBootPandaFiles({stdlib});
40 
41         Runtime::Create(options);
42         EtsCoroutine *coroutine = EtsCoroutine::GetCurrent();
43         vm_ = coroutine->GetPandaVM();
44     }
45 
~EtsErrorTest()46     ~EtsErrorTest() override
47     {
48         Runtime::Destroy();
49     }
50 
51     NO_COPY_SEMANTIC(EtsErrorTest);
52     NO_MOVE_SEMANTIC(EtsErrorTest);
53 
GetErrorMembers()54     static std::vector<MirrorFieldInfo> GetErrorMembers()
55     {
56         return std::vector<MirrorFieldInfo> {
57             MIRROR_FIELD_INFO(Error, name_, "name_"),
58             MIRROR_FIELD_INFO(Error, message_, "message_"),
59             MIRROR_FIELD_INFO(Error, stackLines_, "stackLines"),
60             MIRROR_FIELD_INFO(Error, stack_, "stack_"),
61             MIRROR_FIELD_INFO(Error, cause_, "cause_"),
62         };
63     }
64 
65 protected:
66     PandaEtsVM *vm_ = nullptr;  // NOLINT(misc-non-private-member-variables-in-classes)
67 };
68 
69 // Check both EtsErrorTest and ark::Class<escompat.Error> has the same number of fields
70 // and at the same offsets
TEST_F(EtsErrorTest,EscompatErrorMemoryLayout)71 TEST_F(EtsErrorTest, EscompatErrorMemoryLayout)
72 {
73     EtsClass *errorClass = PlatformTypes(vm_)->escompatError;
74     MirrorFieldInfo::CompareMemberOffsets(errorClass, GetErrorMembers());
75 }
76 
77 }  // namespace ark::ets::test
78