dom/bindings/DOMJSProxyHandler.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef mozilla_dom_DOMJSProxyHandler_h
michael@0 7 #define mozilla_dom_DOMJSProxyHandler_h
michael@0 8
michael@0 9 #include "mozilla/Attributes.h"
michael@0 10 #include "mozilla/Likely.h"
michael@0 11
michael@0 12 #include "jsapi.h"
michael@0 13 #include "jsproxy.h"
michael@0 14 #include "nsString.h"
michael@0 15
michael@0 16 #define DOM_PROXY_OBJECT_SLOT js::PROXY_PRIVATE_SLOT
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19 namespace dom {
michael@0 20
michael@0 21 class DOMClass;
michael@0 22
michael@0 23 enum {
michael@0 24 JSPROXYSLOT_EXPANDO = 0
michael@0 25 };
michael@0 26
michael@0 27 template<typename T> struct Prefable;
michael@0 28
michael@0 29 // This variable exists solely to provide a unique address for use as an identifier.
michael@0 30 extern const char HandlerFamily;
michael@0 31 inline const void* ProxyFamily() { return &HandlerFamily; }
michael@0 32
michael@0 33 inline bool IsDOMProxy(JSObject *obj)
michael@0 34 {
michael@0 35 const js::Class* clasp = js::GetObjectClass(obj);
michael@0 36 return clasp->isProxy() &&
michael@0 37 js::GetProxyHandler(obj)->family() == ProxyFamily();
michael@0 38 }
michael@0 39
michael@0 40 class BaseDOMProxyHandler : public js::BaseProxyHandler
michael@0 41 {
michael@0 42 public:
michael@0 43 BaseDOMProxyHandler(const void* aProxyFamily)
michael@0 44 : js::BaseProxyHandler(aProxyFamily)
michael@0 45 {}
michael@0 46
michael@0 47 // Implementations of traps that can be implemented in terms of
michael@0 48 // fundamental traps.
michael@0 49 bool enumerate(JSContext* cx, JS::Handle<JSObject*> proxy,
michael@0 50 JS::AutoIdVector& props) MOZ_OVERRIDE;
michael@0 51 bool getPropertyDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy,
michael@0 52 JS::Handle<jsid> id,
michael@0 53 JS::MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE;
michael@0 54 bool getOwnPropertyDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy,
michael@0 55 JS::Handle<jsid> id,
michael@0 56 JS::MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE;
michael@0 57
michael@0 58 bool watch(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
michael@0 59 JS::Handle<JSObject*> callable) MOZ_OVERRIDE;
michael@0 60 bool unwatch(JSContext* cx, JS::Handle<JSObject*> proxy,
michael@0 61 JS::Handle<jsid> id) MOZ_OVERRIDE;
michael@0 62 virtual bool getOwnPropertyNames(JSContext* cx, JS::Handle<JSObject*> proxy,
michael@0 63 JS::AutoIdVector &props) MOZ_OVERRIDE;
michael@0 64 // We override keys() and implement it directly instead of using the
michael@0 65 // default implementation, which would getOwnPropertyNames and then
michael@0 66 // filter out the non-enumerable ones. This avoids doing
michael@0 67 // unnecessary work during enumeration.
michael@0 68 virtual bool keys(JSContext* cx, JS::Handle<JSObject*> proxy,
michael@0 69 JS::AutoIdVector &props) MOZ_OVERRIDE;
michael@0 70
michael@0 71 protected:
michael@0 72 // Hook for subclasses to implement shared getOwnPropertyNames()/keys()
michael@0 73 // functionality. The "flags" argument is either JSITER_OWNONLY (for keys())
michael@0 74 // or JSITER_OWNONLY | JSITER_HIDDEN (for getOwnPropertyNames()).
michael@0 75 virtual bool ownPropNames(JSContext* cx, JS::Handle<JSObject*> proxy,
michael@0 76 unsigned flags,
michael@0 77 JS::AutoIdVector& props) = 0;
michael@0 78
michael@0 79 // Hook for subclasses to allow set() to ignore named props while other things
michael@0 80 // that look at property descriptors see them. This is intentionally not
michael@0 81 // named getOwnPropertyDescriptor to avoid subclasses that override it hiding
michael@0 82 // our public getOwnPropertyDescriptor.
michael@0 83 virtual bool getOwnPropDescriptor(JSContext* cx,
michael@0 84 JS::Handle<JSObject*> proxy,
michael@0 85 JS::Handle<jsid> id,
michael@0 86 bool ignoreNamedProps,
michael@0 87 JS::MutableHandle<JSPropertyDescriptor> desc) = 0;
michael@0 88 };
michael@0 89
michael@0 90 class DOMProxyHandler : public BaseDOMProxyHandler
michael@0 91 {
michael@0 92 public:
michael@0 93 DOMProxyHandler()
michael@0 94 : BaseDOMProxyHandler(ProxyFamily())
michael@0 95 {
michael@0 96 }
michael@0 97
michael@0 98 bool preventExtensions(JSContext *cx, JS::Handle<JSObject*> proxy) MOZ_OVERRIDE;
michael@0 99 bool defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
michael@0 100 JS::MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE
michael@0 101 {
michael@0 102 bool unused;
michael@0 103 return defineProperty(cx, proxy, id, desc, &unused);
michael@0 104 }
michael@0 105 virtual bool defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
michael@0 106 JS::MutableHandle<JSPropertyDescriptor> desc, bool* defined);
michael@0 107 bool set(JSContext *cx, JS::Handle<JSObject*> proxy, JS::Handle<JSObject*> receiver,
michael@0 108 JS::Handle<jsid> id, bool strict, JS::MutableHandle<JS::Value> vp) MOZ_OVERRIDE;
michael@0 109 bool delete_(JSContext* cx, JS::Handle<JSObject*> proxy,
michael@0 110 JS::Handle<jsid> id, bool* bp) MOZ_OVERRIDE;
michael@0 111 bool has(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool* bp) MOZ_OVERRIDE;
michael@0 112 bool isExtensible(JSContext *cx, JS::Handle<JSObject*> proxy, bool *extensible) MOZ_OVERRIDE;
michael@0 113
michael@0 114 /*
michael@0 115 * If assigning to proxy[id] hits a named setter with OverrideBuiltins or
michael@0 116 * an indexed setter, call it and set *done to true on success. Otherwise, set
michael@0 117 * *done to false.
michael@0 118 */
michael@0 119 virtual bool setCustom(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
michael@0 120 JS::MutableHandle<JS::Value> vp, bool *done);
michael@0 121
michael@0 122 static JSObject* GetExpandoObject(JSObject* obj)
michael@0 123 {
michael@0 124 MOZ_ASSERT(IsDOMProxy(obj), "expected a DOM proxy object");
michael@0 125 JS::Value v = js::GetProxyExtra(obj, JSPROXYSLOT_EXPANDO);
michael@0 126 if (v.isObject()) {
michael@0 127 return &v.toObject();
michael@0 128 }
michael@0 129
michael@0 130 if (v.isUndefined()) {
michael@0 131 return nullptr;
michael@0 132 }
michael@0 133
michael@0 134 js::ExpandoAndGeneration* expandoAndGeneration =
michael@0 135 static_cast<js::ExpandoAndGeneration*>(v.toPrivate());
michael@0 136 v = expandoAndGeneration->expando;
michael@0 137 return v.isUndefined() ? nullptr : &v.toObject();
michael@0 138 }
michael@0 139 /* GetAndClearExpandoObject does not DROP or clear the preserving wrapper flag. */
michael@0 140 static JSObject* GetAndClearExpandoObject(JSObject* obj);
michael@0 141 static JSObject* EnsureExpandoObject(JSContext* cx,
michael@0 142 JS::Handle<JSObject*> obj);
michael@0 143 };
michael@0 144
michael@0 145 inline DOMProxyHandler*
michael@0 146 GetDOMProxyHandler(JSObject* obj)
michael@0 147 {
michael@0 148 MOZ_ASSERT(IsDOMProxy(obj));
michael@0 149 return static_cast<DOMProxyHandler*>(js::GetProxyHandler(obj));
michael@0 150 }
michael@0 151
michael@0 152 extern jsid s_length_id;
michael@0 153
michael@0 154 int32_t IdToInt32(JSContext* cx, JS::Handle<jsid> id);
michael@0 155
michael@0 156 // XXXbz this should really return uint32_t, with the maximum value
michael@0 157 // meaning "not an index"...
michael@0 158 inline int32_t
michael@0 159 GetArrayIndexFromId(JSContext* cx, JS::Handle<jsid> id)
michael@0 160 {
michael@0 161 if (MOZ_LIKELY(JSID_IS_INT(id))) {
michael@0 162 return JSID_TO_INT(id);
michael@0 163 }
michael@0 164 if (MOZ_LIKELY(id == s_length_id)) {
michael@0 165 return -1;
michael@0 166 }
michael@0 167 if (MOZ_LIKELY(JSID_IS_ATOM(id))) {
michael@0 168 JSAtom* atom = JSID_TO_ATOM(id);
michael@0 169 jschar s = *js::GetAtomChars(atom);
michael@0 170 if (MOZ_LIKELY((unsigned)s >= 'a' && (unsigned)s <= 'z'))
michael@0 171 return -1;
michael@0 172
michael@0 173 uint32_t i;
michael@0 174 JSLinearString* str = js::AtomToLinearString(JSID_TO_ATOM(id));
michael@0 175 return js::StringIsArrayIndex(str, &i) ? i : -1;
michael@0 176 }
michael@0 177 return IdToInt32(cx, id);
michael@0 178 }
michael@0 179
michael@0 180 inline bool
michael@0 181 IsArrayIndex(int32_t index)
michael@0 182 {
michael@0 183 return index >= 0;
michael@0 184 }
michael@0 185
michael@0 186 inline void
michael@0 187 FillPropertyDescriptor(JS::MutableHandle<JSPropertyDescriptor> desc,
michael@0 188 JSObject* obj, bool readonly, bool enumerable = true)
michael@0 189 {
michael@0 190 desc.object().set(obj);
michael@0 191 desc.setAttributes((readonly ? JSPROP_READONLY : 0) |
michael@0 192 (enumerable ? JSPROP_ENUMERATE : 0));
michael@0 193 desc.setGetter(nullptr);
michael@0 194 desc.setSetter(nullptr);
michael@0 195 }
michael@0 196
michael@0 197 inline void
michael@0 198 FillPropertyDescriptor(JS::MutableHandle<JSPropertyDescriptor> desc,
michael@0 199 JSObject* obj, JS::Value v,
michael@0 200 bool readonly, bool enumerable = true)
michael@0 201 {
michael@0 202 desc.value().set(v);
michael@0 203 FillPropertyDescriptor(desc, obj, readonly, enumerable);
michael@0 204 }
michael@0 205
michael@0 206 inline void
michael@0 207 FillPropertyDescriptor(JS::MutableHandle<JSPropertyDescriptor> desc,
michael@0 208 JSObject* obj, unsigned attributes, JS::Value v)
michael@0 209 {
michael@0 210 desc.object().set(obj);
michael@0 211 desc.value().set(v);
michael@0 212 desc.setAttributes(attributes);
michael@0 213 desc.setGetter(nullptr);
michael@0 214 desc.setSetter(nullptr);
michael@0 215 }
michael@0 216
michael@0 217 } // namespace dom
michael@0 218 } // namespace mozilla
michael@0 219
michael@0 220 #endif /* mozilla_dom_DOMProxyHandler_h */

mercurial