|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef jswatchpoint_h |
|
8 #define jswatchpoint_h |
|
9 |
|
10 #include "jsalloc.h" |
|
11 |
|
12 #include "gc/Barrier.h" |
|
13 #include "js/HashTable.h" |
|
14 #include "js/OldDebugAPI.h" |
|
15 |
|
16 namespace js { |
|
17 |
|
18 struct WeakMapTracer; |
|
19 |
|
20 struct WatchKey { |
|
21 WatchKey() {} |
|
22 WatchKey(JSObject *obj, jsid id) : object(obj), id(id) {} |
|
23 WatchKey(const WatchKey &key) : object(key.object.get()), id(key.id.get()) {} |
|
24 EncapsulatedPtrObject object; |
|
25 EncapsulatedId id; |
|
26 |
|
27 bool operator!=(const WatchKey &other) const { |
|
28 return object != other.object || id != other.id; |
|
29 } |
|
30 }; |
|
31 |
|
32 struct Watchpoint { |
|
33 JSWatchPointHandler handler; |
|
34 EncapsulatedPtrObject closure; /* This is always marked in minor GCs and so doesn't require a postbarrier. */ |
|
35 bool held; /* true if currently running handler */ |
|
36 Watchpoint(JSWatchPointHandler handler, JSObject* closure, bool held) |
|
37 : handler(handler), closure(closure), held(held) {} |
|
38 }; |
|
39 |
|
40 template <> |
|
41 struct DefaultHasher<WatchKey> |
|
42 { |
|
43 typedef WatchKey Lookup; |
|
44 static inline js::HashNumber hash(const Lookup &key); |
|
45 |
|
46 static bool match(const WatchKey &k, const Lookup &l) { |
|
47 return k.object == l.object && k.id.get() == l.id.get(); |
|
48 } |
|
49 |
|
50 static void rekey(WatchKey &k, const WatchKey& newKey) { |
|
51 k.object.unsafeSet(newKey.object); |
|
52 k.id.unsafeSet(newKey.id); |
|
53 } |
|
54 }; |
|
55 |
|
56 class WatchpointMap { |
|
57 public: |
|
58 typedef HashMap<WatchKey, Watchpoint, DefaultHasher<WatchKey>, SystemAllocPolicy> Map; |
|
59 |
|
60 bool init(); |
|
61 bool watch(JSContext *cx, HandleObject obj, HandleId id, |
|
62 JSWatchPointHandler handler, HandleObject closure); |
|
63 void unwatch(JSObject *obj, jsid id, |
|
64 JSWatchPointHandler *handlerp, JSObject **closurep); |
|
65 void unwatchObject(JSObject *obj); |
|
66 void clear(); |
|
67 |
|
68 bool triggerWatchpoint(JSContext *cx, HandleObject obj, HandleId id, MutableHandleValue vp); |
|
69 |
|
70 static bool markCompartmentIteratively(JSCompartment *c, JSTracer *trc); |
|
71 bool markIteratively(JSTracer *trc); |
|
72 void markAll(JSTracer *trc); |
|
73 static void sweepAll(JSRuntime *rt); |
|
74 void sweep(); |
|
75 |
|
76 static void traceAll(WeakMapTracer *trc); |
|
77 void trace(WeakMapTracer *trc); |
|
78 |
|
79 private: |
|
80 Map map; |
|
81 }; |
|
82 |
|
83 } |
|
84 |
|
85 #endif /* jswatchpoint_h */ |