2012-05-22 06:46:20 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* 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_dom_DOMJSProxyHandler_h
|
|
|
|
#define mozilla_dom_DOMJSProxyHandler_h
|
|
|
|
|
2013-01-03 13:31:36 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/Likely.h"
|
|
|
|
|
2013-08-28 17:20:24 -07:00
|
|
|
#include "jsapi.h"
|
2013-08-23 19:42:40 -07:00
|
|
|
#include "jsproxy.h"
|
2013-09-23 10:25:00 -07:00
|
|
|
#include "nsString.h"
|
2012-05-22 06:46:20 -07:00
|
|
|
|
2013-06-20 22:39:22 -07:00
|
|
|
#define DOM_PROXY_OBJECT_SLOT js::PROXY_PRIVATE_SLOT
|
2012-05-22 06:46:20 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-23 19:42:39 -07:00
|
|
|
class DOMClass;
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
enum {
|
2014-02-21 11:35:49 -08:00
|
|
|
JSPROXYSLOT_EXPANDO = 0
|
2012-05-22 06:46:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct Prefable;
|
|
|
|
|
2013-09-24 08:21:22 -07:00
|
|
|
// This variable exists solely to provide a unique address for use as an identifier.
|
|
|
|
extern const char HandlerFamily;
|
|
|
|
inline const void* ProxyFamily() { return &HandlerFamily; }
|
2013-08-23 19:42:40 -07:00
|
|
|
|
|
|
|
inline bool IsDOMProxy(JSObject *obj)
|
|
|
|
{
|
2014-02-01 00:29:52 -08:00
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
return clasp->isProxy() &&
|
|
|
|
js::GetProxyHandler(obj)->family() == ProxyFamily();
|
2013-08-23 19:42:40 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 07:45:13 -07:00
|
|
|
class BaseDOMProxyHandler : public js::BaseProxyHandler
|
|
|
|
{
|
|
|
|
public:
|
2013-09-24 08:21:22 -07:00
|
|
|
BaseDOMProxyHandler(const void* aProxyFamily)
|
2013-07-09 07:45:13 -07:00
|
|
|
: js::BaseProxyHandler(aProxyFamily)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Implementations of traps that can be implemented in terms of
|
|
|
|
// fundamental traps.
|
|
|
|
bool enumerate(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::AutoIdVector& props) MOZ_OVERRIDE;
|
|
|
|
bool getPropertyDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
unsigned flags) MOZ_OVERRIDE;
|
2013-10-29 16:39:09 -07:00
|
|
|
|
|
|
|
bool watch(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
|
|
|
JS::Handle<JSObject*> callable) MOZ_OVERRIDE;
|
|
|
|
bool unwatch(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id) MOZ_OVERRIDE;
|
2014-04-15 19:58:44 -07:00
|
|
|
virtual bool getOwnPropertyNames(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::AutoIdVector &props) MOZ_OVERRIDE;
|
|
|
|
// We override keys() and implement it directly instead of using the
|
|
|
|
// default implementation, which would getOwnPropertyNames and then
|
|
|
|
// filter out the non-enumerable ones. This avoids doing
|
|
|
|
// unnecessary work during enumeration.
|
|
|
|
virtual bool keys(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::AutoIdVector &props) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Hook for subclasses to implement shared getOwnPropertyNames()/keys()
|
|
|
|
// functionality. The "flags" argument is either JSITER_OWNONLY (for keys())
|
|
|
|
// or JSITER_OWNONLY | JSITER_HIDDEN (for getOwnPropertyNames()).
|
|
|
|
virtual bool ownPropNames(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
unsigned flags,
|
|
|
|
JS::AutoIdVector& props) = 0;
|
2013-07-09 07:45:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class DOMProxyHandler : public BaseDOMProxyHandler
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-02-01 00:29:52 -08:00
|
|
|
DOMProxyHandler()
|
|
|
|
: BaseDOMProxyHandler(ProxyFamily())
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-22 19:43:03 -07:00
|
|
|
bool preventExtensions(JSContext *cx, JS::Handle<JSObject*> proxy) MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
bool defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
2013-04-30 10:29:40 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE
|
2013-05-29 02:38:57 -07:00
|
|
|
{
|
|
|
|
bool unused;
|
|
|
|
return defineProperty(cx, proxy, id, desc, &unused);
|
|
|
|
}
|
|
|
|
virtual bool defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
2013-04-30 10:29:40 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc, bool* defined);
|
2014-04-25 13:07:18 -07:00
|
|
|
bool set(JSContext *cx, JS::Handle<JSObject*> proxy, JS::Handle<JSObject*> receiver,
|
|
|
|
JS::Handle<jsid> id, bool strict, JS::MutableHandle<JS::Value> vp) MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
bool delete_(JSContext* cx, JS::Handle<JSObject*> proxy,
|
2013-03-21 15:23:48 -07:00
|
|
|
JS::Handle<jsid> id, bool* bp) MOZ_OVERRIDE;
|
2013-03-21 15:23:48 -07:00
|
|
|
bool has(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool* bp) MOZ_OVERRIDE;
|
2013-06-28 14:01:09 -07:00
|
|
|
bool isExtensible(JSContext *cx, JS::Handle<JSObject*> proxy, bool *extensible) MOZ_OVERRIDE;
|
2012-05-22 06:46:20 -07:00
|
|
|
|
2014-04-25 13:07:18 -07:00
|
|
|
/*
|
|
|
|
* If assigning to proxy[id] hits a named setter with OverrideBuiltins or
|
|
|
|
* an indexed setter, call it and set *done to true on success. Otherwise, set
|
|
|
|
* *done to false.
|
|
|
|
*/
|
|
|
|
virtual bool setCustom(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
|
|
|
JS::MutableHandle<JS::Value> vp, bool *done);
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
static JSObject* GetExpandoObject(JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsDOMProxy(obj), "expected a DOM proxy object");
|
|
|
|
JS::Value v = js::GetProxyExtra(obj, JSPROXYSLOT_EXPANDO);
|
2013-04-20 09:04:09 -07:00
|
|
|
if (v.isObject()) {
|
|
|
|
return &v.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v.isUndefined()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
js::ExpandoAndGeneration* expandoAndGeneration =
|
|
|
|
static_cast<js::ExpandoAndGeneration*>(v.toPrivate());
|
|
|
|
v = expandoAndGeneration->expando;
|
|
|
|
return v.isUndefined() ? nullptr : &v.toObject();
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
2013-06-28 12:32:27 -07:00
|
|
|
/* GetAndClearExpandoObject does not DROP or clear the preserving wrapper flag. */
|
2013-04-16 10:02:57 -07:00
|
|
|
static JSObject* GetAndClearExpandoObject(JSObject* obj);
|
2013-05-03 16:29:09 -07:00
|
|
|
static JSObject* EnsureExpandoObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj);
|
2012-05-22 06:46:20 -07:00
|
|
|
};
|
|
|
|
|
2014-04-25 13:07:18 -07:00
|
|
|
inline DOMProxyHandler*
|
|
|
|
GetDOMProxyHandler(JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsDOMProxy(obj));
|
|
|
|
return static_cast<DOMProxyHandler*>(js::GetProxyHandler(obj));
|
|
|
|
}
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
extern jsid s_length_id;
|
|
|
|
|
2013-05-03 16:29:09 -07:00
|
|
|
int32_t IdToInt32(JSContext* cx, JS::Handle<jsid> id);
|
2012-05-22 06:46:20 -07:00
|
|
|
|
2012-11-08 23:48:42 -08:00
|
|
|
// XXXbz this should really return uint32_t, with the maximum value
|
|
|
|
// meaning "not an index"...
|
2012-05-22 06:46:20 -07:00
|
|
|
inline int32_t
|
2013-05-03 16:29:09 -07:00
|
|
|
GetArrayIndexFromId(JSContext* cx, JS::Handle<jsid> id)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
|
|
|
if (MOZ_LIKELY(JSID_IS_INT(id))) {
|
|
|
|
return JSID_TO_INT(id);
|
|
|
|
}
|
|
|
|
if (MOZ_LIKELY(id == s_length_id)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (MOZ_LIKELY(JSID_IS_ATOM(id))) {
|
|
|
|
JSAtom* atom = JSID_TO_ATOM(id);
|
|
|
|
jschar s = *js::GetAtomChars(atom);
|
|
|
|
if (MOZ_LIKELY((unsigned)s >= 'a' && (unsigned)s <= 'z'))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
JSLinearString* str = js::AtomToLinearString(JSID_TO_ATOM(id));
|
|
|
|
return js::StringIsArrayIndex(str, &i) ? i : -1;
|
|
|
|
}
|
|
|
|
return IdToInt32(cx, id);
|
|
|
|
}
|
|
|
|
|
2012-11-08 23:48:42 -08:00
|
|
|
inline bool
|
|
|
|
IsArrayIndex(int32_t index)
|
|
|
|
{
|
|
|
|
return index >= 0;
|
|
|
|
}
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
inline void
|
2014-04-15 19:58:44 -07:00
|
|
|
FillPropertyDescriptor(JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
JSObject* obj, bool readonly, bool enumerable = true)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.object().set(obj);
|
2014-04-15 19:58:44 -07:00
|
|
|
desc.setAttributes((readonly ? JSPROP_READONLY : 0) |
|
|
|
|
(enumerable ? JSPROP_ENUMERATE : 0));
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.setGetter(nullptr);
|
|
|
|
desc.setSetter(nullptr);
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2014-04-15 19:58:44 -07:00
|
|
|
FillPropertyDescriptor(JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
JSObject* obj, JS::Value v,
|
|
|
|
bool readonly, bool enumerable = true)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.value().set(v);
|
2014-04-15 19:58:44 -07:00
|
|
|
FillPropertyDescriptor(desc, obj, readonly, enumerable);
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
2014-01-29 22:33:23 -08:00
|
|
|
inline void
|
|
|
|
FillPropertyDescriptor(JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
JSObject* obj, unsigned attributes, JS::Value v)
|
|
|
|
{
|
|
|
|
desc.object().set(obj);
|
|
|
|
desc.value().set(v);
|
|
|
|
desc.setAttributes(attributes);
|
|
|
|
desc.setGetter(nullptr);
|
|
|
|
desc.setSetter(nullptr);
|
|
|
|
}
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_DOMProxyHandler_h */
|