js/src/jsapi-tests/testPersistentRooted.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #if defined(JSGC_USE_EXACT_ROOTING)
michael@0 6
michael@0 7 #include "js/Class.h"
michael@0 8 #include "jsapi-tests/tests.h"
michael@0 9
michael@0 10 using namespace JS;
michael@0 11
michael@0 12 struct BarkWhenTracedClass {
michael@0 13 static int finalizeCount;
michael@0 14 static int traceCount;
michael@0 15
michael@0 16 static const JSClass class_;
michael@0 17 static void finalize(JSFreeOp *fop, JSObject *obj) { finalizeCount++; }
michael@0 18 static void trace(JSTracer *trc, JSObject *obj) { traceCount++; }
michael@0 19 static void reset() { finalizeCount = 0; traceCount = 0; }
michael@0 20 };
michael@0 21
michael@0 22 int BarkWhenTracedClass::finalizeCount;
michael@0 23 int BarkWhenTracedClass::traceCount;
michael@0 24
michael@0 25 const JSClass BarkWhenTracedClass::class_ = {
michael@0 26 "BarkWhenTracedClass", 0,
michael@0 27 JS_PropertyStub,
michael@0 28 JS_DeletePropertyStub,
michael@0 29 JS_PropertyStub,
michael@0 30 JS_StrictPropertyStub,
michael@0 31 JS_EnumerateStub,
michael@0 32 JS_ResolveStub,
michael@0 33 JS_ConvertStub,
michael@0 34 finalize,
michael@0 35 nullptr,
michael@0 36 nullptr,
michael@0 37 nullptr,
michael@0 38 trace
michael@0 39 };
michael@0 40
michael@0 41 struct Kennel {
michael@0 42 PersistentRootedObject obj;
michael@0 43 Kennel(JSContext *cx) : obj(cx) { }
michael@0 44 Kennel(JSContext *cx, const HandleObject &woof) : obj(cx, woof) { };
michael@0 45 };
michael@0 46
michael@0 47 // A function for allocating a Kennel and a barker. Only allocating
michael@0 48 // PersistentRooteds on the heap, and in this function, helps ensure that the
michael@0 49 // conservative GC doesn't find stray references to the barker. Ugh.
michael@0 50 MOZ_NEVER_INLINE static Kennel *
michael@0 51 Allocate(JSContext *cx)
michael@0 52 {
michael@0 53 RootedObject barker(cx, JS_NewObject(cx, &BarkWhenTracedClass::class_, JS::NullPtr(), JS::NullPtr()));
michael@0 54 if (!barker)
michael@0 55 return nullptr;
michael@0 56
michael@0 57 return new Kennel(cx, barker);
michael@0 58 }
michael@0 59
michael@0 60 // Do a GC, expecting |n| barkers to be finalized.
michael@0 61 static bool
michael@0 62 GCFinalizesNBarkers(JSContext *cx, int n)
michael@0 63 {
michael@0 64 int preGCTrace = BarkWhenTracedClass::traceCount;
michael@0 65 int preGCFinalize = BarkWhenTracedClass::finalizeCount;
michael@0 66
michael@0 67 JS_GC(JS_GetRuntime(cx));
michael@0 68
michael@0 69 return (BarkWhenTracedClass::finalizeCount == preGCFinalize + n &&
michael@0 70 BarkWhenTracedClass::traceCount > preGCTrace);
michael@0 71 }
michael@0 72
michael@0 73 // PersistentRooted instances protect their contents from being recycled.
michael@0 74 BEGIN_TEST(test_PersistentRooted)
michael@0 75 {
michael@0 76 BarkWhenTracedClass::reset();
michael@0 77
michael@0 78 Kennel *kennel = Allocate(cx);
michael@0 79 CHECK(kennel);
michael@0 80
michael@0 81 // GC should be able to find our barker.
michael@0 82 CHECK(GCFinalizesNBarkers(cx, 0));
michael@0 83
michael@0 84 delete(kennel);
michael@0 85
michael@0 86 // Now GC should not be able to find the barker.
michael@0 87 JS_GC(JS_GetRuntime(cx));
michael@0 88 CHECK(BarkWhenTracedClass::finalizeCount == 1);
michael@0 89
michael@0 90 return true;
michael@0 91 }
michael@0 92 END_TEST(test_PersistentRooted)
michael@0 93
michael@0 94 // GC should not be upset by null PersistentRooteds.
michael@0 95 BEGIN_TEST(test_PersistentRootedNull)
michael@0 96 {
michael@0 97 BarkWhenTracedClass::reset();
michael@0 98
michael@0 99 Kennel kennel(cx);
michael@0 100 CHECK(!kennel.obj);
michael@0 101
michael@0 102 JS_GC(JS_GetRuntime(cx));
michael@0 103 CHECK(BarkWhenTracedClass::finalizeCount == 0);
michael@0 104
michael@0 105 return true;
michael@0 106 }
michael@0 107 END_TEST(test_PersistentRootedNull)
michael@0 108
michael@0 109 // Copy construction works.
michael@0 110 BEGIN_TEST(test_PersistentRootedCopy)
michael@0 111 {
michael@0 112 BarkWhenTracedClass::reset();
michael@0 113
michael@0 114 Kennel *kennel = Allocate(cx);
michael@0 115 CHECK(kennel);
michael@0 116
michael@0 117 CHECK(GCFinalizesNBarkers(cx, 0));
michael@0 118
michael@0 119 // Copy construction! AMAZING!
michael@0 120 Kennel *newKennel = new Kennel(*kennel);
michael@0 121
michael@0 122 CHECK(GCFinalizesNBarkers(cx, 0));
michael@0 123
michael@0 124 delete(kennel);
michael@0 125
michael@0 126 CHECK(GCFinalizesNBarkers(cx, 0));
michael@0 127
michael@0 128 delete(newKennel);
michael@0 129
michael@0 130 // Now that kennel and nowKennel are both deallocated, GC should not be
michael@0 131 // able to find the barker.
michael@0 132 JS_GC(JS_GetRuntime(cx));
michael@0 133 CHECK(BarkWhenTracedClass::finalizeCount == 1);
michael@0 134
michael@0 135 return true;
michael@0 136 }
michael@0 137 END_TEST(test_PersistentRootedCopy)
michael@0 138
michael@0 139 // Assignment works.
michael@0 140 BEGIN_TEST(test_PersistentRootedAssign)
michael@0 141 {
michael@0 142 BarkWhenTracedClass::reset();
michael@0 143
michael@0 144 Kennel *kennel = Allocate(cx);
michael@0 145 CHECK(kennel);
michael@0 146
michael@0 147 CHECK(GCFinalizesNBarkers(cx, 0));
michael@0 148
michael@0 149 // Allocate a new, empty kennel.
michael@0 150 Kennel *kennel2 = new Kennel(cx);
michael@0 151
michael@0 152 // Assignment! ASTONISHING!
michael@0 153 *kennel2 = *kennel;
michael@0 154
michael@0 155 // With both kennels referring to the same barker, it is held alive.
michael@0 156 CHECK(GCFinalizesNBarkers(cx, 0));
michael@0 157
michael@0 158 delete(kennel2);
michael@0 159
michael@0 160 // The destination of the assignment alone holds the barker alive.
michael@0 161 CHECK(GCFinalizesNBarkers(cx, 0));
michael@0 162
michael@0 163 // Allocate a second barker.
michael@0 164 kennel2 = Allocate(cx);
michael@0 165 CHECK(kennel);
michael@0 166
michael@0 167 *kennel = *kennel2;
michael@0 168
michael@0 169 // Nothing refers to the first kennel any more.
michael@0 170 CHECK(GCFinalizesNBarkers(cx, 1));
michael@0 171
michael@0 172 delete(kennel);
michael@0 173 delete(kennel2);
michael@0 174
michael@0 175 // Now that kennel and kennel2 are both deallocated, GC should not be
michael@0 176 // able to find the barker.
michael@0 177 JS_GC(JS_GetRuntime(cx));
michael@0 178 CHECK(BarkWhenTracedClass::finalizeCount == 2);
michael@0 179
michael@0 180 return true;
michael@0 181 }
michael@0 182 END_TEST(test_PersistentRootedAssign)
michael@0 183
michael@0 184 #endif // defined(JSGC_USE_EXACT_ROOTING)

mercurial