2013-07-03 07:24:32 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sw=4 et tw=80:
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_jsipc_JavaScriptShared_h__
|
|
|
|
#define mozilla_jsipc_JavaScriptShared_h__
|
|
|
|
|
|
|
|
#include "mozilla/dom/DOMTypes.h"
|
|
|
|
#include "mozilla/jsipc/PJavaScript.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsFrameMessageManager.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace jsipc {
|
|
|
|
|
|
|
|
typedef uint64_t ObjectId;
|
|
|
|
|
2013-07-10 22:05:39 +00:00
|
|
|
class JavaScriptShared;
|
|
|
|
|
|
|
|
class CpowIdHolder : public CpowHolder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CpowIdHolder(JavaScriptShared *js, const InfallibleTArray<CpowEntry> &cpows)
|
|
|
|
: js_(js),
|
|
|
|
cpows_(cpows)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-16 00:02:23 +00:00
|
|
|
bool ToObject(JSContext *cx, JS::MutableHandleObject objp);
|
2013-07-10 22:05:39 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
JavaScriptShared *js_;
|
|
|
|
const InfallibleTArray<CpowEntry> &cpows_;
|
|
|
|
};
|
|
|
|
|
2013-07-03 07:24:32 +00:00
|
|
|
// Map ids -> JSObjects
|
2014-05-16 23:40:35 +00:00
|
|
|
class IdToObjectMap
|
2013-07-03 07:24:32 +00:00
|
|
|
{
|
|
|
|
typedef js::DefaultHasher<ObjectId> TableKeyHasher;
|
|
|
|
|
2014-05-16 23:40:35 +00:00
|
|
|
typedef js::HashMap<ObjectId, JS::Heap<JSObject *>, TableKeyHasher, js::SystemAllocPolicy> Table;
|
2013-07-03 07:24:32 +00:00
|
|
|
|
|
|
|
public:
|
2014-05-16 23:40:35 +00:00
|
|
|
IdToObjectMap();
|
2013-07-03 07:24:32 +00:00
|
|
|
|
|
|
|
bool init();
|
|
|
|
void trace(JSTracer *trc);
|
2014-05-16 23:40:37 +00:00
|
|
|
void finalize(JSFreeOp *fop);
|
2013-07-03 07:24:32 +00:00
|
|
|
|
|
|
|
bool add(ObjectId id, JSObject *obj);
|
|
|
|
JSObject *find(ObjectId id);
|
|
|
|
void remove(ObjectId id);
|
|
|
|
|
|
|
|
private:
|
2014-05-16 23:40:35 +00:00
|
|
|
Table table_;
|
2013-07-03 07:24:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Map JSObjects -> ids
|
2014-05-16 23:40:35 +00:00
|
|
|
class ObjectToIdMap
|
2013-07-03 07:24:32 +00:00
|
|
|
{
|
|
|
|
typedef js::PointerHasher<JSObject *, 3> Hasher;
|
2014-05-16 23:40:35 +00:00
|
|
|
typedef js::HashMap<JSObject *, ObjectId, Hasher, js::SystemAllocPolicy> Table;
|
2013-07-03 07:24:32 +00:00
|
|
|
|
|
|
|
public:
|
2014-05-16 23:40:35 +00:00
|
|
|
ObjectToIdMap();
|
|
|
|
~ObjectToIdMap();
|
2013-07-03 07:24:32 +00:00
|
|
|
|
|
|
|
bool init();
|
2014-05-16 23:40:37 +00:00
|
|
|
void finalize(JSFreeOp *fop);
|
2013-07-03 07:24:32 +00:00
|
|
|
|
2013-08-01 10:21:44 +00:00
|
|
|
bool add(JSContext *cx, JSObject *obj, ObjectId id);
|
2013-07-03 07:24:32 +00:00
|
|
|
ObjectId find(JSObject *obj);
|
|
|
|
void remove(JSObject *obj);
|
|
|
|
|
|
|
|
private:
|
2014-01-21 10:44:39 +00:00
|
|
|
static void keyMarkCallback(JSTracer *trc, JSObject *key, void *data);
|
2013-08-01 10:21:44 +00:00
|
|
|
|
2014-05-16 23:40:35 +00:00
|
|
|
Table *table_;
|
2013-07-03 07:24:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class JavaScriptShared
|
|
|
|
{
|
|
|
|
public:
|
2014-05-16 23:40:37 +00:00
|
|
|
JavaScriptShared(JSRuntime *rt);
|
|
|
|
virtual ~JavaScriptShared() {}
|
|
|
|
|
2013-07-03 07:24:32 +00:00
|
|
|
bool init();
|
|
|
|
|
2014-05-16 23:40:37 +00:00
|
|
|
void decref();
|
|
|
|
void incref();
|
|
|
|
|
2013-07-03 07:24:32 +00:00
|
|
|
static const uint32_t OBJECT_EXTRA_BITS = 1;
|
|
|
|
static const uint32_t OBJECT_IS_CALLABLE = (1 << 0);
|
|
|
|
|
2013-10-16 00:02:23 +00:00
|
|
|
bool Unwrap(JSContext *cx, const InfallibleTArray<CpowEntry> &aCpows, JS::MutableHandleObject objp);
|
2013-07-10 22:05:39 +00:00
|
|
|
bool Wrap(JSContext *cx, JS::HandleObject aObj, InfallibleTArray<CpowEntry> *outCpows);
|
|
|
|
|
2013-07-03 07:24:32 +00:00
|
|
|
protected:
|
2013-10-12 05:02:24 +00:00
|
|
|
bool toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to);
|
2014-05-16 23:40:35 +00:00
|
|
|
bool fromVariant(JSContext *cx, const JSVariant &from, JS::MutableHandleValue to);
|
|
|
|
|
|
|
|
bool fromDescriptor(JSContext *cx, JS::Handle<JSPropertyDescriptor> desc,
|
|
|
|
PPropertyDescriptor *out);
|
2013-04-30 17:29:40 +00:00
|
|
|
bool toDescriptor(JSContext *cx, const PPropertyDescriptor &in,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> out);
|
2014-05-16 23:40:35 +00:00
|
|
|
|
2013-07-03 07:24:32 +00:00
|
|
|
bool convertIdToGeckoString(JSContext *cx, JS::HandleId id, nsString *to);
|
|
|
|
bool convertGeckoStringToId(JSContext *cx, const nsString &from, JS::MutableHandleId id);
|
|
|
|
|
2014-05-16 23:40:36 +00:00
|
|
|
virtual bool toObjectVariant(JSContext *cx, JSObject *obj, ObjectVariant *objVarp) = 0;
|
|
|
|
virtual JSObject *fromObjectVariant(JSContext *cx, ObjectVariant objVar) = 0;
|
2013-07-03 07:24:32 +00:00
|
|
|
|
|
|
|
static void ConvertID(const nsID &from, JSIID *to);
|
|
|
|
static void ConvertID(const JSIID &from, nsID *to);
|
|
|
|
|
2014-05-16 23:40:35 +00:00
|
|
|
JSObject *findCPOWById(uint32_t objId) {
|
2014-05-16 23:40:36 +00:00
|
|
|
return cpows_.find(objId);
|
2014-05-16 23:40:35 +00:00
|
|
|
}
|
|
|
|
JSObject *findObjectById(uint32_t objId) {
|
2013-07-03 07:24:32 +00:00
|
|
|
return objects_.find(objId);
|
|
|
|
}
|
2014-05-16 23:40:37 +00:00
|
|
|
JSObject *findObjectById(JSContext *cx, uint32_t objId);
|
2013-07-03 07:24:32 +00:00
|
|
|
|
|
|
|
protected:
|
2014-05-16 23:40:37 +00:00
|
|
|
JSRuntime *rt_;
|
|
|
|
uintptr_t refcount_;
|
|
|
|
|
2014-05-16 23:40:35 +00:00
|
|
|
IdToObjectMap objects_;
|
2014-05-16 23:40:36 +00:00
|
|
|
IdToObjectMap cpows_;
|
2014-05-16 23:40:37 +00:00
|
|
|
|
|
|
|
ObjectId lastId_;
|
|
|
|
ObjectToIdMap objectIds_;
|
2013-07-03 07:24:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Use 47 at most, to be safe, since jsval privates are encoded as doubles.
|
|
|
|
static const uint64_t MAX_CPOW_IDS = (uint64_t(1) << 47) - 1;
|
|
|
|
|
|
|
|
} // namespace jsipc
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|