2012-03-30 21:42:20 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
#ifndef mozilla_dom_BindingUtils_h__
|
|
|
|
#define mozilla_dom_BindingUtils_h__
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2013-07-24 00:31:06 -07:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "jswrapper.h"
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-08-14 00:00:52 -07:00
|
|
|
#include "mozilla/Alignment.h"
|
2013-11-19 11:28:09 -08:00
|
|
|
#include "mozilla/Array.h"
|
2014-01-23 01:18:35 -08:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-07-24 00:31:06 -07:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/dom/CallbackObject.h"
|
2012-05-02 21:35:38 -07:00
|
|
|
#include "mozilla/dom/DOMJSClass.h"
|
2012-05-22 06:46:20 -07:00
|
|
|
#include "mozilla/dom/DOMJSProxyHandler.h"
|
2013-09-08 20:29:21 -07:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2012-09-19 06:02:37 -07:00
|
|
|
#include "mozilla/dom/NonRefcountedDOMObject.h"
|
2013-07-24 00:31:06 -07:00
|
|
|
#include "mozilla/dom/Nullable.h"
|
2013-09-25 21:04:59 -07:00
|
|
|
#include "mozilla/dom/RootedDictionary.h"
|
2012-04-24 16:50:00 -07:00
|
|
|
#include "mozilla/dom/workers/Workers.h"
|
2012-05-05 18:15:11 -07:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-07-24 00:31:06 -07:00
|
|
|
#include "mozilla/Likely.h"
|
2013-11-20 05:55:09 -08:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-07-04 08:24:59 -07:00
|
|
|
#include "nsCycleCollector.h"
|
2012-05-18 01:29:40 -07:00
|
|
|
#include "nsIXPConnect.h"
|
2014-07-05 08:30:54 -07:00
|
|
|
#include "nsJSUtils.h"
|
2014-02-26 13:36:35 -08:00
|
|
|
#include "nsISupportsImpl.h"
|
2012-05-18 01:29:40 -07:00
|
|
|
#include "qsObjectHelper.h"
|
|
|
|
#include "xpcpublic.h"
|
2013-09-06 10:50:24 -07:00
|
|
|
#include "nsIVariant.h"
|
2014-05-23 14:32:38 -07:00
|
|
|
#include "pldhash.h" // For PLDHashOperator
|
2013-07-24 00:31:06 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
|
|
|
|
2014-02-15 13:12:33 -08:00
|
|
|
class nsIJSID;
|
2013-05-20 10:47:08 -07:00
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
2013-05-29 20:15:31 -07:00
|
|
|
extern nsresult
|
2013-10-11 22:02:39 -07:00
|
|
|
xpc_qsUnwrapArgImpl(JSContext* cx, JS::Handle<JS::Value> v, const nsIID& iid, void** ppArg,
|
|
|
|
nsISupports** ppArgRef, JS::MutableHandle<JS::Value> vp);
|
2013-05-29 20:15:31 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2014-05-23 14:32:38 -07:00
|
|
|
template<typename DataType> class MozMap;
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2013-05-29 20:15:31 -07:00
|
|
|
struct SelfRef
|
|
|
|
{
|
|
|
|
SelfRef() : ptr(nullptr) {}
|
|
|
|
explicit SelfRef(nsISupports *p) : ptr(p) {}
|
|
|
|
~SelfRef() { NS_IF_RELEASE(ptr); }
|
|
|
|
|
|
|
|
nsISupports* ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Convert a jsval to an XPCOM pointer. */
|
|
|
|
template <class Interface, class StrongRefType>
|
|
|
|
inline nsresult
|
2013-10-11 22:02:39 -07:00
|
|
|
UnwrapArg(JSContext* cx, JS::Handle<JS::Value> v, Interface** ppArg,
|
|
|
|
StrongRefType** ppArgRef, JS::MutableHandle<JS::Value> vp)
|
2013-05-29 20:15:31 -07:00
|
|
|
{
|
|
|
|
nsISupports* argRef = *ppArgRef;
|
|
|
|
nsresult rv = xpc_qsUnwrapArgImpl(cx, v, NS_GET_TEMPLATE_IID(Interface),
|
|
|
|
reinterpret_cast<void**>(ppArg), &argRef,
|
|
|
|
vp);
|
|
|
|
*ppArgRef = static_cast<StrongRefType*>(argRef);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-10-16 09:11:25 -07:00
|
|
|
inline const ErrNum
|
|
|
|
GetInvalidThisErrorForMethod(bool aSecurityError)
|
|
|
|
{
|
|
|
|
return aSecurityError ? MSG_METHOD_THIS_UNWRAPPING_DENIED :
|
|
|
|
MSG_METHOD_THIS_DOES_NOT_IMPLEMENT_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const ErrNum
|
|
|
|
GetInvalidThisErrorForGetter(bool aSecurityError)
|
|
|
|
{
|
|
|
|
return aSecurityError ? MSG_GETTER_THIS_UNWRAPPING_DENIED :
|
|
|
|
MSG_GETTER_THIS_DOES_NOT_IMPLEMENT_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const ErrNum
|
|
|
|
GetInvalidThisErrorForSetter(bool aSecurityError)
|
|
|
|
{
|
|
|
|
return aSecurityError ? MSG_SETTER_THIS_UNWRAPPING_DENIED :
|
|
|
|
MSG_SETTER_THIS_DOES_NOT_IMPLEMENT_INTERFACE;
|
|
|
|
}
|
|
|
|
|
2013-06-17 10:07:03 -07:00
|
|
|
bool
|
2013-06-17 13:31:13 -07:00
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
const char* aInterfaceName);
|
2012-07-18 03:36:08 -07:00
|
|
|
|
2014-02-07 12:56:14 -08:00
|
|
|
bool
|
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
prototypes::ID aProtoId);
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
inline bool
|
2012-11-06 15:23:14 -08:00
|
|
|
ThrowMethodFailedWithDetails(JSContext* cx, ErrorResult& rv,
|
2013-07-04 06:24:49 -07:00
|
|
|
const char* ifaceName,
|
2013-09-06 06:27:25 -07:00
|
|
|
const char* memberName,
|
|
|
|
bool reportJSContentExceptions = false)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2012-11-06 15:23:14 -08:00
|
|
|
if (rv.IsTypeError()) {
|
|
|
|
rv.ReportTypeError(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-19 08:54:40 -08:00
|
|
|
if (rv.IsJSException()) {
|
2013-09-03 05:01:53 -07:00
|
|
|
if (reportJSContentExceptions) {
|
|
|
|
rv.ReportJSExceptionFromJSImplementation(cx);
|
|
|
|
} else {
|
|
|
|
rv.ReportJSException(cx);
|
|
|
|
}
|
2013-02-19 08:54:40 -08:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-04 06:24:49 -07:00
|
|
|
if (rv.IsNotEnoughArgsError()) {
|
|
|
|
rv.ReportNotEnoughArgsError(cx, ifaceName, memberName);
|
2014-03-10 14:38:31 -07:00
|
|
|
return false;
|
2013-07-04 06:24:49 -07:00
|
|
|
}
|
2013-09-08 20:29:21 -07:00
|
|
|
return Throw(cx, rv.ErrorCode());
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
// Returns true if the JSClass is used for DOM objects.
|
2012-03-30 21:42:20 -07:00
|
|
|
inline bool
|
|
|
|
IsDOMClass(const JSClass* clasp)
|
|
|
|
{
|
|
|
|
return clasp->flags & JSCLASS_IS_DOMJSCLASS;
|
|
|
|
}
|
|
|
|
|
2012-06-06 12:59:46 -07:00
|
|
|
inline bool
|
|
|
|
IsDOMClass(const js::Class* clasp)
|
|
|
|
{
|
2012-06-01 09:24:52 -07:00
|
|
|
return IsDOMClass(Jsvalify(clasp));
|
2012-06-06 12:59:46 -07:00
|
|
|
}
|
|
|
|
|
2014-02-01 00:29:52 -08:00
|
|
|
// Return true if the JSClass is used for non-proxy DOM objects.
|
|
|
|
inline bool
|
|
|
|
IsNonProxyDOMClass(const js::Class* clasp)
|
|
|
|
{
|
|
|
|
return IsDOMClass(clasp) && !clasp->isProxy();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
IsNonProxyDOMClass(const JSClass* clasp)
|
|
|
|
{
|
|
|
|
return IsNonProxyDOMClass(js::Valueify(clasp));
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
// Returns true if the JSClass is used for DOM interface and interface
|
|
|
|
// prototype objects.
|
|
|
|
inline bool
|
|
|
|
IsDOMIfaceAndProtoClass(const JSClass* clasp)
|
|
|
|
{
|
|
|
|
return clasp->flags & JSCLASS_IS_DOMIFACEANDPROTOJSCLASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
IsDOMIfaceAndProtoClass(const js::Class* clasp)
|
|
|
|
{
|
|
|
|
return IsDOMIfaceAndProtoClass(Jsvalify(clasp));
|
|
|
|
}
|
|
|
|
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(DOM_OBJECT_SLOT == js::PROXY_PRIVATE_SLOT,
|
|
|
|
"js::PROXY_PRIVATE_SLOT doesn't match DOM_OBJECT_SLOT. "
|
|
|
|
"Expect bad things");
|
2012-03-30 21:42:20 -07:00
|
|
|
template <class T>
|
|
|
|
inline T*
|
2012-12-06 12:21:19 -08:00
|
|
|
UnwrapDOMObject(JSObject* obj)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-02-01 00:29:52 -08:00
|
|
|
MOZ_ASSERT(IsDOMClass(js::GetObjectClass(obj)),
|
2012-05-22 06:46:20 -07:00
|
|
|
"Don't pass non-DOM objects to this function");
|
|
|
|
|
2012-12-06 12:21:19 -08:00
|
|
|
JS::Value val = js::GetReservedSlot(obj, DOM_OBJECT_SLOT);
|
2012-03-30 21:42:20 -07:00
|
|
|
return static_cast<T*>(val.toPrivate());
|
|
|
|
}
|
|
|
|
|
2014-08-22 02:25:35 -07:00
|
|
|
inline const DOMJSClass*
|
|
|
|
GetDOMClass(const js::Class* clasp)
|
|
|
|
{
|
|
|
|
return IsDOMClass(clasp) ? DOMJSClass::FromJSClass(clasp) : nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-18 13:19:19 -07:00
|
|
|
inline const DOMJSClass*
|
2012-05-22 06:46:20 -07:00
|
|
|
GetDOMClass(JSObject* obj)
|
|
|
|
{
|
2014-08-22 02:25:35 -07:00
|
|
|
return GetDOMClass(js::GetObjectClass(obj));
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
2013-05-31 23:56:02 -07:00
|
|
|
inline nsISupports*
|
|
|
|
UnwrapDOMObjectToISupports(JSObject* aObject)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* clasp = GetDOMClass(aObject);
|
2012-12-06 12:21:19 -08:00
|
|
|
if (!clasp || !clasp->mDOMObjectIsISupports) {
|
2013-05-31 23:56:02 -07:00
|
|
|
return nullptr;
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
2014-06-18 13:19:19 -07:00
|
|
|
|
2013-05-31 23:56:02 -07:00
|
|
|
return UnwrapDOMObject<nsISupports>(aObject);
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
IsDOMObject(JSObject* obj)
|
|
|
|
{
|
2014-02-01 00:29:52 -08:00
|
|
|
return IsDOMClass(js::GetObjectClass(obj));
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
2013-11-21 04:51:16 -08:00
|
|
|
#define UNWRAP_OBJECT(Interface, obj, value) \
|
2013-09-03 05:01:53 -07:00
|
|
|
mozilla::dom::UnwrapObject<mozilla::dom::prototypes::id::Interface, \
|
2013-11-21 04:51:15 -08:00
|
|
|
mozilla::dom::Interface##Binding::NativeType>(obj, value)
|
2013-08-23 19:42:41 -07:00
|
|
|
|
2012-09-12 09:24:58 -07:00
|
|
|
// Some callers don't want to set an exception when unwrapping fails
|
2012-03-30 21:42:20 -07:00
|
|
|
// (for example, overload resolution uses unwrapping to tell what sort
|
|
|
|
// of thing it's looking at).
|
2012-05-24 22:08:26 -07:00
|
|
|
// U must be something that a T* can be assigned to (e.g. T* or an nsRefPtr<T>).
|
2014-02-07 12:56:14 -08:00
|
|
|
template <class T, typename U>
|
2012-11-27 01:20:05 -08:00
|
|
|
MOZ_ALWAYS_INLINE nsresult
|
2014-02-07 12:56:14 -08:00
|
|
|
UnwrapObject(JSObject* obj, U& value, prototypes::ID protoID,
|
|
|
|
uint32_t protoDepth)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
|
|
|
/* First check to see whether we have a DOM object */
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(obj);
|
2012-12-06 12:21:19 -08:00
|
|
|
if (!domClass) {
|
2012-03-30 21:42:20 -07:00
|
|
|
/* Maybe we have a security wrapper or outer window? */
|
|
|
|
if (!js::IsWrapper(obj)) {
|
|
|
|
/* Not a DOM object, not a wrapper, just bail */
|
|
|
|
return NS_ERROR_XPC_BAD_CONVERT_JS;
|
|
|
|
}
|
|
|
|
|
2013-04-11 11:50:18 -07:00
|
|
|
obj = js::CheckedUnwrap(obj, /* stopAtOuter = */ false);
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_XPC_SECURITY_MANAGER_VETO;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!js::IsWrapper(obj));
|
2012-12-06 12:21:19 -08:00
|
|
|
domClass = GetDOMClass(obj);
|
|
|
|
if (!domClass) {
|
2012-03-30 21:42:20 -07:00
|
|
|
/* We don't have a DOM object */
|
|
|
|
return NS_ERROR_XPC_BAD_CONVERT_JS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This object is a DOM object. Double-check that it is safely
|
|
|
|
castable to T by checking whether it claims to inherit from the
|
|
|
|
class identified by protoID. */
|
2014-02-07 12:56:14 -08:00
|
|
|
if (domClass->mInterfaceChain[protoDepth] == protoID) {
|
2012-12-06 12:21:19 -08:00
|
|
|
value = UnwrapDOMObject<T>(obj);
|
2012-03-30 21:42:20 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's the wrong sort of DOM object */
|
|
|
|
return NS_ERROR_XPC_BAD_CONVERT_JS;
|
|
|
|
}
|
|
|
|
|
2014-02-07 12:56:14 -08:00
|
|
|
template <prototypes::ID PrototypeID, class T, typename U>
|
|
|
|
MOZ_ALWAYS_INLINE nsresult
|
|
|
|
UnwrapObject(JSObject* obj, U& value)
|
|
|
|
{
|
|
|
|
return UnwrapObject<T>(obj, value, PrototypeID,
|
|
|
|
PrototypeTraits<PrototypeID>::Depth);
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
inline bool
|
2013-05-15 18:46:44 -07:00
|
|
|
IsNotDateOrRegExp(JSContext* cx, JS::Handle<JSObject*> obj)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(obj);
|
2013-01-02 19:03:25 -08:00
|
|
|
return !JS_ObjectIsDate(cx, obj) && !JS_ObjectIsRegExp(cx, obj);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2013-01-03 11:03:00 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-05-15 18:46:44 -07:00
|
|
|
IsObjectValueConvertibleToDictionary(JSContext* cx,
|
|
|
|
JS::Handle<JS::Value> objVal)
|
2013-01-03 11:03:00 -08:00
|
|
|
{
|
2013-05-15 18:46:44 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, &objVal.toObject());
|
2013-01-03 11:03:00 -08:00
|
|
|
return IsNotDateOrRegExp(cx, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-05-15 18:46:44 -07:00
|
|
|
IsConvertibleToDictionary(JSContext* cx, JS::Handle<JS::Value> val)
|
2013-01-03 11:03:00 -08:00
|
|
|
{
|
|
|
|
return val.isNullOrUndefined() ||
|
2013-05-15 18:46:44 -07:00
|
|
|
(val.isObject() && IsObjectValueConvertibleToDictionary(cx, val));
|
2013-01-03 11:03:00 -08:00
|
|
|
}
|
|
|
|
|
2013-01-03 11:03:11 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-05-15 18:46:44 -07:00
|
|
|
IsConvertibleToCallbackInterface(JSContext* cx, JS::Handle<JSObject*> obj)
|
2013-01-03 11:03:11 -08:00
|
|
|
{
|
|
|
|
return IsNotDateOrRegExp(cx, obj);
|
|
|
|
}
|
|
|
|
|
2014-03-22 10:04:09 -07:00
|
|
|
// The items in the protoAndIfaceCache are indexed by the prototypes::id::ID and
|
2012-10-09 11:50:27 -07:00
|
|
|
// constructors::id::ID enums, in that order. The end of the prototype objects
|
|
|
|
// should be the start of the interface objects.
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert((size_t)constructors::id::_ID_Start ==
|
|
|
|
(size_t)prototypes::id::_ID_Count,
|
|
|
|
"Overlapping or discontiguous indexes.");
|
2012-10-09 11:50:27 -07:00
|
|
|
const size_t kProtoAndIfaceCacheCount = constructors::id::_ID_Count;
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2014-03-22 10:04:09 -07:00
|
|
|
class ProtoAndIfaceCache
|
2013-11-19 11:28:09 -08:00
|
|
|
{
|
2013-12-10 13:04:55 -08:00
|
|
|
// The caching strategy we use depends on what sort of global we're dealing
|
|
|
|
// with. For a window-like global, we want everything to be as fast as
|
|
|
|
// possible, so we use a flat array, indexed by prototype/constructor ID.
|
|
|
|
// For everything else (e.g. globals for JSMs), space is more important than
|
|
|
|
// speed, so we use a two-level lookup table.
|
|
|
|
|
|
|
|
class ArrayCache : public Array<JS::Heap<JSObject*>, kProtoAndIfaceCacheCount>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JSObject* EntrySlotIfExists(size_t i) {
|
|
|
|
return (*this)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Heap<JSObject*>& EntrySlotOrCreate(size_t i) {
|
|
|
|
return (*this)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Heap<JSObject*>& EntrySlotMustExist(size_t i) {
|
|
|
|
MOZ_ASSERT((*this)[i]);
|
|
|
|
return (*this)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trace(JSTracer* aTracer) {
|
|
|
|
for (size_t i = 0; i < ArrayLength(*this); ++i) {
|
|
|
|
if ((*this)[i]) {
|
2014-08-13 02:05:22 -07:00
|
|
|
JS_CallObjectTracer(aTracer, &(*this)[i], "protoAndIfaceCache[i]");
|
2013-12-10 13:04:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) {
|
|
|
|
return aMallocSizeOf(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PageTableCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PageTableCache() {
|
|
|
|
memset(&mPages, 0, sizeof(mPages));
|
|
|
|
}
|
|
|
|
|
|
|
|
~PageTableCache() {
|
|
|
|
for (size_t i = 0; i < ArrayLength(mPages); ++i) {
|
|
|
|
delete mPages[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* EntrySlotIfExists(size_t i) {
|
|
|
|
MOZ_ASSERT(i < kProtoAndIfaceCacheCount);
|
|
|
|
size_t pageIndex = i / kPageSize;
|
|
|
|
size_t leafIndex = i % kPageSize;
|
|
|
|
Page* p = mPages[pageIndex];
|
|
|
|
if (!p) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return (*p)[leafIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Heap<JSObject*>& EntrySlotOrCreate(size_t i) {
|
|
|
|
MOZ_ASSERT(i < kProtoAndIfaceCacheCount);
|
|
|
|
size_t pageIndex = i / kPageSize;
|
|
|
|
size_t leafIndex = i % kPageSize;
|
|
|
|
Page* p = mPages[pageIndex];
|
|
|
|
if (!p) {
|
|
|
|
p = new Page;
|
|
|
|
mPages[pageIndex] = p;
|
|
|
|
}
|
|
|
|
return (*p)[leafIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Heap<JSObject*>& EntrySlotMustExist(size_t i) {
|
|
|
|
MOZ_ASSERT(i < kProtoAndIfaceCacheCount);
|
|
|
|
size_t pageIndex = i / kPageSize;
|
|
|
|
size_t leafIndex = i % kPageSize;
|
|
|
|
Page* p = mPages[pageIndex];
|
|
|
|
MOZ_ASSERT(p);
|
|
|
|
return (*p)[leafIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trace(JSTracer* trc) {
|
|
|
|
for (size_t i = 0; i < ArrayLength(mPages); ++i) {
|
|
|
|
Page* p = mPages[i];
|
|
|
|
if (p) {
|
|
|
|
for (size_t j = 0; j < ArrayLength(*p); ++j) {
|
|
|
|
if ((*p)[j]) {
|
2014-08-13 02:05:22 -07:00
|
|
|
JS_CallObjectTracer(trc, &(*p)[j], "protoAndIfaceCache[i]");
|
2013-12-10 13:04:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) {
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
for (size_t i = 0; i < ArrayLength(mPages); ++i) {
|
|
|
|
n += aMallocSizeOf(mPages[i]);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const size_t kPageSize = 16;
|
|
|
|
typedef Array<JS::Heap<JSObject*>, kPageSize> Page;
|
|
|
|
static const size_t kNPages = kProtoAndIfaceCacheCount / kPageSize +
|
|
|
|
size_t(bool(kProtoAndIfaceCacheCount % kPageSize));
|
|
|
|
Array<Page*, kNPages> mPages;
|
|
|
|
};
|
|
|
|
|
2013-11-19 11:28:09 -08:00
|
|
|
public:
|
2013-12-10 13:04:55 -08:00
|
|
|
enum Kind {
|
|
|
|
WindowLike,
|
|
|
|
NonWindowLike
|
|
|
|
};
|
|
|
|
|
2014-07-28 10:17:51 -07:00
|
|
|
explicit ProtoAndIfaceCache(Kind aKind) : mKind(aKind) {
|
2014-03-22 10:04:09 -07:00
|
|
|
MOZ_COUNT_CTOR(ProtoAndIfaceCache);
|
2013-12-10 13:04:55 -08:00
|
|
|
if (aKind == WindowLike) {
|
|
|
|
mArrayCache = new ArrayCache();
|
|
|
|
} else {
|
|
|
|
mPageTableCache = new PageTableCache();
|
|
|
|
}
|
2013-11-19 11:28:09 -08:00
|
|
|
}
|
|
|
|
|
2014-03-22 10:04:09 -07:00
|
|
|
~ProtoAndIfaceCache() {
|
2013-12-10 13:04:55 -08:00
|
|
|
if (mKind == WindowLike) {
|
|
|
|
delete mArrayCache;
|
|
|
|
} else {
|
|
|
|
delete mPageTableCache;
|
|
|
|
}
|
2014-03-22 10:04:09 -07:00
|
|
|
MOZ_COUNT_DTOR(ProtoAndIfaceCache);
|
2013-11-19 11:28:09 -08:00
|
|
|
}
|
2013-11-20 05:55:09 -08:00
|
|
|
|
2013-12-10 13:04:55 -08:00
|
|
|
#define FORWARD_OPERATION(opName, args) \
|
|
|
|
do { \
|
|
|
|
if (mKind == WindowLike) { \
|
|
|
|
return mArrayCache->opName args; \
|
|
|
|
} else { \
|
|
|
|
return mPageTableCache->opName args; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
JSObject* EntrySlotIfExists(size_t i) {
|
|
|
|
FORWARD_OPERATION(EntrySlotIfExists, (i));
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Heap<JSObject*>& EntrySlotOrCreate(size_t i) {
|
|
|
|
FORWARD_OPERATION(EntrySlotOrCreate, (i));
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Heap<JSObject*>& EntrySlotMustExist(size_t i) {
|
|
|
|
FORWARD_OPERATION(EntrySlotMustExist, (i));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trace(JSTracer *aTracer) {
|
|
|
|
FORWARD_OPERATION(Trace, (aTracer));
|
|
|
|
}
|
|
|
|
|
2013-11-20 05:55:09 -08:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) {
|
2013-12-10 13:04:55 -08:00
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += (mKind == WindowLike
|
|
|
|
? mArrayCache->SizeOfIncludingThis(aMallocSizeOf)
|
|
|
|
: mPageTableCache->SizeOfIncludingThis(aMallocSizeOf));
|
|
|
|
return n;
|
2013-11-20 05:55:09 -08:00
|
|
|
}
|
2013-12-10 13:04:55 -08:00
|
|
|
#undef FORWARD_OPERATION
|
|
|
|
|
|
|
|
private:
|
|
|
|
union {
|
|
|
|
ArrayCache *mArrayCache;
|
|
|
|
PageTableCache *mPageTableCache;
|
|
|
|
};
|
|
|
|
Kind mKind;
|
2013-11-19 11:28:09 -08:00
|
|
|
};
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
inline void
|
2014-03-22 10:04:09 -07:00
|
|
|
AllocateProtoAndIfaceCache(JSObject* obj, ProtoAndIfaceCache::Kind aKind)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
MOZ_ASSERT(js::GetReservedSlot(obj, DOM_PROTOTYPE_SLOT).isUndefined());
|
|
|
|
|
2014-03-22 10:04:09 -07:00
|
|
|
ProtoAndIfaceCache* protoAndIfaceCache = new ProtoAndIfaceCache(aKind);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
|
|
|
js::SetReservedSlot(obj, DOM_PROTOTYPE_SLOT,
|
2014-03-22 10:04:09 -07:00
|
|
|
JS::PrivateValue(protoAndIfaceCache));
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
VerifyTraceProtoAndIfaceCacheCalled(JSTracer *trc, void **thingp,
|
|
|
|
JSGCTraceKind kind);
|
|
|
|
|
|
|
|
struct VerifyTraceProtoAndIfaceCacheCalledTracer : public JSTracer
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
|
2014-07-28 10:17:51 -07:00
|
|
|
explicit VerifyTraceProtoAndIfaceCacheCalledTracer(JSRuntime *rt)
|
2014-04-08 11:48:37 -07:00
|
|
|
: JSTracer(rt, VerifyTraceProtoAndIfaceCacheCalled), ok(false)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-05-02 05:49:43 -07:00
|
|
|
inline void
|
2012-10-09 11:50:27 -07:00
|
|
|
TraceProtoAndIfaceCache(JSTracer* trc, JSObject* obj)
|
2012-05-02 05:49:43 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (trc->callback == VerifyTraceProtoAndIfaceCacheCalled) {
|
|
|
|
// We don't do anything here, we only want to verify that
|
|
|
|
// TraceProtoAndIfaceCache was called.
|
|
|
|
static_cast<VerifyTraceProtoAndIfaceCacheCalledTracer*>(trc)->ok = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-22 10:04:09 -07:00
|
|
|
if (!HasProtoAndIfaceCache(obj))
|
2012-06-18 17:04:38 -07:00
|
|
|
return;
|
2014-03-22 10:04:09 -07:00
|
|
|
ProtoAndIfaceCache* protoAndIfaceCache = GetProtoAndIfaceCache(obj);
|
|
|
|
protoAndIfaceCache->Trace(trc);
|
2012-05-02 05:49:43 -07:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
inline void
|
2012-10-09 11:50:27 -07:00
|
|
|
DestroyProtoAndIfaceCache(JSObject* obj)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
|
2014-03-22 10:04:09 -07:00
|
|
|
ProtoAndIfaceCache* protoAndIfaceCache = GetProtoAndIfaceCache(obj);
|
2013-11-18 22:05:42 -08:00
|
|
|
|
2014-03-22 10:04:09 -07:00
|
|
|
delete protoAndIfaceCache;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-05-09 03:00:48 -07:00
|
|
|
/**
|
|
|
|
* Add constants to an object.
|
|
|
|
*/
|
|
|
|
bool
|
2013-05-07 19:34:56 -07:00
|
|
|
DefineConstants(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const ConstantSpec* cs);
|
2012-05-09 03:00:48 -07:00
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
struct JSNativeHolder
|
|
|
|
{
|
|
|
|
JSNative mNative;
|
|
|
|
const NativePropertyHooks* mPropertyHooks;
|
|
|
|
};
|
|
|
|
|
2013-01-22 02:51:15 -08:00
|
|
|
struct NamedConstructor
|
|
|
|
{
|
|
|
|
const char* mName;
|
|
|
|
const JSNativeHolder mHolder;
|
|
|
|
unsigned mNargs;
|
|
|
|
};
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
/*
|
|
|
|
* Create a DOM interface object (if constructorClass is non-null) and/or a
|
|
|
|
* DOM interface prototype object (if protoClass is non-null).
|
|
|
|
*
|
2012-04-23 07:10:56 -07:00
|
|
|
* global is used as the parent of the interface object and the interface
|
|
|
|
* prototype object
|
|
|
|
* protoProto is the prototype to use for the interface prototype object.
|
2013-05-02 11:38:19 -07:00
|
|
|
* interfaceProto is the prototype to use for the interface object.
|
2012-03-30 21:42:20 -07:00
|
|
|
* protoClass is the JSClass to use for the interface prototype object.
|
|
|
|
* This is null if we should not create an interface prototype
|
|
|
|
* object.
|
2012-10-09 11:50:27 -07:00
|
|
|
* protoCache a pointer to a JSObject pointer where we should cache the
|
|
|
|
* interface prototype object. This must be null if protoClass is and
|
|
|
|
* vice versa.
|
2012-03-30 21:42:20 -07:00
|
|
|
* constructorClass is the JSClass to use for the interface object.
|
2012-05-02 21:35:38 -07:00
|
|
|
* This is null if we should not create an interface object or
|
|
|
|
* if it should be a function object.
|
2012-10-09 11:50:27 -07:00
|
|
|
* constructor holds the JSNative to back the interface object which should be a
|
|
|
|
* Function, unless constructorClass is non-null in which case it is
|
|
|
|
* ignored. If this is null and constructorClass is also null then
|
|
|
|
* we should not create an interface object at all.
|
2012-05-02 21:35:38 -07:00
|
|
|
* ctorNargs is the length of the constructor function; 0 if no constructor
|
2012-10-09 11:50:27 -07:00
|
|
|
* constructorCache a pointer to a JSObject pointer where we should cache the
|
|
|
|
* interface object. This must be null if both constructorClass
|
|
|
|
* and constructor are null, and non-null otherwise.
|
2012-10-09 11:50:05 -07:00
|
|
|
* properties contains the methods, attributes and constants to be defined on
|
|
|
|
* objects in any compartment.
|
|
|
|
* chromeProperties contains the methods, attributes and constants to be defined
|
|
|
|
* on objects in chrome compartments. This must be null if the
|
|
|
|
* interface doesn't have any ChromeOnly properties or if the
|
|
|
|
* object is being created in non-chrome compartment.
|
2013-08-07 14:40:00 -07:00
|
|
|
* defineOnGlobal controls whether properties should be defined on the given
|
|
|
|
* global for the interface object (if any) and named
|
|
|
|
* constructors (if any) for this interface. This can be
|
|
|
|
* false in situations where we want the properties to only
|
|
|
|
* appear on privileged Xrays but not on the unprivileged
|
|
|
|
* underlying global.
|
2012-03-30 21:42:20 -07:00
|
|
|
*
|
2012-10-09 11:50:27 -07:00
|
|
|
* At least one of protoClass, constructorClass or constructor should be
|
|
|
|
* non-null. If constructorClass or constructor are non-null, the resulting
|
|
|
|
* interface object will be defined on the given global with property name
|
|
|
|
* |name|, which must also be non-null.
|
2012-03-30 21:42:20 -07:00
|
|
|
*/
|
2012-10-09 11:50:27 -07:00
|
|
|
void
|
2013-05-01 20:44:12 -07:00
|
|
|
CreateInterfaceObjects(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Handle<JSObject*> protoProto,
|
2014-08-05 03:46:42 -07:00
|
|
|
const js::Class* protoClass, JS::Heap<JSObject*>* protoCache,
|
2013-05-02 11:38:19 -07:00
|
|
|
JS::Handle<JSObject*> interfaceProto,
|
2014-08-05 03:46:42 -07:00
|
|
|
const js::Class* constructorClass, const JSNativeHolder* constructor,
|
2013-01-22 02:51:15 -08:00
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
2014-05-19 13:37:59 -07:00
|
|
|
JS::Heap<JSObject*>* constructorCache,
|
2012-10-09 11:50:27 -07:00
|
|
|
const NativeProperties* regularProperties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2013-08-07 14:40:00 -07:00
|
|
|
const char* name, bool defineOnGlobal);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2014-02-15 13:12:34 -08:00
|
|
|
/**
|
|
|
|
* Define the properties (regular and chrome-only) on obj.
|
|
|
|
*
|
|
|
|
* obj the object to instal the properties on. This should be the interface
|
|
|
|
* prototype object for regular interfaces and the instance object for
|
|
|
|
* interfaces marked with Global.
|
|
|
|
* properties contains the methods, attributes and constants to be defined on
|
|
|
|
* objects in any compartment.
|
|
|
|
* chromeProperties contains the methods, attributes and constants to be defined
|
|
|
|
* on objects in chrome compartments. This must be null if the
|
|
|
|
* interface doesn't have any ChromeOnly properties or if the
|
|
|
|
* object is being created in non-chrome compartment.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
DefineProperties(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties);
|
|
|
|
|
2014-07-04 06:02:10 -07:00
|
|
|
/*
|
|
|
|
* Define the unforgeable methods on an object.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
DefineUnforgeableMethods(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const Prefable<const JSFunctionSpec>* props);
|
|
|
|
|
2012-10-24 13:10:49 -07:00
|
|
|
/*
|
|
|
|
* Define the unforgeable attributes on an object.
|
|
|
|
*/
|
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
DefineUnforgeableAttributes(JSContext* cx, JS::Handle<JSObject*> obj,
|
2013-04-02 17:20:38 -07:00
|
|
|
const Prefable<const JSPropertySpec>* props);
|
2012-10-24 13:10:49 -07:00
|
|
|
|
2014-02-15 13:12:34 -08:00
|
|
|
bool
|
|
|
|
DefineWebIDLBindingUnforgeablePropertiesOnXPCObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
const NativeProperties* properties);
|
|
|
|
|
2012-11-22 03:09:41 -08:00
|
|
|
bool
|
2013-10-08 09:18:40 -07:00
|
|
|
DefineWebIDLBindingPropertiesOnXPCObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2014-02-15 13:12:34 -08:00
|
|
|
const NativeProperties* properties);
|
2012-11-22 03:09:41 -08:00
|
|
|
|
2012-11-08 23:48:42 -08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define HAS_MEMBER_CHECK(_name) \
|
|
|
|
template<typename V> static yes& Check(char (*)[(&V::_name == 0) + 1])
|
|
|
|
#else
|
|
|
|
#define HAS_MEMBER_CHECK(_name) \
|
|
|
|
template<typename V> static yes& Check(char (*)[sizeof(&V::_name) + 1])
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HAS_MEMBER(_name) \
|
|
|
|
template<typename T> \
|
|
|
|
class Has##_name##Member { \
|
|
|
|
typedef char yes[1]; \
|
|
|
|
typedef char no[2]; \
|
|
|
|
HAS_MEMBER_CHECK(_name); \
|
|
|
|
template<typename V> static no& Check(...); \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
static bool const Value = sizeof(Check<T>(nullptr)) == sizeof(yes); \
|
|
|
|
};
|
|
|
|
|
|
|
|
HAS_MEMBER(WrapObject)
|
|
|
|
|
|
|
|
// HasWrapObject<T>::Value will be true if T has a WrapObject member but it's
|
|
|
|
// not nsWrapperCache::WrapObject.
|
|
|
|
template<typename T>
|
|
|
|
struct HasWrapObject
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef char yes[1];
|
|
|
|
typedef char no[2];
|
2013-04-25 09:29:54 -07:00
|
|
|
typedef JSObject* (nsWrapperCache::*WrapObject)(JSContext*,
|
|
|
|
JS::Handle<JSObject*>);
|
2012-11-08 23:48:42 -08:00
|
|
|
template<typename U, U> struct SFINAE;
|
|
|
|
template <typename V> static no& Check(SFINAE<WrapObject, &V::WrapObject>*);
|
|
|
|
template <typename V> static yes& Check(...);
|
|
|
|
|
|
|
|
public:
|
|
|
|
static bool const Value = HasWrapObjectMember<T>::Value &&
|
|
|
|
sizeof(Check<T>(nullptr)) == sizeof(yes);
|
|
|
|
};
|
|
|
|
|
2012-11-22 02:46:53 -08:00
|
|
|
#ifdef DEBUG
|
2013-07-04 08:24:59 -07:00
|
|
|
template <class T, bool isISupports=IsBaseOf<nsISupports, T>::value>
|
2012-11-22 02:46:53 -08:00
|
|
|
struct
|
|
|
|
CheckWrapperCacheCast
|
|
|
|
{
|
|
|
|
static bool Check()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<uintptr_t>(
|
|
|
|
static_cast<nsWrapperCache*>(
|
|
|
|
reinterpret_cast<T*>(1))) == 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <class T>
|
|
|
|
struct
|
|
|
|
CheckWrapperCacheCast<T, true>
|
|
|
|
{
|
|
|
|
static bool Check()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-11-27 01:20:04 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
CouldBeDOMBinding(void*)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
CouldBeDOMBinding(nsWrapperCache* aCache)
|
|
|
|
{
|
|
|
|
return aCache->IsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2014-02-15 13:10:25 -08:00
|
|
|
inline bool
|
|
|
|
TryToOuterize(JSContext* cx, JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
if (js::IsInnerObject(&rval.toObject())) {
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &rval.toObject());
|
|
|
|
obj = JS_ObjectToOuterObject(cx, obj);
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rval.set(JS::ObjectValue(*obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-11 08:58:29 -07:00
|
|
|
// Make sure to wrap the given string value into the right compartment, as
|
|
|
|
// needed.
|
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
bool
|
|
|
|
MaybeWrapStringValue(JSContext* cx, JS::MutableHandle<JS::Value> rval)
|
2013-01-02 19:03:25 -08:00
|
|
|
{
|
2013-07-11 08:58:29 -07:00
|
|
|
MOZ_ASSERT(rval.isString());
|
|
|
|
JSString* str = rval.toString();
|
2014-07-28 02:39:36 -07:00
|
|
|
if (JS::GetTenuredGCThingZone(str) != js::GetContextZone(cx)) {
|
2013-10-26 09:19:05 -07:00
|
|
|
return JS_WrapValue(cx, rval);
|
2013-03-16 20:36:37 -07:00
|
|
|
}
|
2013-07-11 08:58:29 -07:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-16 20:36:37 -07:00
|
|
|
|
2013-07-11 08:58:29 -07:00
|
|
|
// Make sure to wrap the given object value into the right compartment as
|
|
|
|
// needed. This will work correctly, but possibly slowly, on all objects.
|
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
bool
|
|
|
|
MaybeWrapObjectValue(JSContext* cx, JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(rval.isObject());
|
2013-02-21 18:23:47 -08:00
|
|
|
|
2014-03-26 06:59:20 -07:00
|
|
|
// Cross-compartment always requires wrapping.
|
2013-04-19 12:57:18 -07:00
|
|
|
JSObject* obj = &rval.toObject();
|
|
|
|
if (js::GetObjectCompartment(obj) != js::GetContextCompartment(cx)) {
|
2013-10-26 09:19:05 -07:00
|
|
|
return JS_WrapValue(cx, rval);
|
2013-04-19 12:57:18 -07:00
|
|
|
}
|
2013-03-16 20:36:37 -07:00
|
|
|
|
2014-02-15 13:10:25 -08:00
|
|
|
// We're same-compartment, but even then we might need to wrap
|
|
|
|
// objects specially. Check for that.
|
2014-03-26 06:59:20 -07:00
|
|
|
if (IsDOMObject(obj)) {
|
2014-02-15 13:10:25 -08:00
|
|
|
return TryToOuterize(cx, rval);
|
2013-01-02 19:03:25 -08:00
|
|
|
}
|
|
|
|
|
2013-07-11 08:58:29 -07:00
|
|
|
// It's not a WebIDL object. But it might be an XPConnect one, in which case
|
|
|
|
// we may need to outerize here, so make sure to call JS_WrapValue.
|
2013-10-26 09:19:05 -07:00
|
|
|
return JS_WrapValue(cx, rval);
|
2013-01-02 19:03:25 -08:00
|
|
|
}
|
|
|
|
|
2013-07-11 08:58:29 -07:00
|
|
|
// Like MaybeWrapObjectValue, but also allows null
|
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
bool
|
|
|
|
MaybeWrapObjectOrNullValue(JSContext* cx, JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(rval.isObjectOrNull());
|
|
|
|
if (rval.isNull()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return MaybeWrapObjectValue(cx, rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapping for objects that are known to not be DOM or XPConnect objects
|
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
bool
|
|
|
|
MaybeWrapNonDOMObjectValue(JSContext* cx, JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(rval.isObject());
|
|
|
|
MOZ_ASSERT(!GetDOMClass(&rval.toObject()));
|
|
|
|
MOZ_ASSERT(!(js::GetObjectClass(&rval.toObject())->flags &
|
|
|
|
JSCLASS_PRIVATE_IS_NSISUPPORTS));
|
|
|
|
|
|
|
|
JSObject* obj = &rval.toObject();
|
|
|
|
if (js::GetObjectCompartment(obj) == js::GetContextCompartment(cx)) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-26 09:19:05 -07:00
|
|
|
return JS_WrapValue(cx, rval);
|
2013-07-11 08:58:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Like MaybeWrapNonDOMObjectValue but allows null
|
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
bool
|
|
|
|
MaybeWrapNonDOMObjectOrNullValue(JSContext* cx, JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(rval.isObjectOrNull());
|
|
|
|
if (rval.isNull()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return MaybeWrapNonDOMObjectValue(cx, rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If rval is a gcthing and is not in the compartment of cx, wrap rval
|
|
|
|
// into the compartment of cx (typically by replacing it with an Xray or
|
|
|
|
// cross-compartment wrapper around the original object).
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
MaybeWrapValue(JSContext* cx, JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
if (rval.isString()) {
|
|
|
|
return MaybeWrapStringValue(cx, rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rval.isObject()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MaybeWrapObjectValue(cx, rval);
|
|
|
|
}
|
|
|
|
|
2012-11-27 01:20:04 -08:00
|
|
|
// Create a JSObject wrapping "value", if there isn't one already, and store it
|
2013-06-07 19:45:46 -07:00
|
|
|
// in rval. "value" must be a concrete class that implements a
|
2012-11-27 01:20:04 -08:00
|
|
|
// GetWrapperPreserveColor() which can return its existing wrapper, if any, and
|
|
|
|
// a WrapObject() which will try to create a wrapper. Typically, this is done by
|
|
|
|
// having "value" inherit from nsWrapperCache.
|
2014-09-15 08:00:25 -07:00
|
|
|
//
|
|
|
|
// The value stored in rval will be ready to be exposed to whatever JS
|
|
|
|
// is running on cx right now. In particular, it will be in the
|
|
|
|
// compartment of cx, and outerized as needed.
|
2012-03-30 21:42:20 -07:00
|
|
|
template <class T>
|
2012-11-20 08:36:06 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-04-08 15:27:19 -07:00
|
|
|
WrapNewBindingObject(JSContext* cx, T* value, JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2013-04-24 11:59:15 -07:00
|
|
|
MOZ_ASSERT(value);
|
2012-11-20 08:36:06 -08:00
|
|
|
JSObject* obj = value->GetWrapperPreserveColor();
|
2014-03-19 09:35:45 -07:00
|
|
|
// We can get rid of this when we remove support for hasXPConnectImpls.
|
2012-09-12 13:29:30 -07:00
|
|
|
bool couldBeDOMBinding = CouldBeDOMBinding(value);
|
2012-11-20 08:36:06 -08:00
|
|
|
if (obj) {
|
2013-09-08 20:28:48 -07:00
|
|
|
JS::ExposeObjectToActiveJS(obj);
|
2012-11-27 01:20:04 -08:00
|
|
|
} else {
|
|
|
|
// Inline this here while we have non-dom objects in wrapper caches.
|
2012-09-12 13:29:30 -07:00
|
|
|
if (!couldBeDOMBinding) {
|
2012-11-27 01:20:04 -08:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2014-04-08 15:27:18 -07:00
|
|
|
obj = value->WrapObject(cx);
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!obj) {
|
2013-03-11 16:03:47 -07:00
|
|
|
// At this point, obj is null, so just return false.
|
|
|
|
// Callers seem to be testing JS_IsExceptionPending(cx) to
|
|
|
|
// figure out whether WrapObject() threw.
|
2012-03-30 21:42:20 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-08 23:48:42 -08:00
|
|
|
#ifdef DEBUG
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* clasp = GetDOMClass(obj);
|
2014-04-08 15:27:19 -07:00
|
|
|
// clasp can be null if the cache contained a non-DOM object.
|
2012-12-06 12:21:19 -08:00
|
|
|
if (clasp) {
|
2012-11-22 02:46:53 -08:00
|
|
|
// Some sanity asserts about our object. Specifically:
|
|
|
|
// 1) If our class claims we're nsISupports, we better be nsISupports
|
|
|
|
// XXXbz ideally, we could assert that reinterpret_cast to nsISupports
|
|
|
|
// does the right thing, but I don't see a way to do it. :(
|
|
|
|
// 2) If our class doesn't claim we're nsISupports we better be
|
|
|
|
// reinterpret_castable to nsWrapperCache.
|
|
|
|
MOZ_ASSERT(clasp, "What happened here?");
|
2013-07-04 08:24:59 -07:00
|
|
|
MOZ_ASSERT_IF(clasp->mDOMObjectIsISupports, (IsBaseOf<nsISupports, T>::value));
|
2012-11-22 02:46:53 -08:00
|
|
|
MOZ_ASSERT(CheckWrapperCacheCast<T>::Check());
|
|
|
|
}
|
2012-09-12 13:29:30 -07:00
|
|
|
#endif
|
|
|
|
|
2014-02-15 13:10:25 -08:00
|
|
|
rval.set(JS::ObjectValue(*obj));
|
|
|
|
|
2012-09-12 13:29:30 -07:00
|
|
|
bool sameCompartment =
|
|
|
|
js::GetObjectCompartment(obj) == js::GetContextCompartment(cx);
|
|
|
|
if (sameCompartment && couldBeDOMBinding) {
|
2014-02-15 13:10:25 -08:00
|
|
|
// We only need to outerize Window objects, so anything inheriting from
|
|
|
|
// nsGlobalWindow (which inherits from EventTarget itself).
|
|
|
|
return IsBaseOf<nsGlobalWindow, T>::value || IsSame<EventTarget, T>::value ?
|
|
|
|
TryToOuterize(cx, rval) : true;
|
2012-09-12 13:29:30 -07:00
|
|
|
}
|
|
|
|
|
2013-10-26 09:19:05 -07:00
|
|
|
return JS_WrapValue(cx, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-11-27 01:20:04 -08:00
|
|
|
// Create a JSObject wrapping "value", for cases when "value" is a
|
|
|
|
// non-wrapper-cached object using WebIDL bindings. "value" must implement a
|
|
|
|
// WrapObject() method taking a JSContext and a scope.
|
2012-06-15 13:25:50 -07:00
|
|
|
template <class T>
|
|
|
|
inline bool
|
2013-04-25 09:29:53 -07:00
|
|
|
WrapNewBindingNonWrapperCachedObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> scopeArg,
|
2013-06-07 19:45:46 -07:00
|
|
|
T* value,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-06-15 13:25:50 -07:00
|
|
|
{
|
2013-04-24 11:59:15 -07:00
|
|
|
MOZ_ASSERT(value);
|
2012-06-15 13:25:50 -07:00
|
|
|
// We try to wrap in the compartment of the underlying object of "scope"
|
2013-05-03 16:29:09 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx);
|
2012-06-15 13:25:50 -07:00
|
|
|
{
|
2012-08-21 18:42:53 -07:00
|
|
|
// scope for the JSAutoCompartment so that we restore the compartment
|
|
|
|
// before we call JS_WrapValue.
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-04-25 09:29:53 -07:00
|
|
|
// Maybe<Handle> doesn't so much work, and in any case, adding
|
|
|
|
// more Maybe (one for a Rooted and one for a Handle) adds more
|
|
|
|
// code (and branches!) than just adding a single rooted.
|
|
|
|
JS::Rooted<JSObject*> scope(cx, scopeArg);
|
2012-06-15 13:25:50 -07:00
|
|
|
if (js::IsWrapper(scope)) {
|
2013-04-11 11:50:18 -07:00
|
|
|
scope = js::CheckedUnwrap(scope, /* stopAtOuter = */ false);
|
2012-08-21 18:42:53 -07:00
|
|
|
if (!scope)
|
2012-06-15 13:25:50 -07:00
|
|
|
return false;
|
2014-08-13 15:39:41 -07:00
|
|
|
ac.emplace(cx, scope);
|
2012-06-15 13:25:50 -07:00
|
|
|
}
|
|
|
|
|
2014-04-08 15:27:18 -07:00
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(scope, cx));
|
|
|
|
obj = value->WrapObject(cx);
|
2012-06-15 13:25:50 -07:00
|
|
|
}
|
|
|
|
|
2013-04-19 15:18:31 -07:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-19 01:49:21 -07:00
|
|
|
// We can end up here in all sorts of compartments, per above. Make
|
|
|
|
// sure to JS_WrapValue!
|
2013-06-07 19:45:46 -07:00
|
|
|
rval.set(JS::ObjectValue(*obj));
|
2013-10-26 09:19:05 -07:00
|
|
|
return JS_WrapValue(cx, rval);
|
2013-04-19 01:49:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a JSObject wrapping "value", for cases when "value" is a
|
|
|
|
// non-wrapper-cached owned object using WebIDL bindings. "value" must implement a
|
|
|
|
// WrapObject() method taking a JSContext, a scope, and a boolean outparam that
|
|
|
|
// is true if the JSObject took ownership
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2013-04-25 09:29:53 -07:00
|
|
|
WrapNewBindingNonWrapperCachedOwnedObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> scopeArg,
|
2013-06-07 19:45:46 -07:00
|
|
|
nsAutoPtr<T>& value,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2013-04-19 01:49:21 -07:00
|
|
|
{
|
2013-04-24 11:59:15 -07:00
|
|
|
// We do a runtime check on value, because otherwise we might in
|
|
|
|
// fact end up wrapping a null and invoking methods on it later.
|
|
|
|
if (!value) {
|
|
|
|
NS_RUNTIMEABORT("Don't try to wrap null objects");
|
|
|
|
}
|
2013-04-19 01:49:21 -07:00
|
|
|
// We try to wrap in the compartment of the underlying object of "scope"
|
2013-05-03 16:29:09 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx);
|
2013-04-19 01:49:21 -07:00
|
|
|
{
|
|
|
|
// scope for the JSAutoCompartment so that we restore the compartment
|
|
|
|
// before we call JS_WrapValue.
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-04-25 09:29:53 -07:00
|
|
|
// Maybe<Handle> doesn't so much work, and in any case, adding
|
|
|
|
// more Maybe (one for a Rooted and one for a Handle) adds more
|
|
|
|
// code (and branches!) than just adding a single rooted.
|
|
|
|
JS::Rooted<JSObject*> scope(cx, scopeArg);
|
2013-04-19 01:49:21 -07:00
|
|
|
if (js::IsWrapper(scope)) {
|
|
|
|
scope = js::CheckedUnwrap(scope, /* stopAtOuter = */ false);
|
|
|
|
if (!scope)
|
|
|
|
return false;
|
2014-08-13 15:39:41 -07:00
|
|
|
ac.emplace(cx, scope);
|
2013-04-19 01:49:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tookOwnership = false;
|
2014-04-08 15:27:18 -07:00
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(scope, cx));
|
|
|
|
obj = value->WrapObject(cx, &tookOwnership);
|
2013-05-03 16:29:09 -07:00
|
|
|
MOZ_ASSERT_IF(obj, tookOwnership);
|
2013-04-19 01:49:21 -07:00
|
|
|
if (tookOwnership) {
|
|
|
|
value.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 15:18:31 -07:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-15 13:25:50 -07:00
|
|
|
// We can end up here in all sorts of compartments, per above. Make
|
|
|
|
// sure to JS_WrapValue!
|
2013-06-07 19:45:46 -07:00
|
|
|
rval.set(JS::ObjectValue(*obj));
|
2013-10-26 09:19:05 -07:00
|
|
|
return JS_WrapValue(cx, rval);
|
2012-06-15 13:25:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for smart pointers (nsAutoPtr/nsRefPtr/nsCOMPtr).
|
|
|
|
template <template <typename> class SmartPtr, typename T>
|
|
|
|
inline bool
|
2013-04-25 09:29:53 -07:00
|
|
|
WrapNewBindingNonWrapperCachedObject(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-07 19:45:46 -07:00
|
|
|
const SmartPtr<T>& value,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-06-15 13:25:50 -07:00
|
|
|
{
|
2013-06-07 19:45:46 -07:00
|
|
|
return WrapNewBindingNonWrapperCachedObject(cx, scope, value.get(), rval);
|
2012-06-15 13:25:50 -07:00
|
|
|
}
|
|
|
|
|
2012-11-27 01:20:04 -08:00
|
|
|
// Only set allowNativeWrapper to false if you really know you need it, if in
|
|
|
|
// doubt use true. Setting it to false disables security wrappers.
|
2012-11-27 13:30:39 -08:00
|
|
|
bool
|
2012-11-27 01:20:04 -08:00
|
|
|
NativeInterface2JSObjectAndThrowIfFailed(JSContext* aCx,
|
2013-04-29 14:33:42 -07:00
|
|
|
JS::Handle<JSObject*> aScope,
|
2013-10-11 22:02:39 -07:00
|
|
|
JS::MutableHandle<JS::Value> aRetval,
|
2012-11-27 01:20:04 -08:00
|
|
|
xpcObjectHelper& aHelper,
|
|
|
|
const nsIID* aIID,
|
|
|
|
bool aAllowNativeWrapper);
|
|
|
|
|
2012-11-27 13:30:39 -08:00
|
|
|
/**
|
2012-11-27 01:20:04 -08:00
|
|
|
* A method to handle new-binding wrap failure, by possibly falling back to
|
|
|
|
* wrapping as a non-new-binding object.
|
2012-11-27 13:30:39 -08:00
|
|
|
*/
|
2012-03-30 21:42:20 -07:00
|
|
|
template <class T>
|
2012-11-27 01:20:04 -08:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-04-29 14:33:42 -07:00
|
|
|
HandleNewBindingWrappingFailure(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-07 19:45:46 -07:00
|
|
|
T* value, JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2012-11-27 01:20:04 -08:00
|
|
|
if (JS_IsExceptionPending(cx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qsObjectHelper helper(value, GetWrapperCache(value));
|
2013-10-11 22:02:39 -07:00
|
|
|
return NativeInterface2JSObjectAndThrowIfFailed(cx, scope, rval,
|
2013-06-07 19:45:46 -07:00
|
|
|
helper, nullptr, true);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2013-01-28 05:34:30 -08:00
|
|
|
// Helper for calling HandleNewBindingWrappingFailure with smart pointers
|
|
|
|
// (nsAutoPtr/nsRefPtr/nsCOMPtr) or references.
|
|
|
|
HAS_MEMBER(get)
|
|
|
|
|
|
|
|
template <class T, bool isSmartPtr=HasgetMember<T>::Value>
|
|
|
|
struct HandleNewBindingWrappingFailureHelper
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2013-04-29 14:33:42 -07:00
|
|
|
static inline bool Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-07 19:45:46 -07:00
|
|
|
const T& value, JS::MutableHandle<JS::Value> rval)
|
2013-01-28 05:34:30 -08:00
|
|
|
{
|
2013-06-07 19:45:46 -07:00
|
|
|
return HandleNewBindingWrappingFailure(cx, scope, value.get(), rval);
|
2013-01-28 05:34:30 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct HandleNewBindingWrappingFailureHelper<T, false>
|
|
|
|
{
|
2013-04-29 14:33:42 -07:00
|
|
|
static inline bool Wrap(JSContext* cx, JS::Handle<JSObject*> scope, T& value,
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2013-01-28 05:34:30 -08:00
|
|
|
{
|
2013-06-07 19:45:46 -07:00
|
|
|
return HandleNewBindingWrappingFailure(cx, scope, &value, rval);
|
2013-01-28 05:34:30 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-29 14:33:42 -07:00
|
|
|
HandleNewBindingWrappingFailure(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-07 19:45:46 -07:00
|
|
|
T& value, JS::MutableHandle<JS::Value> rval)
|
2013-01-28 05:34:30 -08:00
|
|
|
{
|
2013-06-07 19:45:46 -07:00
|
|
|
return HandleNewBindingWrappingFailureHelper<T>::Wrap(cx, scope, value, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-07-18 03:36:08 -07:00
|
|
|
template<bool Fatal>
|
|
|
|
inline bool
|
2014-07-02 06:45:04 -07:00
|
|
|
EnumValueNotFound(JSContext* cx, JSString* str, const char* type,
|
|
|
|
const char* sourceDescription)
|
2012-07-18 03:36:08 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool
|
2014-07-02 06:45:04 -07:00
|
|
|
EnumValueNotFound<false>(JSContext* cx, JSString* str, const char* type,
|
|
|
|
const char* sourceDescription)
|
2012-07-18 03:36:08 -07:00
|
|
|
{
|
|
|
|
// TODO: Log a warning to the console.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool
|
2014-07-02 06:45:04 -07:00
|
|
|
EnumValueNotFound<true>(JSContext* cx, JSString* str, const char* type,
|
|
|
|
const char* sourceDescription)
|
2012-07-18 03:36:08 -07:00
|
|
|
{
|
2014-07-02 06:45:04 -07:00
|
|
|
JSAutoByteString deflated(cx, str);
|
|
|
|
if (!deflated) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-17 10:07:03 -07:00
|
|
|
return ThrowErrorMessage(cx, MSG_INVALID_ENUM_VALUE, sourceDescription,
|
2014-07-02 06:45:04 -07:00
|
|
|
deflated.ptr(), type);
|
2012-07-18 03:36:08 -07:00
|
|
|
}
|
|
|
|
|
2014-07-02 06:45:04 -07:00
|
|
|
template<typename CharT>
|
2012-03-30 21:42:20 -07:00
|
|
|
inline int
|
2014-07-02 06:45:04 -07:00
|
|
|
FindEnumStringIndexImpl(const CharT* chars, size_t length, const EnumEntry* values)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (const EnumEntry* value = values; value->value; ++value, ++i) {
|
|
|
|
if (length != value->length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equal = true;
|
|
|
|
const char* val = value->value;
|
|
|
|
for (size_t j = 0; j != length; ++j) {
|
|
|
|
if (unsigned(val[j]) != unsigned(chars[j])) {
|
|
|
|
equal = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (equal) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:45:04 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool InvalidValueFatal>
|
|
|
|
inline int
|
|
|
|
FindEnumStringIndex(JSContext* cx, JS::Handle<JS::Value> v, const EnumEntry* values,
|
|
|
|
const char* type, const char* sourceDescription, bool* ok)
|
|
|
|
{
|
|
|
|
// JS_StringEqualsAscii is slow as molasses, so don't use it here.
|
|
|
|
JSString* str = JS::ToString(cx, v);
|
|
|
|
if (!str) {
|
|
|
|
*ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
size_t length;
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
2014-07-03 04:05:03 -07:00
|
|
|
if (js::StringHasLatin1Chars(str)) {
|
2014-07-02 06:45:04 -07:00
|
|
|
const JS::Latin1Char* chars = JS_GetLatin1StringCharsAndLength(cx, nogc, str,
|
|
|
|
&length);
|
|
|
|
if (!chars) {
|
|
|
|
*ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
index = FindEnumStringIndexImpl(chars, length, values);
|
|
|
|
} else {
|
2014-07-21 21:43:21 -07:00
|
|
|
const char16_t* chars = JS_GetTwoByteStringCharsAndLength(cx, nogc, str,
|
|
|
|
&length);
|
2014-07-02 06:45:04 -07:00
|
|
|
if (!chars) {
|
|
|
|
*ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
index = FindEnumStringIndexImpl(chars, length, values);
|
|
|
|
}
|
|
|
|
if (index >= 0) {
|
|
|
|
*ok = true;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ok = EnumValueNotFound<InvalidValueFatal>(cx, str, type, sourceDescription);
|
2012-06-21 00:11:07 -07:00
|
|
|
return -1;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-05-10 12:24:59 -07:00
|
|
|
inline nsWrapperCache*
|
|
|
|
GetWrapperCache(const ParentObject& aParentObject)
|
|
|
|
{
|
|
|
|
return aParentObject.mWrapperCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2012-10-09 11:50:26 -07:00
|
|
|
inline T*
|
2012-05-10 12:24:59 -07:00
|
|
|
GetParentPointer(T* aObject)
|
|
|
|
{
|
2012-10-09 11:50:26 -07:00
|
|
|
return aObject;
|
2012-05-10 12:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsISupports*
|
|
|
|
GetParentPointer(const ParentObject& aObject)
|
|
|
|
{
|
2012-10-09 11:50:26 -07:00
|
|
|
return aObject.mObject;
|
2012-05-10 12:24:59 -07:00
|
|
|
}
|
|
|
|
|
2014-03-19 09:35:45 -07:00
|
|
|
template <typename T>
|
|
|
|
inline bool
|
|
|
|
GetUseXBLScope(T* aParentObject)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
GetUseXBLScope(const ParentObject& aParentObject)
|
|
|
|
{
|
|
|
|
return aParentObject.mUseXBLScope;
|
|
|
|
}
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
template<class T>
|
|
|
|
inline void
|
|
|
|
ClearWrapper(T* p, nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
cache->ClearWrapper();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline void
|
|
|
|
ClearWrapper(T* p, void*)
|
|
|
|
{
|
|
|
|
nsWrapperCache* cache;
|
|
|
|
CallQueryInterface(p, &cache);
|
|
|
|
ClearWrapper(p, cache);
|
|
|
|
}
|
|
|
|
|
2014-09-17 07:35:11 -07:00
|
|
|
template<class T>
|
|
|
|
inline void
|
|
|
|
UpdateWrapper(T* p, nsWrapperCache* cache, JSObject* obj, const JSObject* old)
|
|
|
|
{
|
|
|
|
JS::AutoAssertGCCallback inCallback(obj);
|
|
|
|
cache->UpdateWrapper(obj, old);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline void
|
|
|
|
UpdateWrapper(T* p, void*, JSObject* obj, const JSObject* old)
|
|
|
|
{
|
|
|
|
JS::AutoAssertGCCallback inCallback(obj);
|
|
|
|
nsWrapperCache* cache;
|
|
|
|
CallQueryInterface(p, &cache);
|
|
|
|
UpdateWrapper(p, cache, obj, old);
|
|
|
|
}
|
|
|
|
|
2012-11-09 10:59:02 -08:00
|
|
|
// Attempt to preserve the wrapper, if any, for a Paris DOM bindings object.
|
|
|
|
// Return true if we successfully preserved the wrapper, or there is no wrapper
|
|
|
|
// to preserve. In the latter case we don't need to preserve the wrapper, because
|
|
|
|
// the object can only be obtained by JS once, or they cannot be meaningfully
|
|
|
|
// owned from the native side.
|
|
|
|
//
|
|
|
|
// This operation will return false only for non-nsISupports cycle-collected
|
|
|
|
// objects, because we cannot determine if they are wrappercached or not.
|
|
|
|
bool
|
|
|
|
TryPreserveWrapper(JSObject* obj);
|
|
|
|
|
2014-05-19 13:37:59 -07:00
|
|
|
// Can only be called with a DOM JSClass.
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2014-05-19 13:37:59 -07:00
|
|
|
InstanceClassHasProtoAtDepth(const js::Class* clasp,
|
|
|
|
uint32_t protoID, uint32_t depth);
|
2012-08-07 22:26:18 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
// Only set allowNativeWrapper to false if you really know you need it, if in
|
|
|
|
// doubt use true. Setting it to false disables security wrappers.
|
|
|
|
bool
|
2013-04-29 14:33:42 -07:00
|
|
|
XPCOMObjectToJsval(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
xpcObjectHelper& helper, const nsIID* iid,
|
2013-10-11 22:02:39 -07:00
|
|
|
bool allowNativeWrapper, JS::MutableHandle<JS::Value> rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2013-04-03 19:22:16 -07:00
|
|
|
// Special-cased wrapping for variants
|
|
|
|
bool
|
2014-04-09 21:58:43 -07:00
|
|
|
VariantToJsval(JSContext* aCx, nsIVariant* aVariant,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval);
|
2013-04-03 19:22:16 -07:00
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Wrap an object "p" which is not using WebIDL bindings yet. This _will_
|
|
|
|
// actually work on WebIDL binding objects that are wrappercached, but will be
|
|
|
|
// much slower than WrapNewBindingObject. "cache" must either be null or be the
|
|
|
|
// nsWrapperCache for "p".
|
2012-03-30 21:42:20 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject(JSContext* cx, T* p, nsWrapperCache* cache, const nsIID* iid,
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-04-09 21:58:42 -07:00
|
|
|
if (xpc_FastGetCachedWrapper(cx, cache, rval))
|
2012-03-30 21:42:20 -07:00
|
|
|
return true;
|
|
|
|
qsObjectHelper helper(p, cache);
|
2014-04-09 21:58:43 -07:00
|
|
|
JS::Rooted<JSObject*> scope(cx, JS::CurrentGlobalOrNull(cx));
|
2013-10-11 22:02:39 -07:00
|
|
|
return XPCOMObjectToJsval(cx, scope, helper, iid, true, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2013-04-03 19:22:16 -07:00
|
|
|
// A specialization of the above for nsIVariant, because that needs to
|
|
|
|
// do something different.
|
|
|
|
template<>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject<nsIVariant>(JSContext* cx, nsIVariant* p,
|
2013-06-07 19:45:46 -07:00
|
|
|
nsWrapperCache* cache, const nsIID* iid,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2013-04-03 19:22:16 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(iid);
|
|
|
|
MOZ_ASSERT(iid->Equals(NS_GET_IID(nsIVariant)));
|
2014-04-09 21:58:43 -07:00
|
|
|
return VariantToJsval(cx, p, rval);
|
2013-04-03 19:22:16 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Wrap an object "p" which is not using WebIDL bindings yet. Just like the
|
|
|
|
// variant that takes an nsWrapperCache above, but will try to auto-derive the
|
|
|
|
// nsWrapperCache* from "p".
|
2012-03-30 21:42:20 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject(JSContext* cx, T* p, const nsIID* iid,
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-04-09 21:58:43 -07:00
|
|
|
return WrapObject(cx, p, GetWrapperCache(p), iid, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Just like the WrapObject above, but without requiring you to pick which
|
|
|
|
// interface you're wrapping as. This should only be used for objects that have
|
|
|
|
// classinfo, for which it doesn't matter what IID is used to wrap.
|
2012-03-30 21:42:20 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject(JSContext* cx, T* p, JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-04-09 21:58:43 -07:00
|
|
|
return WrapObject(cx, p, nullptr, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Helper to make it possible to wrap directly out of an nsCOMPtr
|
2012-03-30 21:42:20 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject(JSContext* cx, const nsCOMPtr<T>& p,
|
2013-06-07 19:45:46 -07:00
|
|
|
const nsIID* iid, JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-04-09 21:58:43 -07:00
|
|
|
return WrapObject(cx, p.get(), iid, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Helper to make it possible to wrap directly out of an nsCOMPtr
|
2012-03-30 21:42:20 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject(JSContext* cx, const nsCOMPtr<T>& p,
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-04-09 21:58:43 -07:00
|
|
|
return WrapObject(cx, p, nullptr, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Helper to make it possible to wrap directly out of an nsRefPtr
|
2012-03-30 21:42:20 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject(JSContext* cx, const nsRefPtr<T>& p,
|
2013-06-07 19:45:46 -07:00
|
|
|
const nsIID* iid, JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-04-09 21:58:43 -07:00
|
|
|
return WrapObject(cx, p.get(), iid, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Helper to make it possible to wrap directly out of an nsRefPtr
|
2012-03-30 21:42:20 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject(JSContext* cx, const nsRefPtr<T>& p,
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-04-09 21:58:43 -07:00
|
|
|
return WrapObject(cx, p, nullptr, rval);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Specialization to make it easy to use WrapObject in codegen.
|
2012-03-30 21:42:20 -07:00
|
|
|
template<>
|
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject<JSObject>(JSContext* cx, JSObject* p,
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2013-06-07 19:45:46 -07:00
|
|
|
rval.set(JS::ObjectOrNullValue(p));
|
2012-03-30 21:42:20 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
inline bool
|
2014-04-09 21:58:43 -07:00
|
|
|
WrapObject(JSContext* cx, JSObject& p, JS::MutableHandle<JS::Value> rval)
|
2013-03-09 03:34:29 -08:00
|
|
|
{
|
2013-06-07 19:45:46 -07:00
|
|
|
rval.set(JS::ObjectValue(p));
|
2013-03-09 03:34:29 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Given an object "p" that inherits from nsISupports, wrap it and return the
|
|
|
|
// result. Null is returned on wrapping failure. This is somewhat similar to
|
|
|
|
// WrapObject() above, but does NOT allow Xrays around the result, since we
|
|
|
|
// don't want those for our parent object.
|
2012-10-19 00:33:18 -07:00
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
2014-04-08 15:27:15 -07:00
|
|
|
WrapNativeISupportsParent(JSContext* cx, T* p, nsWrapperCache* cache)
|
2012-10-19 00:33:18 -07:00
|
|
|
{
|
|
|
|
qsObjectHelper helper(ToSupports(p), cache);
|
2014-04-08 15:27:15 -07:00
|
|
|
JS::Rooted<JSObject*> scope(cx, JS::CurrentGlobalOrNull(cx));
|
2013-05-03 16:29:09 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2013-10-11 22:02:39 -07:00
|
|
|
return XPCOMObjectToJsval(cx, scope, helper, nullptr, false, &v) ?
|
|
|
|
v.toObjectOrNull() :
|
2012-10-19 00:33:18 -07:00
|
|
|
nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
|
|
|
|
// Fallback for when our parent is not a WebIDL binding object.
|
2013-07-04 08:24:59 -07:00
|
|
|
template<typename T, bool isISupports=IsBaseOf<nsISupports, T>::value>
|
2012-10-19 00:33:18 -07:00
|
|
|
struct WrapNativeParentFallback
|
|
|
|
{
|
2014-04-08 15:27:15 -07:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, T* parent, nsWrapperCache* cache)
|
2012-10-19 00:33:18 -07:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Fallback for when our parent is not a WebIDL binding object but _is_ an
|
|
|
|
// nsISupports object.
|
2012-10-19 00:33:18 -07:00
|
|
|
template<typename T >
|
|
|
|
struct WrapNativeParentFallback<T, true >
|
|
|
|
{
|
2014-04-08 15:27:15 -07:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, T* parent, nsWrapperCache* cache)
|
2012-10-19 00:33:18 -07:00
|
|
|
{
|
2014-04-08 15:27:15 -07:00
|
|
|
return WrapNativeISupportsParent(cx, parent, cache);
|
2012-10-19 00:33:18 -07:00
|
|
|
}
|
2012-10-09 11:50:26 -07:00
|
|
|
};
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Wrapping of our native parent, for cases when it's a WebIDL object (though
|
|
|
|
// possibly preffed off).
|
2012-10-09 11:50:26 -07:00
|
|
|
template<typename T, bool hasWrapObject=HasWrapObject<T>::Value >
|
|
|
|
struct WrapNativeParentHelper
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-04-08 15:27:15 -07:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, T* parent, nsWrapperCache* cache)
|
2012-10-09 11:50:26 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(cache);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-10-09 11:50:26 -07:00
|
|
|
JSObject* obj;
|
|
|
|
if ((obj = cache->GetWrapper())) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2012-11-27 01:20:04 -08:00
|
|
|
// Inline this here while we have non-dom objects in wrapper caches.
|
|
|
|
if (!CouldBeDOMBinding(parent)) {
|
2014-04-08 15:27:15 -07:00
|
|
|
obj = WrapNativeParentFallback<T>::Wrap(cx, parent, cache);
|
2012-11-27 01:20:04 -08:00
|
|
|
} else {
|
2014-04-08 15:27:18 -07:00
|
|
|
obj = parent->WrapObject(cx);
|
2012-11-27 01:20:04 -08:00
|
|
|
}
|
|
|
|
|
2012-10-19 00:33:18 -07:00
|
|
|
return obj;
|
2012-10-09 11:50:26 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Wrapping of our native parent, for cases when it's not a WebIDL object. In
|
|
|
|
// this case it must be nsISupports.
|
2012-10-09 11:50:26 -07:00
|
|
|
template<typename T>
|
2012-10-19 00:33:18 -07:00
|
|
|
struct WrapNativeParentHelper<T, false >
|
2012-10-09 11:50:26 -07:00
|
|
|
{
|
2014-04-08 15:27:15 -07:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, T* parent, nsWrapperCache* cache)
|
2012-10-09 11:50:26 -07:00
|
|
|
{
|
|
|
|
JSObject* obj;
|
|
|
|
if (cache && (obj = cache->GetWrapper())) {
|
2012-03-30 21:42:20 -07:00
|
|
|
#ifdef DEBUG
|
2014-04-08 15:27:15 -07:00
|
|
|
NS_ASSERTION(WrapNativeISupportsParent(cx, parent, cache) == obj,
|
2012-10-09 11:50:26 -07:00
|
|
|
"Unexpected object in nsWrapperCache");
|
2012-03-30 21:42:20 -07:00
|
|
|
#endif
|
2012-10-09 11:50:26 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:27:15 -07:00
|
|
|
return WrapNativeISupportsParent(cx, parent, cache);
|
2012-10-09 11:50:26 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Wrapping of our native parent.
|
2012-10-09 11:50:26 -07:00
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
2014-04-08 15:27:15 -07:00
|
|
|
WrapNativeParent(JSContext* cx, T* p, nsWrapperCache* cache,
|
|
|
|
bool useXBLScope = false)
|
2012-10-09 11:50:26 -07:00
|
|
|
{
|
|
|
|
if (!p) {
|
2014-04-08 15:27:15 -07:00
|
|
|
return JS::CurrentGlobalOrNull(cx);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2014-04-08 15:27:15 -07:00
|
|
|
JSObject* parent = WrapNativeParentHelper<T>::Wrap(cx, p, cache);
|
2014-03-19 09:35:45 -07:00
|
|
|
if (!useXBLScope) {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If useXBLScope is true, it means that the canonical reflector for this
|
2014-06-23 16:33:37 -07:00
|
|
|
// native object should live in the content XBL scope. Note that we never put
|
|
|
|
// anonymous content inside an add-on scope.
|
2014-06-02 18:04:21 -07:00
|
|
|
if (xpc::IsInContentXBLScope(parent)) {
|
2014-03-19 09:35:45 -07:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> rootedParent(cx, parent);
|
|
|
|
JS::Rooted<JSObject*> xblScope(cx, xpc::GetXBLScope(cx, rootedParent));
|
2014-08-05 09:10:34 -07:00
|
|
|
NS_ENSURE_TRUE(xblScope, nullptr);
|
2014-03-19 09:35:45 -07:00
|
|
|
JSAutoCompartment ac(cx, xblScope);
|
|
|
|
if (NS_WARN_IF(!JS_WrapObject(cx, &rootedParent))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootedParent;
|
2012-10-09 11:50:26 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:24:32 -08:00
|
|
|
// Wrapping of our native parent, when we don't want to explicitly pass in
|
|
|
|
// things like the nsWrapperCache for it.
|
2012-10-09 11:50:26 -07:00
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
2014-04-08 15:27:15 -07:00
|
|
|
WrapNativeParent(JSContext* cx, const T& p)
|
2012-10-09 11:50:26 -07:00
|
|
|
{
|
2014-04-08 15:27:15 -07:00
|
|
|
return WrapNativeParent(cx, GetParentPointer(p), GetWrapperCache(p), GetUseXBLScope(p));
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2013-04-29 14:33:41 -07:00
|
|
|
// A way to differentiate between nodes, which use the parent object
|
|
|
|
// returned by native->GetParentObject(), and all other objects, which
|
|
|
|
// just use the parent's global.
|
|
|
|
static inline JSObject*
|
|
|
|
GetRealParentObject(void* aParent, JSObject* aParentObject)
|
|
|
|
{
|
|
|
|
return aParentObject ?
|
|
|
|
js::GetGlobalForObjectCrossCompartment(aParentObject) : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline JSObject*
|
|
|
|
GetRealParentObject(Element* aParent, JSObject* aParentObject)
|
|
|
|
{
|
|
|
|
return aParentObject;
|
|
|
|
}
|
|
|
|
|
2012-09-26 07:17:46 -07:00
|
|
|
HAS_MEMBER(GetParentObject)
|
|
|
|
|
|
|
|
template<typename T, bool WrapperCached=HasGetParentObjectMember<T>::Value>
|
|
|
|
struct GetParentObject
|
|
|
|
{
|
2013-04-25 09:29:53 -07:00
|
|
|
static JSObject* Get(JSContext* cx, JS::Handle<JSObject*> obj)
|
2012-09-26 07:17:46 -07:00
|
|
|
{
|
2014-04-08 15:27:15 -07:00
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(obj, cx));
|
2012-09-26 07:17:46 -07:00
|
|
|
T* native = UnwrapDOMObject<T>(obj);
|
2013-04-29 14:33:41 -07:00
|
|
|
return
|
|
|
|
GetRealParentObject(native,
|
2014-04-08 15:27:15 -07:00
|
|
|
WrapNativeParent(cx, native->GetParentObject()));
|
2012-09-26 07:17:46 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct GetParentObject<T, false>
|
|
|
|
{
|
2013-04-25 09:29:53 -07:00
|
|
|
static JSObject* Get(JSContext* cx, JS::Handle<JSObject*> obj)
|
2012-09-26 07:17:46 -07:00
|
|
|
{
|
|
|
|
MOZ_CRASH();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-06 01:42:17 -08:00
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
JSObject* GetJSObjectFromCallback(CallbackObject* callback)
|
|
|
|
{
|
|
|
|
return callback->Callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
JSObject* GetJSObjectFromCallback(void* noncallback)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-09 07:43:57 -08:00
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
2014-04-08 15:27:15 -07:00
|
|
|
WrapCallThisObject(JSContext* cx, const T& p)
|
2012-11-09 07:43:57 -08:00
|
|
|
{
|
2013-02-06 01:42:17 -08:00
|
|
|
// Callbacks are nsISupports, so WrapNativeParent will just happily wrap them
|
|
|
|
// up as an nsISupports XPCWrappedNative... which is not at all what we want.
|
|
|
|
// So we need to special-case them.
|
2013-05-02 02:12:45 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, GetJSObjectFromCallback(p));
|
2012-11-09 07:43:57 -08:00
|
|
|
if (!obj) {
|
2013-02-06 01:42:17 -08:00
|
|
|
// WrapNativeParent is a bit of a Swiss army knife that will
|
|
|
|
// wrap anything for us.
|
2014-04-08 15:27:15 -07:00
|
|
|
obj = WrapNativeParent(cx, p);
|
2013-02-06 01:42:17 -08:00
|
|
|
if (!obj) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-11-09 07:43:57 -08:00
|
|
|
}
|
|
|
|
|
2013-02-06 01:42:17 -08:00
|
|
|
// But all that won't necessarily put things in the compartment of cx.
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(cx, &obj)) {
|
2012-11-09 07:43:57 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2014-01-23 10:47:29 -08:00
|
|
|
/*
|
|
|
|
* This specialized function simply wraps a JS::Rooted<> since
|
|
|
|
* WrapNativeParent() is not applicable for JS objects.
|
|
|
|
*/
|
|
|
|
template<>
|
|
|
|
inline JSObject*
|
|
|
|
WrapCallThisObject<JS::Rooted<JSObject*>>(JSContext* cx,
|
|
|
|
const JS::Rooted<JSObject*>& p)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(cx, p);
|
|
|
|
|
|
|
|
if (!JS_WrapObject(cx, &obj)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2012-11-09 07:43:58 -08:00
|
|
|
// Helper for calling WrapNewBindingObject with smart pointers
|
|
|
|
// (nsAutoPtr/nsRefPtr/nsCOMPtr) or references.
|
|
|
|
template <class T, bool isSmartPtr=HasgetMember<T>::Value>
|
|
|
|
struct WrapNewBindingObjectHelper
|
|
|
|
{
|
2014-04-08 15:27:19 -07:00
|
|
|
static inline bool Wrap(JSContext* cx, const T& value,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-11-09 07:43:58 -08:00
|
|
|
{
|
2014-04-08 15:27:19 -07:00
|
|
|
return WrapNewBindingObject(cx, value.get(), rval);
|
2012-11-09 07:43:58 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct WrapNewBindingObjectHelper<T, false>
|
|
|
|
{
|
2014-04-08 15:27:19 -07:00
|
|
|
static inline bool Wrap(JSContext* cx, T& value,
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-11-09 07:43:58 -08:00
|
|
|
{
|
2014-04-08 15:27:19 -07:00
|
|
|
return WrapNewBindingObject(cx, &value, rval);
|
2012-11-09 07:43:58 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-08 15:27:19 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
|
|
|
WrapNewBindingObject(JSContext* cx, T& value, JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
return WrapNewBindingObjectHelper<T>::Wrap(cx, value, rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need this version of WrapNewBindingObject for codegen, so it'll have the
|
|
|
|
// same signature as WrapNewBindingNonWrapperCachedObject and
|
|
|
|
// WrapNewBindingNonWrapperCachedOwnedObject, which still need the scope.
|
2012-11-09 07:43:58 -08:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-25 09:29:53 -07:00
|
|
|
WrapNewBindingObject(JSContext* cx, JS::Handle<JSObject*> scope, T& value,
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-11-09 07:43:58 -08:00
|
|
|
{
|
2014-04-08 15:27:19 -07:00
|
|
|
return WrapNewBindingObject(cx, value, rval);
|
2012-11-09 07:43:58 -08:00
|
|
|
}
|
|
|
|
|
2013-01-28 05:34:31 -08:00
|
|
|
template <class T>
|
|
|
|
inline JSObject*
|
|
|
|
GetCallbackFromCallbackObject(T* aObj)
|
|
|
|
{
|
|
|
|
return aObj->Callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for getting the callback JSObject* of a smart ptr around a
|
|
|
|
// CallbackObject or a reference to a CallbackObject or something like
|
|
|
|
// that.
|
|
|
|
template <class T, bool isSmartPtr=HasgetMember<T>::Value>
|
|
|
|
struct GetCallbackFromCallbackObjectHelper
|
|
|
|
{
|
|
|
|
static inline JSObject* Get(const T& aObj)
|
|
|
|
{
|
|
|
|
return GetCallbackFromCallbackObject(aObj.get());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct GetCallbackFromCallbackObjectHelper<T, false>
|
|
|
|
{
|
|
|
|
static inline JSObject* Get(T& aObj)
|
|
|
|
{
|
|
|
|
return GetCallbackFromCallbackObject(&aObj);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline JSObject*
|
|
|
|
GetCallbackFromCallbackObject(T& aObj)
|
|
|
|
{
|
|
|
|
return GetCallbackFromCallbackObjectHelper<T>::Get(aObj);
|
|
|
|
}
|
|
|
|
|
2012-06-12 07:22:05 -07:00
|
|
|
static inline bool
|
|
|
|
InternJSString(JSContext* cx, jsid& id, const char* chars)
|
|
|
|
{
|
|
|
|
if (JSString *str = ::JS_InternString(cx, chars)) {
|
|
|
|
id = INTERNED_STRING_TO_JSID(cx, str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
// Spec needs a name property
|
|
|
|
template <typename Spec>
|
|
|
|
static bool
|
2013-04-02 17:20:38 -07:00
|
|
|
InitIds(JSContext* cx, const Prefable<Spec>* prefableSpecs, jsid* ids)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2012-05-23 09:44:48 -07:00
|
|
|
MOZ_ASSERT(prefableSpecs);
|
|
|
|
MOZ_ASSERT(prefableSpecs->specs);
|
2012-03-30 21:42:20 -07:00
|
|
|
do {
|
2012-05-23 09:44:48 -07:00
|
|
|
// We ignore whether the set of ids is enabled and just intern all the IDs,
|
|
|
|
// because this is only done once per application runtime.
|
|
|
|
Spec* spec = prefableSpecs->specs;
|
|
|
|
do {
|
2012-06-12 07:22:05 -07:00
|
|
|
if (!InternJSString(cx, *ids, spec->name)) {
|
2012-05-23 09:44:48 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (++ids, (++spec)->name);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2013-12-16 18:27:43 -08:00
|
|
|
// We ran out of ids for that pref. Put a JSID_VOID in on the id
|
2012-05-23 09:44:48 -07:00
|
|
|
// corresponding to the list terminator for the pref.
|
2013-12-16 18:27:43 -08:00
|
|
|
*ids = JSID_VOID;
|
2012-05-23 09:44:48 -07:00
|
|
|
++ids;
|
|
|
|
} while ((++prefableSpecs)->specs);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
bool
|
2012-03-30 21:42:20 -07:00
|
|
|
QueryInterface(JSContext* cx, unsigned argc, JS::Value* vp);
|
2013-07-04 08:24:59 -07:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct
|
2013-09-24 12:37:53 -07:00
|
|
|
WantsQueryInterface
|
2013-07-04 08:24:59 -07:00
|
|
|
{
|
2013-09-24 12:37:53 -07:00
|
|
|
static_assert(IsBaseOf<nsISupports, T>::value,
|
|
|
|
"QueryInterface can't work without an nsISupports.");
|
2013-07-04 08:24:59 -07:00
|
|
|
static bool Enabled(JSContext* aCx, JSObject* aGlobal)
|
|
|
|
{
|
2013-09-10 11:27:32 -07:00
|
|
|
return NS_IsMainThread() && IsChromeOrXBL(aCx, aGlobal);
|
2013-07-04 08:24:59 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
2014-02-15 13:12:33 -08:00
|
|
|
GetInterfaceImpl(JSContext* aCx, nsIInterfaceRequestor* aRequestor,
|
2014-06-11 13:26:52 -07:00
|
|
|
nsWrapperCache* aCache, nsIJSID* aIID,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval, ErrorResult& aError);
|
2014-02-15 13:12:33 -08:00
|
|
|
|
|
|
|
template<class T>
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
|
|
|
GetInterface(JSContext* aCx, T* aThis, nsIJSID* aIID,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval, ErrorResult& aError)
|
2014-02-15 13:12:33 -08:00
|
|
|
{
|
2014-06-11 13:26:52 -07:00
|
|
|
GetInterfaceImpl(aCx, aThis, aThis, aIID, aRetval, aError);
|
2014-02-15 13:12:33 -08:00
|
|
|
}
|
|
|
|
|
2014-07-11 16:30:27 -07:00
|
|
|
bool
|
|
|
|
UnforgeableValueOf(JSContext* cx, unsigned argc, JS::Value* vp);
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
bool
|
2012-05-02 21:35:38 -07:00
|
|
|
ThrowingConstructor(JSContext* cx, unsigned argc, JS::Value* vp);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2014-03-10 14:38:31 -07:00
|
|
|
bool
|
|
|
|
ThrowConstructorWithoutNew(JSContext* cx, const char* name);
|
|
|
|
|
2013-07-09 07:45:13 -07:00
|
|
|
// vp is allowed to be null; in that case no get will be attempted,
|
|
|
|
// and *found will simply indicate whether the property exists.
|
2012-05-22 06:46:20 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
GetPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id, bool* found,
|
2012-05-22 06:46:20 -07:00
|
|
|
JS::Value* vp);
|
|
|
|
|
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
HasPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id);
|
2012-05-22 06:46:20 -07:00
|
|
|
|
2013-07-09 07:45:13 -07:00
|
|
|
|
|
|
|
// Append the property names in "names" to "props". If
|
|
|
|
// shadowPrototypeProperties is false then skip properties that are also
|
|
|
|
// present on the proto chain of proxy. If shadowPrototypeProperties is true,
|
|
|
|
// then the "proxy" argument is ignored.
|
|
|
|
bool
|
|
|
|
AppendNamedPropertyIds(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
nsTArray<nsString>& names,
|
|
|
|
bool shadowPrototypeProperties, JS::AutoIdVector& props);
|
|
|
|
|
2014-01-22 11:37:10 -08:00
|
|
|
namespace binding_detail {
|
|
|
|
|
2014-07-03 01:03:56 -07:00
|
|
|
// A struct that has the same layout as an nsString but much faster
|
|
|
|
// constructor and destructor behavior. FakeString uses inline storage
|
|
|
|
// for small strings and a nsStringBuffer for longer strings.
|
|
|
|
struct FakeString {
|
|
|
|
FakeString() :
|
|
|
|
mFlags(nsString::F_TERMINATED)
|
2012-07-17 09:18:53 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:03:56 -07:00
|
|
|
~FakeString() {
|
|
|
|
if (mFlags & nsString::F_SHARED) {
|
|
|
|
nsStringBuffer::FromData(mData)->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rebind(const nsString::char_type* aData, nsString::size_type aLength) {
|
|
|
|
MOZ_ASSERT(mFlags == nsString::F_TERMINATED);
|
|
|
|
mData = const_cast<nsString::char_type*>(aData);
|
2012-07-17 09:18:53 -07:00
|
|
|
mLength = aLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Truncate() {
|
2014-07-03 01:03:56 -07:00
|
|
|
MOZ_ASSERT(mFlags == nsString::F_TERMINATED);
|
|
|
|
mData = nsString::char_traits::sEmptyBuffer;
|
2012-07-17 09:18:53 -07:00
|
|
|
mLength = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-23 01:18:35 -08:00
|
|
|
void SetIsVoid(bool aValue) {
|
|
|
|
MOZ_ASSERT(aValue,
|
2014-07-03 01:03:56 -07:00
|
|
|
"We don't support SetIsVoid(false) on FakeString!");
|
2012-07-17 09:18:53 -07:00
|
|
|
Truncate();
|
2014-07-03 01:03:56 -07:00
|
|
|
mFlags |= nsString::F_VOIDED;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsString::char_type* Data() const
|
|
|
|
{
|
|
|
|
return mData;
|
2012-07-17 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
2014-07-03 01:03:56 -07:00
|
|
|
nsString::char_type* BeginWriting()
|
2013-09-04 13:43:12 -07:00
|
|
|
{
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:03:56 -07:00
|
|
|
nsString::size_type Length() const
|
2013-09-04 13:43:12 -07:00
|
|
|
{
|
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
2014-07-19 06:23:19 -07:00
|
|
|
// Reserve space to write aLength chars, not including null-terminator.
|
|
|
|
bool SetLength(nsString::size_type aLength, mozilla::fallible_t const&) {
|
2014-07-03 01:03:56 -07:00
|
|
|
// Use mInlineStorage for small strings.
|
2014-07-19 06:23:19 -07:00
|
|
|
if (aLength < sInlineCapacity) {
|
2014-07-03 01:03:56 -07:00
|
|
|
SetData(mInlineStorage);
|
2014-07-19 06:23:19 -07:00
|
|
|
} else {
|
|
|
|
nsStringBuffer *buf = nsStringBuffer::Alloc((aLength + 1) * sizeof(nsString::char_type)).take();
|
|
|
|
if (MOZ_UNLIKELY(!buf)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-03 01:03:56 -07:00
|
|
|
|
2014-07-19 06:23:19 -07:00
|
|
|
SetData(static_cast<nsString::char_type*>(buf->Data()));
|
|
|
|
mFlags = nsString::F_SHARED | nsString::F_TERMINATED;
|
2014-07-03 01:03:56 -07:00
|
|
|
}
|
|
|
|
mLength = aLength;
|
2014-07-19 06:23:19 -07:00
|
|
|
mData[mLength] = char16_t(0);
|
|
|
|
return true;
|
2014-07-03 01:03:56 -07:00
|
|
|
}
|
|
|
|
|
2013-02-06 01:56:15 -08:00
|
|
|
// If this ever changes, change the corresponding code in the
|
|
|
|
// Optional<nsAString> specialization as well.
|
2012-07-17 09:18:53 -07:00
|
|
|
const nsAString* ToAStringPtr() const {
|
2014-07-03 01:03:56 -07:00
|
|
|
return reinterpret_cast<const nsString*>(this);
|
2012-07-17 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAString* ToAStringPtr() {
|
2014-07-03 01:03:56 -07:00
|
|
|
return reinterpret_cast<nsString*>(this);
|
2012-07-17 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
operator const nsAString& () const {
|
2014-07-03 01:03:56 -07:00
|
|
|
return *reinterpret_cast<const nsString*>(this);
|
2012-07-17 09:18:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-07-03 01:03:56 -07:00
|
|
|
nsString::char_type* mData;
|
|
|
|
nsString::size_type mLength;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mFlags;
|
2012-07-17 09:18:53 -07:00
|
|
|
|
2014-07-03 01:03:56 -07:00
|
|
|
static const size_t sInlineCapacity = 64;
|
|
|
|
nsString::char_type mInlineStorage[sInlineCapacity];
|
|
|
|
|
|
|
|
FakeString(const FakeString& other) MOZ_DELETE;
|
|
|
|
void operator=(const FakeString& other) MOZ_DELETE;
|
|
|
|
|
|
|
|
void SetData(nsString::char_type* aData) {
|
|
|
|
MOZ_ASSERT(mFlags == nsString::F_TERMINATED);
|
|
|
|
mData = const_cast<nsString::char_type*>(aData);
|
|
|
|
}
|
|
|
|
|
2012-07-17 09:18:53 -07:00
|
|
|
// A class to use for our static asserts to ensure our object layout
|
2014-07-03 01:03:56 -07:00
|
|
|
// matches that of nsString.
|
|
|
|
class StringAsserter;
|
|
|
|
friend class StringAsserter;
|
2012-07-17 09:18:53 -07:00
|
|
|
|
2014-07-03 01:03:56 -07:00
|
|
|
class StringAsserter : public nsString {
|
2012-07-17 09:18:53 -07:00
|
|
|
public:
|
|
|
|
static void StaticAsserts() {
|
2014-07-03 01:03:56 -07:00
|
|
|
static_assert(offsetof(FakeString, mInlineStorage) ==
|
|
|
|
sizeof(nsString),
|
|
|
|
"FakeString should include all nsString members");
|
|
|
|
static_assert(offsetof(FakeString, mData) ==
|
|
|
|
offsetof(StringAsserter, mData),
|
2013-07-18 10:59:53 -07:00
|
|
|
"Offset of mData should match");
|
2014-07-03 01:03:56 -07:00
|
|
|
static_assert(offsetof(FakeString, mLength) ==
|
|
|
|
offsetof(StringAsserter, mLength),
|
2013-07-18 10:59:53 -07:00
|
|
|
"Offset of mLength should match");
|
2014-07-03 01:03:56 -07:00
|
|
|
static_assert(offsetof(FakeString, mFlags) ==
|
|
|
|
offsetof(StringAsserter, mFlags),
|
2013-07-18 10:59:53 -07:00
|
|
|
"Offset of mFlags should match");
|
2012-07-17 09:18:53 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-01-22 11:37:10 -08:00
|
|
|
} // namespace binding_detail
|
|
|
|
|
2012-05-18 14:25:46 -07:00
|
|
|
enum StringificationBehavior {
|
|
|
|
eStringify,
|
|
|
|
eEmpty,
|
|
|
|
eNull
|
|
|
|
};
|
|
|
|
|
2014-01-23 01:18:35 -08:00
|
|
|
template<typename T>
|
2012-05-18 14:25:46 -07:00
|
|
|
static inline bool
|
2013-05-24 06:16:00 -07:00
|
|
|
ConvertJSValueToString(JSContext* cx, JS::Handle<JS::Value> v,
|
2012-05-18 14:25:46 -07:00
|
|
|
StringificationBehavior nullBehavior,
|
|
|
|
StringificationBehavior undefinedBehavior,
|
2014-01-23 01:18:35 -08:00
|
|
|
T& result)
|
2012-05-18 14:25:46 -07:00
|
|
|
{
|
|
|
|
JSString *s;
|
|
|
|
if (v.isString()) {
|
|
|
|
s = v.toString();
|
|
|
|
} else {
|
|
|
|
StringificationBehavior behavior;
|
|
|
|
if (v.isNull()) {
|
|
|
|
behavior = nullBehavior;
|
|
|
|
} else if (v.isUndefined()) {
|
|
|
|
behavior = undefinedBehavior;
|
|
|
|
} else {
|
|
|
|
behavior = eStringify;
|
|
|
|
}
|
|
|
|
|
2012-07-30 21:22:23 -07:00
|
|
|
if (behavior != eStringify) {
|
2012-07-17 09:18:53 -07:00
|
|
|
if (behavior == eEmpty) {
|
|
|
|
result.Truncate();
|
|
|
|
} else {
|
2014-01-23 01:18:35 -08:00
|
|
|
result.SetIsVoid(true);
|
2012-07-17 09:18:53 -07:00
|
|
|
}
|
2012-05-18 14:25:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-16 04:31:36 -08:00
|
|
|
s = JS::ToString(cx, v);
|
2012-05-18 14:25:46 -07:00
|
|
|
if (!s) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 08:30:54 -07:00
|
|
|
return AssignJSString(cx, result, s);
|
2012-05-18 14:25:46 -07:00
|
|
|
}
|
|
|
|
|
2014-08-01 13:23:48 -07:00
|
|
|
void
|
|
|
|
NormalizeScalarValueString(JSContext* aCx, nsAString& aString);
|
|
|
|
|
|
|
|
void
|
|
|
|
NormalizeScalarValueString(JSContext* aCx, binding_detail::FakeString& aString);
|
|
|
|
|
2014-08-04 12:20:57 -07:00
|
|
|
template<typename T>
|
|
|
|
inline bool
|
|
|
|
ConvertIdToString(JSContext* cx, JS::HandleId id, T& result, bool& isSymbol)
|
|
|
|
{
|
|
|
|
if (MOZ_LIKELY(JSID_IS_STRING(id))) {
|
|
|
|
if (!AssignJSString(cx, result, JSID_TO_STRING(id))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (JSID_IS_SYMBOL(id)) {
|
|
|
|
isSymbol = true;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
JS::RootedValue nameVal(cx, js::IdToValue(id));
|
|
|
|
if (!ConvertJSValueToString(cx, nameVal, eStringify, eStringify, result)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isSymbol = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-12 22:18:35 -07:00
|
|
|
bool
|
|
|
|
ConvertJSValueToByteString(JSContext* cx, JS::Handle<JS::Value> v,
|
2014-07-03 04:05:03 -07:00
|
|
|
bool nullable, nsACString& result);
|
2013-06-12 22:18:35 -07:00
|
|
|
|
2013-05-16 09:36:55 -07:00
|
|
|
template<typename T>
|
|
|
|
void DoTraceSequence(JSTracer* trc, FallibleTArray<T>& seq);
|
2013-05-16 09:36:56 -07:00
|
|
|
template<typename T>
|
|
|
|
void DoTraceSequence(JSTracer* trc, InfallibleTArray<T>& seq);
|
2013-05-16 09:36:55 -07:00
|
|
|
|
2013-03-01 22:07:43 -08:00
|
|
|
// Class for simple sequence arguments, only used internally by codegen.
|
2014-01-22 11:37:10 -08:00
|
|
|
namespace binding_detail {
|
|
|
|
|
2013-03-01 22:07:43 -08:00
|
|
|
template<typename T>
|
|
|
|
class AutoSequence : public AutoFallibleTArray<T, 16>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoSequence() : AutoFallibleTArray<T, 16>()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Allow converting to const sequences as needed
|
|
|
|
operator const Sequence<T>&() const {
|
|
|
|
return *reinterpret_cast<const Sequence<T>*>(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-22 11:37:10 -08:00
|
|
|
} // namespace binding_detail
|
|
|
|
|
2013-05-16 09:36:55 -07:00
|
|
|
// Class used to trace sequences, with specializations for various
|
|
|
|
// sequence types.
|
2013-08-05 10:40:02 -07:00
|
|
|
template<typename T,
|
|
|
|
bool isDictionary=IsBaseOf<DictionaryBase, T>::value,
|
2014-03-14 11:51:22 -07:00
|
|
|
bool isTypedArray=IsBaseOf<AllTypedArraysBase, T>::value,
|
|
|
|
bool isOwningUnion=IsBaseOf<AllOwningUnionBase, T>::value>
|
2013-05-16 09:36:55 -07:00
|
|
|
class SequenceTracer
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
};
|
|
|
|
|
|
|
|
// sequence<object> or sequence<object?>
|
|
|
|
template<>
|
2014-03-14 11:51:22 -07:00
|
|
|
class SequenceTracer<JSObject*, false, false, false>
|
2013-05-16 09:36:55 -07:00
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, JSObject** objp, JSObject** end) {
|
2013-08-05 10:40:02 -07:00
|
|
|
for (; objp != end; ++objp) {
|
2014-08-13 02:05:22 -07:00
|
|
|
JS_CallUnbarrieredObjectTracer(trc, objp, "sequence<object>");
|
2013-05-16 09:36:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// sequence<any>
|
|
|
|
template<>
|
2014-03-14 11:51:22 -07:00
|
|
|
class SequenceTracer<JS::Value, false, false, false>
|
2013-05-16 09:36:55 -07:00
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, JS::Value* valp, JS::Value* end) {
|
2013-08-05 10:40:02 -07:00
|
|
|
for (; valp != end; ++valp) {
|
2014-08-13 02:05:22 -07:00
|
|
|
JS_CallUnbarrieredValueTracer(trc, valp, "sequence<any>");
|
2013-05-16 09:36:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// sequence<sequence<T>>
|
|
|
|
template<typename T>
|
2014-03-14 11:51:22 -07:00
|
|
|
class SequenceTracer<Sequence<T>, false, false, false>
|
2013-05-16 09:36:55 -07:00
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, Sequence<T>* seqp, Sequence<T>* end) {
|
2013-08-05 10:40:02 -07:00
|
|
|
for (; seqp != end; ++seqp) {
|
2013-05-16 09:36:55 -07:00
|
|
|
DoTraceSequence(trc, *seqp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 09:36:56 -07:00
|
|
|
// sequence<sequence<T>> as return value
|
|
|
|
template<typename T>
|
2014-03-14 11:51:22 -07:00
|
|
|
class SequenceTracer<nsTArray<T>, false, false, false>
|
2013-05-16 09:36:56 -07:00
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, nsTArray<T>* seqp, nsTArray<T>* end) {
|
2013-08-05 10:40:02 -07:00
|
|
|
for (; seqp != end; ++seqp) {
|
2013-05-16 09:36:56 -07:00
|
|
|
DoTraceSequence(trc, *seqp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 09:36:55 -07:00
|
|
|
// sequence<someDictionary>
|
|
|
|
template<typename T>
|
2014-03-14 11:51:22 -07:00
|
|
|
class SequenceTracer<T, true, false, false>
|
2013-05-16 09:36:55 -07:00
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, T* dictp, T* end) {
|
2013-08-05 10:40:02 -07:00
|
|
|
for (; dictp != end; ++dictp) {
|
2013-05-16 09:36:55 -07:00
|
|
|
dictp->TraceDictionary(trc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-05 10:40:02 -07:00
|
|
|
// sequence<SomeTypedArray>
|
2013-05-16 09:36:55 -07:00
|
|
|
template<typename T>
|
2014-03-14 11:51:22 -07:00
|
|
|
class SequenceTracer<T, false, true, false>
|
2013-05-16 09:36:55 -07:00
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
2013-08-05 10:40:02 -07:00
|
|
|
static void TraceSequence(JSTracer* trc, T* arrayp, T* end) {
|
|
|
|
for (; arrayp != end; ++arrayp) {
|
|
|
|
arrayp->TraceSelf(trc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-14 11:51:22 -07:00
|
|
|
// sequence<SomeOwningUnion>
|
|
|
|
template<typename T>
|
|
|
|
class SequenceTracer<T, false, false, true>
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, T* arrayp, T* end) {
|
|
|
|
for (; arrayp != end; ++arrayp) {
|
|
|
|
arrayp->TraceUnion(trc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-05 10:40:02 -07:00
|
|
|
// sequence<T?> with T? being a Nullable<T>
|
|
|
|
template<typename T>
|
2014-03-14 11:51:22 -07:00
|
|
|
class SequenceTracer<Nullable<T>, false, false, false>
|
2013-08-05 10:40:02 -07:00
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, Nullable<T>* seqp,
|
|
|
|
Nullable<T>* end) {
|
|
|
|
for (; seqp != end; ++seqp) {
|
2013-05-16 09:36:55 -07:00
|
|
|
if (!seqp->IsNull()) {
|
2013-08-05 10:40:02 -07:00
|
|
|
// Pretend like we actually have a length-one sequence here so
|
|
|
|
// we can do template instantiation correctly for T.
|
|
|
|
T& val = seqp->Value();
|
|
|
|
T* ptr = &val;
|
|
|
|
SequenceTracer<T>::TraceSequence(trc, ptr, ptr+1);
|
2013-05-16 09:36:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-23 14:32:38 -07:00
|
|
|
// XXXbz It's not clear whether it's better to add a pldhash dependency here
|
|
|
|
// (for PLDHashOperator) or add a BindingUtils.h dependency (for
|
|
|
|
// SequenceTracer) to MozMap.h...
|
|
|
|
template<typename T>
|
|
|
|
static PLDHashOperator
|
|
|
|
TraceMozMapValue(T* aValue, void* aClosure)
|
|
|
|
{
|
|
|
|
JSTracer* trc = static_cast<JSTracer*>(aClosure);
|
|
|
|
// Act like it's a one-element sequence to leverage all that infrastructure.
|
|
|
|
SequenceTracer<T>::TraceSequence(trc, aValue, aValue + 1);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2014-06-23 13:03:58 -07:00
|
|
|
template<typename T>
|
|
|
|
void TraceMozMap(JSTracer* trc, MozMap<T>& map)
|
|
|
|
{
|
|
|
|
map.EnumerateValues(TraceMozMapValue<T>, trc);
|
|
|
|
}
|
|
|
|
|
2014-05-23 14:32:38 -07:00
|
|
|
// sequence<MozMap>
|
|
|
|
template<typename T>
|
|
|
|
class SequenceTracer<MozMap<T>, false, false, false>
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, MozMap<T>* seqp, MozMap<T>* end) {
|
|
|
|
for (; seqp != end; ++seqp) {
|
|
|
|
seqp->EnumerateValues(TraceMozMapValue<T>, trc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 09:36:55 -07:00
|
|
|
template<typename T>
|
|
|
|
void DoTraceSequence(JSTracer* trc, FallibleTArray<T>& seq)
|
|
|
|
{
|
|
|
|
SequenceTracer<T>::TraceSequence(trc, seq.Elements(),
|
|
|
|
seq.Elements() + seq.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void DoTraceSequence(JSTracer* trc, InfallibleTArray<T>& seq)
|
|
|
|
{
|
|
|
|
SequenceTracer<T>::TraceSequence(trc, seq.Elements(),
|
|
|
|
seq.Elements() + seq.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rooter class for sequences; this is what we mostly use in the codegen
|
|
|
|
template<typename T>
|
|
|
|
class MOZ_STACK_CLASS SequenceRooter : private JS::CustomAutoRooter
|
|
|
|
{
|
|
|
|
public:
|
2013-05-17 18:48:25 -07:00
|
|
|
SequenceRooter(JSContext *aCx, FallibleTArray<T>* aSequence
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: JS::CustomAutoRooter(aCx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT),
|
|
|
|
mFallibleArray(aSequence),
|
|
|
|
mSequenceType(eFallibleArray)
|
2013-05-16 09:36:55 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-17 18:48:25 -07:00
|
|
|
SequenceRooter(JSContext *aCx, InfallibleTArray<T>* aSequence
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: JS::CustomAutoRooter(aCx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT),
|
|
|
|
mInfallibleArray(aSequence),
|
|
|
|
mSequenceType(eInfallibleArray)
|
2013-05-16 09:36:55 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-17 18:48:25 -07:00
|
|
|
SequenceRooter(JSContext *aCx, Nullable<nsTArray<T> >* aSequence
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: JS::CustomAutoRooter(aCx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT),
|
|
|
|
mNullableArray(aSequence),
|
|
|
|
mSequenceType(eNullableArray)
|
2013-05-16 09:36:55 -07:00
|
|
|
{
|
2013-05-16 09:36:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-16 09:36:55 -07:00
|
|
|
enum SequenceType {
|
|
|
|
eInfallibleArray,
|
2013-05-16 09:36:56 -07:00
|
|
|
eFallibleArray,
|
|
|
|
eNullableArray
|
2013-05-16 09:36:55 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual void trace(JSTracer *trc) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (mSequenceType == eFallibleArray) {
|
|
|
|
DoTraceSequence(trc, *mFallibleArray);
|
|
|
|
} else if (mSequenceType == eInfallibleArray) {
|
|
|
|
DoTraceSequence(trc, *mInfallibleArray);
|
2013-05-17 18:48:25 -07:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mSequenceType == eNullableArray);
|
2013-05-16 09:36:56 -07:00
|
|
|
if (!mNullableArray->IsNull()) {
|
|
|
|
DoTraceSequence(trc, mNullableArray->Value());
|
|
|
|
}
|
2013-05-16 09:36:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
|
|
|
InfallibleTArray<T>* mInfallibleArray;
|
|
|
|
FallibleTArray<T>* mFallibleArray;
|
2013-05-16 09:36:56 -07:00
|
|
|
Nullable<nsTArray<T> >* mNullableArray;
|
2013-05-16 09:36:55 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
SequenceType mSequenceType;
|
|
|
|
};
|
|
|
|
|
2014-05-23 14:32:38 -07:00
|
|
|
// Rooter class for MozMap; this is what we mostly use in the codegen.
|
|
|
|
template<typename T>
|
|
|
|
class MOZ_STACK_CLASS MozMapRooter : private JS::CustomAutoRooter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MozMapRooter(JSContext *aCx, MozMap<T>* aMozMap
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: JS::CustomAutoRooter(aCx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT),
|
|
|
|
mMozMap(aMozMap),
|
|
|
|
mMozMapType(eMozMap)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MozMapRooter(JSContext *aCx, Nullable<MozMap<T>>* aMozMap
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: JS::CustomAutoRooter(aCx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT),
|
|
|
|
mNullableMozMap(aMozMap),
|
|
|
|
mMozMapType(eNullableMozMap)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum MozMapType {
|
|
|
|
eMozMap,
|
|
|
|
eNullableMozMap
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void trace(JSTracer *trc) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (mMozMapType == eMozMap) {
|
2014-06-23 13:03:58 -07:00
|
|
|
TraceMozMap(trc, *mMozMap);
|
2014-05-23 14:32:38 -07:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mMozMapType == eNullableMozMap);
|
2014-06-23 13:03:58 -07:00
|
|
|
if (!mNullableMozMap->IsNull()) {
|
|
|
|
TraceMozMap(trc, mNullableMozMap->Value());
|
2014-05-23 14:32:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
|
|
|
MozMap<T>* mMozMap;
|
|
|
|
Nullable<MozMap<T>>* mNullableMozMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
MozMapType mMozMapType;
|
|
|
|
};
|
|
|
|
|
2013-09-12 19:34:16 -07:00
|
|
|
template<typename T>
|
|
|
|
class MOZ_STACK_CLASS RootedUnion : public T,
|
|
|
|
private JS::CustomAutoRooter
|
|
|
|
{
|
|
|
|
public:
|
2014-07-28 10:17:51 -07:00
|
|
|
explicit RootedUnion(JSContext* cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
|
2013-09-12 19:34:16 -07:00
|
|
|
T(),
|
|
|
|
JS::CustomAutoRooter(cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void trace(JSTracer *trc) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
this->TraceUnion(trc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class MOZ_STACK_CLASS NullableRootedUnion : public Nullable<T>,
|
|
|
|
private JS::CustomAutoRooter
|
|
|
|
{
|
|
|
|
public:
|
2014-07-28 10:17:51 -07:00
|
|
|
explicit NullableRootedUnion(JSContext* cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
|
2013-09-12 19:34:16 -07:00
|
|
|
Nullable<T>(),
|
|
|
|
JS::CustomAutoRooter(cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void trace(JSTracer *trc) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (!this->IsNull()) {
|
|
|
|
this->Value().TraceUnion(trc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
inline bool
|
|
|
|
IdEquals(jsid id, const char* string)
|
|
|
|
{
|
|
|
|
return JSID_IS_STRING(id) &&
|
|
|
|
JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), string);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
AddStringToIDVector(JSContext* cx, JS::AutoIdVector& vector, const char* name)
|
|
|
|
{
|
|
|
|
return vector.growBy(1) &&
|
2014-04-28 07:53:00 -07:00
|
|
|
InternJSString(cx, *(vector[vector.length() - 1]).address(), name);
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
|
|
|
|
2012-07-18 04:52:07 -07:00
|
|
|
// Implementation of the bits that XrayWrapper needs
|
2012-10-09 11:50:27 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This resolves indexed or named properties of obj.
|
|
|
|
*
|
|
|
|
* wrapper is the Xray JS object.
|
|
|
|
* obj is the target object of the Xray, a binding's instance object or a
|
|
|
|
* interface or interface prototype object.
|
|
|
|
*/
|
2012-07-18 04:52:07 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayResolveOwnProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id,
|
2014-08-22 02:25:07 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
bool& cacheOnHolder);
|
2012-07-18 04:52:07 -07:00
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
/**
|
|
|
|
* This resolves operations, attributes and constants of the interfaces for obj.
|
|
|
|
*
|
|
|
|
* wrapper is the Xray JS object.
|
|
|
|
* obj is the target object of the Xray, a binding's instance object or a
|
|
|
|
* interface or interface prototype object.
|
|
|
|
*/
|
2012-07-18 04:52:07 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2014-08-22 02:25:07 -07:00
|
|
|
JS::Handle<jsid> id, JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
bool& cacheOnHolder);
|
2012-07-18 04:52:07 -07:00
|
|
|
|
2013-05-29 02:38:57 -07:00
|
|
|
/**
|
|
|
|
* Define a property on obj through an Xray wrapper.
|
|
|
|
*
|
|
|
|
* wrapper is the Xray JS object.
|
|
|
|
* obj is the target object of the Xray, a binding's instance object or a
|
|
|
|
* interface or interface prototype object.
|
|
|
|
* defined will be set to true if a property was set as a result of this call.
|
|
|
|
*/
|
|
|
|
bool
|
2013-04-30 10:29:40 -07:00
|
|
|
XrayDefineProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2013-05-29 02:38:57 -07:00
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-04-30 10:29:40 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc, bool* defined);
|
2013-05-29 02:38:57 -07:00
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
/**
|
|
|
|
* This enumerates indexed or named properties of obj and operations, attributes
|
|
|
|
* and constants of the interfaces for obj.
|
|
|
|
*
|
|
|
|
* wrapper is the Xray JS object.
|
|
|
|
* obj is the target object of the Xray, a binding's instance object or a
|
|
|
|
* interface or interface prototype object.
|
2012-11-05 08:58:03 -08:00
|
|
|
* flags are JSITER_* flags.
|
2012-10-09 11:50:27 -07:00
|
|
|
*/
|
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayEnumerateProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2012-11-05 08:58:03 -08:00
|
|
|
unsigned flags, JS::AutoIdVector& props);
|
2012-10-09 11:50:27 -07:00
|
|
|
|
|
|
|
extern NativePropertyHooks sWorkerNativePropertyHooks;
|
|
|
|
|
|
|
|
// We use one constructor JSNative to represent all DOM interface objects (so
|
|
|
|
// we can easily detect when we need to wrap them in an Xray wrapper). We store
|
|
|
|
// the real JSNative in the mNative member of a JSNativeHolder in the
|
|
|
|
// CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT slot of the JSFunction object for a
|
|
|
|
// specific interface object. We also store the NativeProperties in the
|
2014-02-21 11:35:49 -08:00
|
|
|
// JSNativeHolder.
|
2012-10-09 11:50:27 -07:00
|
|
|
// Note that some interface objects are not yet a JSFunction but a normal
|
|
|
|
// JSObject with a DOMJSClass, those do not use these slots.
|
|
|
|
|
|
|
|
enum {
|
2014-02-21 11:35:49 -08:00
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT = 0
|
2012-10-09 11:50:27 -07:00
|
|
|
};
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
bool
|
2012-10-09 11:50:27 -07:00
|
|
|
Constructor(JSContext* cx, unsigned argc, JS::Value* vp);
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
UseDOMXray(JSObject* obj)
|
|
|
|
{
|
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
return IsDOMClass(clasp) ||
|
|
|
|
JS_IsNativeFunction(obj, Constructor) ||
|
|
|
|
IsDOMIfaceAndProtoClass(clasp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
inline bool
|
|
|
|
HasConstructor(JSObject* obj)
|
|
|
|
{
|
|
|
|
return JS_IsNativeFunction(obj, Constructor) ||
|
|
|
|
js::GetObjectClass(obj)->construct;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-19 06:02:37 -07:00
|
|
|
// Transfer reference in ptr to smartPtr.
|
|
|
|
template<class T>
|
|
|
|
inline void
|
|
|
|
Take(nsRefPtr<T>& smartPtr, T* ptr)
|
|
|
|
{
|
|
|
|
smartPtr = dont_AddRef(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer ownership of ptr to smartPtr.
|
|
|
|
template<class T>
|
|
|
|
inline void
|
|
|
|
Take(nsAutoPtr<T>& smartPtr, T* ptr)
|
|
|
|
{
|
|
|
|
smartPtr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
MustInheritFromNonRefcountedDOMObject(NonRefcountedDOMObject*)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
/**
|
|
|
|
* This creates a JSString containing the value that the toString function for
|
|
|
|
* obj should create according to the WebIDL specification, ignoring any
|
|
|
|
* modifications by script. The value is prefixed with pre and postfixed with
|
|
|
|
* post, unless this is called for an object that has a stringifier. It is
|
|
|
|
* specifically for use by Xray code.
|
|
|
|
*
|
|
|
|
* wrapper is the Xray JS object.
|
|
|
|
* obj is the target object of the Xray, a binding's instance object or a
|
|
|
|
* interface or interface prototype object.
|
|
|
|
* v contains the JSString for the value if the function returns true.
|
|
|
|
*/
|
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
NativeToString(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2014-07-07 13:11:24 -07:00
|
|
|
JS::Handle<JSObject*> obj,
|
2013-10-26 09:19:05 -07:00
|
|
|
JS::MutableHandle<JS::Value> v);
|
2012-10-09 11:50:27 -07:00
|
|
|
|
2013-03-17 18:47:12 -07:00
|
|
|
HAS_MEMBER(JSBindingFinalized)
|
|
|
|
|
|
|
|
template<class T, bool hasCallback=HasJSBindingFinalizedMember<T>::Value>
|
|
|
|
struct JSBindingFinalized
|
|
|
|
{
|
|
|
|
static void Finalized(T* self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct JSBindingFinalized<T, true>
|
|
|
|
{
|
|
|
|
static void Finalized(T* self)
|
|
|
|
{
|
|
|
|
self->JSBindingFinalized();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-25 16:03:06 -07:00
|
|
|
// Helpers for creating a const version of a type.
|
|
|
|
template<typename T>
|
|
|
|
const T& Constify(T& arg)
|
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2013-07-22 05:15:43 -07:00
|
|
|
// Helper for turning (Owning)NonNull<T> into T&
|
|
|
|
template<typename T>
|
|
|
|
T& NonNullHelper(T& aArg)
|
|
|
|
{
|
|
|
|
return aArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T& NonNullHelper(NonNull<T>& aArg)
|
|
|
|
{
|
|
|
|
return aArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T& NonNullHelper(const NonNull<T>& aArg)
|
|
|
|
{
|
|
|
|
return aArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T& NonNullHelper(OwningNonNull<T>& aArg)
|
|
|
|
{
|
|
|
|
return aArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T& NonNullHelper(const OwningNonNull<T>& aArg)
|
|
|
|
{
|
|
|
|
return aArg;
|
|
|
|
}
|
|
|
|
|
2014-01-22 11:37:10 -08:00
|
|
|
inline
|
2014-07-03 01:03:56 -07:00
|
|
|
void NonNullHelper(NonNull<binding_detail::FakeString>& aArg)
|
2014-01-22 11:37:10 -08:00
|
|
|
{
|
|
|
|
// This overload is here to make sure that we never end up applying
|
2014-07-03 01:03:56 -07:00
|
|
|
// NonNullHelper to a NonNull<binding_detail::FakeString>. If we
|
2014-01-22 11:37:10 -08:00
|
|
|
// try to, it should fail to compile, since presumably the caller will try to
|
|
|
|
// use our nonexistent return value.
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2014-07-03 01:03:56 -07:00
|
|
|
void NonNullHelper(const NonNull<binding_detail::FakeString>& aArg)
|
2014-01-22 11:37:10 -08:00
|
|
|
{
|
|
|
|
// This overload is here to make sure that we never end up applying
|
2014-07-03 01:03:56 -07:00
|
|
|
// NonNullHelper to a NonNull<binding_detail::FakeString>. If we
|
2014-01-22 11:37:10 -08:00
|
|
|
// try to, it should fail to compile, since presumably the caller will try to
|
|
|
|
// use our nonexistent return value.
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2014-07-03 01:03:56 -07:00
|
|
|
void NonNullHelper(binding_detail::FakeString& aArg)
|
2014-01-22 11:37:10 -08:00
|
|
|
{
|
|
|
|
// This overload is here to make sure that we never end up applying
|
2014-07-03 01:03:56 -07:00
|
|
|
// NonNullHelper to a FakeString before we've constified it. If we
|
2014-01-22 11:37:10 -08:00
|
|
|
// try to, it should fail to compile, since presumably the caller will try to
|
|
|
|
// use our nonexistent return value.
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE
|
2014-07-03 01:03:56 -07:00
|
|
|
const nsAString& NonNullHelper(const binding_detail::FakeString& aArg)
|
2014-01-22 11:37:10 -08:00
|
|
|
{
|
|
|
|
return aArg;
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:03:06 -07:00
|
|
|
// Reparent the wrapper of aObj to whatever its native now thinks its
|
|
|
|
// parent should be.
|
2012-09-26 07:17:46 -07:00
|
|
|
nsresult
|
2013-10-15 15:34:13 -07:00
|
|
|
ReparentWrapper(JSContext* aCx, JS::Handle<JSObject*> aObj);
|
2012-09-26 07:17:46 -07:00
|
|
|
|
2013-01-08 10:05:36 -08:00
|
|
|
/**
|
|
|
|
* Used to implement the hasInstance hook of an interface object.
|
|
|
|
*
|
|
|
|
* instance should not be a security wrapper.
|
|
|
|
*/
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool* bp);
|
|
|
|
bool
|
2013-06-21 06:12:46 -07:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj, JS::MutableHandle<JS::Value> vp,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool* bp);
|
|
|
|
bool
|
2013-08-01 16:45:17 -07:00
|
|
|
InterfaceHasInstance(JSContext* cx, int prototypeID, int depth,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool* bp);
|
2013-01-08 10:05:36 -08:00
|
|
|
|
2013-05-08 12:50:58 -07:00
|
|
|
// Helper for lenient getters/setters to report to console. If this
|
|
|
|
// returns false, we couldn't even get a global.
|
|
|
|
bool
|
2013-09-25 11:38:30 -07:00
|
|
|
ReportLenientThisUnwrappingFailure(JSContext* cx, JSObject* obj);
|
2013-04-08 14:04:21 -07:00
|
|
|
|
2012-12-20 01:56:11 -08:00
|
|
|
inline JSObject*
|
|
|
|
GetUnforgeableHolder(JSObject* aGlobal, prototypes::ID aId)
|
|
|
|
{
|
2014-03-22 10:04:09 -07:00
|
|
|
ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(aGlobal);
|
|
|
|
JSObject* interfaceProto = protoAndIfaceCache.EntrySlotMustExist(aId);
|
2012-12-20 01:56:11 -08:00
|
|
|
return &js::GetReservedSlot(interfaceProto,
|
|
|
|
DOM_INTERFACE_PROTO_SLOTS_BASE).toObject();
|
|
|
|
}
|
|
|
|
|
2013-05-20 10:47:08 -07:00
|
|
|
// Given a JSObject* that represents the chrome side of a JS-implemented WebIDL
|
|
|
|
// interface, get the nsPIDOMWindow corresponding to the content side, if any.
|
|
|
|
// A false return means an exception was thrown.
|
|
|
|
bool
|
|
|
|
GetWindowForJSImplementedObject(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
nsPIDOMWindow** window);
|
|
|
|
|
2014-03-27 21:03:03 -07:00
|
|
|
void
|
|
|
|
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
|
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-05-31 23:56:23 -07:00
|
|
|
already_AddRefed<nsPIDOMWindow>
|
|
|
|
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
|
|
|
|
const GlobalObject& aGlobal,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-06-12 22:18:35 -07:00
|
|
|
/**
|
|
|
|
* Convert an nsCString to jsval, returning true on success.
|
|
|
|
* These functions are intended for ByteString implementations.
|
|
|
|
* As such, the string is not UTF-8 encoded. Any UTF8 strings passed to these
|
|
|
|
* methods will be mangled.
|
|
|
|
*/
|
|
|
|
bool NonVoidByteStringToJsval(JSContext *cx, const nsACString &str,
|
|
|
|
JS::MutableHandle<JS::Value> rval);
|
|
|
|
inline bool ByteStringToJsval(JSContext *cx, const nsACString &str,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
if (str.IsVoid()) {
|
|
|
|
rval.setNull();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return NonVoidByteStringToJsval(cx, str, rval);
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:24:59 -07:00
|
|
|
template<class T, bool isISupports=IsBaseOf<nsISupports, T>::value>
|
|
|
|
struct PreserveWrapperHelper
|
|
|
|
{
|
|
|
|
static void PreserveWrapper(T* aObject)
|
|
|
|
{
|
|
|
|
aObject->PreserveWrapper(aObject, NS_CYCLE_COLLECTION_PARTICIPANT(T));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct PreserveWrapperHelper<T, true>
|
|
|
|
{
|
|
|
|
static void PreserveWrapper(T* aObject)
|
|
|
|
{
|
|
|
|
aObject->PreserveWrapper(reinterpret_cast<nsISupports*>(aObject));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void PreserveWrapper(T* aObject)
|
|
|
|
{
|
|
|
|
PreserveWrapperHelper<T>::PreserveWrapper(aObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, bool isISupports=IsBaseOf<nsISupports, T>::value>
|
|
|
|
struct CastingAssertions
|
|
|
|
{
|
|
|
|
static bool ToSupportsIsCorrect(T*)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static bool ToSupportsIsOnPrimaryInheritanceChain(T*, nsWrapperCache*)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct CastingAssertions<T, true>
|
|
|
|
{
|
|
|
|
static bool ToSupportsIsCorrect(T* aObject)
|
|
|
|
{
|
|
|
|
return ToSupports(aObject) == reinterpret_cast<nsISupports*>(aObject);
|
|
|
|
}
|
|
|
|
static bool ToSupportsIsOnPrimaryInheritanceChain(T* aObject,
|
|
|
|
nsWrapperCache* aCache)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<void*>(aObject) != aCache;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
bool
|
|
|
|
ToSupportsIsCorrect(T* aObject)
|
|
|
|
{
|
|
|
|
return CastingAssertions<T>::ToSupportsIsCorrect(aObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
bool
|
|
|
|
ToSupportsIsOnPrimaryInheritanceChain(T* aObject, nsWrapperCache* aCache)
|
|
|
|
{
|
|
|
|
return CastingAssertions<T>::ToSupportsIsOnPrimaryInheritanceChain(aObject,
|
|
|
|
aCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, template <typename> class SmartPtr,
|
|
|
|
bool isISupports=IsBaseOf<nsISupports, T>::value>
|
|
|
|
class DeferredFinalizer
|
|
|
|
{
|
|
|
|
typedef nsTArray<SmartPtr<T> > SmartPtrArray;
|
|
|
|
|
|
|
|
static void*
|
|
|
|
AppendDeferredFinalizePointer(void* aData, void* aObject)
|
|
|
|
{
|
|
|
|
SmartPtrArray* pointers = static_cast<SmartPtrArray*>(aData);
|
|
|
|
if (!pointers) {
|
|
|
|
pointers = new SmartPtrArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
T* self = static_cast<T*>(aObject);
|
|
|
|
|
|
|
|
SmartPtr<T>* defer = pointers->AppendElement();
|
|
|
|
Take(*defer, self);
|
|
|
|
return pointers;
|
|
|
|
}
|
|
|
|
static bool
|
|
|
|
DeferredFinalize(uint32_t aSlice, void* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aSlice > 0, "nonsensical/useless call with aSlice == 0");
|
|
|
|
SmartPtrArray* pointers = static_cast<SmartPtrArray*>(aData);
|
|
|
|
uint32_t oldLen = pointers->Length();
|
2013-09-18 15:50:32 -07:00
|
|
|
if (oldLen < aSlice) {
|
|
|
|
aSlice = oldLen;
|
|
|
|
}
|
2013-07-04 08:24:59 -07:00
|
|
|
uint32_t newLen = oldLen - aSlice;
|
|
|
|
pointers->RemoveElementsAt(newLen, aSlice);
|
|
|
|
if (newLen == 0) {
|
|
|
|
delete pointers;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void
|
|
|
|
AddForDeferredFinalization(T* aObject)
|
|
|
|
{
|
|
|
|
cyclecollector::DeferredFinalize(AppendDeferredFinalizePointer,
|
|
|
|
DeferredFinalize, aObject);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, template <typename> class SmartPtr>
|
|
|
|
class DeferredFinalizer<T, SmartPtr, true>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void
|
|
|
|
AddForDeferredFinalization(T* aObject)
|
|
|
|
{
|
|
|
|
cyclecollector::DeferredFinalize(reinterpret_cast<nsISupports*>(aObject));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, template <typename> class SmartPtr>
|
|
|
|
static void
|
|
|
|
AddForDeferredFinalization(T* aObject)
|
|
|
|
{
|
|
|
|
DeferredFinalizer<T, SmartPtr>::AddForDeferredFinalization(aObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns T's CC participant if it participates in CC or null if it
|
|
|
|
// doesn't. This also returns null for classes that don't inherit from
|
|
|
|
// nsISupports (QI should be used to get the participant for those).
|
|
|
|
template<class T, bool isISupports=IsBaseOf<nsISupports, T>::value>
|
|
|
|
class GetCCParticipant
|
|
|
|
{
|
|
|
|
// Helper for GetCCParticipant for classes that participate in CC.
|
|
|
|
template<class U>
|
2013-08-14 15:04:24 -07:00
|
|
|
static MOZ_CONSTEXPR nsCycleCollectionParticipant*
|
2013-07-04 08:24:59 -07:00
|
|
|
GetHelper(int, typename U::NS_CYCLE_COLLECTION_INNERCLASS* dummy=nullptr)
|
|
|
|
{
|
|
|
|
return T::NS_CYCLE_COLLECTION_INNERCLASS::GetParticipant();
|
|
|
|
}
|
|
|
|
// Helper for GetCCParticipant for classes that don't participate in CC.
|
|
|
|
template<class U>
|
2013-08-14 15:04:24 -07:00
|
|
|
static MOZ_CONSTEXPR nsCycleCollectionParticipant*
|
2013-07-04 08:24:59 -07:00
|
|
|
GetHelper(double)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2013-08-14 15:04:24 -07:00
|
|
|
static MOZ_CONSTEXPR nsCycleCollectionParticipant*
|
2013-07-04 08:24:59 -07:00
|
|
|
Get()
|
|
|
|
{
|
|
|
|
// Passing int() here will try to call the GetHelper that takes an int as
|
|
|
|
// its firt argument. If T doesn't participate in CC then substitution for
|
|
|
|
// the second argument (with a default value) will fail and because of
|
|
|
|
// SFINAE the next best match (the variant taking a double) will be called.
|
|
|
|
return GetHelper<T>(int());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class GetCCParticipant<T, true>
|
|
|
|
{
|
|
|
|
public:
|
2013-07-29 16:57:28 -07:00
|
|
|
static MOZ_CONSTEXPR nsCycleCollectionParticipant*
|
2013-07-04 08:24:59 -07:00
|
|
|
Get()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-05 10:38:17 -08:00
|
|
|
/*
|
|
|
|
* Helper function for testing whether the given object comes from a
|
|
|
|
* privileged app.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
IsInPrivilegedApp(JSContext* aCx, JSObject* aObj);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function for testing whether the given object comes from a
|
|
|
|
* certified app.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
IsInCertifiedApp(JSContext* aCx, JSObject* aObj);
|
|
|
|
|
2013-11-18 22:05:42 -08:00
|
|
|
void
|
|
|
|
FinalizeGlobal(JSFreeOp* aFop, JSObject* aObj);
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
bool
|
|
|
|
ResolveGlobal(JSContext* aCx, JS::Handle<JSObject*> aObj,
|
2014-04-25 14:11:02 -07:00
|
|
|
JS::Handle<jsid> aId, JS::MutableHandle<JSObject*> aObjp);
|
2013-11-05 06:16:26 -08:00
|
|
|
|
|
|
|
bool
|
|
|
|
EnumerateGlobal(JSContext* aCx, JS::Handle<JSObject*> aObj);
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
template <class T>
|
|
|
|
struct CreateGlobalOptions
|
|
|
|
{
|
|
|
|
static MOZ_CONSTEXPR_VAR ProtoAndIfaceCache::Kind ProtoAndIfaceCacheKind =
|
|
|
|
ProtoAndIfaceCache::NonWindowLike;
|
|
|
|
// Intl API is broken and makes JS_InitStandardClasses fail intermittently,
|
|
|
|
// see bug 934889.
|
|
|
|
static MOZ_CONSTEXPR_VAR bool ForceInitStandardClassesToFalse = true;
|
|
|
|
static void TraceGlobal(JSTracer* aTrc, JSObject* aObj)
|
|
|
|
{
|
|
|
|
mozilla::dom::TraceProtoAndIfaceCache(aTrc, aObj);
|
|
|
|
}
|
|
|
|
static bool PostCreateGlobal(JSContext* aCx, JS::Handle<JSObject*> aGlobal)
|
|
|
|
{
|
|
|
|
MOZ_ALWAYS_TRUE(TryPreserveWrapper(aGlobal));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct CreateGlobalOptions<nsGlobalWindow>
|
2013-11-05 06:16:26 -08:00
|
|
|
{
|
2014-04-08 11:48:37 -07:00
|
|
|
static MOZ_CONSTEXPR_VAR ProtoAndIfaceCache::Kind ProtoAndIfaceCacheKind =
|
|
|
|
ProtoAndIfaceCache::WindowLike;
|
|
|
|
static MOZ_CONSTEXPR_VAR bool ForceInitStandardClassesToFalse = false;
|
|
|
|
static void TraceGlobal(JSTracer* aTrc, JSObject* aObj);
|
|
|
|
static bool PostCreateGlobal(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
|
|
|
|
};
|
2013-11-05 06:16:26 -08:00
|
|
|
|
2014-08-08 06:03:29 -07:00
|
|
|
nsresult
|
|
|
|
RegisterDOMNames();
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
template <class T, ProtoGetter GetProto>
|
|
|
|
bool
|
|
|
|
CreateGlobal(JSContext* aCx, T* aNative, nsWrapperCache* aCache,
|
|
|
|
const JSClass* aClass, JS::CompartmentOptions& aOptions,
|
|
|
|
JSPrincipals* aPrincipal, bool aInitStandardClasses,
|
|
|
|
JS::MutableHandle<JSObject*> aGlobal)
|
|
|
|
{
|
|
|
|
aOptions.setTrace(CreateGlobalOptions<T>::TraceGlobal);
|
2014-04-04 16:58:08 -07:00
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
aGlobal.set(JS_NewGlobalObject(aCx, aClass, aPrincipal,
|
|
|
|
JS::DontFireOnNewGlobalHook, aOptions));
|
|
|
|
if (!aGlobal) {
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_WARNING("Failed to create global");
|
2014-05-28 17:34:36 -07:00
|
|
|
return false;
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
JSAutoCompartment ac(aCx, aGlobal);
|
2013-11-05 06:16:26 -08:00
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
{
|
|
|
|
js::SetReservedSlot(aGlobal, DOM_OBJECT_SLOT, PRIVATE_TO_JSVAL(aNative));
|
|
|
|
NS_ADDREF(aNative);
|
2013-11-05 06:16:26 -08:00
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
aCache->SetIsDOMBinding();
|
|
|
|
aCache->SetWrapper(aGlobal);
|
2013-11-05 06:16:26 -08:00
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
dom::AllocateProtoAndIfaceCache(aGlobal,
|
|
|
|
CreateGlobalOptions<T>::ProtoAndIfaceCacheKind);
|
|
|
|
|
|
|
|
if (!CreateGlobalOptions<T>::PostCreateGlobal(aCx, aGlobal)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
if (aInitStandardClasses &&
|
|
|
|
!CreateGlobalOptions<T>::ForceInitStandardClassesToFalse &&
|
|
|
|
!JS_InitStandardClasses(aCx, aGlobal)) {
|
|
|
|
NS_WARNING("Failed to init standard classes");
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-05 06:16:26 -08:00
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
JS::Handle<JSObject*> proto = GetProto(aCx, aGlobal);
|
|
|
|
if (!proto || !JS_SplicePrototype(aCx, aGlobal, proto)) {
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_WARNING("Failed to set proto");
|
2014-04-08 11:48:37 -07:00
|
|
|
return false;
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
return true;
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
|
|
|
|
2014-01-22 03:28:06 -08:00
|
|
|
/*
|
|
|
|
* Holds a jsid that is initialized to an interned string, with conversion to
|
|
|
|
* Handle<jsid>.
|
|
|
|
*/
|
|
|
|
class InternedStringId
|
|
|
|
{
|
|
|
|
jsid id;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InternedStringId() : id(JSID_VOID) {}
|
|
|
|
|
|
|
|
bool init(JSContext *cx, const char *string) {
|
|
|
|
JSString* str = JS_InternString(cx, string);
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
id = INTERNED_STRING_TO_JSID(cx, str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const jsid& () {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator JS::Handle<jsid> () {
|
|
|
|
/* This is safe because we have interned the string. */
|
|
|
|
return JS::Handle<jsid>::fromMarkedLocation(&id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-07 12:56:14 -08:00
|
|
|
bool
|
|
|
|
GenericBindingGetter(JSContext* cx, unsigned argc, JS::Value* vp);
|
|
|
|
|
|
|
|
bool
|
|
|
|
GenericBindingSetter(JSContext* cx, unsigned argc, JS::Value* vp);
|
|
|
|
|
|
|
|
bool
|
|
|
|
GenericBindingMethod(JSContext* cx, unsigned argc, JS::Value* vp);
|
|
|
|
|
2014-03-21 09:18:24 -07:00
|
|
|
bool
|
|
|
|
GenericPromiseReturningBindingMethod(JSContext* cx, unsigned argc, JS::Value* vp);
|
|
|
|
|
2014-03-21 09:18:24 -07:00
|
|
|
bool
|
|
|
|
StaticMethodPromiseWrapper(JSContext* cx, unsigned argc, JS::Value* vp);
|
|
|
|
|
2014-03-21 09:18:24 -07:00
|
|
|
// ConvertExceptionToPromise should only be called when we have an error
|
|
|
|
// condition (e.g. returned false from a JSAPI method). Note that there may be
|
|
|
|
// no exception on cx, in which case this is an uncatchable failure that will
|
|
|
|
// simply be propagated. Otherwise this method will attempt to convert the
|
|
|
|
// exception to a Promise rejected with the exception that it will store in
|
|
|
|
// rval.
|
|
|
|
//
|
|
|
|
// promiseScope should be the scope in which the Promise should be created.
|
|
|
|
bool
|
|
|
|
ConvertExceptionToPromise(JSContext* cx,
|
|
|
|
JSObject* promiseScope,
|
|
|
|
JS::MutableHandle<JS::Value> rval);
|
|
|
|
|
2014-02-15 13:12:34 -08:00
|
|
|
// While we wait for the outcome of spec discussions on whether properties for
|
|
|
|
// DOM global objects live on the object or the prototype, we supply this one
|
|
|
|
// place to switch the behaviour, so we can easily turn this off on branches.
|
|
|
|
inline bool
|
|
|
|
GlobalPropertiesAreOwn()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:26:23 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
AssertReturnTypeMatchesJitinfo(const JSJitInfo* aJitinfo,
|
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-10-18 04:22:08 -07:00
|
|
|
|
|
|
|
extern void
|
|
|
|
DEBUG_CheckXBLLookup(JSContext *cx, JSPropertyDescriptor *desc);
|
|
|
|
#else
|
|
|
|
#define DEBUG_CheckXBLLookup(a, b) {}
|
|
|
|
|
2014-05-15 10:26:23 -07:00
|
|
|
#endif
|
|
|
|
|
2014-05-25 11:31:07 -07:00
|
|
|
// Returns true if aObj's global has any of the permissions named in aPermissions
|
|
|
|
// set to nsIPermissionManager::ALLOW_ACTION. aPermissions must be null-terminated.
|
|
|
|
bool
|
|
|
|
CheckPermissions(JSContext* aCx, JSObject* aObj, const char* const aPermissions[]);
|
|
|
|
|
2014-08-20 16:32:19 -07:00
|
|
|
//Returns true if page is being prerendered.
|
|
|
|
bool
|
|
|
|
CheckSafetyInPrerendering(JSContext* aCx, JSObject* aObj);
|
|
|
|
|
2014-08-19 18:12:15 -07:00
|
|
|
bool
|
|
|
|
CallerSubsumes(JSObject* aObject);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
CallerSubsumes(JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
if (!aValue.isObject()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return CallerSubsumes(&aValue.toObject());
|
|
|
|
}
|
|
|
|
|
2014-08-25 15:10:22 -07:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2014-09-16 10:15:16 -07:00
|
|
|
WrappedJSToDictionary(JSContext* aCx, nsISupports* aObject, T& aDictionary)
|
2014-08-25 15:10:22 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> wrappedObj = do_QueryInterface(aObject);
|
|
|
|
if (!wrappedObj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-16 10:15:16 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, wrappedObj->GetJSObject());
|
2014-08-25 15:10:22 -07:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-16 10:15:16 -07:00
|
|
|
JSAutoCompartment ac(aCx, obj);
|
|
|
|
JS::Rooted<JS::Value> v(aCx, JS::ObjectValue(*obj));
|
|
|
|
return aDictionary.Init(aCx, v);
|
2014-08-25 15:10:22 -07:00
|
|
|
}
|
|
|
|
|
2014-08-27 15:43:56 -07:00
|
|
|
|
|
|
|
template<class T, class S>
|
|
|
|
inline nsRefPtr<T>
|
|
|
|
StrongOrRawPtr(already_AddRefed<S>&& aPtr)
|
|
|
|
{
|
|
|
|
return aPtr.template downcast<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline T*
|
|
|
|
StrongOrRawPtr(T* aPtr)
|
|
|
|
{
|
|
|
|
return aPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, template<typename> class SmartPtr, class S>
|
|
|
|
inline void
|
|
|
|
StrongOrRawPtr(SmartPtr<S>&& aPtr) MOZ_DELETE;
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
#endif /* mozilla_dom_BindingUtils_h__ */
|