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"
19 typedef uint64_t ObjectId
;
21 class JavaScriptShared
;
23 class CpowIdHolder
: public CpowHolder
26 CpowIdHolder(JavaScriptShared
*js
, const InfallibleTArray
<CpowEntry
> &cpows
)
32 bool ToObject(JSContext
*cx
, JS::MutableHandleObject objp
);
35 JavaScriptShared
*js_
;
36 const InfallibleTArray
<CpowEntry
> &cpows_
;
39 // Map ids -> JSObjects
42 typedef js::DefaultHasher
<ObjectId
> TableKeyHasher
;
44 typedef js::HashMap
<ObjectId
, JS::Heap
<JSObject
*>, TableKeyHasher
, js::SystemAllocPolicy
> Table
;
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
);
61 // Map JSObjects -> ids
64 typedef js::PointerHasher
<JSObject
*, 3> Hasher
;
65 typedef js::HashMap
<JSObject
*, ObjectId
, Hasher
, js::SystemAllocPolicy
> Table
;
72 void finalize(JSFreeOp
*fop
);
74 bool add(JSContext
*cx
, JSObject
*obj
, ObjectId id
);
75 ObjectId
find(JSObject
*obj
);
76 void remove(JSObject
*obj
);
79 static void keyMarkCallback(JSTracer
*trc
, JSObject
*key
, void *data
);
86 class JavaScriptShared
89 JavaScriptShared(JSRuntime
*rt
);
90 virtual ~JavaScriptShared() {}
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
);
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;
140 IdToObjectMap objects_
;
141 IdToObjectMap cpows_
;
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;
155 } // namespace mozilla