js/ipc/JavaScriptShared.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/ipc/JavaScriptShared.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,141 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sw=4 et tw=80:
     1.6 + *
     1.7 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#ifndef mozilla_jsipc_JavaScriptShared_h__
    1.12 +#define mozilla_jsipc_JavaScriptShared_h__
    1.13 +
    1.14 +#include "mozilla/dom/DOMTypes.h"
    1.15 +#include "mozilla/jsipc/PJavaScript.h"
    1.16 +#include "nsJSUtils.h"
    1.17 +#include "nsFrameMessageManager.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace jsipc {
    1.21 +
    1.22 +typedef uint64_t ObjectId;
    1.23 +
    1.24 +class JavaScriptShared;
    1.25 +
    1.26 +class CpowIdHolder : public CpowHolder
    1.27 +{
    1.28 +  public:
    1.29 +    CpowIdHolder(JavaScriptShared *js, const InfallibleTArray<CpowEntry> &cpows)
    1.30 +      : js_(js),
    1.31 +        cpows_(cpows)
    1.32 +    {
    1.33 +    }
    1.34 +
    1.35 +    bool ToObject(JSContext *cx, JS::MutableHandleObject objp);
    1.36 +
    1.37 +  private:
    1.38 +    JavaScriptShared *js_;
    1.39 +    const InfallibleTArray<CpowEntry> &cpows_;
    1.40 +};
    1.41 +
    1.42 +// Map ids -> JSObjects
    1.43 +class ObjectStore
    1.44 +{
    1.45 +    typedef js::DefaultHasher<ObjectId> TableKeyHasher;
    1.46 +
    1.47 +    typedef js::HashMap<ObjectId, JS::Heap<JSObject *>, TableKeyHasher, js::SystemAllocPolicy> ObjectTable;
    1.48 +
    1.49 +  public:
    1.50 +    ObjectStore();
    1.51 +
    1.52 +    bool init();
    1.53 +    void trace(JSTracer *trc);
    1.54 +
    1.55 +    bool add(ObjectId id, JSObject *obj);
    1.56 +    JSObject *find(ObjectId id);
    1.57 +    void remove(ObjectId id);
    1.58 +
    1.59 +  private:
    1.60 +    ObjectTable table_;
    1.61 +};
    1.62 +
    1.63 +// Map JSObjects -> ids
    1.64 +class ObjectIdCache
    1.65 +{
    1.66 +    typedef js::PointerHasher<JSObject *, 3> Hasher;
    1.67 +    typedef js::HashMap<JSObject *, ObjectId, Hasher, js::SystemAllocPolicy> ObjectIdTable;
    1.68 +
    1.69 +  public:
    1.70 +    ObjectIdCache();
    1.71 +    ~ObjectIdCache();
    1.72 +
    1.73 +    bool init();
    1.74 +    void trace(JSTracer *trc);
    1.75 +
    1.76 +    bool add(JSContext *cx, JSObject *obj, ObjectId id);
    1.77 +    ObjectId find(JSObject *obj);
    1.78 +    void remove(JSObject *obj);
    1.79 +
    1.80 +  private:
    1.81 +    static void keyMarkCallback(JSTracer *trc, JSObject *key, void *data);
    1.82 +
    1.83 +    ObjectIdTable *table_;
    1.84 +};
    1.85 +
    1.86 +class JavaScriptShared
    1.87 +{
    1.88 +  public:
    1.89 +    bool init();
    1.90 +
    1.91 +    static const uint32_t OBJECT_EXTRA_BITS  = 1;
    1.92 +    static const uint32_t OBJECT_IS_CALLABLE = (1 << 0);
    1.93 +
    1.94 +    bool Unwrap(JSContext *cx, const InfallibleTArray<CpowEntry> &aCpows, JS::MutableHandleObject objp);
    1.95 +    bool Wrap(JSContext *cx, JS::HandleObject aObj, InfallibleTArray<CpowEntry> *outCpows);
    1.96 +
    1.97 +  protected:
    1.98 +    bool toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to);
    1.99 +    bool toValue(JSContext *cx, const JSVariant &from, JS::MutableHandleValue to);
   1.100 +    bool fromDescriptor(JSContext *cx, JS::Handle<JSPropertyDescriptor> desc, PPropertyDescriptor *out);
   1.101 +    bool toDescriptor(JSContext *cx, const PPropertyDescriptor &in,
   1.102 +                      JS::MutableHandle<JSPropertyDescriptor> out);
   1.103 +    bool convertIdToGeckoString(JSContext *cx, JS::HandleId id, nsString *to);
   1.104 +    bool convertGeckoStringToId(JSContext *cx, const nsString &from, JS::MutableHandleId id);
   1.105 +
   1.106 +    bool toValue(JSContext *cx, const JSVariant &from, jsval *to) {
   1.107 +        JS::RootedValue v(cx);
   1.108 +        if (!toValue(cx, from, &v))
   1.109 +            return false;
   1.110 +        *to = v;
   1.111 +        return true;
   1.112 +    }
   1.113 +
   1.114 +    virtual bool makeId(JSContext *cx, JSObject *obj, ObjectId *idp) = 0;
   1.115 +    virtual JSObject *unwrap(JSContext *cx, ObjectId id) = 0;
   1.116 +
   1.117 +    bool unwrap(JSContext *cx, ObjectId id, JS::MutableHandle<JSObject*> objp) {
   1.118 +        if (!id) {
   1.119 +            objp.set(nullptr);
   1.120 +            return true;
   1.121 +        }
   1.122 +
   1.123 +        objp.set(unwrap(cx, id));
   1.124 +        return bool(objp.get());
   1.125 +    }
   1.126 +
   1.127 +    static void ConvertID(const nsID &from, JSIID *to);
   1.128 +    static void ConvertID(const JSIID &from, nsID *to);
   1.129 +
   1.130 +    JSObject *findObject(uint32_t objId) {
   1.131 +        return objects_.find(objId);
   1.132 +    }
   1.133 +
   1.134 +  protected:
   1.135 +    ObjectStore objects_;
   1.136 +};
   1.137 +
   1.138 +// Use 47 at most, to be safe, since jsval privates are encoded as doubles.
   1.139 +static const uint64_t MAX_CPOW_IDS = (uint64_t(1) << 47) - 1;
   1.140 +
   1.141 +} // namespace jsipc
   1.142 +} // namespace mozilla
   1.143 +
   1.144 +#endif

mercurial