2012-05-22 13:46:20 +00: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 21:31:36 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/Likely.h"
|
|
|
|
|
2013-08-29 00:20:24 +00:00
|
|
|
#include "jsapi.h"
|
2013-08-24 02:42:40 +00:00
|
|
|
#include "jsproxy.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
2012-05-22 13:46:20 +00:00
|
|
|
|
2013-06-21 05:39:22 +00:00
|
|
|
#define DOM_PROXY_OBJECT_SLOT js::PROXY_PRIVATE_SLOT
|
2012-05-22 13:46:20 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
enum {
|
2014-10-01 21:05:54 +00:00
|
|
|
/**
|
|
|
|
* DOM proxies have an extra slot for the expando object at index
|
|
|
|
* JSPROXYSLOT_EXPANDO.
|
|
|
|
*
|
|
|
|
* The expando object is a plain JSObject whose properties correspond to
|
|
|
|
* "expandos" (custom properties set by the script author).
|
|
|
|
*
|
|
|
|
* The exact value stored in the JSPROXYSLOT_EXPANDO slot depends on whether
|
|
|
|
* the interface is annotated with the [OverrideBuiltins] extended attribute.
|
|
|
|
*
|
|
|
|
* If it is, the proxy is initialized with a PrivateValue, which contains a
|
|
|
|
* pointer to a js::ExpandoAndGeneration object; this contains a pointer to
|
|
|
|
* the actual expando object as well as the "generation" of the object.
|
|
|
|
*
|
|
|
|
* If it is not, the proxy is initialized with an UndefinedValue. In
|
|
|
|
* EnsureExpandoObject, it is set to an ObjectValue that points to the
|
|
|
|
* expando object directly. (It is set back to an UndefinedValue only when
|
|
|
|
* the object is about to die.)
|
|
|
|
*/
|
2014-02-21 19:35:49 +00:00
|
|
|
JSPROXYSLOT_EXPANDO = 0
|
2012-05-22 13:46:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct Prefable;
|
|
|
|
|
2013-07-09 14:45:13 +00:00
|
|
|
class BaseDOMProxyHandler : public js::BaseProxyHandler
|
|
|
|
{
|
|
|
|
public:
|
2014-08-28 20:47:16 +00:00
|
|
|
explicit MOZ_CONSTEXPR BaseDOMProxyHandler(const void* aProxyFamily, bool aHasPrototype = false)
|
2014-06-27 11:44:02 +00:00
|
|
|
: js::BaseProxyHandler(aProxyFamily, aHasPrototype)
|
2013-07-09 14:45:13 +00:00
|
|
|
{}
|
|
|
|
|
2014-10-08 17:09:08 +00:00
|
|
|
// Implementations of methods that can be implemented in terms of
|
|
|
|
// other lower-level methods.
|
|
|
|
bool getOwnPropertyDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE;
|
|
|
|
virtual bool ownPropertyKeys(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::AutoIdVector &props) const MOZ_OVERRIDE;
|
|
|
|
|
2013-07-09 14:45:13 +00:00
|
|
|
bool getPropertyDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id,
|
2014-06-27 11:44:06 +00:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE;
|
2013-10-29 23:39:09 +00:00
|
|
|
|
2014-12-16 23:28:38 +00:00
|
|
|
virtual bool enumerate(JSContext *cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::MutableHandle<JSObject*> objp) const MOZ_OVERRIDE;
|
2014-10-09 03:01:55 +00:00
|
|
|
|
|
|
|
// We override getOwnEnumerablePropertyKeys() and implement it directly
|
|
|
|
// instead of using the default implementation, which would call
|
|
|
|
// ownPropertyKeys and then filter out the non-enumerable ones. This avoids
|
2014-04-16 02:58:44 +00:00
|
|
|
// unnecessary work during enumeration.
|
2014-10-09 03:01:55 +00:00
|
|
|
virtual bool getOwnEnumerablePropertyKeys(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::AutoIdVector &props) const MOZ_OVERRIDE;
|
2014-04-16 02:58:44 +00:00
|
|
|
|
2014-10-08 17:09:08 +00:00
|
|
|
bool watch(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
|
|
|
JS::Handle<JSObject*> callable) const MOZ_OVERRIDE;
|
|
|
|
bool unwatch(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id) const MOZ_OVERRIDE;
|
|
|
|
|
2014-04-16 02:58:44 +00:00
|
|
|
protected:
|
2014-09-26 20:16:36 +00:00
|
|
|
// Hook for subclasses to implement shared ownPropertyKeys()/keys()
|
2014-04-16 02:58:44 +00:00
|
|
|
// functionality. The "flags" argument is either JSITER_OWNONLY (for keys())
|
2014-10-06 21:42:33 +00:00
|
|
|
// or JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS (for
|
|
|
|
// ownPropertyKeys()).
|
2014-04-16 02:58:44 +00:00
|
|
|
virtual bool ownPropNames(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
unsigned flags,
|
2014-06-27 11:44:06 +00:00
|
|
|
JS::AutoIdVector& props) const = 0;
|
2014-08-02 03:37:14 +00:00
|
|
|
|
|
|
|
// Hook for subclasses to allow set() to ignore named props while other things
|
|
|
|
// that look at property descriptors see them. This is intentionally not
|
|
|
|
// named getOwnPropertyDescriptor to avoid subclasses that override it hiding
|
|
|
|
// our public getOwnPropertyDescriptor.
|
|
|
|
virtual bool getOwnPropDescriptor(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id,
|
|
|
|
bool ignoreNamedProps,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc) const = 0;
|
2013-07-09 14:45:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class DOMProxyHandler : public BaseDOMProxyHandler
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-08-28 20:47:16 +00:00
|
|
|
MOZ_CONSTEXPR DOMProxyHandler()
|
2014-08-28 00:09:06 +00:00
|
|
|
: BaseDOMProxyHandler(&family)
|
2014-08-28 20:47:16 +00:00
|
|
|
{}
|
2012-05-22 13:46:20 +00:00
|
|
|
|
2013-03-21 22:23:48 +00:00
|
|
|
bool defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
2014-06-27 11:44:06 +00:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE
|
2013-05-29 09:38:57 +00:00
|
|
|
{
|
|
|
|
bool unused;
|
|
|
|
return defineProperty(cx, proxy, id, desc, &unused);
|
|
|
|
}
|
|
|
|
virtual bool defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
2014-06-27 11:44:06 +00:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc, bool* defined)
|
|
|
|
const;
|
2013-03-21 22:23:48 +00:00
|
|
|
bool delete_(JSContext* cx, JS::Handle<JSObject*> proxy,
|
2014-06-27 11:44:06 +00:00
|
|
|
JS::Handle<jsid> id, bool* bp) const MOZ_OVERRIDE;
|
2014-10-19 08:18:43 +00:00
|
|
|
bool preventExtensions(JSContext *cx, JS::Handle<JSObject*> proxy,
|
|
|
|
bool *succeeded) const MOZ_OVERRIDE;
|
2014-10-21 18:40:04 +00:00
|
|
|
bool isExtensible(JSContext *cx, JS::Handle<JSObject*> proxy, bool *extensible)
|
|
|
|
const MOZ_OVERRIDE;
|
2014-10-08 17:09:08 +00:00
|
|
|
bool has(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
|
|
|
bool* bp) const MOZ_OVERRIDE;
|
|
|
|
bool set(JSContext *cx, JS::Handle<JSObject*> proxy, JS::Handle<JSObject*> receiver,
|
|
|
|
JS::Handle<jsid> id, bool strict, JS::MutableHandle<JS::Value> vp)
|
|
|
|
const MOZ_OVERRIDE;
|
2012-05-22 13:46:20 +00:00
|
|
|
|
2014-04-25 20:07:18 +00: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,
|
2014-06-27 11:44:06 +00:00
|
|
|
JS::MutableHandle<JS::Value> vp, bool *done) const;
|
2014-04-25 20:07:18 +00:00
|
|
|
|
2014-08-28 00:09:06 +00:00
|
|
|
static JSObject* GetExpandoObject(JSObject* obj);
|
|
|
|
|
2013-06-28 19:32:27 +00:00
|
|
|
/* GetAndClearExpandoObject does not DROP or clear the preserving wrapper flag. */
|
2013-04-16 17:02:57 +00:00
|
|
|
static JSObject* GetAndClearExpandoObject(JSObject* obj);
|
2013-05-03 23:29:09 +00:00
|
|
|
static JSObject* EnsureExpandoObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj);
|
2014-08-28 00:09:06 +00:00
|
|
|
|
|
|
|
static const char family;
|
2012-05-22 13:46:20 +00:00
|
|
|
};
|
|
|
|
|
2014-08-28 00:09:06 +00:00
|
|
|
inline bool IsDOMProxy(JSObject *obj)
|
|
|
|
{
|
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
return clasp->isProxy() &&
|
|
|
|
js::GetProxyHandler(obj)->family() == &DOMProxyHandler::family;
|
|
|
|
}
|
|
|
|
|
2014-06-27 11:44:08 +00:00
|
|
|
inline const DOMProxyHandler*
|
2014-04-25 20:07:18 +00:00
|
|
|
GetDOMProxyHandler(JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsDOMProxy(obj));
|
2014-06-27 11:44:08 +00:00
|
|
|
return static_cast<const DOMProxyHandler*>(js::GetProxyHandler(obj));
|
2014-04-25 20:07:18 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 13:46:20 +00:00
|
|
|
extern jsid s_length_id;
|
|
|
|
|
2013-05-03 23:29:09 +00:00
|
|
|
int32_t IdToInt32(JSContext* cx, JS::Handle<jsid> id);
|
2012-05-22 13:46:20 +00:00
|
|
|
|
2012-11-09 07:48:42 +00:00
|
|
|
// XXXbz this should really return uint32_t, with the maximum value
|
|
|
|
// meaning "not an index"...
|
2012-05-22 13:46:20 +00:00
|
|
|
inline int32_t
|
2013-05-03 23:29:09 +00:00
|
|
|
GetArrayIndexFromId(JSContext* cx, JS::Handle<jsid> id)
|
2012-05-22 13:46:20 +00: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);
|
2014-07-22 04:43:21 +00:00
|
|
|
char16_t s;
|
2014-07-02 13:45:02 +00:00
|
|
|
{
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
if (js::AtomHasLatin1Chars(atom)) {
|
|
|
|
s = *js::GetLatin1AtomChars(nogc, atom);
|
|
|
|
} else {
|
|
|
|
s = *js::GetTwoByteAtomChars(nogc, atom);
|
|
|
|
}
|
|
|
|
}
|
2012-05-22 13:46:20 +00:00
|
|
|
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-09 07:48:42 +00:00
|
|
|
inline bool
|
|
|
|
IsArrayIndex(int32_t index)
|
|
|
|
{
|
|
|
|
return index >= 0;
|
|
|
|
}
|
|
|
|
|
2012-05-22 13:46:20 +00:00
|
|
|
inline void
|
2014-04-16 02:58:44 +00:00
|
|
|
FillPropertyDescriptor(JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
JSObject* obj, bool readonly, bool enumerable = true)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
2013-04-30 17:29:40 +00:00
|
|
|
desc.object().set(obj);
|
2014-04-16 02:58:44 +00:00
|
|
|
desc.setAttributes((readonly ? JSPROP_READONLY : 0) |
|
|
|
|
(enumerable ? JSPROP_ENUMERATE : 0));
|
2013-04-30 17:29:40 +00:00
|
|
|
desc.setGetter(nullptr);
|
|
|
|
desc.setSetter(nullptr);
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2014-04-16 02:58:44 +00:00
|
|
|
FillPropertyDescriptor(JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
JSObject* obj, JS::Value v,
|
|
|
|
bool readonly, bool enumerable = true)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
2013-04-30 17:29:40 +00:00
|
|
|
desc.value().set(v);
|
2014-04-16 02:58:44 +00:00
|
|
|
FillPropertyDescriptor(desc, obj, readonly, enumerable);
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 06:33:23 +00: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 13:46:20 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_DOMProxyHandler_h */
|