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
|
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cmath>
#include <iostream>
#include <limits>
#include <memory>
#include "src/ast/ast-value-factory.h"
#include "src/handles/handles-inl.h"
#include "src/handles/handles.h"
#include "src/heap/off-thread-factory.h"
#include "src/objects/fixed-array.h"
#include "src/objects/string.h"
#include "test/unittests/test-utils.h"
namespace v8 {
namespace internal {
class OffThreadFactoryTest : public TestWithIsolateAndZone {
public:
OffThreadFactoryTest()
: TestWithIsolateAndZone(), off_thread_factory_(isolate()) {}
OffThreadFactory* off_thread_factory() { return &off_thread_factory_; }
// We only internalize strings which are referred to in other slots, so create
// a wrapper pointing at the off_thread_string.
OffThreadHandle<FixedArray> WrapString(OffThreadHandle<String> string) {
// TODO(leszeks): Replace with a different factory method (e.g. FixedArray)
// once OffThreadFactory supports it.
return off_thread_factory()->StringWrapperForTest(string);
}
private:
OffThreadFactory off_thread_factory_;
};
TEST_F(OffThreadFactoryTest, HandleOrOffThreadHandle_IsNullWhenConstructed) {
// Default constructed HandleOrOffThreadHandles should be considered both null
// and uninitialized.
EXPECT_TRUE(HandleOrOffThreadHandle<HeapObject>().is_null());
#ifdef DEBUG
EXPECT_TRUE(!HandleOrOffThreadHandle<HeapObject>().is_initialized());
#endif
// Default constructed HandleOrOffThreadHandles should work as both null
// handles and null off-thread handles.
EXPECT_TRUE(HandleOrOffThreadHandle<HeapObject>().get<Factory>().is_null());
EXPECT_TRUE(
HandleOrOffThreadHandle<HeapObject>().get<OffThreadFactory>().is_null());
}
TEST_F(OffThreadFactoryTest, OneByteInternalizedString_IsAddedToStringTable) {
Vector<const uint8_t> string_vector = StaticCharVector("foo");
uint32_t hash_field = StringHasher::HashSequentialString<uint8_t>(
string_vector.begin(), string_vector.length(), HashSeed(isolate()));
OffThreadHandle<String> off_thread_string =
off_thread_factory()->NewOneByteInternalizedString(string_vector,
hash_field);
OffThreadHandle<FixedArray> off_thread_wrapper =
off_thread_factory()->StringWrapperForTest(off_thread_string);
off_thread_factory()->FinishOffThread();
Handle<FixedArray> wrapper = handle(*off_thread_wrapper, isolate());
off_thread_factory()->Publish(isolate());
Handle<String> string = handle(String::cast(wrapper->get(0)), isolate());
EXPECT_TRUE(string->IsOneByteEqualTo(CStrVector("foo")));
EXPECT_TRUE(string->IsInternalizedString());
Handle<String> same_string = isolate()
->factory()
->NewStringFromOneByte(string_vector)
.ToHandleChecked();
EXPECT_NE(*string, *same_string);
EXPECT_FALSE(same_string->IsInternalizedString());
Handle<String> internalized_string =
isolate()->factory()->InternalizeString(same_string);
EXPECT_EQ(*string, *internalized_string);
}
TEST_F(OffThreadFactoryTest,
OneByteInternalizedString_DuplicateIsDeduplicated) {
Vector<const uint8_t> string_vector = StaticCharVector("foo");
uint32_t hash_field = StringHasher::HashSequentialString<uint8_t>(
string_vector.begin(), string_vector.length(), HashSeed(isolate()));
OffThreadHandle<String> off_thread_string_1 =
off_thread_factory()->NewOneByteInternalizedString(string_vector,
hash_field);
OffThreadHandle<String> off_thread_string_2 =
off_thread_factory()->NewOneByteInternalizedString(string_vector,
hash_field);
OffThreadHandle<FixedArray> off_thread_wrapper_1 =
WrapString(off_thread_string_1);
OffThreadHandle<FixedArray> off_thread_wrapper_2 =
WrapString(off_thread_string_2);
off_thread_factory()->FinishOffThread();
Handle<FixedArray> wrapper_1 = handle(*off_thread_wrapper_1, isolate());
Handle<FixedArray> wrapper_2 = handle(*off_thread_wrapper_2, isolate());
off_thread_factory()->Publish(isolate());
Handle<String> string_1 = handle(String::cast(wrapper_1->get(0)), isolate());
Handle<String> string_2 = handle(String::cast(wrapper_2->get(0)), isolate());
EXPECT_TRUE(string_1->IsOneByteEqualTo(CStrVector("foo")));
EXPECT_TRUE(string_1->IsInternalizedString());
EXPECT_EQ(*string_1, *string_2);
}
TEST_F(OffThreadFactoryTest, AstRawString_IsInternalized) {
AstValueFactory ast_value_factory(zone(), isolate()->ast_string_constants(),
HashSeed(isolate()));
const AstRawString* raw_string = ast_value_factory.GetOneByteString("foo");
ast_value_factory.Internalize(off_thread_factory());
OffThreadHandle<FixedArray> off_thread_wrapper =
WrapString(raw_string->string().get<OffThreadFactory>());
off_thread_factory()->FinishOffThread();
Handle<FixedArray> wrapper = handle(*off_thread_wrapper, isolate());
off_thread_factory()->Publish(isolate());
Handle<String> string = handle(String::cast(wrapper->get(0)), isolate());
EXPECT_TRUE(string->IsOneByteEqualTo(CStrVector("foo")));
EXPECT_TRUE(string->IsInternalizedString());
}
TEST_F(OffThreadFactoryTest, AstConsString_CreatesConsString) {
AstValueFactory ast_value_factory(zone(), isolate()->ast_string_constants(),
HashSeed(isolate()));
const AstRawString* foo_string = ast_value_factory.GetOneByteString("foo");
const AstRawString* bar_string =
ast_value_factory.GetOneByteString("bar-plus-padding-for-length");
const AstConsString* foobar_string =
ast_value_factory.NewConsString(foo_string, bar_string);
ast_value_factory.Internalize(off_thread_factory());
OffThreadHandle<FixedArray> off_thread_wrapper =
WrapString(foobar_string->string().get<OffThreadFactory>());
off_thread_factory()->FinishOffThread();
Handle<FixedArray> wrapper = handle(*off_thread_wrapper, isolate());
off_thread_factory()->Publish(isolate());
Handle<String> string = handle(String::cast(wrapper->get(0)), isolate());
EXPECT_TRUE(string->IsConsString());
EXPECT_TRUE(string->Equals(*isolate()->factory()->NewStringFromStaticChars(
"foobar-plus-padding-for-length")));
}
} // namespace internal
} // namespace v8
|