Bumping manifests a=b2g-bump
[gecko.git] / js / ipc / JavaScriptShared.h
blobf8c6f81c62a59677a859c122c6d9190c03c96a62
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=80:
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef mozilla_jsipc_JavaScriptShared_h__
9 #define mozilla_jsipc_JavaScriptShared_h__
11 #include "mozilla/dom/DOMTypes.h"
12 #include "mozilla/jsipc/PJavaScript.h"
13 #include "nsJSUtils.h"
14 #include "nsFrameMessageManager.h"
16 namespace mozilla {
17 namespace jsipc {
19 typedef uint64_t ObjectId;
21 class JavaScriptShared;
23 class CpowIdHolder : public CpowHolder
25 public:
26 CpowIdHolder(JavaScriptShared *js, const InfallibleTArray<CpowEntry> &cpows)
27 : js_(js),
28 cpows_(cpows)
32 bool ToObject(JSContext *cx, JS::MutableHandleObject objp);
34 private:
35 JavaScriptShared *js_;
36 const InfallibleTArray<CpowEntry> &cpows_;
39 // Map ids -> JSObjects
40 class IdToObjectMap
42 typedef js::DefaultHasher<ObjectId> TableKeyHasher;
44 typedef js::HashMap<ObjectId, JS::Heap<JSObject *>, TableKeyHasher, js::SystemAllocPolicy> Table;
46 public:
47 IdToObjectMap();
49 bool init();
50 void trace(JSTracer *trc);
51 void finalize(JSFreeOp *fop);
53 bool add(ObjectId id, JSObject *obj);
54 JSObject *find(ObjectId id);
55 void remove(ObjectId id);
57 private:
58 Table table_;
61 // Map JSObjects -> ids
62 class ObjectToIdMap
64 typedef js::PointerHasher<JSObject *, 3> Hasher;
65 typedef js::HashMap<JSObject *, ObjectId, Hasher, js::SystemAllocPolicy> Table;
67 public:
68 ObjectToIdMap();
69 ~ObjectToIdMap();
71 bool init();
72 void finalize(JSFreeOp *fop);
74 bool add(JSContext *cx, JSObject *obj, ObjectId id);
75 ObjectId find(JSObject *obj);
76 void remove(JSObject *obj);
78 private:
79 static void keyMarkCallback(JSTracer *trc, JSObject *key, void *data);
81 Table *table_;
84 class Logging;
86 class JavaScriptShared
88 public:
89 JavaScriptShared(JSRuntime *rt);
90 virtual ~JavaScriptShared() {}
92 bool init();
94 void decref();
95 void incref();
97 static const uint32_t OBJECT_EXTRA_BITS = 1;
98 static const uint32_t OBJECT_IS_CALLABLE = (1 << 0);
100 bool Unwrap(JSContext *cx, const InfallibleTArray<CpowEntry> &aCpows, JS::MutableHandleObject objp);
101 bool Wrap(JSContext *cx, JS::HandleObject aObj, InfallibleTArray<CpowEntry> *outCpows);
103 protected:
104 bool toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to);
105 bool fromVariant(JSContext *cx, const JSVariant &from, JS::MutableHandleValue to);
107 bool fromDescriptor(JSContext *cx, JS::Handle<JSPropertyDescriptor> desc,
108 PPropertyDescriptor *out);
109 bool toDescriptor(JSContext *cx, const PPropertyDescriptor &in,
110 JS::MutableHandle<JSPropertyDescriptor> out);
112 bool convertIdToGeckoString(JSContext *cx, JS::HandleId id, nsString *to);
113 bool convertGeckoStringToId(JSContext *cx, const nsString &from, JS::MutableHandleId id);
115 virtual bool toObjectVariant(JSContext *cx, JSObject *obj, ObjectVariant *objVarp) = 0;
116 virtual JSObject *fromObjectVariant(JSContext *cx, ObjectVariant objVar) = 0;
118 static void ConvertID(const nsID &from, JSIID *to);
119 static void ConvertID(const JSIID &from, nsID *to);
121 JSObject *findCPOWById(uint32_t objId) {
122 return cpows_.find(objId);
124 JSObject *findObjectById(uint32_t objId) {
125 return objects_.find(objId);
127 JSObject *findObjectById(JSContext *cx, uint32_t objId);
129 static bool LoggingEnabled() { return sLoggingEnabled; }
130 static bool StackLoggingEnabled() { return sStackLoggingEnabled; }
132 friend class Logging;
134 virtual bool isParent() = 0;
136 protected:
137 JSRuntime *rt_;
138 uintptr_t refcount_;
140 IdToObjectMap objects_;
141 IdToObjectMap cpows_;
143 ObjectId lastId_;
144 ObjectToIdMap objectIds_;
146 static bool sLoggingInitialized;
147 static bool sLoggingEnabled;
148 static bool sStackLoggingEnabled;
151 // Use 47 at most, to be safe, since jsval privates are encoded as doubles.
152 static const uint64_t MAX_CPOW_IDS = (uint64_t(1) << 47) - 1;
154 } // namespace jsipc
155 } // namespace mozilla
157 #endif