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"
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
#include "jsapi.h"
|
2012-09-06 17:51:50 -07:00
|
|
|
#include "jsfriendapi.h"
|
2012-05-22 06:46:20 -07:00
|
|
|
#include "jsproxy.h"
|
|
|
|
#include "xpcpublic.h"
|
2012-12-25 14:00:15 -08:00
|
|
|
#include "nsStringGlue.h"
|
2012-05-22 06:46:20 -07:00
|
|
|
|
|
|
|
#define DOM_PROXY_OBJECT_SLOT js::JSSLOT_PROXY_PRIVATE
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
enum {
|
2012-10-05 09:59:23 -07:00
|
|
|
JSPROXYSLOT_EXPANDO = 0,
|
|
|
|
JSPROXYSLOT_XRAY_EXPANDO
|
2012-05-22 06:46:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct Prefable;
|
|
|
|
|
2012-10-13 05:50:24 -07:00
|
|
|
class DOMProxyHandler : public js::BaseProxyHandler
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DOMProxyHandler(const DOMClass& aClass)
|
2012-10-13 05:50:24 -07:00
|
|
|
: js::BaseProxyHandler(ProxyFamily()),
|
2012-05-22 06:46:20 -07:00
|
|
|
mClass(aClass)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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 getPropertyDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
|
|
|
JSPropertyDescriptor* desc, unsigned flags) MOZ_OVERRIDE;
|
|
|
|
bool defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
2013-01-03 13:31:36 -08:00
|
|
|
JSPropertyDescriptor* desc) 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 enumerate(JSContext* cx, JS::Handle<JSObject*> proxy, JS::AutoIdVector& props) MOZ_OVERRIDE;
|
|
|
|
bool has(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool* bp) MOZ_OVERRIDE;
|
2013-03-22 19:43:03 -07:00
|
|
|
bool isExtensible(JSObject *proxy) MOZ_OVERRIDE;
|
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-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
|
|
|
|
|
|
|
const DOMClass& mClass;
|
|
|
|
|
|
|
|
protected:
|
2013-04-20 09:04:09 -07:00
|
|
|
// Append the property names in "names" to "props". If
|
|
|
|
// shadowPrototypeProperties is false then skip properties that are also
|
|
|
|
// present on our proto chain.
|
2013-05-03 16:29:08 -07:00
|
|
|
bool AppendNamedPropertyIds(JSContext* cx, JS::Handle<JSObject*> proxy,
|
2012-11-05 08:58:03 -08:00
|
|
|
nsTArray<nsString>& names,
|
2013-04-20 09:04:09 -07:00
|
|
|
bool shadowPrototypeProperties,
|
2012-11-05 08:58:03 -08:00
|
|
|
JS::AutoIdVector& props);
|
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
|
|
|
|
FillPropertyDescriptor(JSPropertyDescriptor* desc, JSObject* obj, bool readonly)
|
|
|
|
{
|
|
|
|
desc->obj = obj;
|
|
|
|
desc->attrs = (readonly ? JSPROP_READONLY : 0) | JSPROP_ENUMERATE;
|
|
|
|
desc->getter = NULL;
|
|
|
|
desc->setter = NULL;
|
|
|
|
desc->shortid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2013-04-02 16:05:37 -07:00
|
|
|
FillPropertyDescriptor(JSPropertyDescriptor* desc, JSObject* obj, JS::Value v, bool readonly)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
|
|
|
desc->value = v;
|
|
|
|
FillPropertyDescriptor(desc, obj, readonly);
|
|
|
|
}
|
|
|
|
|
2013-03-21 15:23:48 -07:00
|
|
|
JSObject*
|
2013-05-03 16:29:09 -07:00
|
|
|
EnsureExpandoObject(JSContext* cx, JS::Handle<JSObject*> obj);
|
2012-05-22 06:46:20 -07:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_DOMProxyHandler_h */
|