summaryrefslogtreecommitdiff
path: root/js/src/jsapi-tests/testGCWeakCache.cpp
blob: 2e86ef63e69b38ecc1835ef884492cfce1bad294 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*/
/* 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 "gc/Policy.h"
#include "js/GCHashTable.h"
#include "js/RootingAPI.h"
#include "js/SweepingAPI.h"

#include "jsapi-tests/tests.h"

// Exercise WeakCache<GCHashSet>.
BEGIN_TEST(testWeakCacheSet)
{
    // Create two objects tenured and two in the nursery. If zeal is on,
    // this may fail and we'll get more tenured objects. That's fine:
    // the test will continue to work, it will just not test as much.
    JS::RootedObject tenured1(cx, JS_NewPlainObject(cx));
    JS::RootedObject tenured2(cx, JS_NewPlainObject(cx));
    JS_GC(cx);
    JS::RootedObject nursery1(cx, JS_NewPlainObject(cx));
    JS::RootedObject nursery2(cx, JS_NewPlainObject(cx));

    using ObjectSet = js::GCHashSet<JS::Heap<JSObject*>,
                                    js::MovableCellHasher<JS::Heap<JSObject*>>,
                                    js::SystemAllocPolicy>;
    using Cache = JS::WeakCache<ObjectSet>;
    auto cache = Cache(JS::GetObjectZone(tenured1), ObjectSet());
    CHECK(cache.init());

    cache.put(tenured1);
    cache.put(tenured2);
    cache.put(nursery1);
    cache.put(nursery2);

    // Verify relocation and that we don't sweep too aggressively.
    JS_GC(cx);
    CHECK(cache.has(tenured1));
    CHECK(cache.has(tenured2));
    CHECK(cache.has(nursery1));
    CHECK(cache.has(nursery2));

    // Unroot two entries and verify that they get removed.
    tenured2 = nursery2 = nullptr;
    JS_GC(cx);
    CHECK(cache.has(tenured1));
    CHECK(cache.has(nursery1));
    CHECK(cache.count() == 2);

    return true;
}
END_TEST(testWeakCacheSet)

// Exercise WeakCache<GCHashMap>.
BEGIN_TEST(testWeakCacheMap)
{
    // Create two objects tenured and two in the nursery. If zeal is on,
    // this may fail and we'll get more tenured objects. That's fine:
    // the test will continue to work, it will just not test as much.
    JS::RootedObject tenured1(cx, JS_NewPlainObject(cx));
    JS::RootedObject tenured2(cx, JS_NewPlainObject(cx));
    JS_GC(cx);
    JS::RootedObject nursery1(cx, JS_NewPlainObject(cx));
    JS::RootedObject nursery2(cx, JS_NewPlainObject(cx));

    using ObjectMap = js::GCHashMap<JS::Heap<JSObject*>, uint32_t,
                                    js::MovableCellHasher<JS::Heap<JSObject*>>>;
    using Cache = JS::WeakCache<ObjectMap>;
    auto cache = Cache(JS::GetObjectZone(tenured1), ObjectMap(cx));
    CHECK(cache.init());

    cache.put(tenured1, 1);
    cache.put(tenured2, 2);
    cache.put(nursery1, 3);
    cache.put(nursery2, 4);

    JS_GC(cx);
    CHECK(cache.has(tenured1));
    CHECK(cache.has(tenured2));
    CHECK(cache.has(nursery1));
    CHECK(cache.has(nursery2));

    tenured2 = nursery2 = nullptr;
    JS_GC(cx);
    CHECK(cache.has(tenured1));
    CHECK(cache.has(nursery1));
    CHECK(cache.count() == 2);

    return true;
}
END_TEST(testWeakCacheMap)