2012-03-31 04:42:20 +00: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-03 04:35:38 +00:00
|
|
|
#ifndef mozilla_dom_BindingUtils_h__
|
|
|
|
#define mozilla_dom_BindingUtils_h__
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-05-03 04:35:38 +00:00
|
|
|
#include "mozilla/dom/DOMJSClass.h"
|
2012-05-22 13:46:20 +00:00
|
|
|
#include "mozilla/dom/DOMJSProxyHandler.h"
|
2012-09-19 13:02:37 +00:00
|
|
|
#include "mozilla/dom/NonRefcountedDOMObject.h"
|
2012-04-24 23:50:00 +00:00
|
|
|
#include "mozilla/dom/workers/Workers.h"
|
2012-05-06 01:15:11 +00:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-01-14 17:43:00 +00:00
|
|
|
#include "jsfriendapi.h"
|
2012-05-18 08:29:40 +00:00
|
|
|
#include "jswrapper.h"
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-05-18 08:29:40 +00:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "qsObjectHelper.h"
|
|
|
|
#include "xpcpublic.h"
|
2012-03-31 04:42:20 +00:00
|
|
|
#include "nsTraceRefcnt.h"
|
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-06-13 15:14:15 +00:00
|
|
|
#include "mozilla/Likely.h"
|
2012-12-25 22:00:15 +00:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2013-02-06 09:42:17 +00:00
|
|
|
#include "mozilla/dom/CallbackObject.h"
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-05-20 17:47:08 +00:00
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
2013-05-30 03:15:31 +00:00
|
|
|
extern nsresult
|
|
|
|
xpc_qsUnwrapArgImpl(JSContext* cx, jsval v, const nsIID& iid, void** ppArg,
|
|
|
|
nsISupports** ppArgRef, jsval* vp);
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-05-30 03:15:31 +00: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
|
|
|
|
UnwrapArg(JSContext* cx, jsval v, Interface** ppArg,
|
|
|
|
StrongRefType** ppArgRef, jsval* vp)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-18 10:36:08 +00:00
|
|
|
bool
|
|
|
|
ThrowErrorMessage(JSContext* aCx, const ErrNum aErrorNumber, ...);
|
2013-06-17 17:07:03 +00:00
|
|
|
bool
|
2013-06-17 20:31:13 +00:00
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
const char* aInterfaceName);
|
2012-07-18 10:36:08 +00:00
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
template<bool mainThread>
|
|
|
|
inline bool
|
|
|
|
Throw(JSContext* cx, nsresult rv)
|
|
|
|
{
|
2012-04-24 23:50:00 +00:00
|
|
|
using mozilla::dom::workers::exceptions::ThrowDOMExceptionForNSResult;
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
// XXX Introduce exception machinery.
|
|
|
|
if (mainThread) {
|
2012-05-18 08:29:40 +00:00
|
|
|
xpc::Throw(cx, rv);
|
2012-03-31 04:42:20 +00:00
|
|
|
} else {
|
|
|
|
if (!JS_IsExceptionPending(cx)) {
|
2012-04-24 23:50:00 +00:00
|
|
|
ThrowDOMExceptionForNSResult(cx, rv);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool mainThread>
|
|
|
|
inline bool
|
2012-11-06 23:23:14 +00:00
|
|
|
ThrowMethodFailedWithDetails(JSContext* cx, ErrorResult& rv,
|
2013-07-04 13:24:49 +00:00
|
|
|
const char* ifaceName,
|
|
|
|
const char* memberName)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-11-06 23:23:14 +00:00
|
|
|
if (rv.IsTypeError()) {
|
|
|
|
rv.ReportTypeError(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-19 16:54:40 +00:00
|
|
|
if (rv.IsJSException()) {
|
|
|
|
rv.ReportJSException(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-04 13:24:49 +00:00
|
|
|
if (rv.IsNotEnoughArgsError()) {
|
|
|
|
rv.ReportNotEnoughArgsError(cx, ifaceName, memberName);
|
|
|
|
}
|
2012-05-06 01:15:11 +00:00
|
|
|
return Throw<mainThread>(cx, rv.ErrorCode());
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 18:50:27 +00:00
|
|
|
// Returns true if the JSClass is used for DOM objects.
|
2012-03-31 04:42:20 +00:00
|
|
|
inline bool
|
|
|
|
IsDOMClass(const JSClass* clasp)
|
|
|
|
{
|
|
|
|
return clasp->flags & JSCLASS_IS_DOMJSCLASS;
|
|
|
|
}
|
|
|
|
|
2012-06-06 19:59:46 +00:00
|
|
|
inline bool
|
|
|
|
IsDOMClass(const js::Class* clasp)
|
|
|
|
{
|
2012-06-01 16:24:52 +00:00
|
|
|
return IsDOMClass(Jsvalify(clasp));
|
2012-06-06 19:59:46 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 18:50:27 +00: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-06-21 05:39:22 +00:00
|
|
|
MOZ_STATIC_ASSERT(DOM_OBJECT_SLOT == js::PROXY_PRIVATE_SLOT,
|
|
|
|
"js::PROXY_PRIVATE_SLOT doesn't match DOM_OBJECT_SLOT. "
|
2012-12-06 20:21:19 +00:00
|
|
|
"Expect bad things");
|
2012-03-31 04:42:20 +00:00
|
|
|
template <class T>
|
|
|
|
inline T*
|
2012-12-06 20:21:19 +00:00
|
|
|
UnwrapDOMObject(JSObject* obj)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-12-06 20:21:19 +00:00
|
|
|
MOZ_ASSERT(IsDOMClass(js::GetObjectClass(obj)) || IsDOMProxy(obj),
|
2012-05-22 13:46:20 +00:00
|
|
|
"Don't pass non-DOM objects to this function");
|
|
|
|
|
2012-12-06 20:21:19 +00:00
|
|
|
JS::Value val = js::GetReservedSlot(obj, DOM_OBJECT_SLOT);
|
2012-03-31 04:42:20 +00:00
|
|
|
return static_cast<T*>(val.toPrivate());
|
|
|
|
}
|
|
|
|
|
2012-05-22 13:46:20 +00:00
|
|
|
inline const DOMClass*
|
|
|
|
GetDOMClass(JSObject* obj)
|
|
|
|
{
|
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
if (IsDOMClass(clasp)) {
|
|
|
|
return &DOMJSClass::FromJSClass(clasp)->mClass;
|
|
|
|
}
|
|
|
|
|
2013-06-21 04:27:28 +00:00
|
|
|
if (js::IsProxyClass(clasp)) {
|
2012-05-22 13:46:20 +00:00
|
|
|
js::BaseProxyHandler* handler = js::GetProxyHandler(obj);
|
2012-10-13 12:50:24 +00:00
|
|
|
if (handler->family() == ProxyFamily()) {
|
2012-12-06 20:21:19 +00:00
|
|
|
return &static_cast<DOMProxyHandler*>(handler)->mClass;
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 20:21:19 +00:00
|
|
|
return nullptr;
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 06:56:02 +00:00
|
|
|
inline nsISupports*
|
|
|
|
UnwrapDOMObjectToISupports(JSObject* aObject)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
2013-06-01 06:56:02 +00:00
|
|
|
const DOMClass* clasp = GetDOMClass(aObject);
|
2012-12-06 20:21:19 +00:00
|
|
|
if (!clasp || !clasp->mDOMObjectIsISupports) {
|
2013-06-01 06:56:02 +00:00
|
|
|
return nullptr;
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 06:56:02 +00:00
|
|
|
return UnwrapDOMObject<nsISupports>(aObject);
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
IsDOMObject(JSObject* obj)
|
|
|
|
{
|
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
2012-10-09 18:50:27 +00:00
|
|
|
return IsDOMClass(clasp) || IsDOMProxy(obj, clasp);
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 16:24:58 +00:00
|
|
|
// Some callers don't want to set an exception when unwrapping fails
|
2012-03-31 04:42:20 +00:00
|
|
|
// (for example, overload resolution uses unwrapping to tell what sort
|
|
|
|
// of thing it's looking at).
|
2012-05-25 05:08:26 +00:00
|
|
|
// U must be something that a T* can be assigned to (e.g. T* or an nsRefPtr<T>).
|
|
|
|
template <prototypes::ID PrototypeID, class T, typename U>
|
2012-11-27 09:20:05 +00:00
|
|
|
MOZ_ALWAYS_INLINE nsresult
|
2012-05-25 05:08:26 +00:00
|
|
|
UnwrapObject(JSContext* cx, JSObject* obj, U& value)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
/* First check to see whether we have a DOM object */
|
2012-12-06 20:21:19 +00:00
|
|
|
const DOMClass* domClass = GetDOMClass(obj);
|
|
|
|
if (!domClass) {
|
2012-03-31 04:42:20 +00: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 18:50:18 +00:00
|
|
|
obj = js::CheckedUnwrap(obj, /* stopAtOuter = */ false);
|
2012-03-31 04:42:20 +00:00
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_XPC_SECURITY_MANAGER_VETO;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!js::IsWrapper(obj));
|
2012-12-06 20:21:19 +00:00
|
|
|
domClass = GetDOMClass(obj);
|
|
|
|
if (!domClass) {
|
2012-03-31 04:42:20 +00: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. */
|
|
|
|
if (domClass->mInterfaceChain[PrototypeTraits<PrototypeID>::Depth] ==
|
|
|
|
PrototypeID) {
|
2012-12-06 20:21:19 +00:00
|
|
|
value = UnwrapDOMObject<T>(obj);
|
2012-03-31 04:42:20 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's the wrong sort of DOM object */
|
|
|
|
return NS_ERROR_XPC_BAD_CONVERT_JS;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2013-05-16 01:46:44 +00:00
|
|
|
IsNotDateOrRegExp(JSContext* cx, JS::Handle<JSObject*> obj)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(obj);
|
2013-01-03 03:03:25 +00:00
|
|
|
return !JS_ObjectIsDate(cx, obj) && !JS_ObjectIsRegExp(cx, obj);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2013-01-03 19:03:00 +00:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-05-16 01:46:44 +00:00
|
|
|
IsArrayLike(JSContext* cx, JS::Handle<JSObject*> obj)
|
2013-01-03 19:03:00 +00:00
|
|
|
{
|
|
|
|
return IsNotDateOrRegExp(cx, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-05-16 01:46:44 +00:00
|
|
|
IsObjectValueConvertibleToDictionary(JSContext* cx,
|
|
|
|
JS::Handle<JS::Value> objVal)
|
2013-01-03 19:03:00 +00:00
|
|
|
{
|
2013-05-16 01:46:44 +00:00
|
|
|
JS::Rooted<JSObject*> obj(cx, &objVal.toObject());
|
2013-01-03 19:03:00 +00:00
|
|
|
return IsNotDateOrRegExp(cx, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-05-16 01:46:44 +00:00
|
|
|
IsConvertibleToDictionary(JSContext* cx, JS::Handle<JS::Value> val)
|
2013-01-03 19:03:00 +00:00
|
|
|
{
|
|
|
|
return val.isNullOrUndefined() ||
|
2013-05-16 01:46:44 +00:00
|
|
|
(val.isObject() && IsObjectValueConvertibleToDictionary(cx, val));
|
2013-01-03 19:03:00 +00:00
|
|
|
}
|
|
|
|
|
2013-01-03 19:03:11 +00:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-05-16 01:46:44 +00:00
|
|
|
IsConvertibleToCallbackInterface(JSContext* cx, JS::Handle<JSObject*> obj)
|
2013-01-03 19:03:11 +00:00
|
|
|
{
|
|
|
|
return IsNotDateOrRegExp(cx, obj);
|
|
|
|
}
|
|
|
|
|
2012-05-25 05:08:26 +00:00
|
|
|
// U must be something that a T* can be assigned to (e.g. T* or an nsRefPtr<T>).
|
|
|
|
template <class T, typename U>
|
2012-03-31 04:42:20 +00:00
|
|
|
inline nsresult
|
2012-05-25 05:08:26 +00:00
|
|
|
UnwrapObject(JSContext* cx, JSObject* obj, U& value)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
return UnwrapObject<static_cast<prototypes::ID>(
|
2012-06-13 15:14:15 +00:00
|
|
|
PrototypeIDMap<T>::PrototypeID), T>(cx, obj, value);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 18:50:27 +00:00
|
|
|
// The items in the protoAndIfaceArray are indexed by the prototypes::id::ID and
|
|
|
|
// constructors::id::ID enums, in that order. The end of the prototype objects
|
|
|
|
// should be the start of the interface objects.
|
|
|
|
MOZ_STATIC_ASSERT((size_t)constructors::id::_ID_Start ==
|
|
|
|
(size_t)prototypes::id::_ID_Count,
|
|
|
|
"Overlapping or discontiguous indexes.");
|
|
|
|
const size_t kProtoAndIfaceCacheCount = constructors::id::_ID_Count;
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
inline void
|
2012-10-09 18:50:27 +00:00
|
|
|
AllocateProtoAndIfaceCache(JSObject* obj)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
MOZ_ASSERT(js::GetReservedSlot(obj, DOM_PROTOTYPE_SLOT).isUndefined());
|
|
|
|
|
|
|
|
// Important: The () at the end ensure zero-initialization
|
2012-10-09 18:50:27 +00:00
|
|
|
JSObject** protoAndIfaceArray = new JSObject*[kProtoAndIfaceCacheCount]();
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
js::SetReservedSlot(obj, DOM_PROTOTYPE_SLOT,
|
2012-10-09 18:50:27 +00:00
|
|
|
JS::PrivateValue(protoAndIfaceArray));
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 12:49:43 +00:00
|
|
|
inline void
|
2012-10-09 18:50:27 +00:00
|
|
|
TraceProtoAndIfaceCache(JSTracer* trc, JSObject* obj)
|
2012-05-02 12:49:43 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
|
2012-10-09 18:50:27 +00:00
|
|
|
if (!HasProtoAndIfaceArray(obj))
|
2012-06-19 00:04:38 +00:00
|
|
|
return;
|
2012-10-09 18:50:27 +00:00
|
|
|
JSObject** protoAndIfaceArray = GetProtoAndIfaceArray(obj);
|
|
|
|
for (size_t i = 0; i < kProtoAndIfaceCacheCount; ++i) {
|
2013-03-28 20:37:22 +00:00
|
|
|
if (protoAndIfaceArray[i]) {
|
|
|
|
JS_CallObjectTracer(trc, &protoAndIfaceArray[i], "protoAndIfaceArray[i]");
|
2012-05-02 12:49:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
inline void
|
2012-10-09 18:50:27 +00:00
|
|
|
DestroyProtoAndIfaceCache(JSObject* obj)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
|
2012-10-09 18:50:27 +00:00
|
|
|
JSObject** protoAndIfaceArray = GetProtoAndIfaceArray(obj);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-10-09 18:50:27 +00:00
|
|
|
delete [] protoAndIfaceArray;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-05-09 10:00:48 +00:00
|
|
|
/**
|
|
|
|
* Add constants to an object.
|
|
|
|
*/
|
|
|
|
bool
|
2013-05-08 02:34:56 +00:00
|
|
|
DefineConstants(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const ConstantSpec* cs);
|
2012-05-09 10:00:48 +00:00
|
|
|
|
2012-10-09 18:50:27 +00:00
|
|
|
struct JSNativeHolder
|
|
|
|
{
|
|
|
|
JSNative mNative;
|
|
|
|
const NativePropertyHooks* mPropertyHooks;
|
|
|
|
};
|
|
|
|
|
2013-01-22 10:51:15 +00:00
|
|
|
struct NamedConstructor
|
|
|
|
{
|
|
|
|
const char* mName;
|
|
|
|
const JSNativeHolder mHolder;
|
|
|
|
unsigned mNargs;
|
|
|
|
};
|
|
|
|
|
2012-03-31 04:42:20 +00: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 14:10:56 +00: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 18:38:19 +00:00
|
|
|
* interfaceProto is the prototype to use for the interface object.
|
2012-03-31 04:42:20 +00: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 18:50:27 +00: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-31 04:42:20 +00:00
|
|
|
* constructorClass is the JSClass to use for the interface object.
|
2012-05-03 04:35:38 +00:00
|
|
|
* This is null if we should not create an interface object or
|
|
|
|
* if it should be a function object.
|
2012-10-09 18:50:27 +00: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-03 04:35:38 +00:00
|
|
|
* ctorNargs is the length of the constructor function; 0 if no constructor
|
2012-10-09 18:50:27 +00: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 18:50:05 +00:00
|
|
|
* domClass is the DOMClass of instance objects for this class. This can be
|
|
|
|
* null if this is not a concrete proto.
|
|
|
|
* 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.
|
2012-03-31 04:42:20 +00:00
|
|
|
*
|
2012-10-09 18:50:27 +00: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-31 04:42:20 +00:00
|
|
|
*/
|
2012-10-09 18:50:27 +00:00
|
|
|
void
|
2013-05-02 03:44:12 +00:00
|
|
|
CreateInterfaceObjects(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 03:44:11 +00:00
|
|
|
JS::Handle<JSObject*> protoProto,
|
2012-10-09 18:50:27 +00:00
|
|
|
JSClass* protoClass, JSObject** protoCache,
|
2013-05-02 18:38:19 +00:00
|
|
|
JS::Handle<JSObject*> interfaceProto,
|
2013-01-22 10:51:15 +00:00
|
|
|
JSClass* constructorClass, const JSNativeHolder* constructor,
|
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
|
|
|
JSObject** constructorCache, const DOMClass* domClass,
|
2012-10-09 18:50:27 +00:00
|
|
|
const NativeProperties* regularProperties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2012-10-09 18:50:05 +00:00
|
|
|
const char* name);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-10-24 20:10:49 +00:00
|
|
|
/*
|
|
|
|
* Define the unforgeable attributes on an object.
|
|
|
|
*/
|
|
|
|
bool
|
2013-05-03 23:29:08 +00:00
|
|
|
DefineUnforgeableAttributes(JSContext* cx, JS::Handle<JSObject*> obj,
|
2013-04-03 00:20:38 +00:00
|
|
|
const Prefable<const JSPropertySpec>* props);
|
2012-10-24 20:10:49 +00:00
|
|
|
|
2012-11-22 11:09:41 +00:00
|
|
|
bool
|
2013-05-03 23:29:08 +00:00
|
|
|
DefineWebIDLBindingPropertiesOnXPCProto(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> proto,
|
|
|
|
const NativeProperties* properties);
|
2012-11-22 11:09:41 +00:00
|
|
|
|
2012-11-09 07:48:42 +00: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(AddRef)
|
|
|
|
HAS_MEMBER(Release)
|
|
|
|
HAS_MEMBER(QueryInterface)
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct IsRefCounted
|
|
|
|
{
|
|
|
|
static bool const Value = HasAddRefMember<T>::Value &&
|
|
|
|
HasReleaseMember<T>::Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct IsISupports
|
|
|
|
{
|
|
|
|
static bool const Value = IsRefCounted<T>::Value &&
|
|
|
|
HasQueryInterfaceMember<T>::Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 16:29:54 +00:00
|
|
|
typedef JSObject* (nsWrapperCache::*WrapObject)(JSContext*,
|
|
|
|
JS::Handle<JSObject*>);
|
2012-11-09 07:48:42 +00: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 10:46:53 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
template <class T, bool isISupports=IsISupports<T>::Value>
|
|
|
|
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 09:20:04 +00:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
CouldBeDOMBinding(void*)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
CouldBeDOMBinding(nsWrapperCache* aCache)
|
|
|
|
{
|
|
|
|
return aCache->IsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2012-09-12 20:29:30 +00:00
|
|
|
// The DOM_OBJECT_SLOT_SOW slot contains a JS::ObjectValue which points to the
|
|
|
|
// cached system object wrapper (SOW) or JS::UndefinedValue if this class
|
|
|
|
// doesn't need SOWs.
|
|
|
|
|
|
|
|
inline const JS::Value&
|
|
|
|
GetSystemOnlyWrapperSlot(JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsDOMClass(js::GetObjectJSClass(obj)) &&
|
|
|
|
!(js::GetObjectJSClass(obj)->flags & JSCLASS_DOM_GLOBAL));
|
|
|
|
return js::GetReservedSlot(obj, DOM_OBJECT_SLOT_SOW);
|
|
|
|
}
|
|
|
|
inline void
|
|
|
|
SetSystemOnlyWrapperSlot(JSObject* obj, const JS::Value& v)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsDOMClass(js::GetObjectJSClass(obj)) &&
|
|
|
|
!(js::GetObjectJSClass(obj)->flags & JSCLASS_DOM_GLOBAL));
|
|
|
|
js::SetReservedSlot(obj, DOM_OBJECT_SLOT_SOW, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
GetSameCompartmentWrapperForDOMBinding(JSObject*& obj)
|
|
|
|
{
|
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
if (dom::IsDOMClass(clasp)) {
|
|
|
|
if (!(clasp->flags & JSCLASS_DOM_GLOBAL)) {
|
|
|
|
JS::Value v = GetSystemOnlyWrapperSlot(obj);
|
|
|
|
if (v.isObject()) {
|
|
|
|
obj = &v.toObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return IsDOMProxy(obj, clasp);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
SetSystemOnlyWrapper(JSObject* obj, nsWrapperCache* cache, JSObject& wrapper)
|
|
|
|
{
|
|
|
|
SetSystemOnlyWrapperSlot(obj, JS::ObjectValue(wrapper));
|
|
|
|
cache->SetHasSystemOnlyWrapper();
|
|
|
|
}
|
|
|
|
|
2013-07-11 15:58:29 +00: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-03 03:03:25 +00:00
|
|
|
{
|
2013-07-11 15:58:29 +00:00
|
|
|
MOZ_ASSERT(rval.isString());
|
|
|
|
JSString* str = rval.toString();
|
|
|
|
if (JS::GetGCThingZone(str) != js::GetContextZone(cx)) {
|
|
|
|
return JS_WrapValue(cx, rval.address());
|
2013-03-17 03:36:37 +00:00
|
|
|
}
|
2013-07-11 15:58:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-17 03:36:37 +00:00
|
|
|
|
2013-07-11 15:58:29 +00: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-22 02:23:47 +00:00
|
|
|
|
2013-04-19 19:57:18 +00:00
|
|
|
JSObject* obj = &rval.toObject();
|
|
|
|
if (js::GetObjectCompartment(obj) != js::GetContextCompartment(cx)) {
|
|
|
|
return JS_WrapValue(cx, rval.address());
|
|
|
|
}
|
2013-03-17 03:36:37 +00:00
|
|
|
|
2013-04-19 19:57:18 +00:00
|
|
|
// We're same-compartment, but even then we might need to wrap
|
|
|
|
// objects specially. Check for that.
|
|
|
|
if (GetSameCompartmentWrapperForDOMBinding(obj)) {
|
|
|
|
// We're a new-binding object, and "obj" now points to the right thing
|
|
|
|
rval.set(JS::ObjectValue(*obj));
|
|
|
|
return true;
|
2013-01-03 03:03:25 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 15:58:29 +00: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-04-19 19:57:18 +00:00
|
|
|
return JS_WrapValue(cx, rval.address());
|
2013-01-03 03:03:25 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 15:58:29 +00: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;
|
|
|
|
}
|
|
|
|
return JS_WrapValue(cx, rval.address());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-12 20:29:30 +00:00
|
|
|
static inline void
|
|
|
|
WrapNewBindingForSameCompartment(JSContext* cx, JSObject* obj, void* value,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-09-12 20:29:30 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
rval.set(JS::ObjectValue(*obj));
|
2012-09-12 20:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
WrapNewBindingForSameCompartment(JSContext* cx, JSObject* obj,
|
2013-06-08 02:45:46 +00:00
|
|
|
nsWrapperCache* value,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-09-12 20:29:30 +00:00
|
|
|
{
|
|
|
|
if (value->HasSystemOnlyWrapper()) {
|
2013-06-08 02:45:46 +00:00
|
|
|
rval.set(GetSystemOnlyWrapperSlot(obj));
|
|
|
|
MOZ_ASSERT(rval.isObject());
|
2012-09-12 20:29:30 +00:00
|
|
|
} else {
|
2013-06-08 02:45:46 +00:00
|
|
|
rval.set(JS::ObjectValue(*obj));
|
2012-09-12 20:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 09:20:04 +00:00
|
|
|
// Create a JSObject wrapping "value", if there isn't one already, and store it
|
2013-06-08 02:45:46 +00:00
|
|
|
// in rval. "value" must be a concrete class that implements a
|
2012-11-27 09:20:04 +00: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.
|
2012-03-31 04:42:20 +00:00
|
|
|
template <class T>
|
2012-11-20 16:36:06 +00:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-04-25 16:29:53 +00:00
|
|
|
WrapNewBindingObject(JSContext* cx, JS::Handle<JSObject*> scope, T* value,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-04-24 18:59:15 +00:00
|
|
|
MOZ_ASSERT(value);
|
2012-11-20 16:36:06 +00:00
|
|
|
JSObject* obj = value->GetWrapperPreserveColor();
|
2012-09-12 20:29:30 +00:00
|
|
|
bool couldBeDOMBinding = CouldBeDOMBinding(value);
|
2012-11-20 16:36:06 +00:00
|
|
|
if (obj) {
|
|
|
|
xpc_UnmarkNonNullGrayObject(obj);
|
2012-11-27 09:20:04 +00:00
|
|
|
} else {
|
|
|
|
// Inline this here while we have non-dom objects in wrapper caches.
|
2012-09-12 20:29:30 +00:00
|
|
|
if (!couldBeDOMBinding) {
|
2012-11-27 09:20:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-03-11 23:03:47 +00:00
|
|
|
obj = value->WrapObject(cx, scope);
|
2012-03-31 04:42:20 +00:00
|
|
|
if (!obj) {
|
2013-03-11 23:03:47 +00: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-31 04:42:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 07:48:42 +00:00
|
|
|
#ifdef DEBUG
|
2012-12-06 20:21:19 +00:00
|
|
|
const DOMClass* clasp = GetDOMClass(obj);
|
|
|
|
// clasp can be null if the cache contained a non-DOM object from a
|
2012-11-22 10:46:53 +00:00
|
|
|
// different compartment than scope.
|
2012-12-06 20:21:19 +00:00
|
|
|
if (clasp) {
|
2012-11-22 10:46:53 +00: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?");
|
|
|
|
MOZ_ASSERT_IF(clasp->mDOMObjectIsISupports, IsISupports<T>::Value);
|
|
|
|
MOZ_ASSERT(CheckWrapperCacheCast<T>::Check());
|
|
|
|
}
|
2012-11-09 07:48:42 +00:00
|
|
|
|
2012-04-18 16:11:33 +00:00
|
|
|
// When called via XrayWrapper, we end up here while running in the
|
|
|
|
// chrome compartment. But the obj we have would be created in
|
|
|
|
// whatever the content compartment is. So at this point we need to
|
|
|
|
// make sure it's correctly wrapped for the compartment of |scope|.
|
|
|
|
// cx should already be in the compartment of |scope| here.
|
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(scope, cx));
|
2012-09-12 20:29:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bool sameCompartment =
|
|
|
|
js::GetObjectCompartment(obj) == js::GetContextCompartment(cx);
|
|
|
|
if (sameCompartment && couldBeDOMBinding) {
|
2013-06-08 02:45:46 +00:00
|
|
|
WrapNewBindingForSameCompartment(cx, obj, value, rval);
|
2012-09-12 20:29:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-08 02:45:46 +00:00
|
|
|
rval.set(JS::ObjectValue(*obj));
|
2013-04-19 19:57:18 +00:00
|
|
|
return JS_WrapValue(cx, rval.address());
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 09:20:04 +00: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 20:25:50 +00:00
|
|
|
template <class T>
|
|
|
|
inline bool
|
2013-04-25 16:29:53 +00:00
|
|
|
WrapNewBindingNonWrapperCachedObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> scopeArg,
|
2013-06-08 02:45:46 +00:00
|
|
|
T* value,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-06-15 20:25:50 +00:00
|
|
|
{
|
2013-04-24 18:59:15 +00:00
|
|
|
MOZ_ASSERT(value);
|
2012-06-15 20:25:50 +00:00
|
|
|
// We try to wrap in the compartment of the underlying object of "scope"
|
2013-05-03 23:29:09 +00:00
|
|
|
JS::Rooted<JSObject*> obj(cx);
|
2012-06-15 20:25:50 +00:00
|
|
|
{
|
2012-08-22 01:42:53 +00:00
|
|
|
// scope for the JSAutoCompartment so that we restore the compartment
|
|
|
|
// before we call JS_WrapValue.
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-04-25 16:29:53 +00: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 20:25:50 +00:00
|
|
|
if (js::IsWrapper(scope)) {
|
2013-04-11 18:50:18 +00:00
|
|
|
scope = js::CheckedUnwrap(scope, /* stopAtOuter = */ false);
|
2012-08-22 01:42:53 +00:00
|
|
|
if (!scope)
|
2012-06-15 20:25:50 +00:00
|
|
|
return false;
|
2012-08-22 01:42:53 +00:00
|
|
|
ac.construct(cx, scope);
|
2012-06-15 20:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj = value->WrapObject(cx, scope);
|
|
|
|
}
|
|
|
|
|
2013-04-19 22:18:31 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-19 08:49:21 +00:00
|
|
|
// We can end up here in all sorts of compartments, per above. Make
|
|
|
|
// sure to JS_WrapValue!
|
2013-06-08 02:45:46 +00:00
|
|
|
rval.set(JS::ObjectValue(*obj));
|
|
|
|
return JS_WrapValue(cx, rval.address());
|
2013-04-19 08:49:21 +00: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 16:29:53 +00:00
|
|
|
WrapNewBindingNonWrapperCachedOwnedObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> scopeArg,
|
2013-06-08 02:45:46 +00:00
|
|
|
nsAutoPtr<T>& value,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2013-04-19 08:49:21 +00:00
|
|
|
{
|
2013-04-24 18:59:15 +00: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 08:49:21 +00:00
|
|
|
// We try to wrap in the compartment of the underlying object of "scope"
|
2013-05-03 23:29:09 +00:00
|
|
|
JS::Rooted<JSObject*> obj(cx);
|
2013-04-19 08:49:21 +00:00
|
|
|
{
|
|
|
|
// scope for the JSAutoCompartment so that we restore the compartment
|
|
|
|
// before we call JS_WrapValue.
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-04-25 16:29:53 +00: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 08:49:21 +00:00
|
|
|
if (js::IsWrapper(scope)) {
|
|
|
|
scope = js::CheckedUnwrap(scope, /* stopAtOuter = */ false);
|
|
|
|
if (!scope)
|
|
|
|
return false;
|
|
|
|
ac.construct(cx, scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tookOwnership = false;
|
|
|
|
obj = value->WrapObject(cx, scope, &tookOwnership);
|
2013-05-03 23:29:09 +00:00
|
|
|
MOZ_ASSERT_IF(obj, tookOwnership);
|
2013-04-19 08:49:21 +00:00
|
|
|
if (tookOwnership) {
|
|
|
|
value.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 22:18:31 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-15 20:25:50 +00:00
|
|
|
// We can end up here in all sorts of compartments, per above. Make
|
|
|
|
// sure to JS_WrapValue!
|
2013-06-08 02:45:46 +00:00
|
|
|
rval.set(JS::ObjectValue(*obj));
|
|
|
|
return JS_WrapValue(cx, rval.address());
|
2012-06-15 20:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for smart pointers (nsAutoPtr/nsRefPtr/nsCOMPtr).
|
|
|
|
template <template <typename> class SmartPtr, typename T>
|
|
|
|
inline bool
|
2013-04-25 16:29:53 +00:00
|
|
|
WrapNewBindingNonWrapperCachedObject(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-08 02:45:46 +00:00
|
|
|
const SmartPtr<T>& value,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-06-15 20:25:50 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapNewBindingNonWrapperCachedObject(cx, scope, value.get(), rval);
|
2012-06-15 20:25:50 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 09:20:04 +00: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 21:30:39 +00:00
|
|
|
bool
|
2012-11-27 09:20:04 +00:00
|
|
|
NativeInterface2JSObjectAndThrowIfFailed(JSContext* aCx,
|
2013-04-29 21:33:42 +00:00
|
|
|
JS::Handle<JSObject*> aScope,
|
2012-11-27 09:20:04 +00:00
|
|
|
JS::Value* aRetval,
|
|
|
|
xpcObjectHelper& aHelper,
|
|
|
|
const nsIID* aIID,
|
|
|
|
bool aAllowNativeWrapper);
|
|
|
|
|
2012-11-27 21:30:39 +00:00
|
|
|
/**
|
2012-11-27 09:20:04 +00:00
|
|
|
* A method to handle new-binding wrap failure, by possibly falling back to
|
|
|
|
* wrapping as a non-new-binding object.
|
2012-11-27 21:30:39 +00:00
|
|
|
*/
|
2012-03-31 04:42:20 +00:00
|
|
|
template <class T>
|
2012-11-27 09:20:04 +00:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
2013-04-29 21:33:42 +00:00
|
|
|
HandleNewBindingWrappingFailure(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-08 02:45:46 +00:00
|
|
|
T* value, JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-11-27 09:20:04 +00:00
|
|
|
if (JS_IsExceptionPending(cx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qsObjectHelper helper(value, GetWrapperCache(value));
|
2013-06-08 02:45:46 +00:00
|
|
|
return NativeInterface2JSObjectAndThrowIfFailed(cx, scope, rval.address(),
|
|
|
|
helper, nullptr, true);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 13:34:30 +00: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-31 04:42:20 +00:00
|
|
|
{
|
2013-04-29 21:33:42 +00:00
|
|
|
static inline bool Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-08 02:45:46 +00:00
|
|
|
const T& value, JS::MutableHandle<JS::Value> rval)
|
2013-01-28 13:34:30 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return HandleNewBindingWrappingFailure(cx, scope, value.get(), rval);
|
2013-01-28 13:34:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct HandleNewBindingWrappingFailureHelper<T, false>
|
|
|
|
{
|
2013-04-29 21:33:42 +00:00
|
|
|
static inline bool Wrap(JSContext* cx, JS::Handle<JSObject*> scope, T& value,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2013-01-28 13:34:30 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return HandleNewBindingWrappingFailure(cx, scope, &value, rval);
|
2013-01-28 13:34:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
HandleNewBindingWrappingFailure(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-08 02:45:46 +00:00
|
|
|
T& value, JS::MutableHandle<JS::Value> rval)
|
2013-01-28 13:34:30 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return HandleNewBindingWrappingFailureHelper<T>::Wrap(cx, scope, value, rval);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-07-18 10:36:08 +00:00
|
|
|
template<bool Fatal>
|
|
|
|
inline bool
|
|
|
|
EnumValueNotFound(JSContext* cx, const jschar* chars, size_t length,
|
2013-06-17 17:07:03 +00:00
|
|
|
const char* type, const char* sourceDescription)
|
2012-07-18 10:36:08 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool
|
|
|
|
EnumValueNotFound<false>(JSContext* cx, const jschar* chars, size_t length,
|
2013-06-17 17:07:03 +00:00
|
|
|
const char* type, const char* sourceDescription)
|
2012-07-18 10:36:08 +00:00
|
|
|
{
|
|
|
|
// TODO: Log a warning to the console.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool
|
|
|
|
EnumValueNotFound<true>(JSContext* cx, const jschar* chars, size_t length,
|
2013-06-17 17:07:03 +00:00
|
|
|
const char* type, const char* sourceDescription)
|
2012-07-18 10:36:08 +00:00
|
|
|
{
|
|
|
|
NS_LossyConvertUTF16toASCII deflated(static_cast<const PRUnichar*>(chars),
|
|
|
|
length);
|
2013-06-17 17:07:03 +00:00
|
|
|
return ThrowErrorMessage(cx, MSG_INVALID_ENUM_VALUE, sourceDescription,
|
|
|
|
deflated.get(), type);
|
2012-07-18 10:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<bool InvalidValueFatal>
|
2012-03-31 04:42:20 +00:00
|
|
|
inline int
|
2012-07-18 10:36:08 +00:00
|
|
|
FindEnumStringIndex(JSContext* cx, JS::Value v, const EnumEntry* values,
|
2013-06-17 17:07:03 +00:00
|
|
|
const char* type, const char* sourceDescription, bool* ok)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
|
|
|
// JS_StringEqualsAscii is slow as molasses, so don't use it here.
|
|
|
|
JSString* str = JS_ValueToString(cx, v);
|
|
|
|
if (!str) {
|
|
|
|
*ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
JS::Anchor<JSString*> anchor(str);
|
|
|
|
size_t length;
|
|
|
|
const jschar* chars = JS_GetStringCharsAndLength(cx, str, &length);
|
|
|
|
if (!chars) {
|
|
|
|
*ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
*ok = true;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-17 17:07:03 +00:00
|
|
|
*ok = EnumValueNotFound<InvalidValueFatal>(cx, chars, length, type,
|
|
|
|
sourceDescription);
|
2012-06-21 07:11:07 +00:00
|
|
|
return -1;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-05-10 19:24:59 +00:00
|
|
|
inline nsWrapperCache*
|
|
|
|
GetWrapperCache(const ParentObject& aParentObject)
|
|
|
|
{
|
|
|
|
return aParentObject.mWrapperCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2012-10-09 18:50:26 +00:00
|
|
|
inline T*
|
2012-05-10 19:24:59 +00:00
|
|
|
GetParentPointer(T* aObject)
|
|
|
|
{
|
2012-10-09 18:50:26 +00:00
|
|
|
return aObject;
|
2012-05-10 19:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsISupports*
|
|
|
|
GetParentPointer(const ParentObject& aObject)
|
|
|
|
{
|
2012-10-09 18:50:26 +00:00
|
|
|
return aObject.mObject;
|
2012-05-10 19:24:59 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 13:46:20 +00: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);
|
|
|
|
}
|
|
|
|
|
2012-11-09 18:59:02 +00: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);
|
|
|
|
|
2012-08-08 05:26:18 +00:00
|
|
|
// Can only be called with the immediate prototype of the instance object. Can
|
|
|
|
// only be called on the prototype of an object known to be a DOM instance.
|
|
|
|
JSBool
|
2013-06-21 13:12:46 +00:00
|
|
|
InstanceClassHasProtoAtDepth(JS::Handle<JSObject*> protoObject, uint32_t protoID,
|
2012-08-08 05:26:18 +00:00
|
|
|
uint32_t depth);
|
|
|
|
|
2012-03-31 04:42:20 +00: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 21:33:42 +00:00
|
|
|
XPCOMObjectToJsval(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
xpcObjectHelper& helper, const nsIID* iid,
|
|
|
|
bool allowNativeWrapper, JS::Value* rval);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2013-04-04 02:22:16 +00:00
|
|
|
// Special-cased wrapping for variants
|
|
|
|
bool
|
2013-04-29 21:33:42 +00:00
|
|
|
VariantToJsval(JSContext* aCx, JS::Handle<JSObject*> aScope,
|
|
|
|
nsIVariant* aVariant, JS::Value* aRetval);
|
2013-04-04 02:22:16 +00:00
|
|
|
|
2012-11-09 16:24:32 +00: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-31 04:42:20 +00:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject(JSContext* cx, JS::Handle<JSObject*> scope, T* p,
|
2013-06-08 02:45:46 +00:00
|
|
|
nsWrapperCache* cache, const nsIID* iid,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
if (xpc_FastGetCachedWrapper(cache, scope, rval.address()))
|
2012-03-31 04:42:20 +00:00
|
|
|
return true;
|
|
|
|
qsObjectHelper helper(p, cache);
|
2013-06-08 02:45:46 +00:00
|
|
|
return XPCOMObjectToJsval(cx, scope, helper, iid, true, rval.address());
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 02:22:16 +00:00
|
|
|
// A specialization of the above for nsIVariant, because that needs to
|
|
|
|
// do something different.
|
|
|
|
template<>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject<nsIVariant>(JSContext* cx, JS::Handle<JSObject*> scope, nsIVariant* p,
|
2013-06-08 02:45:46 +00:00
|
|
|
nsWrapperCache* cache, const nsIID* iid,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2013-04-04 02:22:16 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(iid);
|
|
|
|
MOZ_ASSERT(iid->Equals(NS_GET_IID(nsIVariant)));
|
2013-06-08 02:45:46 +00:00
|
|
|
return VariantToJsval(cx, scope, p, rval.address());
|
2013-04-04 02:22:16 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00: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-31 04:42:20 +00:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject(JSContext* cx, JS::Handle<JSObject*> scope, T* p, const nsIID* iid,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapObject(cx, scope, p, GetWrapperCache(p), iid, rval);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00: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-31 04:42:20 +00:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-06-08 02:45:46 +00:00
|
|
|
WrapObject(JSContext* cx, JS::Handle<JSObject*> scope, T* p,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapObject(cx, scope, p, NULL, rval);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Helper to make it possible to wrap directly out of an nsCOMPtr
|
2012-03-31 04:42:20 +00:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject(JSContext* cx, JS::Handle<JSObject*> scope, const nsCOMPtr<T>& p,
|
2013-06-08 02:45:46 +00:00
|
|
|
const nsIID* iid, JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapObject(cx, scope, p.get(), iid, rval);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Helper to make it possible to wrap directly out of an nsCOMPtr
|
2012-03-31 04:42:20 +00:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject(JSContext* cx, JS::Handle<JSObject*> scope, const nsCOMPtr<T>& p,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapObject(cx, scope, p, NULL, rval);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Helper to make it possible to wrap directly out of an nsRefPtr
|
2012-03-31 04:42:20 +00:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject(JSContext* cx, JS::Handle<JSObject*> scope, const nsRefPtr<T>& p,
|
2013-06-08 02:45:46 +00:00
|
|
|
const nsIID* iid, JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapObject(cx, scope, p.get(), iid, rval);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Helper to make it possible to wrap directly out of an nsRefPtr
|
2012-03-31 04:42:20 +00:00
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject(JSContext* cx, JS::Handle<JSObject*> scope, const nsRefPtr<T>& p,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapObject(cx, scope, p, NULL, rval);
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Specialization to make it easy to use WrapObject in codegen.
|
2012-03-31 04:42:20 +00:00
|
|
|
template<>
|
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject<JSObject>(JSContext* cx, JS::Handle<JSObject*> scope, JSObject* p,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
rval.set(JS::ObjectOrNullValue(p));
|
2012-03-31 04:42:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-09 11:34:29 +00:00
|
|
|
inline bool
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapObject(JSContext* cx, JS::Handle<JSObject*> scope, JSObject& p,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2013-03-09 11:34:29 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
rval.set(JS::ObjectValue(p));
|
2013-03-09 11:34:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00: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 07:33:18 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
2013-04-29 21:33:42 +00:00
|
|
|
WrapNativeISupportsParent(JSContext* cx, JS::Handle<JSObject*> scope, T* p,
|
2012-10-19 07:33:18 +00:00
|
|
|
nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
qsObjectHelper helper(ToSupports(p), cache);
|
2013-05-03 23:29:09 +00:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
|
|
|
return XPCOMObjectToJsval(cx, scope, helper, nullptr, false, v.address()) ?
|
2012-10-19 07:33:18 +00:00
|
|
|
JSVAL_TO_OBJECT(v) :
|
|
|
|
nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
|
|
|
|
// Fallback for when our parent is not a WebIDL binding object.
|
2012-10-19 07:33:18 +00:00
|
|
|
template<typename T, bool isISupports=IsISupports<T>::Value >
|
|
|
|
struct WrapNativeParentFallback
|
|
|
|
{
|
2013-04-29 21:33:42 +00:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
T* parent, nsWrapperCache* cache)
|
2012-10-19 07:33:18 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Fallback for when our parent is not a WebIDL binding object but _is_ an
|
|
|
|
// nsISupports object.
|
2012-10-19 07:33:18 +00:00
|
|
|
template<typename T >
|
|
|
|
struct WrapNativeParentFallback<T, true >
|
|
|
|
{
|
2013-04-29 21:33:42 +00:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
T* parent, nsWrapperCache* cache)
|
2012-10-19 07:33:18 +00:00
|
|
|
{
|
|
|
|
return WrapNativeISupportsParent(cx, scope, parent, cache);
|
|
|
|
}
|
2012-10-09 18:50:26 +00:00
|
|
|
};
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Wrapping of our native parent, for cases when it's a WebIDL object (though
|
|
|
|
// possibly preffed off).
|
2012-10-09 18:50:26 +00:00
|
|
|
template<typename T, bool hasWrapObject=HasWrapObject<T>::Value >
|
|
|
|
struct WrapNativeParentHelper
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2013-04-25 16:29:53 +00:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
T* parent, nsWrapperCache* cache)
|
2012-10-09 18:50:26 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(cache);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-10-09 18:50:26 +00:00
|
|
|
JSObject* obj;
|
|
|
|
if ((obj = cache->GetWrapper())) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2012-11-27 09:20:04 +00:00
|
|
|
// Inline this here while we have non-dom objects in wrapper caches.
|
|
|
|
if (!CouldBeDOMBinding(parent)) {
|
2013-03-11 23:03:47 +00:00
|
|
|
obj = WrapNativeParentFallback<T>::Wrap(cx, scope, parent, cache);
|
2012-11-27 09:20:04 +00:00
|
|
|
} else {
|
2013-03-11 23:03:47 +00:00
|
|
|
obj = parent->WrapObject(cx, scope);
|
2012-11-27 09:20:04 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 07:33:18 +00:00
|
|
|
return obj;
|
2012-10-09 18:50:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-09 16:24:32 +00: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 18:50:26 +00:00
|
|
|
template<typename T>
|
2012-10-19 07:33:18 +00:00
|
|
|
struct WrapNativeParentHelper<T, false >
|
2012-10-09 18:50:26 +00:00
|
|
|
{
|
2013-04-25 16:29:53 +00:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
T* parent, nsWrapperCache* cache)
|
2012-10-09 18:50:26 +00:00
|
|
|
{
|
|
|
|
JSObject* obj;
|
|
|
|
if (cache && (obj = cache->GetWrapper())) {
|
2012-03-31 04:42:20 +00:00
|
|
|
#ifdef DEBUG
|
2012-10-19 07:33:18 +00:00
|
|
|
NS_ASSERTION(WrapNativeISupportsParent(cx, scope, parent, cache) == obj,
|
2012-10-09 18:50:26 +00:00
|
|
|
"Unexpected object in nsWrapperCache");
|
2012-03-31 04:42:20 +00:00
|
|
|
#endif
|
2012-10-09 18:50:26 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2012-10-19 07:33:18 +00:00
|
|
|
return WrapNativeISupportsParent(cx, scope, parent, cache);
|
2012-10-09 18:50:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Wrapping of our native parent.
|
2012-10-09 18:50:26 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
2013-04-25 16:29:53 +00:00
|
|
|
WrapNativeParent(JSContext* cx, JS::Handle<JSObject*> scope, T* p,
|
|
|
|
nsWrapperCache* cache)
|
2012-10-09 18:50:26 +00:00
|
|
|
{
|
|
|
|
if (!p) {
|
|
|
|
return scope;
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 18:50:26 +00:00
|
|
|
return WrapNativeParentHelper<T>::Wrap(cx, scope, p, cache);
|
|
|
|
}
|
|
|
|
|
2012-11-09 16:24:32 +00:00
|
|
|
// Wrapping of our native parent, when we don't want to explicitly pass in
|
|
|
|
// things like the nsWrapperCache for it.
|
2012-10-09 18:50:26 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
2013-04-25 16:29:53 +00:00
|
|
|
WrapNativeParent(JSContext* cx, JS::Handle<JSObject*> scope, const T& p)
|
2012-10-09 18:50:26 +00:00
|
|
|
{
|
|
|
|
return WrapNativeParent(cx, scope, GetParentPointer(p), GetWrapperCache(p));
|
2012-03-31 04:42:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 21:33:41 +00: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 14:17:46 +00:00
|
|
|
HAS_MEMBER(GetParentObject)
|
|
|
|
|
|
|
|
template<typename T, bool WrapperCached=HasGetParentObjectMember<T>::Value>
|
|
|
|
struct GetParentObject
|
|
|
|
{
|
2013-04-25 16:29:53 +00:00
|
|
|
static JSObject* Get(JSContext* cx, JS::Handle<JSObject*> obj)
|
2012-09-26 14:17:46 +00:00
|
|
|
{
|
|
|
|
T* native = UnwrapDOMObject<T>(obj);
|
2013-04-29 21:33:41 +00:00
|
|
|
return
|
|
|
|
GetRealParentObject(native,
|
|
|
|
WrapNativeParent(cx, obj, native->GetParentObject()));
|
2012-09-26 14:17:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct GetParentObject<T, false>
|
|
|
|
{
|
2013-04-25 16:29:53 +00:00
|
|
|
static JSObject* Get(JSContext* cx, JS::Handle<JSObject*> obj)
|
2012-09-26 14:17:46 +00:00
|
|
|
{
|
|
|
|
MOZ_CRASH();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-06 09:42:17 +00:00
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
JSObject* GetJSObjectFromCallback(CallbackObject* callback)
|
|
|
|
{
|
|
|
|
return callback->Callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
JSObject* GetJSObjectFromCallback(void* noncallback)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-09 15:43:57 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
2013-04-25 16:29:53 +00:00
|
|
|
WrapCallThisObject(JSContext* cx, JS::Handle<JSObject*> scope, const T& p)
|
2012-11-09 15:43:57 +00:00
|
|
|
{
|
2013-02-06 09:42:17 +00: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 09:12:45 +00:00
|
|
|
JS::Rooted<JSObject*> obj(cx, GetJSObjectFromCallback(p));
|
2012-11-09 15:43:57 +00:00
|
|
|
if (!obj) {
|
2013-02-06 09:42:17 +00:00
|
|
|
// WrapNativeParent is a bit of a Swiss army knife that will
|
|
|
|
// wrap anything for us.
|
|
|
|
obj = WrapNativeParent(cx, scope, p);
|
|
|
|
if (!obj) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-11-09 15:43:57 +00:00
|
|
|
}
|
|
|
|
|
2013-02-06 09:42:17 +00:00
|
|
|
// But all that won't necessarily put things in the compartment of cx.
|
2013-05-02 09:12:45 +00:00
|
|
|
if (!JS_WrapObject(cx, obj.address())) {
|
2012-11-09 15:43:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2012-11-09 15:43:58 +00:00
|
|
|
// Helper for calling WrapNewBindingObject with smart pointers
|
|
|
|
// (nsAutoPtr/nsRefPtr/nsCOMPtr) or references.
|
|
|
|
template <class T, bool isSmartPtr=HasgetMember<T>::Value>
|
|
|
|
struct WrapNewBindingObjectHelper
|
|
|
|
{
|
2013-04-25 16:29:53 +00:00
|
|
|
static inline bool Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
|
2013-06-08 02:45:46 +00:00
|
|
|
const T& value, JS::MutableHandle<JS::Value> rval)
|
2012-11-09 15:43:58 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapNewBindingObject(cx, scope, value.get(), rval);
|
2012-11-09 15:43:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct WrapNewBindingObjectHelper<T, false>
|
|
|
|
{
|
2013-04-25 16:29:53 +00:00
|
|
|
static inline bool Wrap(JSContext* cx, JS::Handle<JSObject*> scope, T& value,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-11-09 15:43:58 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapNewBindingObject(cx, scope, &value, rval);
|
2012-11-09 15:43:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
2013-04-25 16:29:53 +00:00
|
|
|
WrapNewBindingObject(JSContext* cx, JS::Handle<JSObject*> scope, T& value,
|
2013-06-08 02:45:46 +00:00
|
|
|
JS::MutableHandle<JS::Value> rval)
|
2012-11-09 15:43:58 +00:00
|
|
|
{
|
2013-06-08 02:45:46 +00:00
|
|
|
return WrapNewBindingObjectHelper<T>::Wrap(cx, scope, value, rval);
|
2012-11-09 15:43:58 +00:00
|
|
|
}
|
|
|
|
|
2013-01-28 13:34:31 +00: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 14:22:05 +00: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-31 04:42:20 +00:00
|
|
|
// Spec needs a name property
|
|
|
|
template <typename Spec>
|
|
|
|
static bool
|
2013-04-03 00:20:38 +00:00
|
|
|
InitIds(JSContext* cx, const Prefable<Spec>* prefableSpecs, jsid* ids)
|
2012-03-31 04:42:20 +00:00
|
|
|
{
|
2012-05-23 16:44:48 +00:00
|
|
|
MOZ_ASSERT(prefableSpecs);
|
|
|
|
MOZ_ASSERT(prefableSpecs->specs);
|
2012-03-31 04:42:20 +00:00
|
|
|
do {
|
2012-05-23 16:44:48 +00: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 14:22:05 +00:00
|
|
|
if (!InternJSString(cx, *ids, spec->name)) {
|
2012-05-23 16:44:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (++ids, (++spec)->name);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-05-23 16:44:48 +00:00
|
|
|
// We ran out of ids for that pref. Put a JSID_VOID in on the id
|
|
|
|
// corresponding to the list terminator for the pref.
|
|
|
|
*ids = JSID_VOID;
|
|
|
|
++ids;
|
|
|
|
} while ((++prefableSpecs)->specs);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
QueryInterface(JSContext* cx, unsigned argc, JS::Value* vp);
|
2012-05-03 04:35:38 +00:00
|
|
|
JSBool
|
|
|
|
ThrowingConstructor(JSContext* cx, unsigned argc, JS::Value* vp);
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2012-05-22 13:46:20 +00:00
|
|
|
bool
|
2013-05-03 23:29:08 +00:00
|
|
|
GetPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id, bool* found,
|
2012-05-22 13:46:20 +00:00
|
|
|
JS::Value* vp);
|
|
|
|
|
|
|
|
bool
|
2013-05-03 23:29:08 +00:00
|
|
|
HasPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
DOMProxyHandler* handler,
|
|
|
|
JS::Handle<jsid> id);
|
2012-05-22 13:46:20 +00:00
|
|
|
|
2012-05-15 18:23:29 +00:00
|
|
|
template<class T>
|
|
|
|
class OwningNonNull
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OwningNonNull()
|
|
|
|
#ifdef DEBUG
|
|
|
|
: inited(false)
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
|
|
|
operator T&() {
|
|
|
|
MOZ_ASSERT(inited);
|
|
|
|
MOZ_ASSERT(ptr, "OwningNonNull<T> was set to null");
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(T* t) {
|
|
|
|
init(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(const already_AddRefed<T>& t) {
|
|
|
|
init(t);
|
|
|
|
}
|
|
|
|
|
2012-11-09 15:43:58 +00:00
|
|
|
already_AddRefed<T> forget() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
inited = false;
|
|
|
|
#endif
|
|
|
|
return ptr.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-09 15:43:58 +00:00
|
|
|
// Make us work with smart-ptr helpers that expect a get()
|
|
|
|
T* get() const {
|
|
|
|
MOZ_ASSERT(inited);
|
|
|
|
MOZ_ASSERT(ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2012-05-15 18:23:29 +00:00
|
|
|
protected:
|
|
|
|
template<typename U>
|
|
|
|
void init(U t) {
|
|
|
|
ptr = t;
|
|
|
|
MOZ_ASSERT(ptr);
|
|
|
|
#ifdef DEBUG
|
|
|
|
inited = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<T> ptr;
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool inited;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2012-07-17 16:18:53 +00:00
|
|
|
// A struct that has the same layout as an nsDependentString but much
|
|
|
|
// faster constructor and destructor behavior
|
|
|
|
struct FakeDependentString {
|
|
|
|
FakeDependentString() :
|
|
|
|
mFlags(nsDependentString::F_TERMINATED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetData(const nsDependentString::char_type* aData,
|
|
|
|
nsDependentString::size_type aLength) {
|
|
|
|
MOZ_ASSERT(mFlags == nsDependentString::F_TERMINATED);
|
|
|
|
mData = aData;
|
|
|
|
mLength = aLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Truncate() {
|
2012-07-17 17:20:40 +00:00
|
|
|
mData = nsDependentString::char_traits::sEmptyBuffer;
|
2012-07-17 16:18:53 +00:00
|
|
|
mLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
Truncate();
|
|
|
|
mFlags |= nsDependentString::F_VOIDED;
|
|
|
|
}
|
|
|
|
|
2013-02-06 09:56:15 +00:00
|
|
|
// If this ever changes, change the corresponding code in the
|
|
|
|
// Optional<nsAString> specialization as well.
|
2012-07-17 16:18:53 +00:00
|
|
|
const nsAString* ToAStringPtr() const {
|
|
|
|
return reinterpret_cast<const nsDependentString*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAString* ToAStringPtr() {
|
|
|
|
return reinterpret_cast<nsDependentString*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const nsAString& () const {
|
|
|
|
return *reinterpret_cast<const nsDependentString*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const nsDependentString::char_type* mData;
|
|
|
|
nsDependentString::size_type mLength;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mFlags;
|
2012-07-17 16:18:53 +00:00
|
|
|
|
|
|
|
// A class to use for our static asserts to ensure our object layout
|
|
|
|
// matches that of nsDependentString.
|
|
|
|
class DependentStringAsserter;
|
|
|
|
friend class DependentStringAsserter;
|
|
|
|
|
|
|
|
class DepedentStringAsserter : public nsDependentString {
|
|
|
|
public:
|
|
|
|
static void StaticAsserts() {
|
|
|
|
MOZ_STATIC_ASSERT(sizeof(FakeDependentString) == sizeof(nsDependentString),
|
|
|
|
"Must have right object size");
|
|
|
|
MOZ_STATIC_ASSERT(offsetof(FakeDependentString, mData) ==
|
|
|
|
offsetof(DepedentStringAsserter, mData),
|
|
|
|
"Offset of mData should match");
|
|
|
|
MOZ_STATIC_ASSERT(offsetof(FakeDependentString, mLength) ==
|
|
|
|
offsetof(DepedentStringAsserter, mLength),
|
|
|
|
"Offset of mLength should match");
|
|
|
|
MOZ_STATIC_ASSERT(offsetof(FakeDependentString, mFlags) ==
|
|
|
|
offsetof(DepedentStringAsserter, mFlags),
|
|
|
|
"Offset of mFlags should match");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-05-18 21:25:46 +00:00
|
|
|
enum StringificationBehavior {
|
|
|
|
eStringify,
|
|
|
|
eEmpty,
|
|
|
|
eNull
|
|
|
|
};
|
|
|
|
|
2012-07-31 04:22:23 +00:00
|
|
|
// pval must not be null and must point to a rooted JS::Value
|
2012-05-18 21:25:46 +00:00
|
|
|
static inline bool
|
2013-05-24 13:16:00 +00:00
|
|
|
ConvertJSValueToString(JSContext* cx, JS::Handle<JS::Value> v,
|
|
|
|
JS::MutableHandle<JS::Value> pval,
|
2012-05-18 21:25:46 +00:00
|
|
|
StringificationBehavior nullBehavior,
|
|
|
|
StringificationBehavior undefinedBehavior,
|
2012-07-17 16:18:53 +00:00
|
|
|
FakeDependentString& result)
|
2012-05-18 21:25:46 +00: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-31 04:22:23 +00:00
|
|
|
if (behavior != eStringify) {
|
2012-07-17 16:18:53 +00:00
|
|
|
if (behavior == eEmpty) {
|
|
|
|
result.Truncate();
|
|
|
|
} else {
|
|
|
|
result.SetNull();
|
|
|
|
}
|
2012-05-18 21:25:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = JS_ValueToString(cx, v);
|
|
|
|
if (!s) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-24 13:16:00 +00:00
|
|
|
pval.set(JS::StringValue(s)); // Root the new string.
|
2012-05-18 21:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
const jschar *chars = JS_GetStringCharsZAndLength(cx, s, &len);
|
|
|
|
if (!chars) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-17 16:18:53 +00:00
|
|
|
result.SetData(chars, len);
|
2012-05-18 21:25:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-13 05:18:35 +00:00
|
|
|
bool
|
|
|
|
ConvertJSValueToByteString(JSContext* cx, JS::Handle<JS::Value> v,
|
|
|
|
JS::MutableHandle<JS::Value> pval, bool nullable,
|
|
|
|
nsACString& result);
|
|
|
|
|
2012-05-18 21:25:47 +00:00
|
|
|
// Class for holding the type of members of a union. The union type has an enum
|
|
|
|
// to keep track of which of its UnionMembers has been constructed.
|
|
|
|
template<class T>
|
|
|
|
class UnionMember {
|
|
|
|
AlignedStorage2<T> storage;
|
|
|
|
|
|
|
|
public:
|
|
|
|
T& SetValue() {
|
|
|
|
new (storage.addr()) T();
|
|
|
|
return *storage.addr();
|
|
|
|
}
|
2013-05-16 16:36:55 +00:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
T& SetValue(const T1 &t1, const T2 &t2)
|
|
|
|
{
|
|
|
|
new (storage.addr()) T(t1, t2);
|
|
|
|
return *storage.addr();
|
|
|
|
}
|
2012-05-18 21:25:47 +00:00
|
|
|
const T& Value() const {
|
|
|
|
return *storage.addr();
|
|
|
|
}
|
|
|
|
void Destroy() {
|
|
|
|
storage.addr()->~T();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 16:36:55 +00:00
|
|
|
template<typename T>
|
|
|
|
void DoTraceSequence(JSTracer* trc, FallibleTArray<T>& seq);
|
2013-05-16 16:36:56 +00:00
|
|
|
template<typename T>
|
|
|
|
void DoTraceSequence(JSTracer* trc, InfallibleTArray<T>& seq);
|
2013-05-16 16:36:55 +00:00
|
|
|
|
2013-03-02 06:07:43 +00:00
|
|
|
// Class for simple sequence arguments, only used internally by codegen.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 16:36:55 +00:00
|
|
|
// Class used to trace sequences, with specializations for various
|
|
|
|
// sequence types.
|
|
|
|
template<typename T, bool isDictionary=IsBaseOf<DictionaryBase, T>::value>
|
|
|
|
class SequenceTracer
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
};
|
|
|
|
|
|
|
|
// sequence<object> or sequence<object?>
|
|
|
|
template<>
|
|
|
|
class SequenceTracer<JSObject*, false>
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, JSObject** objp, JSObject** end) {
|
|
|
|
for ( ; objp != end; ++objp) {
|
|
|
|
JS_CallObjectTracer(trc, objp, "sequence<object>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// sequence<any>
|
|
|
|
template<>
|
|
|
|
class SequenceTracer<JS::Value, false>
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, JS::Value* valp, JS::Value* end) {
|
|
|
|
for ( ; valp != end; ++valp) {
|
|
|
|
JS_CallValueTracer(trc, valp, "sequence<any>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// sequence<sequence<T>>
|
|
|
|
template<typename T>
|
|
|
|
class SequenceTracer<Sequence<T>, false>
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, Sequence<T>* seqp, Sequence<T>* end) {
|
|
|
|
for ( ; seqp != end; ++seqp) {
|
|
|
|
DoTraceSequence(trc, *seqp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 16:36:56 +00:00
|
|
|
// sequence<sequence<T>> as return value
|
|
|
|
template<typename T>
|
|
|
|
class SequenceTracer<nsTArray<T>, false>
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, nsTArray<T>* seqp, nsTArray<T>* end) {
|
|
|
|
for ( ; seqp != end; ++seqp) {
|
|
|
|
DoTraceSequence(trc, *seqp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-16 16:36:55 +00:00
|
|
|
// sequence<someDictionary>
|
|
|
|
template<typename T>
|
|
|
|
class SequenceTracer<T, true>
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, T* dictp, T* end) {
|
|
|
|
for ( ; dictp != end; ++dictp) {
|
|
|
|
dictp->TraceDictionary(trc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// sequence<sequence<T>?>
|
|
|
|
template<typename T>
|
|
|
|
class SequenceTracer<Nullable<Sequence<T> >, false>
|
|
|
|
{
|
|
|
|
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void TraceSequence(JSTracer* trc, Nullable<Sequence<T> >* seqp,
|
|
|
|
Nullable<Sequence<T> >* end) {
|
|
|
|
for ( ; seqp != end; ++seqp) {
|
|
|
|
if (!seqp->IsNull()) {
|
|
|
|
DoTraceSequence(trc, seqp->Value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-18 01:48:25 +00: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 16:36:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-18 01:48:25 +00: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 16:36:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-18 01:48:25 +00: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 16:36:55 +00:00
|
|
|
{
|
2013-05-16 16:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-16 16:36:55 +00:00
|
|
|
enum SequenceType {
|
|
|
|
eInfallibleArray,
|
2013-05-16 16:36:56 +00:00
|
|
|
eFallibleArray,
|
|
|
|
eNullableArray
|
2013-05-16 16:36:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual void trace(JSTracer *trc) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (mSequenceType == eFallibleArray) {
|
|
|
|
DoTraceSequence(trc, *mFallibleArray);
|
|
|
|
} else if (mSequenceType == eInfallibleArray) {
|
|
|
|
DoTraceSequence(trc, *mInfallibleArray);
|
2013-05-18 01:48:25 +00:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mSequenceType == eNullableArray);
|
2013-05-16 16:36:56 +00:00
|
|
|
if (!mNullableArray->IsNull()) {
|
|
|
|
DoTraceSequence(trc, mNullableArray->Value());
|
|
|
|
}
|
2013-05-16 16:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
|
|
|
InfallibleTArray<T>* mInfallibleArray;
|
|
|
|
FallibleTArray<T>* mFallibleArray;
|
2013-05-16 16:36:56 +00:00
|
|
|
Nullable<nsTArray<T> >* mNullableArray;
|
2013-05-16 16:36:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SequenceType mSequenceType;
|
|
|
|
};
|
|
|
|
|
2013-05-16 16:36:55 +00:00
|
|
|
template<typename T>
|
2013-05-18 01:48:25 +00:00
|
|
|
class MOZ_STACK_CLASS RootedDictionary : public T,
|
|
|
|
private JS::CustomAutoRooter
|
2013-05-16 16:36:55 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-05-18 01:48:25 +00:00
|
|
|
RootedDictionary(JSContext* cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
|
|
|
|
T(),
|
|
|
|
JS::CustomAutoRooter(cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT)
|
2013-05-16 16:36:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-18 01:48:25 +00:00
|
|
|
virtual void trace(JSTracer *trc) MOZ_OVERRIDE
|
2013-05-16 16:36:55 +00:00
|
|
|
{
|
2013-05-18 01:48:25 +00:00
|
|
|
this->TraceDictionary(trc);
|
2013-05-16 16:36:55 +00:00
|
|
|
}
|
2013-05-18 01:48:25 +00:00
|
|
|
};
|
2013-05-16 16:36:55 +00:00
|
|
|
|
2013-05-18 01:48:25 +00:00
|
|
|
template<typename T>
|
|
|
|
class MOZ_STACK_CLASS NullableRootedDictionary : public Nullable<T>,
|
|
|
|
private JS::CustomAutoRooter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NullableRootedDictionary(JSContext* cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
|
|
|
|
Nullable<T>(),
|
|
|
|
JS::CustomAutoRooter(cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT)
|
|
|
|
{
|
|
|
|
}
|
2013-05-16 16:36:55 +00:00
|
|
|
|
2013-05-18 01:48:25 +00:00
|
|
|
virtual void trace(JSTracer *trc) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (!this->IsNull()) {
|
|
|
|
this->Value().TraceDictionary(trc);
|
2013-05-16 16:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-09 18:50:27 +00: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) &&
|
|
|
|
InternJSString(cx, vector[vector.length() - 1], name);
|
|
|
|
}
|
|
|
|
|
2012-07-18 11:52:07 +00:00
|
|
|
// Implementation of the bits that XrayWrapper needs
|
2012-10-09 18:50:27 +00: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 11:52:07 +00:00
|
|
|
bool
|
2013-05-03 23:29:08 +00:00
|
|
|
XrayResolveOwnProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id,
|
|
|
|
JSPropertyDescriptor* desc, unsigned flags);
|
2012-07-18 11:52:07 +00:00
|
|
|
|
2012-10-09 18:50:27 +00: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 11:52:07 +00:00
|
|
|
bool
|
2013-05-03 23:29:08 +00:00
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id, JSPropertyDescriptor* desc);
|
2012-07-18 11:52:07 +00:00
|
|
|
|
2013-05-29 09:38:57 +00: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
|
|
|
|
XrayDefineProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
|
|
|
JSPropertyDescriptor* desc, bool* defined);
|
|
|
|
|
2012-10-09 18:50:27 +00: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 16:58:03 +00:00
|
|
|
* flags are JSITER_* flags.
|
2012-10-09 18:50:27 +00:00
|
|
|
*/
|
|
|
|
bool
|
2013-05-03 23:29:08 +00:00
|
|
|
XrayEnumerateProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2012-11-05 16:58:03 +00:00
|
|
|
unsigned flags, JS::AutoIdVector& props);
|
2012-10-09 18:50:27 +00: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
|
|
|
|
// JSNativeHolder. The CONSTRUCTOR_XRAY_EXPANDO_SLOT is used to store the
|
|
|
|
// expando chain of the Xray for the interface object.
|
|
|
|
// Note that some interface objects are not yet a JSFunction but a normal
|
|
|
|
// JSObject with a DOMJSClass, those do not use these slots.
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT = 0,
|
|
|
|
CONSTRUCTOR_XRAY_EXPANDO_SLOT
|
|
|
|
};
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
Constructor(JSContext* cx, unsigned argc, JS::Value* vp);
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
UseDOMXray(JSObject* obj)
|
|
|
|
{
|
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
return IsDOMClass(clasp) ||
|
|
|
|
IsDOMProxy(obj, 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 13:02:37 +00: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-05 16:59:23 +00:00
|
|
|
// Set the chain of expando objects for various consumers of the given object.
|
|
|
|
// For Paris Bindings only. See the relevant infrastructure in XrayWrapper.cpp.
|
|
|
|
JSObject* GetXrayExpandoChain(JSObject *obj);
|
|
|
|
void SetXrayExpandoChain(JSObject *obj, JSObject *chain);
|
|
|
|
|
2012-10-09 18:50:27 +00: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.
|
|
|
|
* pre is a string that should be prefixed to the value.
|
|
|
|
* post is a string that should be prefixed to the value.
|
|
|
|
* v contains the JSString for the value if the function returns true.
|
|
|
|
*/
|
|
|
|
bool
|
2013-05-03 23:29:08 +00:00
|
|
|
NativeToString(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, const char* pre,
|
2012-10-09 18:50:27 +00:00
|
|
|
const char* post, JS::Value* v);
|
|
|
|
|
2013-03-18 01:47:12 +00: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 23:03:06 +00:00
|
|
|
// Helpers for creating a const version of a type.
|
|
|
|
template<typename T>
|
|
|
|
const T& Constify(T& arg)
|
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reparent the wrapper of aObj to whatever its native now thinks its
|
|
|
|
// parent should be.
|
2012-09-26 14:17:46 +00:00
|
|
|
nsresult
|
2013-04-15 17:32:55 +00:00
|
|
|
ReparentWrapper(JSContext* aCx, JS::HandleObject aObj);
|
2012-09-26 14:17:46 +00:00
|
|
|
|
2013-01-08 18:05:36 +00:00
|
|
|
/**
|
|
|
|
* Used to implement the hasInstance hook of an interface object.
|
|
|
|
*
|
|
|
|
* instance should not be a security wrapper.
|
|
|
|
*/
|
|
|
|
JSBool
|
2013-05-03 23:29:08 +00:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-01-08 18:05:36 +00:00
|
|
|
JSBool* bp);
|
|
|
|
JSBool
|
2013-06-21 13:12:46 +00:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj, JS::MutableHandle<JS::Value> vp,
|
2013-01-08 18:05:36 +00:00
|
|
|
JSBool* bp);
|
|
|
|
|
2013-05-08 19:50:58 +00:00
|
|
|
// Helper for lenient getters/setters to report to console. If this
|
|
|
|
// returns false, we couldn't even get a global.
|
|
|
|
bool
|
2013-04-08 21:04:21 +00:00
|
|
|
ReportLenientThisUnwrappingFailure(JSContext* cx, JS::Handle<JSObject*> obj);
|
|
|
|
|
2012-12-20 09:56:11 +00:00
|
|
|
inline JSObject*
|
|
|
|
GetUnforgeableHolder(JSObject* aGlobal, prototypes::ID aId)
|
|
|
|
{
|
|
|
|
JSObject** protoAndIfaceArray = GetProtoAndIfaceArray(aGlobal);
|
|
|
|
JSObject* interfaceProto = protoAndIfaceArray[aId];
|
|
|
|
return &js::GetReservedSlot(interfaceProto,
|
|
|
|
DOM_INTERFACE_PROTO_SLOTS_BASE).toObject();
|
|
|
|
}
|
|
|
|
|
2013-05-20 17:47:08 +00: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);
|
|
|
|
|
2013-06-01 06:56:23 +00:00
|
|
|
already_AddRefed<nsPIDOMWindow>
|
|
|
|
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
|
|
|
|
const GlobalObject& aGlobal,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-06-13 05:18:35 +00: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);
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-05-03 04:35:38 +00:00
|
|
|
#endif /* mozilla_dom_BindingUtils_h__ */
|