Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "jswatchpoint.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "jsatom.h" |
michael@0 | 10 | #include "jscompartment.h" |
michael@0 | 11 | #include "jsfriendapi.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "gc/Marking.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "jsgcinlines.h" |
michael@0 | 16 | |
michael@0 | 17 | using namespace js; |
michael@0 | 18 | using namespace js::gc; |
michael@0 | 19 | |
michael@0 | 20 | inline HashNumber |
michael@0 | 21 | DefaultHasher<WatchKey>::hash(const Lookup &key) |
michael@0 | 22 | { |
michael@0 | 23 | return DefaultHasher<JSObject *>::hash(key.object.get()) ^ HashId(key.id.get()); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | namespace { |
michael@0 | 27 | |
michael@0 | 28 | class AutoEntryHolder { |
michael@0 | 29 | typedef WatchpointMap::Map Map; |
michael@0 | 30 | Map ↦ |
michael@0 | 31 | Map::Ptr p; |
michael@0 | 32 | uint32_t gen; |
michael@0 | 33 | RootedObject obj; |
michael@0 | 34 | RootedId id; |
michael@0 | 35 | |
michael@0 | 36 | public: |
michael@0 | 37 | AutoEntryHolder(JSContext *cx, Map &map, Map::Ptr p) |
michael@0 | 38 | : map(map), p(p), gen(map.generation()), obj(cx, p->key().object), id(cx, p->key().id) |
michael@0 | 39 | { |
michael@0 | 40 | JS_ASSERT(!p->value().held); |
michael@0 | 41 | p->value().held = true; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | ~AutoEntryHolder() { |
michael@0 | 45 | if (gen != map.generation()) |
michael@0 | 46 | p = map.lookup(WatchKey(obj, id)); |
michael@0 | 47 | if (p) |
michael@0 | 48 | p->value().held = false; |
michael@0 | 49 | } |
michael@0 | 50 | }; |
michael@0 | 51 | |
michael@0 | 52 | } /* anonymous namespace */ |
michael@0 | 53 | |
michael@0 | 54 | bool |
michael@0 | 55 | WatchpointMap::init() |
michael@0 | 56 | { |
michael@0 | 57 | return map.init(); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | bool |
michael@0 | 61 | WatchpointMap::watch(JSContext *cx, HandleObject obj, HandleId id, |
michael@0 | 62 | JSWatchPointHandler handler, HandleObject closure) |
michael@0 | 63 | { |
michael@0 | 64 | JS_ASSERT(JSID_IS_STRING(id) || JSID_IS_INT(id)); |
michael@0 | 65 | |
michael@0 | 66 | if (!obj->setWatched(cx)) |
michael@0 | 67 | return false; |
michael@0 | 68 | |
michael@0 | 69 | Watchpoint w(handler, closure, false); |
michael@0 | 70 | if (!map.put(WatchKey(obj, id), w)) { |
michael@0 | 71 | js_ReportOutOfMemory(cx); |
michael@0 | 72 | return false; |
michael@0 | 73 | } |
michael@0 | 74 | /* |
michael@0 | 75 | * For generational GC, we don't need to post-barrier writes to the |
michael@0 | 76 | * hashtable here because we mark all watchpoints as part of root marking in |
michael@0 | 77 | * markAll(). |
michael@0 | 78 | */ |
michael@0 | 79 | return true; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void |
michael@0 | 83 | WatchpointMap::unwatch(JSObject *obj, jsid id, |
michael@0 | 84 | JSWatchPointHandler *handlerp, JSObject **closurep) |
michael@0 | 85 | { |
michael@0 | 86 | if (Map::Ptr p = map.lookup(WatchKey(obj, id))) { |
michael@0 | 87 | if (handlerp) |
michael@0 | 88 | *handlerp = p->value().handler; |
michael@0 | 89 | if (closurep) { |
michael@0 | 90 | // Read barrier to prevent an incorrectly gray closure from escaping the |
michael@0 | 91 | // watchpoint. See the comment before UnmarkGrayChildren in gc/Marking.cpp |
michael@0 | 92 | JS::ExposeGCThingToActiveJS(p->value().closure, JSTRACE_OBJECT); |
michael@0 | 93 | *closurep = p->value().closure; |
michael@0 | 94 | } |
michael@0 | 95 | map.remove(p); |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | void |
michael@0 | 100 | WatchpointMap::unwatchObject(JSObject *obj) |
michael@0 | 101 | { |
michael@0 | 102 | for (Map::Enum e(map); !e.empty(); e.popFront()) { |
michael@0 | 103 | Map::Entry &entry = e.front(); |
michael@0 | 104 | if (entry.key().object == obj) |
michael@0 | 105 | e.removeFront(); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | void |
michael@0 | 110 | WatchpointMap::clear() |
michael@0 | 111 | { |
michael@0 | 112 | map.clear(); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | bool |
michael@0 | 116 | WatchpointMap::triggerWatchpoint(JSContext *cx, HandleObject obj, HandleId id, MutableHandleValue vp) |
michael@0 | 117 | { |
michael@0 | 118 | Map::Ptr p = map.lookup(WatchKey(obj, id)); |
michael@0 | 119 | if (!p || p->value().held) |
michael@0 | 120 | return true; |
michael@0 | 121 | |
michael@0 | 122 | AutoEntryHolder holder(cx, map, p); |
michael@0 | 123 | |
michael@0 | 124 | /* Copy the entry, since GC would invalidate p. */ |
michael@0 | 125 | JSWatchPointHandler handler = p->value().handler; |
michael@0 | 126 | RootedObject closure(cx, p->value().closure); |
michael@0 | 127 | |
michael@0 | 128 | /* Determine the property's old value. */ |
michael@0 | 129 | Value old; |
michael@0 | 130 | old.setUndefined(); |
michael@0 | 131 | if (obj->isNative()) { |
michael@0 | 132 | if (Shape *shape = obj->nativeLookup(cx, id)) { |
michael@0 | 133 | if (shape->hasSlot()) |
michael@0 | 134 | old = obj->nativeGetSlot(shape->slot()); |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // Read barrier to prevent an incorrectly gray closure from escaping the |
michael@0 | 139 | // watchpoint. See the comment before UnmarkGrayChildren in gc/Marking.cpp |
michael@0 | 140 | JS::ExposeGCThingToActiveJS(closure, JSTRACE_OBJECT); |
michael@0 | 141 | |
michael@0 | 142 | /* Call the handler. */ |
michael@0 | 143 | return handler(cx, obj, id, old, vp.address(), closure); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | bool |
michael@0 | 147 | WatchpointMap::markCompartmentIteratively(JSCompartment *c, JSTracer *trc) |
michael@0 | 148 | { |
michael@0 | 149 | if (!c->watchpointMap) |
michael@0 | 150 | return false; |
michael@0 | 151 | return c->watchpointMap->markIteratively(trc); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | bool |
michael@0 | 155 | WatchpointMap::markIteratively(JSTracer *trc) |
michael@0 | 156 | { |
michael@0 | 157 | bool marked = false; |
michael@0 | 158 | for (Map::Enum e(map); !e.empty(); e.popFront()) { |
michael@0 | 159 | Map::Entry &entry = e.front(); |
michael@0 | 160 | JSObject *priorKeyObj = entry.key().object; |
michael@0 | 161 | jsid priorKeyId(entry.key().id.get()); |
michael@0 | 162 | bool objectIsLive = |
michael@0 | 163 | IsObjectMarked(const_cast<EncapsulatedPtrObject *>(&entry.key().object)); |
michael@0 | 164 | if (objectIsLive || entry.value().held) { |
michael@0 | 165 | if (!objectIsLive) { |
michael@0 | 166 | MarkObject(trc, const_cast<EncapsulatedPtrObject *>(&entry.key().object), |
michael@0 | 167 | "held Watchpoint object"); |
michael@0 | 168 | marked = true; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | JS_ASSERT(JSID_IS_STRING(priorKeyId) || JSID_IS_INT(priorKeyId)); |
michael@0 | 172 | MarkId(trc, const_cast<EncapsulatedId *>(&entry.key().id), "WatchKey::id"); |
michael@0 | 173 | |
michael@0 | 174 | if (entry.value().closure && !IsObjectMarked(&entry.value().closure)) { |
michael@0 | 175 | MarkObject(trc, &entry.value().closure, "Watchpoint::closure"); |
michael@0 | 176 | marked = true; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | /* We will sweep this entry in sweepAll if !objectIsLive. */ |
michael@0 | 180 | if (priorKeyObj != entry.key().object || priorKeyId != entry.key().id) |
michael@0 | 181 | e.rekeyFront(WatchKey(entry.key().object, entry.key().id)); |
michael@0 | 182 | } |
michael@0 | 183 | } |
michael@0 | 184 | return marked; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | void |
michael@0 | 188 | WatchpointMap::markAll(JSTracer *trc) |
michael@0 | 189 | { |
michael@0 | 190 | for (Map::Enum e(map); !e.empty(); e.popFront()) { |
michael@0 | 191 | Map::Entry &entry = e.front(); |
michael@0 | 192 | WatchKey key = entry.key(); |
michael@0 | 193 | WatchKey prior = key; |
michael@0 | 194 | JS_ASSERT(JSID_IS_STRING(prior.id) || JSID_IS_INT(prior.id)); |
michael@0 | 195 | |
michael@0 | 196 | MarkObject(trc, const_cast<EncapsulatedPtrObject *>(&key.object), |
michael@0 | 197 | "held Watchpoint object"); |
michael@0 | 198 | MarkId(trc, const_cast<EncapsulatedId *>(&key.id), "WatchKey::id"); |
michael@0 | 199 | MarkObject(trc, &entry.value().closure, "Watchpoint::closure"); |
michael@0 | 200 | |
michael@0 | 201 | if (prior.object != key.object || prior.id != key.id) |
michael@0 | 202 | e.rekeyFront(key); |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | void |
michael@0 | 207 | WatchpointMap::sweepAll(JSRuntime *rt) |
michael@0 | 208 | { |
michael@0 | 209 | for (GCCompartmentsIter c(rt); !c.done(); c.next()) { |
michael@0 | 210 | if (WatchpointMap *wpmap = c->watchpointMap) |
michael@0 | 211 | wpmap->sweep(); |
michael@0 | 212 | } |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | void |
michael@0 | 216 | WatchpointMap::sweep() |
michael@0 | 217 | { |
michael@0 | 218 | for (Map::Enum e(map); !e.empty(); e.popFront()) { |
michael@0 | 219 | Map::Entry &entry = e.front(); |
michael@0 | 220 | JSObject *obj(entry.key().object); |
michael@0 | 221 | if (IsObjectAboutToBeFinalized(&obj)) { |
michael@0 | 222 | JS_ASSERT(!entry.value().held); |
michael@0 | 223 | e.removeFront(); |
michael@0 | 224 | } else if (obj != entry.key().object) { |
michael@0 | 225 | e.rekeyFront(WatchKey(obj, entry.key().id)); |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | void |
michael@0 | 231 | WatchpointMap::traceAll(WeakMapTracer *trc) |
michael@0 | 232 | { |
michael@0 | 233 | JSRuntime *rt = trc->runtime; |
michael@0 | 234 | for (CompartmentsIter comp(rt, SkipAtoms); !comp.done(); comp.next()) { |
michael@0 | 235 | if (WatchpointMap *wpmap = comp->watchpointMap) |
michael@0 | 236 | wpmap->trace(trc); |
michael@0 | 237 | } |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | void |
michael@0 | 241 | WatchpointMap::trace(WeakMapTracer *trc) |
michael@0 | 242 | { |
michael@0 | 243 | for (Map::Range r = map.all(); !r.empty(); r.popFront()) { |
michael@0 | 244 | Map::Entry &entry = r.front(); |
michael@0 | 245 | trc->callback(trc, nullptr, |
michael@0 | 246 | entry.key().object.get(), JSTRACE_OBJECT, |
michael@0 | 247 | entry.value().closure.get(), JSTRACE_OBJECT); |
michael@0 | 248 | } |
michael@0 | 249 | } |