forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoratedObjectTest.cpp
More file actions
92 lines (80 loc) · 2.71 KB
/
DecoratedObjectTest.cpp
File metadata and controls
92 lines (80 loc) · 2.71 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
/*
* 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 "hermes/VM/DecoratedObject.h"
#include "AdditionalSlots.h"
#include "TestHelpers.h"
#include "gtest/gtest.h"
#include <vector>
using namespace hermes::vm;
namespace {
using DecoratedObjectTest = RuntimeTestFixture;
// A decoration which increments a shared_ptr<int> in its destructor.
struct TestDecoration : public DecoratedObject::Decoration {
std::shared_ptr<int> counter;
explicit TestDecoration(std::shared_ptr<int> counter) : counter(counter) {}
virtual ~TestDecoration() {
++*counter;
}
};
TEST_F(DecoratedObjectTest, DecoratedObjectFinalizerRunsOnce) {
auto counter = std::make_shared<int>(0);
{
GCScope scope{runtime, "DecoratedObjectTest"};
(void)runtime->makeHandle(DecoratedObject::create(
runtime,
Handle<JSObject>::vmcast(&runtime->objectPrototype),
std::make_unique<TestDecoration>(counter)));
runtime->collect("test");
// should not have been finalized yet
EXPECT_EQ(0, *counter);
}
// should finalize once
runtime->collect("test");
EXPECT_EQ(1, *counter);
runtime->collect("test");
runtime->collect("test");
EXPECT_EQ(1, *counter);
}
TEST_F(DecoratedObjectTest, ChangeDecoration) {
// Should be possisble to swap out decorations.
auto counter = std::make_shared<int>(0);
{
GCScope scope{runtime, "DecoratedObjectTest"};
auto handle = runtime->makeHandle(DecoratedObject::create(
runtime,
Handle<JSObject>::vmcast(&runtime->objectPrototype),
std::make_unique<TestDecoration>(counter)));
EXPECT_EQ(0, *counter);
handle->setDecoration(std::make_unique<TestDecoration>(counter));
// Old decoration was deallocated.
EXPECT_EQ(1, *counter);
}
runtime->collect("test");
// Old and new deallocated.
EXPECT_EQ(2, *counter);
}
TEST_F(DecoratedObjectTest, NullDecoration) {
// Null decorations do not crash.
{
GCScope scope{runtime, "DecoratedObjectTest"};
auto handle = runtime->makeHandle(DecoratedObject::create(
runtime, Handle<JSObject>::vmcast(&runtime->objectPrototype), nullptr));
EXPECT_EQ(nullptr, handle->getDecoration());
}
runtime->collect("test");
}
TEST_F(DecoratedObjectTest, AdditionalSlots) {
auto counter = std::make_shared<int>(0);
GCScope scope{runtime, "DecoratedObjectTest"};
auto handle = runtime->makeHandle(DecoratedObject::create(
runtime,
Handle<JSObject>::vmcast(&runtime->objectPrototype),
std::make_unique<TestDecoration>(counter),
numAdditionalSlotsForTest<DecoratedObject>()));
testAdditionalSlots(runtime, handle);
}
} // namespace