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
|
|
|
|
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"
|
2012-09-19 06:02:37 -07:00
|
|
|
#include "mozilla/dom/NonRefcountedDOMObject.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"
|
2012-03-30 21:42:20 -07:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2012-01-14 09:43:00 -08:00
|
|
|
#include "jsfriendapi.h"
|
2012-05-18 01:29:40 -07:00
|
|
|
#include "jswrapper.h"
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-05-18 01:29:40 -07:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "qsObjectHelper.h"
|
|
|
|
#include "xpcpublic.h"
|
2012-03-30 21:42:20 -07:00
|
|
|
#include "nsTraceRefcnt.h"
|
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-06-13 08:14:15 -07:00
|
|
|
#include "mozilla/Likely.h"
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
// nsGlobalWindow implements nsWrapperCache, but doesn't always use it. Don't
|
|
|
|
// try to use it without fixing that first.
|
|
|
|
class nsGlobalWindow;
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2012-07-18 03:36:08 -07:00
|
|
|
enum ErrNum {
|
|
|
|
#define MSG_DEF(_name, _argc, _str) \
|
|
|
|
_name,
|
|
|
|
#include "mozilla/dom/Errors.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
Err_Limit
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThrowErrorMessage(JSContext* aCx, const ErrNum aErrorNumber, ...);
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
template<bool mainThread>
|
|
|
|
inline bool
|
|
|
|
Throw(JSContext* cx, nsresult rv)
|
|
|
|
{
|
2012-04-24 16:50:00 -07:00
|
|
|
using mozilla::dom::workers::exceptions::ThrowDOMExceptionForNSResult;
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
// XXX Introduce exception machinery.
|
|
|
|
if (mainThread) {
|
2012-05-18 01:29:40 -07:00
|
|
|
xpc::Throw(cx, rv);
|
2012-03-30 21:42:20 -07:00
|
|
|
} else {
|
|
|
|
if (!JS_IsExceptionPending(cx)) {
|
2012-04-24 16:50:00 -07:00
|
|
|
ThrowDOMExceptionForNSResult(cx, rv);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool mainThread>
|
|
|
|
inline bool
|
2012-05-05 18:15:11 -07:00
|
|
|
ThrowMethodFailedWithDetails(JSContext* cx, const ErrorResult& rv,
|
2012-03-30 21:42:20 -07:00
|
|
|
const char* /* ifaceName */,
|
|
|
|
const char* /* memberName */)
|
|
|
|
{
|
2012-05-05 18:15:11 -07:00
|
|
|
return Throw<mainThread>(cx, rv.ErrorCode());
|
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
|
|
|
}
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
// It's ok for eRegularDOMObject and eProxyDOMObject to be the same, but
|
|
|
|
// eNonDOMObject should always be different from the other two. This enum
|
|
|
|
// shouldn't be used to differentiate between non-proxy and proxy bindings.
|
|
|
|
enum DOMObjectSlot {
|
|
|
|
eNonDOMObject = -1,
|
|
|
|
eRegularDOMObject = DOM_OBJECT_SLOT,
|
|
|
|
eProxyDOMObject = DOM_PROXY_OBJECT_SLOT
|
|
|
|
};
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
template <class T>
|
|
|
|
inline T*
|
2012-05-22 06:46:20 -07:00
|
|
|
UnwrapDOMObject(JSObject* obj, DOMObjectSlot slot)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2012-05-22 06:46:20 -07:00
|
|
|
MOZ_ASSERT(slot != eNonDOMObject,
|
|
|
|
"Don't pass non-DOM objects to this function");
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (IsDOMClass(js::GetObjectClass(obj))) {
|
|
|
|
MOZ_ASSERT(slot == eRegularDOMObject);
|
|
|
|
} else {
|
2012-10-13 05:50:24 -07:00
|
|
|
MOZ_ASSERT(IsDOMProxy(obj));
|
2012-05-22 06:46:20 -07:00
|
|
|
MOZ_ASSERT(slot == eProxyDOMObject);
|
|
|
|
}
|
|
|
|
#endif
|
2012-06-08 14:42:51 -07:00
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
JS::Value val = js::GetReservedSlot(obj, slot);
|
2012-03-30 21:42:20 -07:00
|
|
|
// XXXbz/khuey worker code tries to unwrap interface objects (which have
|
|
|
|
// nothing here). That needs to stop.
|
|
|
|
// XXX We don't null-check UnwrapObject's result; aren't we going to crash
|
|
|
|
// anyway?
|
|
|
|
if (val.isUndefined()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<T*>(val.toPrivate());
|
|
|
|
}
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
// Only use this with a new DOM binding object (either proxy or regular).
|
|
|
|
inline const DOMClass*
|
|
|
|
GetDOMClass(JSObject* obj)
|
|
|
|
{
|
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
if (IsDOMClass(clasp)) {
|
|
|
|
return &DOMJSClass::FromJSClass(clasp)->mClass;
|
|
|
|
}
|
|
|
|
|
2012-10-13 05:50:24 -07:00
|
|
|
MOZ_ASSERT(IsDOMProxy(obj));
|
2012-05-22 06:46:20 -07:00
|
|
|
js::BaseProxyHandler* handler = js::GetProxyHandler(obj);
|
|
|
|
return &static_cast<DOMProxyHandler*>(handler)->mClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DOMObjectSlot
|
|
|
|
GetDOMClass(JSObject* obj, const DOMClass*& result)
|
|
|
|
{
|
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
if (IsDOMClass(clasp)) {
|
|
|
|
result = &DOMJSClass::FromJSClass(clasp)->mClass;
|
|
|
|
return eRegularDOMObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (js::IsObjectProxyClass(clasp) || js::IsFunctionProxyClass(clasp)) {
|
|
|
|
js::BaseProxyHandler* handler = js::GetProxyHandler(obj);
|
2012-10-13 05:50:24 -07:00
|
|
|
if (handler->family() == ProxyFamily()) {
|
2012-05-22 06:46:20 -07:00
|
|
|
result = &static_cast<DOMProxyHandler*>(handler)->mClass;
|
|
|
|
return eProxyDOMObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return eNonDOMObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
UnwrapDOMObjectToISupports(JSObject* obj, nsISupports*& result)
|
|
|
|
{
|
|
|
|
const DOMClass* clasp;
|
|
|
|
DOMObjectSlot slot = GetDOMClass(obj, clasp);
|
|
|
|
if (slot == eNonDOMObject || !clasp->mDOMObjectIsISupports) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = UnwrapDOMObject<nsISupports>(obj, slot);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
IsDOMObject(JSObject* obj)
|
|
|
|
{
|
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
return IsDOMClass(clasp) ||
|
|
|
|
((js::IsObjectProxyClass(clasp) || js::IsFunctionProxyClass(clasp)) &&
|
2012-10-13 05:50:24 -07:00
|
|
|
js::GetProxyHandler(obj)->family() == ProxyFamily());
|
2012-05-22 06:46:20 -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>).
|
|
|
|
template <prototypes::ID PrototypeID, class T, typename U>
|
2012-03-30 21:42:20 -07:00
|
|
|
inline nsresult
|
2012-05-24 22:08:26 -07:00
|
|
|
UnwrapObject(JSContext* cx, JSObject* obj, U& value)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
|
|
|
/* First check to see whether we have a DOM object */
|
2012-05-22 06:46:20 -07:00
|
|
|
const DOMClass* domClass;
|
|
|
|
DOMObjectSlot slot = GetDOMClass(obj, domClass);
|
|
|
|
if (slot == eNonDOMObject) {
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-18 01:29:40 -07:00
|
|
|
obj = xpc::Unwrap(cx, obj, false);
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_XPC_SECURITY_MANAGER_VETO;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!js::IsWrapper(obj));
|
2012-05-22 06:46:20 -07:00
|
|
|
slot = GetDOMClass(obj, domClass);
|
|
|
|
if (slot == eNonDOMObject) {
|
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. */
|
|
|
|
if (domClass->mInterfaceChain[PrototypeTraits<PrototypeID>::Depth] ==
|
|
|
|
PrototypeID) {
|
2012-05-22 06:46:20 -07:00
|
|
|
value = UnwrapDOMObject<T>(obj, slot);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
IsArrayLike(JSContext* cx, JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(obj);
|
2012-05-15 11:23:29 -07:00
|
|
|
// For simplicity, check for security wrappers up front. In case we
|
|
|
|
// have a security wrapper, don't forget to enter the compartment of
|
|
|
|
// the underlying object after unwrapping.
|
2012-08-21 18:42:53 -07:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
2012-03-30 21:42:20 -07:00
|
|
|
if (js::IsWrapper(obj)) {
|
2012-05-18 01:29:40 -07:00
|
|
|
obj = xpc::Unwrap(cx, obj, false);
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!obj) {
|
|
|
|
// Let's say it's not
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-15 11:23:29 -07:00
|
|
|
|
2012-08-21 18:42:53 -07:00
|
|
|
ac.construct(cx, obj);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXXbz need to detect platform objects (including listbinding
|
|
|
|
// ones) with indexGetters here!
|
2012-05-15 11:23:29 -07:00
|
|
|
return JS_IsArrayObject(cx, obj) || JS_IsTypedArrayObject(obj, cx);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
IsPlatformObject(JSContext* cx, JSObject* obj)
|
|
|
|
{
|
|
|
|
// XXXbz Should be treating list-binding objects as platform objects
|
|
|
|
// too? The one consumer so far wants non-array-like platform
|
|
|
|
// objects, so listbindings that have an indexGetter should test
|
|
|
|
// false from here. Maybe this function should have a different
|
|
|
|
// name?
|
|
|
|
MOZ_ASSERT(obj);
|
|
|
|
// Fast-path the common case
|
|
|
|
JSClass* clasp = js::GetObjectJSClass(obj);
|
|
|
|
if (IsDOMClass(clasp)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Now for simplicity check for security wrappers before anything else
|
|
|
|
if (js::IsWrapper(obj)) {
|
2012-05-18 01:29:40 -07:00
|
|
|
obj = xpc::Unwrap(cx, obj, false);
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!obj) {
|
|
|
|
// Let's say it's not
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
clasp = js::GetObjectJSClass(obj);
|
|
|
|
}
|
|
|
|
return IS_WRAPPER_CLASS(js::Valueify(clasp)) || IsDOMClass(clasp) ||
|
2012-01-14 09:43:00 -08:00
|
|
|
JS_IsArrayBufferObject(obj, cx);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
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>).
|
|
|
|
template <class T, typename U>
|
2012-03-30 21:42:20 -07:00
|
|
|
inline nsresult
|
2012-05-24 22:08:26 -07:00
|
|
|
UnwrapObject(JSContext* cx, JSObject* obj, U& value)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
|
|
|
return UnwrapObject<static_cast<prototypes::ID>(
|
2012-06-13 08:14:15 -07:00
|
|
|
PrototypeIDMap<T>::PrototypeID), T>(cx, obj, value);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const size_t kProtoOrIfaceCacheCount =
|
|
|
|
prototypes::id::_ID_Count + constructors::id::_ID_Count;
|
|
|
|
|
|
|
|
inline void
|
|
|
|
AllocateProtoOrIfaceCache(JSObject* obj)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
JSObject** protoOrIfaceArray = new JSObject*[kProtoOrIfaceCacheCount]();
|
|
|
|
|
|
|
|
js::SetReservedSlot(obj, DOM_PROTOTYPE_SLOT,
|
|
|
|
JS::PrivateValue(protoOrIfaceArray));
|
|
|
|
}
|
|
|
|
|
2012-05-02 05:49:43 -07:00
|
|
|
inline void
|
|
|
|
TraceProtoOrIfaceCache(JSTracer* trc, JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
|
2012-06-18 17:04:38 -07:00
|
|
|
if (!HasProtoOrIfaceArray(obj))
|
|
|
|
return;
|
2012-05-02 05:49:43 -07:00
|
|
|
JSObject** protoOrIfaceArray = GetProtoOrIfaceArray(obj);
|
|
|
|
for (size_t i = 0; i < kProtoOrIfaceCacheCount; ++i) {
|
|
|
|
JSObject* proto = protoOrIfaceArray[i];
|
|
|
|
if (proto) {
|
|
|
|
JS_CALL_OBJECT_TRACER(trc, proto, "protoOrIfaceArray[i]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
inline void
|
|
|
|
DestroyProtoOrIfaceCache(JSObject* obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
|
|
|
|
JSObject** protoOrIfaceArray = GetProtoOrIfaceArray(obj);
|
|
|
|
|
|
|
|
delete [] protoOrIfaceArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ConstantSpec
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
JS::Value value;
|
|
|
|
};
|
|
|
|
|
2012-05-09 03:00:48 -07:00
|
|
|
/**
|
|
|
|
* Add constants to an object.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
DefineConstants(JSContext* cx, JSObject* obj, ConstantSpec* cs);
|
|
|
|
|
2012-05-23 09:44:48 -07:00
|
|
|
template<typename T>
|
|
|
|
struct Prefable {
|
|
|
|
// A boolean indicating whether this set of specs is enabled
|
|
|
|
bool enabled;
|
|
|
|
// Array of specs, terminated in whatever way is customary for T.
|
|
|
|
// Null to indicate a end-of-array for Prefable, when such an
|
|
|
|
// indicator is needed.
|
|
|
|
T* specs;
|
|
|
|
};
|
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
struct NativeProperties
|
|
|
|
{
|
|
|
|
Prefable<JSFunctionSpec>* staticMethods;
|
|
|
|
jsid* staticMethodIds;
|
|
|
|
JSFunctionSpec* staticMethodsSpecs;
|
|
|
|
Prefable<JSFunctionSpec>* methods;
|
|
|
|
jsid* methodIds;
|
|
|
|
JSFunctionSpec* methodsSpecs;
|
|
|
|
Prefable<JSPropertySpec>* attributes;
|
|
|
|
jsid* attributeIds;
|
|
|
|
JSPropertySpec* attributeSpecs;
|
2012-10-24 13:10:49 -07:00
|
|
|
Prefable<JSPropertySpec>* unforgeableAttributes;
|
|
|
|
jsid* unforgeableAttributeIds;
|
|
|
|
JSPropertySpec* unforgeableAttributeSpecs;
|
2012-10-09 11:50:05 -07:00
|
|
|
Prefable<ConstantSpec>* constants;
|
|
|
|
jsid* constantIds;
|
|
|
|
ConstantSpec* constantSpecs;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
* receiver is the object on which we need to define the interface object as a
|
|
|
|
* property
|
|
|
|
* protoProto is the prototype to use for the interface prototype 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.
|
|
|
|
* 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.
|
|
|
|
* constructor is the JSNative to use as a constructor. If this is non-null, it
|
|
|
|
* should be used as a JSNative to back the interface object, which
|
|
|
|
* should be a Function. If this is null, then we should create an
|
|
|
|
* object of constructorClass, unless that's also null, in which
|
|
|
|
* case we should not create an interface object at all.
|
|
|
|
* ctorNargs is the length of the constructor function; 0 if no constructor
|
2012-10-09 11:50:05 -07: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-30 21:42:20 -07:00
|
|
|
*
|
|
|
|
* At least one of protoClass and constructorClass should be non-null.
|
|
|
|
* If constructorClass is non-null, the resulting interface object will be
|
|
|
|
* defined on the given global with property name |name|, which must also be
|
|
|
|
* non-null.
|
|
|
|
*
|
|
|
|
* returns the interface prototype object if protoClass is non-null, else it
|
|
|
|
* returns the interface object.
|
|
|
|
*/
|
|
|
|
JSObject*
|
2012-04-23 07:10:56 -07:00
|
|
|
CreateInterfaceObjects(JSContext* cx, JSObject* global, JSObject* receiver,
|
|
|
|
JSObject* protoProto, JSClass* protoClass,
|
2012-05-02 21:35:38 -07:00
|
|
|
JSClass* constructorClass, JSNative constructor,
|
2012-05-22 06:46:20 -07:00
|
|
|
unsigned ctorNargs, const DOMClass* domClass,
|
2012-10-09 11:50:05 -07:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeProperties,
|
|
|
|
const char* name);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-10-24 13:10:49 -07:00
|
|
|
/*
|
|
|
|
* Define the unforgeable attributes on an object.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
DefineUnforgeableAttributes(JSContext* cx, JSObject* obj,
|
|
|
|
Prefable<JSPropertySpec>* props);
|
|
|
|
|
2012-10-22 10:08:52 -07:00
|
|
|
inline bool
|
|
|
|
MaybeWrapValue(JSContext* cx, JSObject* obj, JS::Value* vp)
|
|
|
|
{
|
|
|
|
if (vp->isObject() &&
|
|
|
|
js::GetObjectCompartment(&vp->toObject()) != js::GetObjectCompartment(obj)) {
|
|
|
|
return JS_WrapValue(cx, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
template <class T>
|
|
|
|
inline bool
|
|
|
|
WrapNewBindingObject(JSContext* cx, JSObject* scope, T* value, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JSObject* obj = value->GetWrapper();
|
|
|
|
if (obj && js::GetObjectCompartment(obj) == js::GetObjectCompartment(scope)) {
|
|
|
|
*vp = JS::ObjectValue(*obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!obj) {
|
|
|
|
bool triedToWrap;
|
|
|
|
obj = value->WrapObject(cx, scope, &triedToWrap);
|
|
|
|
if (!obj) {
|
|
|
|
// At this point, obj is null, so just return false. We could
|
|
|
|
// try to communicate triedToWrap to the caller, but in practice
|
|
|
|
// callers seem to be testing JS_IsExceptionPending(cx) to
|
|
|
|
// figure out whether WrapObject() threw instead.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 09:11:33 -07: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-03-30 21:42:20 -07:00
|
|
|
*vp = JS::ObjectValue(*obj);
|
|
|
|
return JS_WrapValue(cx, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for smart pointers (nsAutoPtr/nsRefPtr/nsCOMPtr).
|
2012-06-12 14:19:52 -07:00
|
|
|
template <template <typename> class SmartPtr, class T>
|
2012-03-30 21:42:20 -07:00
|
|
|
inline bool
|
|
|
|
WrapNewBindingObject(JSContext* cx, JSObject* scope, const SmartPtr<T>& value,
|
|
|
|
JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapNewBindingObject(cx, scope, value.get(), vp);
|
|
|
|
}
|
|
|
|
|
2012-06-15 13:25:50 -07:00
|
|
|
template <class T>
|
|
|
|
inline bool
|
|
|
|
WrapNewBindingNonWrapperCachedObject(JSContext* cx, JSObject* scope, T* value,
|
|
|
|
JS::Value* vp)
|
|
|
|
{
|
|
|
|
// We try to wrap in the compartment of the underlying object of "scope"
|
|
|
|
JSObject* obj;
|
|
|
|
{
|
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;
|
2012-06-15 13:25:50 -07:00
|
|
|
if (js::IsWrapper(scope)) {
|
|
|
|
scope = xpc::Unwrap(cx, scope, false);
|
2012-08-21 18:42:53 -07:00
|
|
|
if (!scope)
|
2012-06-15 13:25:50 -07:00
|
|
|
return false;
|
2012-08-21 18:42:53 -07:00
|
|
|
ac.construct(cx, scope);
|
2012-06-15 13:25:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
obj = value->WrapObject(cx, scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can end up here in all sorts of compartments, per above. Make
|
|
|
|
// sure to JS_WrapValue!
|
|
|
|
*vp = JS::ObjectValue(*obj);
|
|
|
|
return JS_WrapValue(cx, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for smart pointers (nsAutoPtr/nsRefPtr/nsCOMPtr).
|
|
|
|
template <template <typename> class SmartPtr, typename T>
|
|
|
|
inline bool
|
|
|
|
WrapNewBindingNonWrapperCachedObject(JSContext* cx, JSObject* scope,
|
|
|
|
const SmartPtr<T>& value, JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapNewBindingNonWrapperCachedObject(cx, scope, value.get(), vp);
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
/**
|
|
|
|
* A method to handle new-binding wrap failure, by possibly falling back to
|
|
|
|
* wrapping as a non-new-binding object.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
DoHandleNewBindingWrappingFailure(JSContext* cx, JSObject* scope,
|
|
|
|
nsISupports* value, JS::Value* vp);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An easy way to call the above when you have a value which
|
|
|
|
* multiply-inherits from nsISupports.
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
bool
|
|
|
|
HandleNewBindingWrappingFailure(JSContext* cx, JSObject* scope, T* value,
|
|
|
|
JS::Value* vp)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> val;
|
|
|
|
CallQueryInterface(value, getter_AddRefs(val));
|
|
|
|
return DoHandleNewBindingWrappingFailure(cx, scope, val, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for smart pointers (nsAutoPtr/nsRefPtr/nsCOMPtr).
|
2012-06-12 14:19:52 -07:00
|
|
|
template <template <typename> class SmartPtr, class T>
|
2012-03-30 21:42:20 -07:00
|
|
|
MOZ_ALWAYS_INLINE bool
|
|
|
|
HandleNewBindingWrappingFailure(JSContext* cx, JSObject* scope,
|
|
|
|
const SmartPtr<T>& value, JS::Value* vp)
|
|
|
|
{
|
|
|
|
return HandleNewBindingWrappingFailure(cx, scope, value.get(), vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EnumEntry {
|
|
|
|
const char* value;
|
|
|
|
size_t length;
|
|
|
|
};
|
|
|
|
|
2012-07-18 03:36:08 -07:00
|
|
|
template<bool Fatal>
|
|
|
|
inline bool
|
|
|
|
EnumValueNotFound(JSContext* cx, const jschar* chars, size_t length,
|
|
|
|
const char* type)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool
|
|
|
|
EnumValueNotFound<false>(JSContext* cx, const jschar* chars, size_t length,
|
|
|
|
const char* type)
|
|
|
|
{
|
|
|
|
// TODO: Log a warning to the console.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool
|
|
|
|
EnumValueNotFound<true>(JSContext* cx, const jschar* chars, size_t length,
|
|
|
|
const char* type)
|
|
|
|
{
|
|
|
|
NS_LossyConvertUTF16toASCII deflated(static_cast<const PRUnichar*>(chars),
|
|
|
|
length);
|
|
|
|
return ThrowErrorMessage(cx, MSG_INVALID_ENUM_VALUE, deflated.get(), type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<bool InvalidValueFatal>
|
2012-03-30 21:42:20 -07:00
|
|
|
inline int
|
2012-07-18 03:36:08 -07:00
|
|
|
FindEnumStringIndex(JSContext* cx, JS::Value v, const EnumEntry* values,
|
|
|
|
const char* type, bool* ok)
|
2012-03-30 21:42:20 -07: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 03:36:08 -07:00
|
|
|
*ok = EnumValueNotFound<InvalidValueFatal>(cx, chars, length, type);
|
2012-06-21 00:11:07 -07:00
|
|
|
return -1;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsWrapperCache*
|
|
|
|
GetWrapperCache(nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsWrapperCache*
|
|
|
|
GetWrapperCache(nsGlobalWindow* not_allowed);
|
|
|
|
|
|
|
|
inline nsWrapperCache*
|
|
|
|
GetWrapperCache(void* p)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-10 12:24:59 -07:00
|
|
|
struct ParentObject {
|
|
|
|
template<class T>
|
|
|
|
ParentObject(T* aObject) :
|
|
|
|
mObject(aObject),
|
|
|
|
mWrapperCache(GetWrapperCache(aObject))
|
|
|
|
{}
|
|
|
|
|
2012-06-12 14:19:52 -07:00
|
|
|
template<class T, template<typename> class SmartPtr>
|
2012-05-10 12:24:59 -07:00
|
|
|
ParentObject(const SmartPtr<T>& aObject) :
|
|
|
|
mObject(aObject.get()),
|
|
|
|
mWrapperCache(GetWrapperCache(aObject.get()))
|
|
|
|
{}
|
|
|
|
|
|
|
|
ParentObject(nsISupports* aObject, nsWrapperCache* aCache) :
|
|
|
|
mObject(aObject),
|
|
|
|
mWrapperCache(aCache)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsISupports* const mObject;
|
|
|
|
nsWrapperCache* const mWrapperCache;
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-08-07 22:26:18 -07: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
|
|
|
|
InstanceClassHasProtoAtDepth(JSHandleObject protoObject, uint32_t protoID,
|
|
|
|
uint32_t depth);
|
|
|
|
|
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
|
|
|
|
XPCOMObjectToJsval(JSContext* cx, JSObject* scope, xpcObjectHelper &helper,
|
|
|
|
const nsIID* iid, bool allowNativeWrapper, JS::Value* rval);
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
|
|
|
WrapObject(JSContext* cx, JSObject* scope, T* p, nsWrapperCache* cache,
|
|
|
|
const nsIID* iid, JS::Value* vp)
|
|
|
|
{
|
|
|
|
if (xpc_FastGetCachedWrapper(cache, scope, vp))
|
|
|
|
return true;
|
|
|
|
qsObjectHelper helper(p, cache);
|
|
|
|
return XPCOMObjectToJsval(cx, scope, helper, iid, true, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
|
|
|
WrapObject(JSContext* cx, JSObject* scope, T* p, const nsIID* iid,
|
|
|
|
JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapObject(cx, scope, p, GetWrapperCache(p), iid, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
|
|
|
WrapObject(JSContext* cx, JSObject* scope, T* p, JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapObject(cx, scope, p, NULL, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
|
|
|
WrapObject(JSContext* cx, JSObject* scope, nsCOMPtr<T> &p, const nsIID* iid,
|
|
|
|
JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapObject(cx, scope, p.get(), iid, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
|
|
|
WrapObject(JSContext* cx, JSObject* scope, nsCOMPtr<T> &p, JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapObject(cx, scope, p, NULL, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
|
|
|
WrapObject(JSContext* cx, JSObject* scope, nsRefPtr<T> &p, const nsIID* iid,
|
|
|
|
JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapObject(cx, scope, p.get(), iid, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool
|
|
|
|
WrapObject(JSContext* cx, JSObject* scope, nsRefPtr<T> &p, JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapObject(cx, scope, p, NULL, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool
|
|
|
|
WrapObject<JSObject>(JSContext* cx, JSObject* scope, JSObject* p, JS::Value* vp)
|
|
|
|
{
|
|
|
|
vp->setObjectOrNull(p);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-10 12:57:57 -07:00
|
|
|
bool
|
|
|
|
WrapCallbackInterface(JSContext *cx, JSObject *scope, nsISupports* callback,
|
|
|
|
JS::Value* vp);
|
|
|
|
|
2012-10-19 00:33:18 -07: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.
|
2012-05-10 12:24:59 -07:00
|
|
|
template<typename T>
|
2012-10-09 11:50:26 -07:00
|
|
|
struct HasWrapObject
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef char yes[1];
|
|
|
|
typedef char no[2];
|
2012-10-19 00:33:18 -07:00
|
|
|
typedef JSObject* (nsWrapperCache::*WrapObject)(JSContext*, JSObject*, bool*);
|
2012-10-09 11:50:26 -07:00
|
|
|
template<typename U, U> struct SFINAE;
|
2012-10-19 00:33:18 -07:00
|
|
|
template <typename V> static no& Check(SFINAE<WrapObject, &V::WrapObject>*);
|
|
|
|
template <typename V> static yes& Check(...);
|
2012-10-09 11:50:26 -07:00
|
|
|
|
|
|
|
public:
|
2012-10-19 00:33:18 -07:00
|
|
|
static bool const Value = HasWrapObjectMember<T>::Value &&
|
|
|
|
sizeof(Check<T>(nullptr)) == sizeof(yes);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
|
|
|
WrapNativeISupportsParent(JSContext* cx, JSObject* scope, T* p,
|
|
|
|
nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
qsObjectHelper helper(ToSupports(p), cache);
|
|
|
|
JS::Value v;
|
|
|
|
return XPCOMObjectToJsval(cx, scope, helper, nullptr, false, &v) ?
|
|
|
|
JSVAL_TO_OBJECT(v) :
|
|
|
|
nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, bool isISupports=IsISupports<T>::Value >
|
|
|
|
struct WrapNativeParentFallback
|
|
|
|
{
|
|
|
|
static inline JSObject* Wrap(JSContext* cx, JSObject* scope, T* parent,
|
|
|
|
nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
MOZ_NOT_REACHED("Don't know how to deal with triedToWrap == false for "
|
|
|
|
"non-nsISupports classes");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T >
|
|
|
|
struct WrapNativeParentFallback<T, true >
|
|
|
|
{
|
|
|
|
static inline JSObject* Wrap(JSContext* cx, JSObject* scope, T* parent,
|
|
|
|
nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
return WrapNativeISupportsParent(cx, scope, parent, cache);
|
|
|
|
}
|
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
|
|
|
{
|
2012-10-09 11:50:26 -07:00
|
|
|
static inline JSObject* Wrap(JSContext* cx, JSObject* scope, T* parent,
|
|
|
|
nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool triedToWrap;
|
2012-10-19 00:33:18 -07:00
|
|
|
obj = parent->WrapObject(cx, scope, &triedToWrap);
|
|
|
|
if (!triedToWrap) {
|
|
|
|
obj = WrapNativeParentFallback<T>::Wrap(cx, scope, parent, cache);
|
|
|
|
}
|
|
|
|
return obj;
|
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
|
|
|
{
|
|
|
|
static inline JSObject* Wrap(JSContext* cx, JSObject* scope, T* parent,
|
|
|
|
nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
JSObject* obj;
|
|
|
|
if (cache && (obj = cache->GetWrapper())) {
|
2012-03-30 21:42:20 -07:00
|
|
|
#ifdef DEBUG
|
2012-10-19 00:33:18 -07:00
|
|
|
NS_ASSERTION(WrapNativeISupportsParent(cx, scope, 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;
|
|
|
|
}
|
|
|
|
|
2012-10-19 00:33:18 -07:00
|
|
|
return WrapNativeISupportsParent(cx, scope, parent, cache);
|
2012-10-09 11:50:26 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
|
|
|
WrapNativeParent(JSContext* cx, JSObject* scope, T* p, nsWrapperCache* cache)
|
|
|
|
{
|
|
|
|
if (!p) {
|
|
|
|
return scope;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:26 -07:00
|
|
|
return WrapNativeParentHelper<T>::Wrap(cx, scope, p, cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static inline JSObject*
|
|
|
|
WrapNativeParent(JSContext* cx, JSObject* scope, const T& p)
|
|
|
|
{
|
|
|
|
return WrapNativeParent(cx, scope, GetParentPointer(p), GetWrapperCache(p));
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
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
|
2012-05-23 09:44:48 -07:00
|
|
|
InitIds(JSContext* cx, 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
|
|
|
|
2012-05-23 09:44:48 -07: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-30 21:42:20 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
QueryInterface(JSContext* cx, unsigned argc, JS::Value* vp);
|
2012-05-02 21:35:38 -07:00
|
|
|
JSBool
|
|
|
|
ThrowingConstructor(JSContext* cx, unsigned argc, JS::Value* vp);
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
bool
|
|
|
|
GetPropertyOnPrototype(JSContext* cx, JSObject* proxy, jsid id, bool* found,
|
|
|
|
JS::Value* vp);
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasPropertyOnPrototype(JSContext* cx, JSObject* proxy, DOMProxyHandler* handler,
|
|
|
|
jsid id);
|
|
|
|
|
2012-05-15 11:23:29 -07:00
|
|
|
template<class T>
|
|
|
|
class NonNull
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NonNull()
|
|
|
|
#ifdef DEBUG
|
|
|
|
: inited(false)
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
|
|
|
operator T&() {
|
|
|
|
MOZ_ASSERT(inited);
|
|
|
|
MOZ_ASSERT(ptr, "NonNull<T> was set to null");
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2012-05-29 20:45:18 -07:00
|
|
|
operator const T&() const {
|
|
|
|
MOZ_ASSERT(inited);
|
|
|
|
MOZ_ASSERT(ptr, "NonNull<T> was set to null");
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2012-05-15 11:23:29 -07:00
|
|
|
void operator=(T* t) {
|
|
|
|
ptr = t;
|
|
|
|
MOZ_ASSERT(ptr);
|
|
|
|
#ifdef DEBUG
|
|
|
|
inited = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-07-17 09:18:53 -07:00
|
|
|
template<typename U>
|
|
|
|
void operator=(U* t) {
|
|
|
|
ptr = t->ToAStringPtr();
|
|
|
|
MOZ_ASSERT(ptr);
|
|
|
|
#ifdef DEBUG
|
|
|
|
inited = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-05-15 11:23:29 -07:00
|
|
|
T** Slot() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
inited = true;
|
|
|
|
#endif
|
|
|
|
return &ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
T* ptr;
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool inited;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-22 10:08:52 -07:00
|
|
|
// Helper for OwningNonNull
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
|
|
|
WrapNewBindingObject(JSContext* cx, JSObject* scope, OwningNonNull<T>& value,
|
|
|
|
JS::Value* vp)
|
|
|
|
{
|
|
|
|
return WrapNewBindingObject(cx, scope, &static_cast<T&>(value), vp);
|
|
|
|
}
|
|
|
|
|
2012-07-17 09:18:53 -07: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 10:20:40 -07:00
|
|
|
mData = nsDependentString::char_traits::sEmptyBuffer;
|
2012-07-17 09:18:53 -07:00
|
|
|
mLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
Truncate();
|
|
|
|
mFlags |= nsDependentString::F_VOIDED;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 08:56:38 -07:00
|
|
|
uint32_t mFlags;
|
2012-07-17 09:18:53 -07: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 14:25:46 -07:00
|
|
|
enum StringificationBehavior {
|
|
|
|
eStringify,
|
|
|
|
eEmpty,
|
|
|
|
eNull
|
|
|
|
};
|
|
|
|
|
2012-07-30 21:22:23 -07:00
|
|
|
// pval must not be null and must point to a rooted JS::Value
|
2012-05-18 14:25:46 -07:00
|
|
|
static inline bool
|
|
|
|
ConvertJSValueToString(JSContext* cx, const JS::Value& v, JS::Value* pval,
|
|
|
|
StringificationBehavior nullBehavior,
|
|
|
|
StringificationBehavior undefinedBehavior,
|
2012-07-17 09:18:53 -07:00
|
|
|
FakeDependentString& 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 {
|
|
|
|
result.SetNull();
|
|
|
|
}
|
2012-05-18 14:25:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = JS_ValueToString(cx, v);
|
|
|
|
if (!s) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pval->setString(s); // Root the new string.
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
const jschar *chars = JS_GetStringCharsZAndLength(cx, s, &len);
|
|
|
|
if (!chars) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-17 09:18:53 -07:00
|
|
|
result.SetData(chars, len);
|
2012-05-18 14:25:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-29 20:45:18 -07:00
|
|
|
// Class for representing optional arguments.
|
|
|
|
template<typename T>
|
|
|
|
class Optional {
|
|
|
|
public:
|
|
|
|
Optional() {}
|
|
|
|
|
|
|
|
bool WasPassed() const {
|
|
|
|
return !mImpl.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Construct() {
|
|
|
|
mImpl.construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T1, class T2>
|
|
|
|
void Construct(const T1 &t1, const T2 &t2) {
|
|
|
|
mImpl.construct(t1, t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& Value() const {
|
|
|
|
return mImpl.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
T& Value() {
|
|
|
|
return mImpl.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Forbid copy-construction and assignment
|
|
|
|
Optional(const Optional& other) MOZ_DELETE;
|
|
|
|
const Optional &operator=(const Optional &other) MOZ_DELETE;
|
|
|
|
|
|
|
|
Maybe<T> mImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specialization for strings.
|
|
|
|
template<>
|
|
|
|
class Optional<nsAString> {
|
|
|
|
public:
|
|
|
|
Optional() : mPassed(false) {}
|
|
|
|
|
|
|
|
bool WasPassed() const {
|
|
|
|
return mPassed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(const nsAString* str) {
|
|
|
|
MOZ_ASSERT(str);
|
|
|
|
mStr = str;
|
|
|
|
mPassed = true;
|
|
|
|
}
|
|
|
|
|
2012-07-17 09:18:53 -07:00
|
|
|
void operator=(const FakeDependentString* str) {
|
|
|
|
MOZ_ASSERT(str);
|
|
|
|
mStr = str->ToAStringPtr();
|
|
|
|
mPassed = true;
|
|
|
|
}
|
|
|
|
|
2012-05-29 20:45:18 -07:00
|
|
|
const nsAString& Value() const {
|
|
|
|
MOZ_ASSERT(WasPassed());
|
|
|
|
return *mStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Forbid copy-construction and assignment
|
|
|
|
Optional(const Optional& other) MOZ_DELETE;
|
|
|
|
const Optional &operator=(const Optional &other) MOZ_DELETE;
|
|
|
|
|
|
|
|
bool mPassed;
|
|
|
|
const nsAString* mStr;
|
|
|
|
};
|
|
|
|
|
2012-05-24 22:08:22 -07:00
|
|
|
// Class for representing sequences in arguments. We use an auto array that can
|
|
|
|
// hold 16 elements, to avoid having to allocate in common cases. This needs to
|
|
|
|
// be fallible because web content controls the length of the array, and can
|
|
|
|
// easily try to create very large lengths.
|
|
|
|
template<typename T>
|
|
|
|
class Sequence : public AutoFallibleTArray<T, 16>
|
|
|
|
{
|
2012-05-29 20:45:18 -07:00
|
|
|
public:
|
|
|
|
Sequence() : AutoFallibleTArray<T, 16>() {}
|
2012-05-24 22:08:22 -07:00
|
|
|
};
|
|
|
|
|
2012-05-18 14:25:47 -07: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();
|
|
|
|
}
|
|
|
|
const T& Value() const {
|
|
|
|
return *storage.addr();
|
|
|
|
}
|
|
|
|
void Destroy() {
|
|
|
|
storage.addr()->~T();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-18 04:52:07 -07:00
|
|
|
// Implementation of the bits that XrayWrapper needs
|
|
|
|
bool
|
|
|
|
XrayResolveProperty(JSContext* cx, JSObject* wrapper, jsid id,
|
|
|
|
JSPropertyDescriptor* desc,
|
2012-10-09 11:50:05 -07:00
|
|
|
const NativeProperties* nativeProperties,
|
|
|
|
const NativeProperties* chromeOnlyNativeProperties);
|
2012-07-18 04:52:07 -07:00
|
|
|
|
|
|
|
bool
|
2012-10-09 11:50:05 -07:00
|
|
|
XrayEnumerateProperties(JSObject* wrapper,
|
|
|
|
JS::AutoIdVector& props,
|
|
|
|
const NativeProperties* nativeProperties,
|
|
|
|
const NativeProperties* chromeOnlyNativeProperties);
|
2012-07-18 04:52:07 -07:00
|
|
|
|
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-05 09:59:23 -07: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-16 07:52:10 -07:00
|
|
|
struct MainThreadDictionaryBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
JSContext* ParseJSON(const nsAString& aJSON,
|
|
|
|
mozilla::Maybe<JSAutoRequest>& aAr,
|
|
|
|
mozilla::Maybe<JSAutoCompartment>& aAc,
|
|
|
|
JS::Value& aVal);
|
|
|
|
};
|
|
|
|
|
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__ */
|