forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandleTest.cpp
More file actions
214 lines (178 loc) · 5.79 KB
/
Copy pathHandleTest.cpp
File metadata and controls
214 lines (178 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TestHelpers.h"
#include "hermes/BCGen/HBC/Bytecode.h"
#include "hermes/VM/Runtime.h"
#include "hermes/VM/StringPrimitive.h"
#include "gtest/gtest.h"
using namespace hermes::vm;
namespace {
using HandleTest = RuntimeTestFixture;
using HandleDeathTest = RuntimeTestFixture;
TEST_F(HandleTest, AddAndRemoveTest) {
auto str1 = StringPrimitive::createNoThrow(runtime, createUTF16Ref(u"str1"));
auto str2 = StringPrimitive::createNoThrow(runtime, createUTF16Ref(u"str2"));
auto str3 = StringPrimitive::createNoThrow(runtime, createUTF16Ref(u"str3"));
{
Handle<StringPrimitive> h1(runtime, str1.get());
{
GCScope scope(runtime);
Handle<StringPrimitive> h2(runtime, str2.get());
Handle<StringPrimitive> h3(runtime, str3.get());
ASSERT_EQ(str1, h1);
ASSERT_EQ(str2, h2);
ASSERT_EQ(str3, h3);
}
ASSERT_EQ(str1, h1);
{
GCScope scope(runtime);
// Check operator=.
Handle<StringPrimitive> h3 = h1;
MutableHandle<StringPrimitive> h4(runtime, h1.get());
Handle<StringPrimitive> h5 = h4;
ASSERT_EQ(str1, h3);
ASSERT_EQ(str1, h4);
ASSERT_EQ(str1, h5);
h4 = h1.get();
ASSERT_EQ(str1, h4);
}
{
GCScope scope(runtime);
// Check copy constructor.
Handle<StringPrimitive> h3(h1);
Handle<StringPrimitive> h4(h1);
ASSERT_EQ(str1, h3);
ASSERT_EQ(str1, h4);
}
{
GCScope scope(runtime);
// Check mov constructor.
Handle<StringPrimitive> h5(Handle<StringPrimitive>(runtime, str2.get()));
ASSERT_EQ(str2, h5);
}
}
}
TEST_F(HandleTest, ValueAddAndRemoveTest) {
auto str1 = Handle<>(runtime, HermesValue::encodeNumberValue(1));
auto str2 = Handle<>(runtime, HermesValue::encodeNumberValue(2));
auto str3 = Handle<>(runtime, HermesValue::encodeNumberValue(3));
{
MutableHandle<> h1(runtime, str1.get());
{
MutableHandle<> h2(runtime, str2.get());
Handle<> h3(runtime, str3.get());
ASSERT_EQ(str1, h1);
ASSERT_EQ(str2, h2);
ASSERT_EQ(str3, h3);
h2.set(h3.get());
ASSERT_EQ(str3, h2);
}
// h2 is out of scope, so it should be gone.
{
// Check operator=.
Handle<> h3 = h1;
MutableHandle<> h4(runtime, h1.get());
Handle<> h5 = h4;
ASSERT_EQ(str1, h3);
ASSERT_EQ(str1, h4);
ASSERT_EQ(str1, h5);
h4 = h1.get();
ASSERT_EQ(str1, h4);
}
{
// Check copy constructor.
Handle<> h3(h1);
Handle<> h4(h1);
ASSERT_EQ(str1, h3);
ASSERT_EQ(str1, h4);
}
{
// Check mov constructor.
MutableHandle<> h5(runtime, h1.get());
MutableHandle<> h6(std::move(h5));
ASSERT_EQ(str1, h6);
}
}
}
TEST_F(HandleTest, MarkTest) {
GCScope gcScope{runtime};
Handle<> v1(runtime, HermesValue::encodeNumberValue(1));
(void)v1;
Handle<> v2(runtime, HermesValue::encodeNumberValue(2));
(void)v2;
ASSERT_EQ(2u, gcScope.getHandleCountDbg());
auto marker = gcScope.createMarker();
Handle<> v3(runtime, HermesValue::encodeNumberValue(3));
(void)v3;
ASSERT_EQ(3u, gcScope.getHandleCountDbg());
gcScope.flushToMarker(marker);
ASSERT_EQ(2u, gcScope.getHandleCountDbg());
Handle<> v4(runtime, HermesValue::encodeNumberValue(4));
(void)v4;
ASSERT_EQ(3u, gcScope.getHandleCountDbg());
gcScope.flushToMarker(marker);
ASSERT_EQ(2u, gcScope.getHandleCountDbg());
}
/// Make sure that related Handle-s can be assigned.
TEST_F(HandleTest, ScopedPointerConstructorTest) {
auto function = runtime->makeHandle<JSFunction>(
JSFunction::create(runtime, domain, Handle<JSObject>(runtime, nullptr)));
Handle<JSObject> obj = function;
ASSERT_EQ(function.get(), obj.get());
}
/// Make sure that we can create handles one scope up in the scope chain.
TEST_F(HandleTest, CreateInParentScope) {
GCScope theParent{runtime};
auto h1 = runtime->makeHandle(HermesValue::encodeBoolValue(true));
(void)h1;
#ifndef NDEBUG
ASSERT_EQ(1, theParent.getHandleCountDbg());
#endif
auto func = [this, &theParent]() -> Handle<> {
GCScope theChild{runtime};
EXPECT_EQ(&theParent, runtime->getTopGCScopesParent());
auto h2 = runtime->makeHandle(HermesValue::encodeBoolValue(true));
(void)h2;
auto h3 =
runtime->makeHandleInParentScope(HermesValue::encodeBoolValue(true));
return h3;
};
auto h3 = func();
(void)h3;
#ifndef NDEBUG
ASSERT_EQ(2, theParent.getHandleCountDbg());
#endif
}
#ifdef HERMES_SLOW_DEBUG
TEST_F(HandleDeathTest, UseFlushedHandle) {
auto marker = gcScope.createMarker();
auto handle = StringPrimitive::createNoThrow(runtime, "hello");
// This flush should destroy the handle created after the marker, using it
// afterwards should trigger an assertion failure.
gcScope.flushToMarker(marker);
ASSERT_DEATH(
{ handle->getStringLength(); }, "Assertion.*Reading from flushed handle");
}
TEST_F(HandleDeathTest, UseRAIIFlushedHandle) {
GCScopeMarkerRAII marker(gcScope);
auto handle = StringPrimitive::createNoThrow(runtime, "hello");
// This flush should destroy the handle created after the marker, using it
// afterwards should trigger an assertion failure.
marker.flush();
ASSERT_DEATH(
{ handle->getStringLength(); }, "Assertion.*Reading from flushed handle");
}
TEST_F(HandleDeathTest, FlushToSmallCount) {
auto handle = StringPrimitive::createNoThrow(runtime, "hello");
// This flush should destroy the handle created after the marker, using it
// afterwards should trigger an assertion failure.
gcScope.flushToSmallCount(0);
ASSERT_DEATH(
{ handle->getStringLength(); }, "Assertion.*Reading from flushed handle");
}
#endif
} // namespace