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 "ets_coroutine.h"
19
20 #include "types/ets_class.h"
21 #include "types/ets_promise.h"
22 #include "types/ets_promise_ref.h"
23 #include "tests/runtime/types/ets_test_mirror_classes.h"
24
25 namespace ark::ets::test {
26
27 class EtsPromiseTest : public testing::Test {
28 public:
EtsPromiseTest()29 EtsPromiseTest()
30 {
31 RuntimeOptions options;
32 options.SetShouldLoadBootPandaFiles(true);
33 options.SetShouldInitializeIntrinsics(false);
34 options.SetCompilerEnableJit(false);
35 options.SetGcType("epsilon");
36 options.SetLoadRuntimes({"ets"});
37
38 auto stdlib = std::getenv("PANDA_STD_LIB");
39 if (stdlib == nullptr) {
40 std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
41 std::abort();
42 }
43 options.SetBootPandaFiles({stdlib});
44
45 Runtime::Create(options);
46 EtsCoroutine *coroutine = EtsCoroutine::GetCurrent();
47 vm_ = coroutine->GetPandaVM();
48 }
49
~EtsPromiseTest()50 ~EtsPromiseTest() override
51 {
52 Runtime::Destroy();
53 }
54
55 NO_COPY_SEMANTIC(EtsPromiseTest);
56 NO_MOVE_SEMANTIC(EtsPromiseTest);
57
GetPromiseMembers()58 static std::vector<MirrorFieldInfo> GetPromiseMembers()
59 {
60 return std::vector<MirrorFieldInfo> {
61 MIRROR_FIELD_INFO(EtsPromise, value_, "value"),
62 MIRROR_FIELD_INFO(EtsPromise, mutex_, "mutex"),
63 MIRROR_FIELD_INFO(EtsPromise, event_, "event"),
64 MIRROR_FIELD_INFO(EtsPromise, callbackQueue_, "callbackQueue"),
65 MIRROR_FIELD_INFO(EtsPromise, launchModeQueue_, "launchModeQueue"),
66 MIRROR_FIELD_INFO(EtsPromise, interopObject_, "interopObject"),
67 MIRROR_FIELD_INFO(EtsPromise, linkedPromise_, "linkedPromise"),
68 MIRROR_FIELD_INFO(EtsPromise, queueSize_, "queueSize"),
69 MIRROR_FIELD_INFO(EtsPromise, state_, "state"),
70 };
71 }
72
GetPromiseRefMembers()73 static std::vector<MirrorFieldInfo> GetPromiseRefMembers()
74 {
75 return std::vector<MirrorFieldInfo> {MIRROR_FIELD_INFO(EtsPromiseRef, target_, "target")};
76 }
77
78 protected:
79 PandaEtsVM *vm_ = nullptr; // NOLINT(misc-non-private-member-variables-in-classes)
80 };
81
82 // Check both EtsPromise and ark::Class<Promise> has the same number of fields
83 // and at the same offsets
TEST_F(EtsPromiseTest,PromiseMemoryLayout)84 TEST_F(EtsPromiseTest, PromiseMemoryLayout)
85 {
86 EtsClass *promiseClass = PlatformTypes(vm_)->corePromise;
87 MirrorFieldInfo::CompareMemberOffsets(promiseClass, GetPromiseMembers());
88 }
89
TEST_F(EtsPromiseTest,PromiseRefMemoryLayout)90 TEST_F(EtsPromiseTest, PromiseRefMemoryLayout)
91 {
92 EtsClass *promiseRefClass = PlatformTypes(vm_)->corePromiseRef;
93 MirrorFieldInfo::CompareMemberOffsets(promiseRefClass, GetPromiseRefMembers());
94 }
95 } // namespace ark::ets::test
96