summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testPersistentRooted.cpp
blob: ed9bed50cdec3f7eef50018c896a0e374251e29e (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "js/Class.h"
#include "jsapi-tests/tests.h"

using namespace JS;

struct BarkWhenTracedClass {
  static int finalizeCount;
  static int traceCount;

  static const JSClass class_;
  static void finalize(JSFreeOp* fop, JSObject* obj) { finalizeCount++; }
  static void trace(JSTracer* trc, JSObject* obj) { traceCount++; }
  static void reset() {
    finalizeCount = 0;
    traceCount = 0;
  }
};

int BarkWhenTracedClass::finalizeCount;
int BarkWhenTracedClass::traceCount;

static const JSClassOps BarkWhenTracedClassClassOps = {
    nullptr,                        // addProperty
    nullptr,                        // delProperty
    nullptr,                        // enumerate
    nullptr,                        // newEnumerate
    nullptr,                        // resolve
    nullptr,                        // mayResolve
    BarkWhenTracedClass::finalize,  // finalize
    nullptr,                        // call
    nullptr,                        // hasInstance
    nullptr,                        // construct
    BarkWhenTracedClass::trace,     // trace
};

const JSClass BarkWhenTracedClass::class_ = {"BarkWhenTracedClass",
                                             JSCLASS_FOREGROUND_FINALIZE,
                                             &BarkWhenTracedClassClassOps};

struct Kennel {
  PersistentRootedObject obj;
  Kennel() {}
  explicit Kennel(JSContext* cx) : obj(cx) {}
  Kennel(JSContext* cx, const HandleObject& woof) : obj(cx, woof) {}
  void init(JSContext* cx, const HandleObject& woof) { obj.init(cx, woof); }
  void clear() { obj = nullptr; }
};

// A function for allocating a Kennel and a barker. Only allocating
// PersistentRooteds on the heap, and in this function, helps ensure that the
// conservative GC doesn't find stray references to the barker. Ugh.
MOZ_NEVER_INLINE static Kennel* Allocate(JSContext* cx) {
  RootedObject barker(cx, JS_NewObject(cx, &BarkWhenTracedClass::class_));
  if (!barker) {
    return nullptr;
  }

  return new Kennel(cx, barker);
}

// Do a GC, expecting |n| barkers to be finalized.
static bool GCFinalizesNBarkers(JSContext* cx, int n) {
  int preGCTrace = BarkWhenTracedClass::traceCount;
  int preGCFinalize = BarkWhenTracedClass::finalizeCount;

  JS_GC(cx);

  return (BarkWhenTracedClass::finalizeCount == preGCFinalize + n &&
          BarkWhenTracedClass::traceCount > preGCTrace);
}

// PersistentRooted instances protect their contents from being recycled.
BEGIN_TEST(test_PersistentRooted) {
  BarkWhenTracedClass::reset();

  mozilla::UniquePtr<Kennel> kennel(Allocate(cx));
  CHECK(kennel.get());

  // GC should be able to find our barker.
  CHECK(GCFinalizesNBarkers(cx, 0));

  kennel = nullptr;

  // Now GC should not be able to find the barker.
  JS_GC(cx);
  CHECK(BarkWhenTracedClass::finalizeCount == 1);

  return true;
}
END_TEST(test_PersistentRooted)

// GC should not be upset by null PersistentRooteds.
BEGIN_TEST(test_PersistentRootedNull) {
  BarkWhenTracedClass::reset();

  Kennel kennel(cx);
  CHECK(!kennel.obj);

  JS_GC(cx);
  CHECK(BarkWhenTracedClass::finalizeCount == 0);

  return true;
}
END_TEST(test_PersistentRootedNull)

// Copy construction works.
BEGIN_TEST(test_PersistentRootedCopy) {
  BarkWhenTracedClass::reset();

  mozilla::UniquePtr<Kennel> kennel(Allocate(cx));
  CHECK(kennel.get());

  CHECK(GCFinalizesNBarkers(cx, 0));

  // Copy construction! AMAZING!
  mozilla::UniquePtr<Kennel> newKennel(new Kennel(*kennel));

  CHECK(GCFinalizesNBarkers(cx, 0));

  kennel = nullptr;

  CHECK(GCFinalizesNBarkers(cx, 0));

  newKennel = nullptr;

  // Now that kennel and nowKennel are both deallocated, GC should not be
  // able to find the barker.
  JS_GC(cx);
  CHECK(BarkWhenTracedClass::finalizeCount == 1);

  return true;
}
END_TEST(test_PersistentRootedCopy)

// Assignment works.
BEGIN_TEST(test_PersistentRootedAssign) {
  BarkWhenTracedClass::reset();

  mozilla::UniquePtr<Kennel> kennel(Allocate(cx));
  CHECK(kennel.get());

  CHECK(GCFinalizesNBarkers(cx, 0));

  // Allocate a new, empty kennel.
  mozilla::UniquePtr<Kennel> kennel2(new Kennel(cx));

  // Assignment! ASTONISHING!
  *kennel2 = *kennel;

  // With both kennels referring to the same barker, it is held alive.
  CHECK(GCFinalizesNBarkers(cx, 0));

  kennel2 = nullptr;

  // The destination of the assignment alone holds the barker alive.
  CHECK(GCFinalizesNBarkers(cx, 0));

  // Allocate a second barker.
  kennel2 = mozilla::UniquePtr<Kennel>(Allocate(cx));
  CHECK(kennel2.get());

  *kennel = *kennel2;

  // Nothing refers to the first kennel any more.
  CHECK(GCFinalizesNBarkers(cx, 1));

  kennel = nullptr;
  kennel2 = nullptr;

  // Now that kennel and kennel2 are both deallocated, GC should not be
  // able to find the barker.
  JS_GC(cx);
  CHECK(BarkWhenTracedClass::finalizeCount == 2);

  return true;
}
END_TEST(test_PersistentRootedAssign)

static PersistentRootedObject gGlobalRoot;

// PersistentRooted instances can initialized in a separate step to allow for
// global PersistentRooteds.
BEGIN_TEST(test_GlobalPersistentRooted) {
  BarkWhenTracedClass::reset();

  CHECK(!gGlobalRoot.initialized());

  {
    RootedObject barker(cx, JS_NewObject(cx, &BarkWhenTracedClass::class_));
    CHECK(barker);

    gGlobalRoot.init(cx, barker);
  }

  CHECK(gGlobalRoot.initialized());

  // GC should be able to find our barker.
  CHECK(GCFinalizesNBarkers(cx, 0));

  gGlobalRoot.reset();
  CHECK(!gGlobalRoot.initialized());

  // Now GC should not be able to find the barker.
  JS_GC(cx);
  CHECK(BarkWhenTracedClass::finalizeCount == 1);

  return true;
}
END_TEST(test_GlobalPersistentRooted)