2012-03-31 08:42:20 +04: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/. */
|
|
|
|
|
2013-08-14 10:56:21 +04:00
|
|
|
#include "BindingUtils.h"
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
#include <algorithm>
|
2012-07-18 14:36:08 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2013-08-02 03:45:17 +04:00
|
|
|
#include "JavaScriptParent.h"
|
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-05-02 22:38:19 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2013-06-13 09:18:35 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-05-09 01:16:00 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-12-15 03:58:45 +04:00
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
#include "AccessCheck.h"
|
2013-06-01 10:56:23 +04:00
|
|
|
#include "jsfriendapi.h"
|
2013-10-03 16:34:47 +04:00
|
|
|
#include "js/OldDebugAPI.h"
|
2014-02-05 22:38:17 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-06-01 10:56:23 +04:00
|
|
|
#include "nsIDOMGlobalPropertyInitializer.h"
|
2014-02-05 22:38:17 +04:00
|
|
|
#include "nsIPrincipal.h"
|
2012-09-26 18:17:46 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2012-05-22 17:46:20 +04:00
|
|
|
#include "WrapperFactory.h"
|
2012-05-18 12:29:40 +04:00
|
|
|
#include "xpcprivate.h"
|
|
|
|
#include "XPCQuickStubs.h"
|
2012-09-26 18:17:46 +04:00
|
|
|
#include "XrayWrapper.h"
|
2013-07-04 17:24:49 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2013-08-18 02:50:18 +04:00
|
|
|
#include "prprf.h"
|
2012-05-18 12:29:40 +04:00
|
|
|
|
2013-12-12 05:51:58 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-09-03 16:01:53 +04:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
#include "mozilla/dom/DOMErrorBinding.h"
|
2013-01-07 03:55:14 +04:00
|
|
|
#include "mozilla/dom/HTMLObjectElement.h"
|
|
|
|
#include "mozilla/dom/HTMLObjectElementBinding.h"
|
2013-03-17 18:42:05 +04:00
|
|
|
#include "mozilla/dom/HTMLSharedObjectElement.h"
|
|
|
|
#include "mozilla/dom/HTMLEmbedElementBinding.h"
|
|
|
|
#include "mozilla/dom/HTMLAppletElementBinding.h"
|
2014-03-21 20:18:24 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2013-09-09 07:29:21 +04:00
|
|
|
#include "WorkerPrivate.h"
|
2013-01-07 03:55:14 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2012-07-18 14:36:08 +04:00
|
|
|
JSErrorFormatString ErrorFormatString[] = {
|
|
|
|
#define MSG_DEF(_name, _argc, _str) \
|
|
|
|
{ _str, _argc, JSEXN_TYPEERR },
|
|
|
|
#include "mozilla/dom/Errors.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
};
|
|
|
|
|
|
|
|
const JSErrorFormatString*
|
|
|
|
GetErrorMessage(void* aUserRef, const char* aLocale,
|
|
|
|
const unsigned aErrorNumber)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aErrorNumber < ArrayLength(ErrorFormatString));
|
|
|
|
return &ErrorFormatString[aErrorNumber];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThrowErrorMessage(JSContext* aCx, const ErrNum aErrorNumber, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, aErrorNumber);
|
2013-06-17 21:07:03 +04:00
|
|
|
JS_ReportErrorNumberVA(aCx, GetErrorMessage, nullptr,
|
2012-07-18 14:36:08 +04:00
|
|
|
static_cast<const unsigned>(aErrorNumber), ap);
|
|
|
|
va_end(ap);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-17 21:07:03 +04:00
|
|
|
bool
|
2013-06-18 00:31:13 +04:00
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
const char* aInterfaceName)
|
2013-06-17 21:07:03 +04:00
|
|
|
{
|
|
|
|
NS_ConvertASCIItoUTF16 ifaceName(aInterfaceName);
|
2013-06-18 00:31:13 +04:00
|
|
|
// This should only be called for DOM methods/getters/setters, which
|
|
|
|
// are JSNative-backed functions, so we can assume that
|
|
|
|
// JS_ValueToFunction and JS_GetFunctionDisplayId will both return
|
|
|
|
// non-null and that JS_GetStringCharsZ returns non-null.
|
2013-06-17 21:07:03 +04:00
|
|
|
JS::Rooted<JSFunction*> func(aCx, JS_ValueToFunction(aCx, aArgs.calleev()));
|
|
|
|
MOZ_ASSERT(func);
|
|
|
|
JS::Rooted<JSString*> funcName(aCx, JS_GetFunctionDisplayId(func));
|
|
|
|
MOZ_ASSERT(funcName);
|
|
|
|
JS_ReportErrorNumberUC(aCx, GetErrorMessage, nullptr,
|
2013-06-18 00:31:13 +04:00
|
|
|
static_cast<const unsigned>(aErrorNumber),
|
2013-06-17 21:07:03 +04:00
|
|
|
JS_GetStringCharsZ(aCx, funcName),
|
|
|
|
ifaceName.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:56:14 +04:00
|
|
|
bool
|
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
prototypes::ID aProtoId)
|
|
|
|
{
|
|
|
|
return ThrowInvalidThis(aCx, aArgs, aErrorNumber,
|
2014-02-10 19:24:38 +04:00
|
|
|
NamesOfInterfacesWithProtos(aProtoId));
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThrowNoSetterArg(JSContext* aCx, prototypes::ID aProtoId)
|
|
|
|
{
|
|
|
|
nsPrintfCString errorMessage("%s attribute setter",
|
2014-02-10 19:24:38 +04:00
|
|
|
NamesOfInterfacesWithProtos(aProtoId));
|
2014-02-08 00:56:14 +04:00
|
|
|
return ThrowErrorMessage(aCx, MSG_MISSING_ARGUMENTS, errorMessage.get());
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
} // namespace dom
|
|
|
|
|
|
|
|
struct ErrorResult::Message {
|
|
|
|
nsTArray<nsString> mArgs;
|
|
|
|
dom::ErrNum mErrorNumber;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ThrowTypeError(const dom::ErrNum errorNumber, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, errorNumber);
|
2013-02-19 20:54:40 +04:00
|
|
|
if (IsJSException()) {
|
|
|
|
// We have rooted our mJSException, and we don't have the info
|
|
|
|
// needed to unroot here, so just bail.
|
|
|
|
va_end(ap);
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"Ignoring ThrowTypeError call because we have a JS exception");
|
|
|
|
return;
|
|
|
|
}
|
2012-11-07 03:23:14 +04:00
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
}
|
|
|
|
mResult = NS_ERROR_TYPE_ERR;
|
|
|
|
Message* message = new Message();
|
|
|
|
message->mErrorNumber = errorNumber;
|
|
|
|
uint16_t argCount =
|
|
|
|
dom::GetErrorMessage(nullptr, nullptr, errorNumber)->argCount;
|
|
|
|
MOZ_ASSERT(argCount <= 10);
|
|
|
|
argCount = std::min<uint16_t>(argCount, 10);
|
|
|
|
while (argCount--) {
|
|
|
|
message->mArgs.AppendElement(*va_arg(ap, nsString*));
|
|
|
|
}
|
|
|
|
mMessage = message;
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ReportTypeError(JSContext* aCx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMessage, "ReportTypeError() can be called only once");
|
|
|
|
|
|
|
|
Message* message = mMessage;
|
|
|
|
const uint32_t argCount = message->mArgs.Length();
|
|
|
|
const jschar* args[11];
|
|
|
|
for (uint32_t i = 0; i < argCount; ++i) {
|
|
|
|
args[i] = message->mArgs.ElementAt(i).get();
|
|
|
|
}
|
|
|
|
args[argCount] = nullptr;
|
|
|
|
|
|
|
|
JS_ReportErrorNumberUCArray(aCx, dom::GetErrorMessage, nullptr,
|
|
|
|
static_cast<const unsigned>(message->mErrorNumber),
|
|
|
|
argCount > 0 ? args : nullptr);
|
|
|
|
|
2013-02-19 17:30:28 +04:00
|
|
|
ClearMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ClearMessage()
|
|
|
|
{
|
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
mMessage = nullptr;
|
|
|
|
}
|
2012-11-07 03:23:14 +04:00
|
|
|
}
|
|
|
|
|
2013-02-19 20:54:40 +04:00
|
|
|
void
|
2013-04-11 22:31:06 +04:00
|
|
|
ErrorResult::ThrowJSException(JSContext* cx, JS::Handle<JS::Value> exn)
|
2013-02-19 20:54:40 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to throw a JS exception?");
|
|
|
|
|
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
}
|
|
|
|
|
2013-04-09 01:05:22 +04:00
|
|
|
// Make sure mJSException is initialized _before_ we try to root it. But
|
|
|
|
// don't set it to exn yet, because we don't want to do that until after we
|
|
|
|
// root.
|
|
|
|
mJSException = JS::UndefinedValue();
|
2014-04-16 12:47:57 +04:00
|
|
|
if (!js::AddRawValueRoot(cx, &mJSException, "ErrorResult::mJSException")) {
|
2013-02-19 20:54:40 +04:00
|
|
|
// Don't use NS_ERROR_DOM_JS_EXCEPTION, because that indicates we have
|
|
|
|
// in fact rooted mJSException.
|
|
|
|
mResult = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
mJSException = exn;
|
|
|
|
mResult = NS_ERROR_DOM_JS_EXCEPTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ReportJSException(JSContext* cx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to handle JS exceptions?");
|
2013-10-26 20:19:05 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exception(cx, mJSException);
|
|
|
|
if (JS_WrapValue(cx, &exception)) {
|
2013-09-19 11:54:01 +04:00
|
|
|
JS_SetPendingException(cx, exception);
|
2013-02-19 20:54:40 +04:00
|
|
|
}
|
2013-10-26 20:19:05 +04:00
|
|
|
mJSException = exception;
|
2013-02-19 20:54:40 +04:00
|
|
|
// If JS_WrapValue failed, not much we can do about it... No matter
|
|
|
|
// what, go ahead and unroot mJSException.
|
2014-04-16 12:47:57 +04:00
|
|
|
js::RemoveRawValueRoot(cx, &mJSException);
|
2013-02-19 20:54:40 +04:00
|
|
|
}
|
|
|
|
|
2013-09-03 16:01:53 +04:00
|
|
|
void
|
|
|
|
ErrorResult::ReportJSExceptionFromJSImplementation(JSContext* aCx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to handle JS exceptions?");
|
|
|
|
|
|
|
|
dom::DOMError* domError;
|
2013-11-21 16:51:16 +04:00
|
|
|
nsresult rv = UNWRAP_OBJECT(DOMError, &mJSException.toObject(), domError);
|
2013-09-03 16:01:53 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Unwrapping really shouldn't fail here, if mExceptionHandling is set to
|
|
|
|
// eRethrowContentExceptions then the CallSetup destructor only stores an
|
|
|
|
// exception if it unwraps to DOMError. If we reach this then either
|
|
|
|
// mExceptionHandling wasn't set to eRethrowContentExceptions and we
|
|
|
|
// shouldn't be calling ReportJSExceptionFromJSImplementation or something
|
|
|
|
// went really wrong.
|
|
|
|
NS_RUNTIMEABORT("We stored a non-DOMError exception!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString message;
|
|
|
|
domError->GetMessage(message);
|
|
|
|
|
2013-10-18 15:22:09 +04:00
|
|
|
JS_ReportError(aCx, "%hs", message.get());
|
2014-04-16 12:47:57 +04:00
|
|
|
js::RemoveRawValueRoot(aCx, &mJSException);
|
2014-02-13 11:38:36 +04:00
|
|
|
|
2013-09-03 16:01:53 +04:00
|
|
|
// We no longer have a useful exception but we do want to signal that an error
|
|
|
|
// occured.
|
|
|
|
mResult = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
void
|
|
|
|
ErrorResult::StealJSException(JSContext* cx,
|
|
|
|
JS::MutableHandle<JS::Value> value)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mMightHaveUnreportedJSException,
|
|
|
|
"Must call WouldReportJSException unconditionally in all codepaths that might call StealJSException");
|
|
|
|
MOZ_ASSERT(IsJSException(), "No exception to steal");
|
|
|
|
|
|
|
|
value.set(mJSException);
|
2014-04-16 12:47:57 +04:00
|
|
|
js::RemoveRawValueRoot(cx, &mJSException);
|
2013-06-12 05:41:21 +04:00
|
|
|
mResult = NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-04 17:24:49 +04:00
|
|
|
void
|
|
|
|
ErrorResult::ReportNotEnoughArgsError(JSContext* cx,
|
|
|
|
const char* ifaceName,
|
|
|
|
const char* memberName)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(ErrorCode() == NS_ERROR_XPC_NOT_ENOUGH_ARGS);
|
|
|
|
|
|
|
|
nsPrintfCString errorMessage("%s.%s", ifaceName, memberName);
|
|
|
|
ThrowErrorMessage(cx, dom::MSG_MISSING_ARGUMENTS, errorMessage.get());
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2012-05-09 14:00:48 +04:00
|
|
|
bool
|
2013-05-08 06:34:56 +04:00
|
|
|
DefineConstants(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const ConstantSpec* cs)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2014-01-22 06:11:17 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
2012-03-31 08:42:20 +04:00
|
|
|
for (; cs->name; ++cs) {
|
2014-01-22 06:11:17 +04:00
|
|
|
value = cs->value;
|
2013-08-09 02:53:04 +04:00
|
|
|
bool ok =
|
2014-01-22 06:11:17 +04:00
|
|
|
JS_DefineProperty(cx, obj, cs->name, value,
|
2012-03-31 08:42:20 +04:00
|
|
|
JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-23 20:44:48 +04:00
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const JSFunctionSpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return JS_DefineFunctions(cx, obj, spec);
|
|
|
|
}
|
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const JSPropertySpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return JS_DefineProperties(cx, obj, spec);
|
|
|
|
}
|
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const ConstantSpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return DefineConstants(cx, obj, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
DefinePrefable(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const Prefable<T>* props)
|
2012-05-23 20:44:48 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(props);
|
|
|
|
MOZ_ASSERT(props->specs);
|
|
|
|
do {
|
|
|
|
// Define if enabled
|
2013-02-19 20:54:40 +04:00
|
|
|
if (props->isEnabled(cx, obj)) {
|
2012-05-23 20:44:48 +04:00
|
|
|
if (!Define(cx, obj, props->specs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((++props)->specs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-25 00:10:49 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
DefineUnforgeableAttributes(JSContext* cx, JS::Handle<JSObject*> obj,
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSPropertySpec>* props)
|
2012-10-25 00:10:49 +04:00
|
|
|
{
|
|
|
|
return DefinePrefable(cx, obj, props);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-02 17:28:17 +04:00
|
|
|
// We should use JSFunction objects for interface objects, but we need a custom
|
|
|
|
// hasInstance hook because we have new interface objects on prototype chains of
|
|
|
|
// old (XPConnect-based) bindings. Because Function.prototype.toString throws if
|
|
|
|
// passed a non-Function object we also need to provide our own toString method
|
|
|
|
// for interface objects.
|
|
|
|
|
|
|
|
enum {
|
|
|
|
TOSTRING_CLASS_RESERVED_SLOT = 0,
|
|
|
|
TOSTRING_NAME_RESERVED_SLOT = 1
|
|
|
|
};
|
|
|
|
|
2013-10-16 02:34:13 +04:00
|
|
|
static bool
|
2012-05-02 17:28:17 +04:00
|
|
|
InterfaceObjectToString(JSContext* cx, unsigned argc, JS::Value *vp)
|
|
|
|
{
|
2013-10-12 09:02:39 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> callee(cx, &args.callee());
|
2012-05-02 17:28:17 +04:00
|
|
|
|
2013-10-21 21:52:22 +04:00
|
|
|
if (!args.thisv().isObject()) {
|
2013-10-28 18:04:12 +04:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_CANT_CONVERT_TO, "null", "object");
|
2012-05-02 17:28:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-03 03:05:37 +04:00
|
|
|
JS::Value v = js::GetFunctionNativeReserved(callee,
|
|
|
|
TOSTRING_CLASS_RESERVED_SLOT);
|
2013-10-16 02:34:13 +04:00
|
|
|
const JSClass* clasp = static_cast<const JSClass*>(v.toPrivate());
|
2012-05-02 17:28:17 +04:00
|
|
|
|
|
|
|
v = js::GetFunctionNativeReserved(callee, TOSTRING_NAME_RESERVED_SLOT);
|
2014-04-28 06:45:46 +04:00
|
|
|
JSString* jsname = static_cast<JSString*>(v.toString());
|
2012-05-02 17:28:17 +04:00
|
|
|
size_t length;
|
|
|
|
const jschar* name = JS_GetInternedStringCharsAndLength(jsname, &length);
|
|
|
|
|
2013-10-21 21:52:22 +04:00
|
|
|
if (js::GetObjectJSClass(&args.thisv().toObject()) != clasp) {
|
2013-10-28 18:04:12 +04:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO,
|
2012-05-02 17:28:17 +04:00
|
|
|
NS_ConvertUTF16toUTF8(name).get(), "toString",
|
|
|
|
"object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString str;
|
|
|
|
str.AppendLiteral("function ");
|
|
|
|
str.Append(name, length);
|
|
|
|
str.AppendLiteral("() {");
|
|
|
|
str.Append('\n');
|
|
|
|
str.AppendLiteral(" [native code]");
|
|
|
|
str.Append('\n');
|
2014-05-22 07:48:51 +04:00
|
|
|
str.Append('}');
|
2012-05-02 17:28:17 +04:00
|
|
|
|
2013-10-12 09:02:39 +04:00
|
|
|
return xpc::NonVoidStringToJsval(cx, str, args.rval());
|
2012-05-02 17:28:17 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
bool
|
2012-10-09 22:50:27 +04:00
|
|
|
Constructor(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2014-03-19 21:05:04 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2012-10-09 22:50:27 +04:00
|
|
|
const JS::Value& v =
|
2014-03-19 21:05:04 +04:00
|
|
|
js::GetFunctionNativeReserved(&args.callee(),
|
2012-10-09 22:50:27 +04:00
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT);
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* nativeHolder =
|
|
|
|
static_cast<const JSNativeHolder*>(v.toPrivate());
|
2012-10-09 22:50:27 +04:00
|
|
|
return (nativeHolder->mNative)(cx, argc, vp);
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:51:15 +04:00
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateConstructor(JSContext* cx, JS::Handle<JSObject*> global, const char* name,
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* nativeHolder, unsigned ctorNargs)
|
|
|
|
{
|
|
|
|
JSFunction* fun = js::NewFunctionWithReserved(cx, Constructor, ctorNargs,
|
|
|
|
JSFUN_CONSTRUCTOR, global,
|
|
|
|
name);
|
|
|
|
if (!fun) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* constructor = JS_GetFunctionObject(fun);
|
|
|
|
js::SetFunctionNativeReserved(constructor,
|
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT,
|
|
|
|
js::PrivateValue(const_cast<JSNativeHolder*>(nativeHolder)));
|
|
|
|
return constructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-05-02 07:44:12 +04:00
|
|
|
DefineConstructor(JSContext* cx, JS::Handle<JSObject*> global, const char* name,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> constructor)
|
2013-01-22 14:51:15 +04:00
|
|
|
{
|
2013-08-09 02:53:04 +04:00
|
|
|
bool alreadyDefined;
|
2013-01-22 14:51:15 +04:00
|
|
|
if (!JS_AlreadyHasOwnProperty(cx, global, name, &alreadyDefined)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is Enumerable: False per spec.
|
|
|
|
return alreadyDefined ||
|
2014-01-22 06:11:17 +04:00
|
|
|
JS_DefineProperty(cx, global, name, constructor, 0);
|
2013-01-22 14:51:15 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfaceObject(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 22:38:19 +04:00
|
|
|
JS::Handle<JSObject*> constructorProto,
|
2013-09-11 16:49:05 +04:00
|
|
|
const JSClass* constructorClass,
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* constructorNative,
|
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> proto,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2013-08-08 01:40:00 +04:00
|
|
|
const char* name, bool defineOnGlobal)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> constructor(cx);
|
2012-05-03 08:35:38 +04:00
|
|
|
if (constructorClass) {
|
2013-05-02 22:38:19 +04:00
|
|
|
MOZ_ASSERT(constructorProto);
|
2012-12-07 00:21:18 +04:00
|
|
|
constructor = JS_NewObject(cx, constructorClass, constructorProto, global);
|
2012-05-03 08:35:38 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(constructorNative);
|
2013-05-02 22:38:19 +04:00
|
|
|
MOZ_ASSERT(constructorProto == JS_GetFunctionPrototype(cx, global));
|
2013-01-22 14:51:15 +04:00
|
|
|
constructor = CreateConstructor(cx, global, name, constructorNative,
|
|
|
|
ctorNargs);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
if (!constructor) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-05-02 22:38:19 +04:00
|
|
|
if (constructorClass) {
|
2012-12-07 00:21:18 +04:00
|
|
|
// Have to shadow Function.prototype.toString, since that throws
|
|
|
|
// on things that are not js::FunctionClass.
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSFunction*> toString(cx,
|
|
|
|
js::DefineFunctionWithReserved(cx, constructor,
|
|
|
|
"toString",
|
|
|
|
InterfaceObjectToString,
|
|
|
|
0, 0));
|
2012-05-02 17:28:17 +04:00
|
|
|
if (!toString) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-05-02 17:28:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSString *str = ::JS_InternString(cx, name);
|
|
|
|
if (!str) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-05-02 17:28:17 +04:00
|
|
|
}
|
2013-05-02 07:44:11 +04:00
|
|
|
JSObject* toStringObj = JS_GetFunctionObject(toString);
|
|
|
|
js::SetFunctionNativeReserved(toStringObj, TOSTRING_CLASS_RESERVED_SLOT,
|
2013-09-11 16:49:05 +04:00
|
|
|
PRIVATE_TO_JSVAL(const_cast<JSClass *>(constructorClass)));
|
2013-05-02 07:44:11 +04:00
|
|
|
|
2012-05-02 17:28:17 +04:00
|
|
|
js::SetFunctionNativeReserved(toStringObj, TOSTRING_NAME_RESERVED_SLOT,
|
|
|
|
STRING_TO_JSVAL(str));
|
2012-12-14 23:10:49 +04:00
|
|
|
|
2014-01-22 06:11:17 +04:00
|
|
|
if (!JS_DefineProperty(cx, constructor, "length", ctorNargs,
|
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT)) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-12-14 23:10:49 +04:00
|
|
|
}
|
2012-05-02 17:28:17 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties) {
|
|
|
|
if (properties->staticMethods &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->staticMethods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (properties->staticAttributes &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->staticAttributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties->constants &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->constants)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties) {
|
|
|
|
if (chromeOnlyProperties->staticMethods &&
|
|
|
|
!DefinePrefable(cx, constructor, chromeOnlyProperties->staticMethods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (chromeOnlyProperties->staticAttributes &&
|
|
|
|
!DefinePrefable(cx, constructor,
|
|
|
|
chromeOnlyProperties->staticAttributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (chromeOnlyProperties->constants &&
|
|
|
|
!DefinePrefable(cx, constructor, chromeOnlyProperties->constants)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (proto && !JS_LinkConstructorAndPrototype(cx, constructor, proto)) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-08-08 01:40:00 +04:00
|
|
|
if (defineOnGlobal && !DefineConstructor(cx, global, name, constructor)) {
|
2013-01-22 14:51:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (namedConstructors) {
|
|
|
|
int namedConstructorSlot = DOM_INTERFACE_SLOTS_BASE;
|
|
|
|
while (namedConstructors->mName) {
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> namedConstructor(cx,
|
|
|
|
CreateConstructor(cx, global, namedConstructors->mName,
|
|
|
|
&namedConstructors->mHolder,
|
|
|
|
namedConstructors->mNargs));
|
2013-01-22 14:51:15 +04:00
|
|
|
if (!namedConstructor ||
|
|
|
|
!JS_DefineProperty(cx, namedConstructor, "prototype",
|
2014-01-22 06:11:17 +04:00
|
|
|
proto, JSPROP_PERMANENT | JSPROP_READONLY,
|
|
|
|
JS_PropertyStub, JS_StrictPropertyStub) ||
|
2013-08-08 01:40:00 +04:00
|
|
|
(defineOnGlobal &&
|
|
|
|
!DefineConstructor(cx, global, namedConstructors->mName,
|
|
|
|
namedConstructor))) {
|
2013-01-22 14:51:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
js::SetReservedSlot(constructor, namedConstructorSlot++,
|
|
|
|
JS::ObjectValue(*namedConstructor));
|
|
|
|
++namedConstructors;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
}
|
|
|
|
|
2014-02-16 01:12:34 +04:00
|
|
|
bool
|
|
|
|
DefineWebIDLBindingUnforgeablePropertiesOnXPCObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
const NativeProperties* properties)
|
|
|
|
{
|
|
|
|
if (properties->unforgeableAttributes &&
|
|
|
|
!DefinePrefable(cx, obj, properties->unforgeableAttributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:09:41 +04:00
|
|
|
bool
|
2013-10-08 20:18:40 +04:00
|
|
|
DefineWebIDLBindingPropertiesOnXPCObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2014-02-16 01:12:34 +04:00
|
|
|
const NativeProperties* properties)
|
2012-11-22 15:09:41 +04:00
|
|
|
{
|
|
|
|
if (properties->methods &&
|
2013-10-08 20:18:40 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->methods)) {
|
2012-11-22 15:09:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties->attributes &&
|
2013-10-08 20:18:40 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->attributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:09:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfacePrototypeObject(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> parentProto,
|
2013-09-11 16:49:05 +04:00
|
|
|
const JSClass* protoClass,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> ourProto(cx,
|
|
|
|
JS_NewObjectWithUniqueType(cx, protoClass, parentProto, global));
|
2014-02-16 01:12:34 +04:00
|
|
|
if (!ourProto ||
|
|
|
|
!DefineProperties(cx, ourProto, properties, chromeOnlyProperties)) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2014-02-16 01:12:34 +04:00
|
|
|
return ourProto;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DefineProperties(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties)
|
|
|
|
{
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties) {
|
|
|
|
if (properties->methods &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->methods)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties->attributes &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->attributes)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (properties->constants &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->constants)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (chromeOnlyProperties) {
|
|
|
|
if (chromeOnlyProperties->methods &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->methods)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties->attributes &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->attributes)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties->constants &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->constants)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2014-02-16 01:12:34 +04:00
|
|
|
return true;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
void
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfaceObjects(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> protoProto,
|
2013-09-11 16:49:05 +04:00
|
|
|
const JSClass* protoClass, JS::Heap<JSObject*>* protoCache,
|
2013-05-02 22:38:19 +04:00
|
|
|
JS::Handle<JSObject*> constructorProto,
|
2013-09-11 16:49:05 +04:00
|
|
|
const JSClass* constructorClass, const JSNativeHolder* constructor,
|
2013-01-22 14:51:15 +04:00
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
2014-05-20 00:37:59 +04:00
|
|
|
JS::Heap<JSObject*>* constructorCache,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2013-08-08 01:40:00 +04:00
|
|
|
const char* name, bool defineOnGlobal)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-05-03 08:35:38 +04:00
|
|
|
MOZ_ASSERT(protoClass || constructorClass || constructor,
|
|
|
|
"Need at least one class or a constructor!");
|
2012-10-09 22:50:05 +04:00
|
|
|
MOZ_ASSERT(!((properties &&
|
|
|
|
(properties->methods || properties->attributes)) ||
|
|
|
|
(chromeOnlyProperties &&
|
|
|
|
(chromeOnlyProperties->methods ||
|
|
|
|
chromeOnlyProperties->attributes))) || protoClass,
|
2012-03-31 08:42:20 +04:00
|
|
|
"Methods or properties but no protoClass!");
|
2012-10-19 11:34:28 +04:00
|
|
|
MOZ_ASSERT(!((properties &&
|
|
|
|
(properties->staticMethods || properties->staticAttributes)) ||
|
|
|
|
(chromeOnlyProperties &&
|
|
|
|
(chromeOnlyProperties->staticMethods ||
|
|
|
|
chromeOnlyProperties->staticAttributes))) ||
|
2012-10-09 22:50:05 +04:00
|
|
|
constructorClass || constructor,
|
2012-05-03 08:35:38 +04:00
|
|
|
"Static methods but no constructorClass or constructor!");
|
|
|
|
MOZ_ASSERT(bool(name) == bool(constructorClass || constructor),
|
2012-03-31 08:42:20 +04:00
|
|
|
"Must have name precisely when we have an interface object");
|
2012-05-03 08:35:38 +04:00
|
|
|
MOZ_ASSERT(!constructorClass || !constructor);
|
2012-10-09 22:50:27 +04:00
|
|
|
MOZ_ASSERT(!protoClass == !protoCache,
|
|
|
|
"If, and only if, there is an interface prototype object we need "
|
|
|
|
"to cache it");
|
|
|
|
MOZ_ASSERT(!(constructorClass || constructor) == !constructorCache,
|
|
|
|
"If, and only if, there is an interface object we need to cache "
|
|
|
|
"it");
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (protoClass) {
|
2013-05-02 07:44:11 +04:00
|
|
|
proto =
|
|
|
|
CreateInterfacePrototypeObject(cx, global, protoProto, protoClass,
|
|
|
|
properties, chromeOnlyProperties);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!proto) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-08-08 09:26:18 +04:00
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
*protoCache = proto;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-05-02 07:44:11 +04:00
|
|
|
MOZ_ASSERT(!proto);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* interface;
|
2012-05-03 08:35:38 +04:00
|
|
|
if (constructorClass || constructor) {
|
2013-05-02 22:38:19 +04:00
|
|
|
interface = CreateInterfaceObject(cx, global, constructorProto,
|
|
|
|
constructorClass, constructor,
|
2013-01-22 14:51:15 +04:00
|
|
|
ctorNargs, namedConstructors, proto,
|
2013-08-08 01:40:00 +04:00
|
|
|
properties, chromeOnlyProperties, name,
|
|
|
|
defineOnGlobal);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!interface) {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (protoCache) {
|
|
|
|
// If we fail we need to make sure to clear the value of protoCache we
|
|
|
|
// set above.
|
|
|
|
*protoCache = nullptr;
|
|
|
|
}
|
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
*constructorCache = interface;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:20:04 +04:00
|
|
|
bool
|
|
|
|
NativeInterface2JSObjectAndThrowIfFailed(JSContext* aCx,
|
2013-04-30 01:33:42 +04:00
|
|
|
JS::Handle<JSObject*> aScope,
|
2013-10-12 09:02:39 +04:00
|
|
|
JS::MutableHandle<JS::Value> aRetval,
|
2012-03-31 08:42:20 +04:00
|
|
|
xpcObjectHelper& aHelper,
|
|
|
|
const nsIID* aIID,
|
|
|
|
bool aAllowNativeWrapper)
|
|
|
|
{
|
2014-04-09 02:27:18 +04:00
|
|
|
js::AssertSameCompartment(aCx, aScope);
|
2012-03-31 08:42:20 +04:00
|
|
|
nsresult rv;
|
2013-09-10 01:58:29 +04:00
|
|
|
// Inline some logic from XPCConvert::NativeInterfaceToJSObject that we need
|
|
|
|
// on all threads.
|
|
|
|
nsWrapperCache *cache = aHelper.GetWrapperCache();
|
|
|
|
|
|
|
|
if (cache && cache->IsDOMBinding()) {
|
2013-10-16 02:34:13 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, cache->GetWrapper());
|
2013-09-10 01:58:29 +04:00
|
|
|
if (!obj) {
|
2014-04-09 02:27:18 +04:00
|
|
|
obj = cache->WrapObject(aCx);
|
2013-09-10 01:58:29 +04:00
|
|
|
}
|
|
|
|
|
2013-10-16 04:02:23 +04:00
|
|
|
if (obj && aAllowNativeWrapper && !JS_WrapObject(aCx, &obj)) {
|
2013-09-10 01:58:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj) {
|
2013-10-12 09:02:39 +04:00
|
|
|
aRetval.setObject(*obj);
|
2013-09-10 01:58:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-10-28 18:04:12 +04:00
|
|
|
if (!XPCConvert::NativeInterface2JSObject(aRetval, nullptr, aHelper, aIID,
|
|
|
|
nullptr, aAllowNativeWrapper, &rv)) {
|
2012-03-31 08:42:20 +04:00
|
|
|
// I can't tell if NativeInterface2JSObject throws JS exceptions
|
|
|
|
// or not. This is a sloppy stab at the right semantics; the
|
|
|
|
// method really ought to be fixed to behave consistently.
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, NS_FAILED(rv) ? rv : NS_ERROR_UNEXPECTED);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-09 22:59:02 +04:00
|
|
|
bool
|
|
|
|
TryPreserveWrapper(JSObject* obj)
|
|
|
|
{
|
2013-07-01 11:14:36 +04:00
|
|
|
MOZ_ASSERT(IsDOMObject(obj));
|
|
|
|
|
2013-06-01 10:56:02 +04:00
|
|
|
if (nsISupports* native = UnwrapDOMObjectToISupports(obj)) {
|
2012-11-09 22:59:02 +04:00
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(native, &cache);
|
|
|
|
if (cache) {
|
2013-06-23 11:15:42 +04:00
|
|
|
cache->PreserveWrapper(native);
|
2012-11-09 22:59:02 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this DOMClass is not cycle collected, then it isn't wrappercached,
|
|
|
|
// so it does not need to be preserved. If it is cycle collected, then
|
|
|
|
// we can't tell if it is wrappercached or not, so we just return false.
|
|
|
|
const DOMClass* domClass = GetDOMClass(obj);
|
|
|
|
return domClass && !domClass->mParticipant;
|
|
|
|
}
|
|
|
|
|
2014-05-20 00:37:59 +04:00
|
|
|
// Can only be called with a DOM JSClass.
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2014-05-20 00:37:59 +04:00
|
|
|
InstanceClassHasProtoAtDepth(const js::Class* clasp,
|
|
|
|
uint32_t protoID, uint32_t depth)
|
2012-08-08 09:26:18 +04:00
|
|
|
{
|
2014-05-20 00:37:59 +04:00
|
|
|
const DOMClass& domClass = DOMJSClass::FromJSClass(clasp)->mClass;
|
|
|
|
return static_cast<uint32_t>(domClass.mInterfaceChain[depth]) == protoID;
|
2012-08-08 09:26:18 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04: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-30 01:33:42 +04:00
|
|
|
XPCOMObjectToJsval(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
xpcObjectHelper& helper, const nsIID* iid,
|
2013-10-12 09:02:39 +04:00
|
|
|
bool allowNativeWrapper, JS::MutableHandle<JS::Value> rval)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-11-27 13:20:04 +04:00
|
|
|
if (!NativeInterface2JSObjectAndThrowIfFailed(cx, scope, rval, helper, iid,
|
2012-03-31 08:42:20 +04:00
|
|
|
allowNativeWrapper)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-10-16 02:34:13 +04:00
|
|
|
JSObject* jsobj = rval.toObjectOrNull();
|
2012-03-31 08:42:20 +04:00
|
|
|
if (jsobj && !js::GetObjectParent(jsobj))
|
|
|
|
NS_ASSERTION(js::GetObjectClass(jsobj)->flags & JSCLASS_IS_GLOBAL,
|
|
|
|
"Why did we recreate this wrapper?");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-04 06:22:16 +04:00
|
|
|
bool
|
2014-04-10 08:58:43 +04:00
|
|
|
VariantToJsval(JSContext* aCx, nsIVariant* aVariant,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval)
|
2013-04-04 06:22:16 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2013-06-05 08:11:19 +04:00
|
|
|
if (!XPCVariant::VariantDataToJS(aVariant, &rv, aRetval)) {
|
2013-04-04 06:22:16 +04:00
|
|
|
// Does it throw? Who knows
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, NS_FAILED(rv) ? rv : NS_ERROR_UNEXPECTED);
|
2013-04-04 06:22:16 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
bool
|
2012-03-31 08:42:20 +04:00
|
|
|
QueryInterface(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2013-06-08 06:45:46 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JS::Value> thisv(cx, JS_THIS(cx, vp));
|
2013-04-30 01:33:42 +04:00
|
|
|
if (thisv.isNull())
|
2012-03-31 08:42:20 +04:00
|
|
|
return false;
|
|
|
|
|
2012-06-12 17:44:21 +04:00
|
|
|
// Get the object. It might be a security wrapper, in which case we do a checked
|
|
|
|
// unwrap.
|
2013-04-30 01:33:42 +04:00
|
|
|
JS::Rooted<JSObject*> origObj(cx, &thisv.toObject());
|
2014-02-16 01:12:33 +04:00
|
|
|
JSObject* obj = js::CheckedUnwrap(origObj, /* stopAtOuter = */ false);
|
2012-11-14 21:56:26 +04:00
|
|
|
if (!obj) {
|
|
|
|
JS_ReportError(cx, "Permission denied to access object");
|
2012-06-12 17:44:21 +04:00
|
|
|
return false;
|
2012-11-14 21:56:26 +04:00
|
|
|
}
|
2012-06-12 17:44:21 +04:00
|
|
|
|
2014-02-16 01:12:33 +04:00
|
|
|
// Switch this to UnwrapDOMObjectToISupports once our global objects are
|
|
|
|
// using new bindings.
|
|
|
|
JS::Rooted<JS::Value> val(cx, JS::ObjectValue(*obj));
|
|
|
|
nsISupports* native = nullptr;
|
|
|
|
nsCOMPtr<nsISupports> nativeRef;
|
|
|
|
xpc_qsUnwrapArg<nsISupports>(cx, val, &native,
|
|
|
|
static_cast<nsISupports**>(getter_AddRefs(nativeRef)),
|
|
|
|
&val);
|
2013-06-01 10:56:02 +04:00
|
|
|
if (!native) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 1) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-10-16 02:34:13 +04:00
|
|
|
if (!args[0].isObject()) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:51:55 +04:00
|
|
|
nsIJSID* iid;
|
2013-05-30 07:15:31 +04:00
|
|
|
SelfRef iidRef;
|
2013-10-12 09:02:39 +04:00
|
|
|
if (NS_FAILED(xpc_qsUnwrapArg<nsIJSID>(cx, args[0], &iid, &iidRef.ptr,
|
|
|
|
args[0]))) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(iid);
|
|
|
|
|
|
|
|
if (iid->GetID()->Equals(NS_GET_IID(nsIClassInfo))) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIClassInfo> ci = do_QueryInterface(native, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, rv);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:58:43 +04:00
|
|
|
return WrapObject(cx, ci, &NS_GET_IID(nsIClassInfo), args.rval());
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-06-12 17:44:21 +04:00
|
|
|
|
2013-01-14 14:29:49 +04:00
|
|
|
nsCOMPtr<nsISupports> unused;
|
|
|
|
nsresult rv = native->QueryInterface(*iid->GetID(), getter_AddRefs(unused));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, rv);
|
2013-01-14 14:29:49 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
*vp = thisv;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-16 01:12:33 +04:00
|
|
|
JS::Value
|
|
|
|
GetInterfaceImpl(JSContext* aCx, nsIInterfaceRequestor* aRequestor,
|
|
|
|
nsWrapperCache* aCache, nsIJSID* aIID, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
const nsID* iid = aIID->GetID();
|
|
|
|
|
|
|
|
nsRefPtr<nsISupports> result;
|
|
|
|
aError = aRequestor->GetInterface(*iid, getter_AddRefs(result));
|
|
|
|
if (aError.Failed()) {
|
|
|
|
return JS::NullValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> v(aCx, JSVAL_NULL);
|
2014-04-10 08:58:43 +04:00
|
|
|
if (!WrapObject(aCx, result, iid, &v)) {
|
2014-02-16 01:12:33 +04:00
|
|
|
aError.Throw(NS_ERROR_FAILURE);
|
|
|
|
return JS::NullValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
bool
|
2012-05-03 08:35:38 +04:00
|
|
|
ThrowingConstructor(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2012-08-04 11:44:00 +04:00
|
|
|
return ThrowErrorMessage(cx, MSG_ILLEGAL_CONSTRUCTOR);
|
2012-05-03 08:35:38 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 01:38:31 +04:00
|
|
|
bool
|
|
|
|
ThrowConstructorWithoutNew(JSContext* cx, const char* name)
|
|
|
|
{
|
|
|
|
return ThrowErrorMessage(cx, MSG_CONSTRUCTOR_WITHOUT_NEW, name);
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
inline const NativePropertyHooks*
|
2013-05-04 03:29:08 +04:00
|
|
|
GetNativePropertyHooks(JSContext *cx, JS::Handle<JSObject*> obj,
|
|
|
|
DOMObjectType& type)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
2012-12-07 00:21:19 +04:00
|
|
|
const DOMClass* domClass = GetDOMClass(obj);
|
|
|
|
if (domClass) {
|
2012-10-09 22:50:27 +04:00
|
|
|
type = eInstance;
|
|
|
|
return domClass->mNativeHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JS_ObjectIsFunction(cx, obj)) {
|
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
type = eInterface;
|
|
|
|
const JS::Value& v =
|
|
|
|
js::GetFunctionNativeReserved(obj,
|
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT);
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* nativeHolder =
|
|
|
|
static_cast<const JSNativeHolder*>(v.toPrivate());
|
2012-10-09 22:50:27 +04:00
|
|
|
return nativeHolder->mPropertyHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(obj)));
|
|
|
|
const DOMIfaceAndProtoJSClass* ifaceAndProtoJSClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(js::GetObjectClass(obj));
|
|
|
|
type = ifaceAndProtoJSClass->mType;
|
|
|
|
return ifaceAndProtoJSClass->mNativeHooks;
|
|
|
|
}
|
|
|
|
|
2013-09-25 22:38:29 +04:00
|
|
|
// Try to resolve a property as an unforgeable property from the given
|
|
|
|
// NativeProperties, if it's there. nativeProperties is allowed to be null (in
|
|
|
|
// which case we of course won't resolve anything).
|
|
|
|
static bool
|
|
|
|
XrayResolveUnforgeableProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
const NativeProperties* nativeProperties);
|
|
|
|
|
2013-09-25 22:38:30 +04:00
|
|
|
static bool
|
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
const NativePropertyHooks* nativePropertyHooks,
|
|
|
|
DOMObjectType type, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc);
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveOwnProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2014-04-26 01:11:02 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks *nativePropertyHooks =
|
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
|
|
|
|
2013-09-25 22:38:29 +04:00
|
|
|
if (type != eInstance) {
|
2013-09-25 22:38:30 +04:00
|
|
|
// For prototype objects and interface objects, just return their
|
|
|
|
// normal set of properties.
|
|
|
|
return XrayResolveNativeProperty(cx, wrapper, nativePropertyHooks, type,
|
|
|
|
obj, id, desc);
|
2013-09-25 22:38:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for unforgeable properties before doing mResolveOwnProperty weirdness
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
if (!XrayResolveUnforgeableProperty(cx, wrapper, obj, id, desc,
|
|
|
|
nativeProperties.regular)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc.object()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!XrayResolveUnforgeableProperty(cx, wrapper, obj, id, desc,
|
|
|
|
nativeProperties.chromeOnly)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc.object()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !nativePropertyHooks->mResolveOwnProperty ||
|
2014-04-26 01:11:02 +04:00
|
|
|
nativePropertyHooks->mResolveOwnProperty(cx, wrapper, obj, id, desc);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveAttribute(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSPropertySpec>* attributes, jsid* attributeIds,
|
2013-08-12 15:09:14 +04:00
|
|
|
const JSPropertySpec* attributeSpecs, JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
for (; attributes->specs; ++attributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (attributes->isEnabled(cx, obj)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = attributes->specs - attributeSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; attributeIds[i] != JSID_VOID; ++i) {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (id == attributeIds[i]) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSPropertySpec& attrSpec = attributeSpecs[i];
|
2012-10-09 22:50:27 +04:00
|
|
|
// Because of centralization, we need to make sure we fault in the
|
|
|
|
// JitInfos as well. At present, until the JSAPI changes, the easiest
|
|
|
|
// way to do this is wrap them up as functions ourselves.
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setAttributes(attrSpec.flags & ~JSPROP_NATIVE_ACCESSORS);
|
2012-10-09 22:50:27 +04:00
|
|
|
// They all have getters, so we can just make it.
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSFunction*> fun(cx,
|
2014-01-10 21:11:31 +04:00
|
|
|
JS_NewFunctionById(cx, (JSNative)attrSpec.getter.propertyOp.op,
|
2014-03-04 22:05:07 +04:00
|
|
|
0, 0, wrapper, id));
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
2014-01-10 21:11:31 +04:00
|
|
|
SET_JITINFO(fun, attrSpec.getter.propertyOp.info);
|
2012-10-09 22:50:27 +04:00
|
|
|
JSObject *funobj = JS_GetFunctionObject(fun);
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setGetterObject(funobj);
|
|
|
|
desc.attributesRef() |= JSPROP_GETTER;
|
2014-01-10 21:11:31 +04:00
|
|
|
if (attrSpec.setter.propertyOp.op) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// We have a setter! Make it.
|
2014-01-10 21:11:31 +04:00
|
|
|
fun = JS_NewFunctionById(cx, (JSNative)attrSpec.setter.propertyOp.op, 1, 0,
|
2014-03-04 22:05:07 +04:00
|
|
|
wrapper, id);
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
2014-01-10 21:11:31 +04:00
|
|
|
SET_JITINFO(fun, attrSpec.setter.propertyOp.info);
|
2012-10-09 22:50:27 +04:00
|
|
|
funobj = JS_GetFunctionObject(fun);
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setSetterObject(funobj);
|
|
|
|
desc.attributesRef() |= JSPROP_SETTER;
|
2012-10-09 22:50:27 +04:00
|
|
|
} else {
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setSetter(nullptr);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.object().set(wrapper);
|
2012-10-09 22:50:27 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-25 22:38:29 +04:00
|
|
|
/* static */ bool
|
|
|
|
XrayResolveUnforgeableProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
const NativeProperties* nativeProperties)
|
|
|
|
{
|
|
|
|
return !nativeProperties || !nativeProperties->unforgeableAttributes ||
|
|
|
|
XrayResolveAttribute(cx, wrapper, obj, id,
|
|
|
|
nativeProperties->unforgeableAttributes,
|
|
|
|
nativeProperties->unforgeableAttributeIds,
|
|
|
|
nativeProperties->unforgeableAttributeSpecs,
|
|
|
|
desc);
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-08-12 15:09:14 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc, DOMObjectType type,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 15:52:07 +04:00
|
|
|
{
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* methods;
|
2012-10-09 22:50:27 +04:00
|
|
|
jsid* methodIds;
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSFunctionSpec* methodsSpecs;
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
methods = nativeProperties->staticMethods;
|
|
|
|
methodIds = nativeProperties->staticMethodIds;
|
|
|
|
methodsSpecs = nativeProperties->staticMethodsSpecs;
|
|
|
|
} else {
|
|
|
|
methods = nativeProperties->methods;
|
|
|
|
methodIds = nativeProperties->methodIds;
|
|
|
|
methodsSpecs = nativeProperties->methodsSpecs;
|
|
|
|
}
|
|
|
|
if (methods) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* method;
|
2012-10-09 22:50:27 +04:00
|
|
|
for (method = methods; method->specs; ++method) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (method->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2012-10-09 22:50:27 +04:00
|
|
|
size_t i = method->specs - methodsSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; methodIds[i] != JSID_VOID; ++i) {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (id == methodIds[i]) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSFunctionSpec& methodSpec = methodsSpecs[i];
|
2013-09-26 18:11:38 +04:00
|
|
|
JSFunction *fun;
|
|
|
|
if (methodSpec.selfHostedName) {
|
|
|
|
fun = JS::GetSelfHostedFunction(cx, methodSpec.selfHostedName, id, methodSpec.nargs);
|
|
|
|
if (!fun) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!methodSpec.call.op, "Bad FunctionSpec declaration: non-null native");
|
|
|
|
MOZ_ASSERT(!methodSpec.call.info, "Bad FunctionSpec declaration: non-null jitinfo");
|
|
|
|
} else {
|
|
|
|
fun = JS_NewFunctionById(cx, methodSpec.call.op, methodSpec.nargs, 0, wrapper, id);
|
|
|
|
if (!fun) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SET_JITINFO(fun, methodSpec.call.info);
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
JSObject *funobj = JS_GetFunctionObject(fun);
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.value().setObject(*funobj);
|
|
|
|
desc.setAttributes(methodSpec.flags);
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.setSetter(nullptr);
|
|
|
|
desc.setGetter(nullptr);
|
2012-10-09 22:50:05 +04:00
|
|
|
return true;
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
if (nativeProperties->staticAttributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-19 11:34:28 +04:00
|
|
|
nativeProperties->staticAttributes,
|
|
|
|
nativeProperties->staticAttributeIds,
|
|
|
|
nativeProperties->staticAttributeSpecs, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 15:09:14 +04:00
|
|
|
if (desc.object()) {
|
2012-10-19 11:34:28 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (nativeProperties->attributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties->attributes,
|
|
|
|
nativeProperties->attributeIds,
|
|
|
|
nativeProperties->attributeSpecs, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 15:09:14 +04:00
|
|
|
if (desc.object()) {
|
2013-09-26 02:22:33 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (nativeProperties->constants) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2012-10-09 22:50:05 +04:00
|
|
|
for (constant = nativeProperties->constants; constant->specs; ++constant) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (constant->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = constant->specs - nativeProperties->constantSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; nativeProperties->constantIds[i] != JSID_VOID; ++i) {
|
2012-10-09 22:50:05 +04:00
|
|
|
if (id == nativeProperties->constantIds[i]) {
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setAttributes(JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.value().set(nativeProperties->constantSpecs[i].value);
|
2012-10-09 22:50:05 +04:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
ResolvePrototypeOrConstructor(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2014-03-22 21:04:09 +04:00
|
|
|
size_t protoAndIfaceCacheIndex, unsigned attrs,
|
2013-08-12 15:09:14 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> global(cx, js::GetGlobalForObjectCrossCompartment(obj));
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, global);
|
2014-03-22 21:04:09 +04:00
|
|
|
ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
|
2013-12-11 01:04:55 +04:00
|
|
|
JSObject* protoOrIface =
|
2014-03-22 21:04:09 +04:00
|
|
|
protoAndIfaceCache.EntrySlotIfExists(protoAndIfaceCacheIndex);
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!protoOrIface) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.setAttributes(attrs);
|
|
|
|
desc.setGetter(JS_PropertyStub);
|
|
|
|
desc.setSetter(JS_StrictPropertyStub);
|
|
|
|
desc.value().set(JS::ObjectValue(*protoOrIface));
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
|
|
|
}
|
|
|
|
|
2013-09-25 22:38:30 +04:00
|
|
|
/* static */ bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2012-10-09 22:50:27 +04:00
|
|
|
const NativePropertyHooks* nativePropertyHooks,
|
2013-05-04 03:29:08 +04:00
|
|
|
DOMObjectType type, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id,
|
2013-08-12 15:09:14 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 22:50:05 +04:00
|
|
|
{
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface && IdEquals(id, "prototype")) {
|
|
|
|
return nativePropertyHooks->mPrototypeID == prototypes::id::_ID_Count ||
|
|
|
|
ResolvePrototypeOrConstructor(cx, wrapper, obj,
|
|
|
|
nativePropertyHooks->mPrototypeID,
|
2012-10-30 00:55:13 +04:00
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY,
|
2012-10-09 22:50:27 +04:00
|
|
|
desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eInterfacePrototype && IdEquals(id, "constructor")) {
|
|
|
|
return nativePropertyHooks->mConstructorID == constructors::id::_ID_Count ||
|
|
|
|
ResolvePrototypeOrConstructor(cx, wrapper, obj,
|
|
|
|
nativePropertyHooks->mConstructorID,
|
2012-10-30 00:55:13 +04:00
|
|
|
0, desc);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
|
|
|
|
if (nativeProperties.regular &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayResolveProperty(cx, wrapper, obj, id, desc, type,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.regular)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-12 15:09:14 +04:00
|
|
|
if (!desc.object() &&
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.chromeOnly &&
|
2012-10-09 22:50:05 +04:00
|
|
|
xpc::AccessCheck::isChrome(js::GetObjectCompartment(wrapper)) &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayResolveProperty(cx, wrapper, obj, id, desc, type,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.chromeOnly)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-18 15:52:07 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2013-08-12 15:09:14 +04:00
|
|
|
JS::Handle<jsid> id, JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks* nativePropertyHooks =
|
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
|
|
|
|
|
|
|
if (type == eInstance) {
|
|
|
|
// Force the type to be eInterfacePrototype, since we need to walk the
|
|
|
|
// prototype chain.
|
|
|
|
type = eInterfacePrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eInterfacePrototype) {
|
|
|
|
do {
|
|
|
|
if (!XrayResolveNativeProperty(cx, wrapper, nativePropertyHooks, type,
|
|
|
|
obj, id, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-12 15:09:14 +04:00
|
|
|
if (desc.object()) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while ((nativePropertyHooks = nativePropertyHooks->mProtoHooks));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XrayResolveNativeProperty(cx, wrapper, nativePropertyHooks, type, obj,
|
|
|
|
id, desc);
|
|
|
|
}
|
|
|
|
|
2013-05-29 13:38:57 +04:00
|
|
|
bool
|
|
|
|
XrayDefineProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-04-30 21:29:40 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc, bool* defined)
|
2013-05-29 13:38:57 +04:00
|
|
|
{
|
|
|
|
if (!js::IsProxy(obj))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
MOZ_ASSERT(IsDOMProxy(obj), "What kind of proxy is this?");
|
|
|
|
|
|
|
|
DOMProxyHandler* handler =
|
|
|
|
static_cast<DOMProxyHandler*>(js::GetProxyHandler(obj));
|
|
|
|
return handler->defineProperty(cx, wrapper, id, desc, defined);
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayEnumerateAttributes(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSPropertySpec>* attributes,
|
|
|
|
jsid* attributeIds, const JSPropertySpec* attributeSpecs,
|
2012-11-05 20:58:03 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
for (; attributes->specs; ++attributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (attributes->isEnabled(cx, obj)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = attributes->specs - attributeSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; attributeIds[i] != JSID_VOID; ++i) {
|
2012-11-05 20:58:03 +04:00
|
|
|
if (((flags & JSITER_HIDDEN) ||
|
|
|
|
(attributeSpecs[i].flags & JSPROP_ENUMERATE)) &&
|
2012-10-09 22:50:27 +04:00
|
|
|
!props.append(attributeIds[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayEnumerateProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2013-02-19 20:54:40 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props,
|
2012-11-05 20:58:03 +04:00
|
|
|
DOMObjectType type,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 15:52:07 +04:00
|
|
|
{
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* methods;
|
2012-10-09 22:50:27 +04:00
|
|
|
jsid* methodIds;
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSFunctionSpec* methodsSpecs;
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
methods = nativeProperties->staticMethods;
|
|
|
|
methodIds = nativeProperties->staticMethodIds;
|
|
|
|
methodsSpecs = nativeProperties->staticMethodsSpecs;
|
|
|
|
} else {
|
|
|
|
methods = nativeProperties->methods;
|
|
|
|
methodIds = nativeProperties->methodIds;
|
|
|
|
methodsSpecs = nativeProperties->methodsSpecs;
|
|
|
|
}
|
|
|
|
if (methods) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* method;
|
2012-10-09 22:50:27 +04:00
|
|
|
for (method = methods; method->specs; ++method) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (method->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2012-10-09 22:50:27 +04:00
|
|
|
size_t i = method->specs - methodsSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; methodIds[i] != JSID_VOID; ++i) {
|
2012-11-05 20:58:03 +04:00
|
|
|
if (((flags & JSITER_HIDDEN) ||
|
|
|
|
(methodsSpecs[i].flags & JSPROP_ENUMERATE)) &&
|
2012-10-09 22:50:27 +04:00
|
|
|
!props.append(methodIds[i])) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
if (nativeProperties->staticAttributes &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->staticAttributes,
|
2012-10-19 11:34:28 +04:00
|
|
|
nativeProperties->staticAttributeIds,
|
|
|
|
nativeProperties->staticAttributeSpecs,
|
2012-11-05 20:58:03 +04:00
|
|
|
flags, props)) {
|
2012-10-19 11:34:28 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (nativeProperties->attributes &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->attributes,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties->attributeIds,
|
2012-11-05 20:58:03 +04:00
|
|
|
nativeProperties->attributeSpecs,
|
|
|
|
flags, props)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nativeProperties->unforgeableAttributes &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->unforgeableAttributes,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties->unforgeableAttributeIds,
|
|
|
|
nativeProperties->unforgeableAttributeSpecs,
|
2012-11-05 20:58:03 +04:00
|
|
|
flags, props)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (nativeProperties->constants) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2012-10-09 22:50:05 +04:00
|
|
|
for (constant = nativeProperties->constants; constant->specs; ++constant) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (constant->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = constant->specs - nativeProperties->constantSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; nativeProperties->constantIds[i] != JSID_VOID; ++i) {
|
2012-10-09 22:50:05 +04:00
|
|
|
if (!props.append(nativeProperties->constantIds[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayEnumerateNativeProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2012-10-09 22:50:27 +04:00
|
|
|
const NativePropertyHooks* nativePropertyHooks,
|
2013-05-04 03:29:08 +04:00
|
|
|
DOMObjectType type, JS::Handle<JSObject*> obj,
|
2012-11-05 20:58:03 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:05 +04:00
|
|
|
{
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface &&
|
|
|
|
nativePropertyHooks->mPrototypeID != prototypes::id::_ID_Count &&
|
|
|
|
!AddStringToIDVector(cx, props, "prototype")) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterfacePrototype &&
|
|
|
|
nativePropertyHooks->mConstructorID != constructors::id::_ID_Count &&
|
2012-11-05 20:58:03 +04:00
|
|
|
(flags & JSITER_HIDDEN) &&
|
2012-10-09 22:50:27 +04:00
|
|
|
!AddStringToIDVector(cx, props, "constructor")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
|
|
|
|
if (nativeProperties.regular &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateProperties(cx, wrapper, obj, flags, props, type,
|
|
|
|
nativeProperties.regular)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeProperties.chromeOnly &&
|
2012-10-09 22:50:05 +04:00
|
|
|
xpc::AccessCheck::isChrome(js::GetObjectCompartment(wrapper)) &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateProperties(cx, wrapper, obj, flags, props, type,
|
|
|
|
nativeProperties.chromeOnly)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayEnumerateProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2012-11-05 20:58:03 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks* nativePropertyHooks =
|
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
|
|
|
|
|
|
|
if (type == eInstance) {
|
|
|
|
if (nativePropertyHooks->mEnumerateOwnProperties &&
|
|
|
|
!nativePropertyHooks->mEnumerateOwnProperties(cx, wrapper, obj,
|
|
|
|
props)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
if (flags & JSITER_OWNONLY) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the type to be eInterfacePrototype, since we need to walk the
|
|
|
|
// prototype chain.
|
|
|
|
type = eInterfacePrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eInterfacePrototype) {
|
|
|
|
do {
|
|
|
|
if (!XrayEnumerateNativeProperties(cx, wrapper, nativePropertyHooks, type,
|
2012-11-05 20:58:03 +04:00
|
|
|
obj, flags, props)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-05 20:58:03 +04:00
|
|
|
|
|
|
|
if (flags & JSITER_OWNONLY) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
} while ((nativePropertyHooks = nativePropertyHooks->mProtoHooks));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XrayEnumerateNativeProperties(cx, wrapper, nativePropertyHooks, type,
|
2012-11-05 20:58:03 +04:00
|
|
|
obj, flags, props);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NativePropertyHooks sWorkerNativePropertyHooks = {
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
|
|
|
},
|
|
|
|
prototypes::id::_ID_Count,
|
|
|
|
constructors::id::_ID_Count,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2012-05-22 17:46:20 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
GetPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id, bool* found,
|
2012-05-22 17:46:20 +04:00
|
|
|
JS::Value* vp)
|
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-05-05 11:03:14 +04:00
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
2012-09-04 03:42:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 17:46:20 +04:00
|
|
|
if (!proto) {
|
|
|
|
*found = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool hasProp;
|
2012-05-22 17:46:20 +04:00
|
|
|
if (!JS_HasPropertyById(cx, proto, id, &hasProp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*found = hasProp;
|
|
|
|
if (!hasProp || !vp) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-26 13:00:38 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
if (!JS_ForwardGetPropertyTo(cx, proto, id, proxy, &value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = value;
|
|
|
|
return true;
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
HasPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id)
|
2012-05-22 17:46:20 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, proxy);
|
2012-08-22 05:42:53 +04:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-05-04 03:29:08 +04:00
|
|
|
if (xpc::WrapperFactory::IsXrayWrapper(obj)) {
|
|
|
|
obj = js::UncheckedUnwrap(obj);
|
|
|
|
ac.construct(cx, obj);
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool found;
|
2013-07-09 18:45:13 +04:00
|
|
|
// We ignore an error from GetPropertyOnPrototype. We pass nullptr
|
|
|
|
// for vp so that GetPropertyOnPrototype won't actually do a get.
|
|
|
|
return !GetPropertyOnPrototype(cx, obj, id, &found, nullptr) || found;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AppendNamedPropertyIds(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
nsTArray<nsString>& names,
|
|
|
|
bool shadowPrototypeProperties,
|
|
|
|
JS::AutoIdVector& props)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < names.Length(); ++i) {
|
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2013-10-12 09:02:39 +04:00
|
|
|
if (!xpc::NonVoidStringToJsval(cx, names[i], &v)) {
|
2013-07-09 18:45:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<jsid> id(cx);
|
2014-01-17 22:08:51 +04:00
|
|
|
if (!JS_ValueToId(cx, v, &id)) {
|
2013-07-09 18:45:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shadowPrototypeProperties || !HasPropertyOnPrototype(cx, proxy, id)) {
|
|
|
|
if (!props.append(id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 01:18:03 +04:00
|
|
|
bool
|
2014-02-07 06:08:28 +04:00
|
|
|
DictionaryBase::ParseJSON(JSContext* aCx,
|
|
|
|
const nsAString& aJSON,
|
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2012-10-16 18:52:10 +04:00
|
|
|
{
|
2012-10-16 19:37:44 +04:00
|
|
|
if (aJSON.IsEmpty()) {
|
2013-05-08 01:18:03 +04:00
|
|
|
return true;
|
2012-10-16 18:52:10 +04:00
|
|
|
}
|
2013-05-08 01:18:03 +04:00
|
|
|
return JS_ParseJSON(aCx,
|
|
|
|
static_cast<const jschar*>(PromiseFlatString(aJSON).get()),
|
2013-05-29 12:29:39 +04:00
|
|
|
aJSON.Length(), aVal);
|
2012-10-16 18:52:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
static JSString*
|
2013-09-17 05:33:40 +04:00
|
|
|
ConcatJSString(JSContext* cx, const char* pre, JS::Handle<JSString*> str, const char* post)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
if (!str) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSString*> preString(cx, JS_NewStringCopyN(cx, pre, strlen(pre)));
|
|
|
|
JS::Rooted<JSString*> postString(cx, JS_NewStringCopyN(cx, post, strlen(post)));
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!preString || !postString) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-17 05:33:40 +04:00
|
|
|
preString = JS_ConcatStrings(cx, preString, str);
|
|
|
|
if (!preString) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-17 05:33:40 +04:00
|
|
|
return JS_ConcatStrings(cx, preString, postString);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
NativeToString(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, const char* pre,
|
2013-10-26 20:19:05 +04:00
|
|
|
const char* post,
|
|
|
|
JS::MutableHandle<JS::Value> v)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSPropertyDescriptor> toStringDesc(cx);
|
|
|
|
toStringDesc.object().set(nullptr);
|
|
|
|
toStringDesc.setAttributes(0);
|
|
|
|
toStringDesc.setGetter(nullptr);
|
|
|
|
toStringDesc.setSetter(nullptr);
|
|
|
|
toStringDesc.value().set(JS::UndefinedValue());
|
|
|
|
JS::Rooted<jsid> id(cx,
|
|
|
|
nsXPConnect::GetRuntimeInstance()->GetStringID(XPCJSRuntime::IDX_TO_STRING));
|
2013-08-12 15:09:14 +04:00
|
|
|
if (!XrayResolveNativeProperty(cx, wrapper, obj, id, &toStringDesc)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSString*> str(cx);
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
2013-05-04 03:29:08 +04:00
|
|
|
if (toStringDesc.object()) {
|
|
|
|
JS::Rooted<JS::Value> toString(cx, toStringDesc.value());
|
2013-10-26 20:19:05 +04:00
|
|
|
if (!JS_WrapValue(cx, &toString)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(JS_ObjectIsCallable(cx, &toString.toObject()));
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JS::Value> toStringResult(cx);
|
2014-02-13 11:38:36 +04:00
|
|
|
if (JS_CallFunctionValue(cx, obj, toString, JS::HandleValueArray::empty(),
|
|
|
|
&toStringResult)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
str = toStringResult.toString();
|
|
|
|
} else {
|
|
|
|
str = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-01 12:29:52 +04:00
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
if (IsDOMClass(clasp)) {
|
|
|
|
str = JS_NewStringCopyZ(cx, clasp->name);
|
|
|
|
str = ConcatJSString(cx, "[object ", str, "]");
|
|
|
|
} else if (IsDOMIfaceAndProtoClass(clasp)) {
|
|
|
|
const DOMIfaceAndProtoJSClass* ifaceAndProtoJSClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(clasp);
|
|
|
|
str = JS_NewStringCopyZ(cx, ifaceAndProtoJSClass->mToString);
|
2012-11-05 20:58:03 +04:00
|
|
|
} else {
|
2014-02-01 12:29:52 +04:00
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
JS::Rooted<JSFunction*> fun(cx, JS_GetObjectFunction(obj));
|
|
|
|
str = JS_DecompileFunction(cx, fun, 0);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
str = ConcatJSString(cx, pre, str, post);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-26 20:19:05 +04:00
|
|
|
v.setString(str);
|
2012-10-09 22:50:27 +04:00
|
|
|
return JS_WrapValue(cx, v);
|
|
|
|
}
|
|
|
|
|
2012-09-26 18:17:46 +04:00
|
|
|
// Dynamically ensure that two objects don't end up with the same reserved slot.
|
2013-04-12 07:20:18 +04:00
|
|
|
class MOZ_STACK_CLASS AutoCloneDOMObjectSlotGuard
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-05-04 03:29:08 +04:00
|
|
|
AutoCloneDOMObjectSlotGuard(JSContext* aCx, JSObject* aOld, JSObject* aNew)
|
|
|
|
: mOldReflector(aCx, aOld), mNewReflector(aCx, aNew)
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetReservedSlot(aOld, DOM_OBJECT_SLOT) ==
|
|
|
|
js::GetReservedSlot(aNew, DOM_OBJECT_SLOT));
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoCloneDOMObjectSlotGuard()
|
|
|
|
{
|
|
|
|
if (js::GetReservedSlot(mOldReflector, DOM_OBJECT_SLOT).toPrivate()) {
|
|
|
|
js::SetReservedSlot(mNewReflector, DOM_OBJECT_SLOT,
|
|
|
|
JS::PrivateValue(nullptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> mOldReflector;
|
|
|
|
JS::Rooted<JSObject*> mNewReflector;
|
2012-09-26 18:17:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
2013-10-16 02:34:13 +04:00
|
|
|
ReparentWrapper(JSContext* aCx, JS::Handle<JSObject*> aObjArg)
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
2014-04-09 02:27:15 +04:00
|
|
|
js::AssertSameCompartment(aCx, aObjArg);
|
|
|
|
|
2014-03-07 03:26:17 +04:00
|
|
|
// Check if we're near the stack limit before we get anywhere near the
|
|
|
|
// transplanting code.
|
|
|
|
JS_CHECK_RECURSION(aCx, return NS_ERROR_FAILURE);
|
|
|
|
|
2013-10-16 02:34:13 +04:00
|
|
|
JS::Rooted<JSObject*> aObj(aCx, aObjArg);
|
2012-09-26 18:17:46 +04:00
|
|
|
const DOMClass* domClass = GetDOMClass(aObj);
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> oldParent(aCx, JS_GetParent(aObj));
|
|
|
|
JS::Rooted<JSObject*> newParent(aCx, domClass->mGetParent(aCx, aObj));
|
2012-09-26 18:17:46 +04:00
|
|
|
|
|
|
|
JSAutoCompartment oldAc(aCx, oldParent);
|
|
|
|
|
2013-10-23 16:02:42 +04:00
|
|
|
JSCompartment* oldCompartment = js::GetObjectCompartment(oldParent);
|
|
|
|
JSCompartment* newCompartment = js::GetObjectCompartment(newParent);
|
|
|
|
if (oldCompartment == newCompartment) {
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!JS_SetParent(aCx, aObj, newParent)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:02:42 +04:00
|
|
|
// Telemetry.
|
|
|
|
xpc::RecordDonatedNode(oldCompartment);
|
|
|
|
xpc::RecordAdoptedNode(newCompartment);
|
|
|
|
|
2013-06-01 10:56:02 +04:00
|
|
|
nsISupports* native = UnwrapDOMObjectToISupports(aObj);
|
|
|
|
if (!native) {
|
2012-09-26 18:17:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-16 21:02:57 +04:00
|
|
|
bool isProxy = js::IsProxy(aObj);
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> expandoObject(aCx);
|
2013-04-16 21:02:57 +04:00
|
|
|
if (isProxy) {
|
|
|
|
expandoObject = DOMProxyHandler::GetAndClearExpandoObject(aObj);
|
|
|
|
}
|
|
|
|
|
2012-09-26 18:17:46 +04:00
|
|
|
JSAutoCompartment newAc(aCx, newParent);
|
|
|
|
|
|
|
|
// First we clone the reflector. We get a copy of its properties and clone its
|
|
|
|
// expando chain. The only part that is dangerous here is that if we have to
|
|
|
|
// return early we must avoid ending up with two reflectors pointing to the
|
|
|
|
// same native. Other than that, the objects we create will just go away.
|
|
|
|
|
2013-05-02 07:44:12 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx,
|
|
|
|
js::GetGlobalForObjectCrossCompartment(newParent));
|
|
|
|
JS::Handle<JSObject*> proto = (domClass->mGetProto)(aCx, global);
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!proto) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> newobj(aCx, JS_CloneObject(aCx, aObj, proto, newParent));
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!newobj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
js::SetReservedSlot(newobj, DOM_OBJECT_SLOT,
|
|
|
|
js::GetReservedSlot(aObj, DOM_OBJECT_SLOT));
|
|
|
|
|
|
|
|
// At this point, both |aObj| and |newobj| point to the same native
|
|
|
|
// which is bad, because one of them will end up being finalized with a
|
|
|
|
// native it does not own. |cloneGuard| ensures that if we exit before
|
|
|
|
// clearing |aObj|'s reserved slot the reserved slot of |newobj| will be
|
|
|
|
// set to null. |aObj| will go away soon, because we swap it with
|
|
|
|
// another object during the transplant and let that object die.
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> propertyHolder(aCx);
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
AutoCloneDOMObjectSlotGuard cloneGuard(aCx, aObj, newobj);
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> copyFrom(aCx, isProxy ? expandoObject : aObj);
|
2013-04-16 21:02:57 +04:00
|
|
|
if (copyFrom) {
|
2014-01-16 21:48:58 +04:00
|
|
|
propertyHolder = JS_NewObjectWithGivenProto(aCx, nullptr, JS::NullPtr(),
|
2013-04-16 21:02:57 +04:00
|
|
|
newParent);
|
|
|
|
if (!propertyHolder) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2013-04-16 21:02:57 +04:00
|
|
|
if (!JS_CopyPropertiesFrom(aCx, propertyHolder, copyFrom)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
propertyHolder = nullptr;
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expandos from other compartments are attached to the target JS object.
|
|
|
|
// Copy them over, and let the old ones die a natural death.
|
|
|
|
if (!xpc::XrayUtils::CloneExpandoChain(aCx, newobj, aObj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've set up |newobj|, so we make it own the native by nulling
|
|
|
|
// out the reserved slot of |obj|.
|
|
|
|
//
|
|
|
|
// NB: It's important to do this _after_ copying the properties to
|
|
|
|
// propertyHolder. Otherwise, an object with |foo.x === foo| will
|
|
|
|
// crash when JS_CopyPropertiesFrom tries to call wrap() on foo.x.
|
|
|
|
js::SetReservedSlot(aObj, DOM_OBJECT_SLOT, JS::PrivateValue(nullptr));
|
|
|
|
}
|
|
|
|
|
2014-03-19 20:35:45 +04:00
|
|
|
aObj = xpc::TransplantObject(aCx, aObj, newobj);
|
|
|
|
if (!aObj) {
|
2013-10-17 12:00:02 +04:00
|
|
|
MOZ_CRASH();
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
2014-03-19 20:35:45 +04:00
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(native, &cache);
|
2012-09-26 18:17:46 +04:00
|
|
|
bool preserving = cache->PreservingWrapper();
|
|
|
|
cache->SetPreservingWrapper(false);
|
|
|
|
cache->SetWrapper(aObj);
|
|
|
|
cache->SetPreservingWrapper(preserving);
|
2013-04-16 21:02:57 +04:00
|
|
|
|
|
|
|
if (propertyHolder) {
|
2014-03-17 20:17:58 +04:00
|
|
|
JS::Rooted<JSObject*> copyTo(aCx);
|
2013-04-16 21:02:57 +04:00
|
|
|
if (isProxy) {
|
|
|
|
copyTo = DOMProxyHandler::EnsureExpandoObject(aCx, aObj);
|
|
|
|
} else {
|
|
|
|
copyTo = aObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!copyTo || !JS_CopyPropertiesFrom(aCx, copyTo, propertyHolder)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
2013-03-17 18:42:05 +04:00
|
|
|
nsObjectLoadingContent* htmlobject;
|
2013-11-21 16:51:16 +04:00
|
|
|
nsresult rv = UNWRAP_OBJECT(HTMLObjectElement, aObj, htmlobject);
|
2013-03-17 18:42:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = UnwrapObject<prototypes::id::HTMLEmbedElement,
|
2013-11-21 16:51:15 +04:00
|
|
|
HTMLSharedObjectElement>(aObj, htmlobject);
|
2013-03-17 18:42:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = UnwrapObject<prototypes::id::HTMLAppletElement,
|
2013-11-21 16:51:15 +04:00
|
|
|
HTMLSharedObjectElement>(aObj, htmlobject);
|
2013-03-17 18:42:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
htmlobject = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (htmlobject) {
|
2013-01-07 03:55:14 +04:00
|
|
|
htmlobject->SetupProtoChain(aCx, aObj);
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
|
|
|
|
// Now we can just fix up the parent and return the wrapper
|
|
|
|
|
|
|
|
if (newParent && !JS_SetParent(aCx, aObj, newParent)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-23 09:17:08 +04:00
|
|
|
GlobalObject::GlobalObject(JSContext* aCx, JSObject* aObject)
|
|
|
|
: mGlobalJSObject(aCx),
|
|
|
|
mCx(aCx),
|
|
|
|
mGlobalObject(nullptr)
|
2012-12-03 20:07:49 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, aObject);
|
|
|
|
if (js::IsWrapper(obj)) {
|
|
|
|
obj = js::CheckedUnwrap(obj, /* stopAtOuter = */ false);
|
|
|
|
if (!obj) {
|
2013-08-23 09:17:08 +04:00
|
|
|
// We should never end up here on a worker thread, since there shouldn't
|
|
|
|
// be any security wrappers to worry about.
|
|
|
|
if (!MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, NS_ERROR_XPC_SECURITY_MANAGER_VETO);
|
2013-08-23 09:17:08 +04:00
|
|
|
return;
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 07:19:43 +04:00
|
|
|
mGlobalJSObject = js::GetGlobalForObjectCrossCompartment(obj);
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:17:08 +04:00
|
|
|
nsISupports*
|
|
|
|
GlobalObject::GetAsSupports() const
|
2012-12-03 20:07:49 +04:00
|
|
|
{
|
2013-08-23 09:17:08 +04:00
|
|
|
if (mGlobalObject) {
|
|
|
|
return mGlobalObject;
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
if (!NS_IsMainThread()) {
|
2014-03-07 01:05:11 +04:00
|
|
|
mGlobalObject = UnwrapDOMObjectToISupports(mGlobalJSObject);
|
|
|
|
return mGlobalObject;
|
2014-02-26 01:34:55 +04:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:17:08 +04:00
|
|
|
JS::Rooted<JS::Value> val(mCx, JS::ObjectValue(*mGlobalJSObject));
|
2012-12-03 20:07:49 +04:00
|
|
|
|
|
|
|
// Switch this to UnwrapDOMObjectToISupports once our global objects are
|
|
|
|
// using new bindings.
|
2013-08-23 09:17:08 +04:00
|
|
|
nsresult rv = xpc_qsUnwrapArg<nsISupports>(mCx, val, &mGlobalObject,
|
2012-12-03 20:07:49 +04:00
|
|
|
static_cast<nsISupports**>(getter_AddRefs(mGlobalObjectRef)),
|
2013-10-12 09:02:39 +04:00
|
|
|
&val);
|
2012-12-03 20:07:49 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mGlobalObject = nullptr;
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(mCx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:17:08 +04:00
|
|
|
return mGlobalObject;
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-08-09 02:53:04 +04:00
|
|
|
bool* bp)
|
2013-01-08 22:05:36 +04:00
|
|
|
{
|
|
|
|
const DOMIfaceAndProtoJSClass* clasp =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(js::GetObjectClass(obj));
|
|
|
|
|
2013-04-11 22:50:18 +04:00
|
|
|
const DOMClass* domClass = GetDOMClass(js::UncheckedUnwrap(instance));
|
2013-01-08 22:05:36 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!domClass || clasp->mPrototypeID != prototypes::id::_ID_Count,
|
|
|
|
"Why do we have a hasInstance hook if we don't have a prototype "
|
|
|
|
"ID?");
|
|
|
|
|
2013-02-22 13:25:24 +04:00
|
|
|
if (domClass &&
|
|
|
|
domClass->mInterfaceChain[clasp->mDepth] == clasp->mPrototypeID) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 03:45:17 +04:00
|
|
|
JS::Rooted<JSObject*> unwrapped(cx, js::CheckedUnwrap(instance, true));
|
2014-05-17 03:40:35 +04:00
|
|
|
if (unwrapped && jsipc::IsCPOW(unwrapped)) {
|
2013-08-02 03:45:17 +04:00
|
|
|
bool boolp = false;
|
2014-05-17 03:40:35 +04:00
|
|
|
if (!jsipc::DOMInstanceOf(cx, unwrapped, clasp->mPrototypeID,
|
2013-08-02 03:45:17 +04:00
|
|
|
clasp->mDepth, &boolp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*bp = boolp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JS::Value> protov(cx);
|
2013-07-26 13:00:38 +04:00
|
|
|
DebugOnly<bool> ok = JS_GetProperty(cx, obj, "prototype", &protov);
|
2013-02-22 13:25:24 +04:00
|
|
|
MOZ_ASSERT(ok, "Someone messed with our prototype property?");
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> interfacePrototype(cx, &protov.toObject());
|
2013-02-22 13:25:24 +04:00
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(interfacePrototype)),
|
|
|
|
"Someone messed with our prototype property?");
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-07-31 20:20:33 +04:00
|
|
|
if (!JS_GetPrototype(cx, instance, &proto)) {
|
2013-02-22 13:25:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (proto) {
|
|
|
|
if (proto == interfacePrototype) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-31 20:20:33 +04:00
|
|
|
if (!JS_GetPrototype(cx, proto, &proto)) {
|
2013-02-22 13:25:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bp = false;
|
2013-01-08 22:05:36 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-06-21 17:12:46 +04:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj, JS::MutableHandle<JS::Value> vp,
|
2013-08-09 02:53:04 +04:00
|
|
|
bool* bp)
|
2013-01-08 22:05:36 +04:00
|
|
|
{
|
|
|
|
if (!vp.isObject()) {
|
|
|
|
*bp = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> instanceObject(cx, &vp.toObject());
|
|
|
|
return InterfaceHasInstance(cx, obj, instanceObject, bp);
|
2013-01-08 22:05:36 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-08-02 03:45:17 +04:00
|
|
|
InterfaceHasInstance(JSContext* cx, int prototypeID, int depth,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-08-09 02:53:04 +04:00
|
|
|
bool* bp)
|
2013-08-02 03:45:17 +04:00
|
|
|
{
|
|
|
|
const DOMClass* domClass = GetDOMClass(js::UncheckedUnwrap(instance));
|
|
|
|
|
|
|
|
MOZ_ASSERT(!domClass || prototypeID != prototypes::id::_ID_Count,
|
|
|
|
"Why do we have a hasInstance hook if we don't have a prototype "
|
|
|
|
"ID?");
|
|
|
|
|
|
|
|
*bp = (domClass && domClass->mInterfaceChain[depth] == prototypeID);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-08 23:50:58 +04:00
|
|
|
bool
|
2013-09-25 22:38:30 +04:00
|
|
|
ReportLenientThisUnwrappingFailure(JSContext* cx, JSObject* obj)
|
2013-04-09 01:04:21 +04:00
|
|
|
{
|
2013-09-25 22:38:30 +04:00
|
|
|
JS::Rooted<JSObject*> rootedObj(cx, obj);
|
|
|
|
GlobalObject global(cx, rootedObj);
|
2013-05-08 23:50:58 +04:00
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(global.GetAsSupports());
|
2013-04-09 01:04:21 +04:00
|
|
|
if (window && window->GetDoc()) {
|
|
|
|
window->GetDoc()->WarnOnceAbout(nsIDocument::eLenientThis);
|
|
|
|
}
|
2013-05-08 23:50:58 +04:00
|
|
|
return true;
|
2013-04-09 01:04:21 +04:00
|
|
|
}
|
|
|
|
|
2013-05-20 21:47:08 +04:00
|
|
|
bool
|
|
|
|
GetWindowForJSImplementedObject(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
nsPIDOMWindow** window)
|
|
|
|
{
|
|
|
|
// Be very careful to not get tricked here.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!xpc::AccessCheck::isChrome(js::GetObjectCompartment(obj))) {
|
|
|
|
NS_RUNTIMEABORT("Should have a chrome object here");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the content-side object.
|
|
|
|
JS::Rooted<JS::Value> domImplVal(cx);
|
2013-07-26 13:00:38 +04:00
|
|
|
if (!JS_GetProperty(cx, obj, "__DOM_IMPL__", &domImplVal)) {
|
2013-05-20 21:47:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!domImplVal.isObject()) {
|
2013-06-17 21:07:03 +04:00
|
|
|
ThrowErrorMessage(cx, MSG_NOT_OBJECT, "Value");
|
2013-05-20 21:47:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go ahead and get the global from it. GlobalObject will handle
|
|
|
|
// doing unwrapping as needed.
|
|
|
|
GlobalObject global(cx, &domImplVal.toObject());
|
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's OK if we have null here: that just means the content-side
|
|
|
|
// object really wasn't associated with any window.
|
2013-08-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(global.GetAsSupports()));
|
2013-05-20 21:47:08 +04:00
|
|
|
win.forget(window);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-01 10:56:23 +04:00
|
|
|
already_AddRefed<nsPIDOMWindow>
|
|
|
|
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
|
|
|
|
const GlobalObject& aGlobal,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Get the window to use as a parent and for initialization.
|
2013-08-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-06-01 10:56:23 +04:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:03:03 +04:00
|
|
|
ConstructJSImplementation(aCx, aContractId, window, aObject, aRv);
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return window.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
|
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2013-12-12 05:51:58 +04:00
|
|
|
// Make sure to divorce ourselves from the calling JS while creating and
|
2013-06-01 10:56:23 +04:00
|
|
|
// initializing the object, so exceptions from that will get reported
|
|
|
|
// properly, since those are never exceptions that a spec wants to be thrown.
|
2013-12-12 05:51:58 +04:00
|
|
|
{
|
2014-04-15 07:27:00 +04:00
|
|
|
AutoNoJSAPI nojsapi;
|
2013-12-12 05:51:58 +04:00
|
|
|
|
2013-06-01 10:56:23 +04:00
|
|
|
// Get the XPCOM component containing the JS implementation.
|
|
|
|
nsCOMPtr<nsISupports> implISupports = do_CreateInstance(aContractId);
|
|
|
|
if (!implISupports) {
|
|
|
|
NS_WARNING("Failed to get JS implementation for contract");
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2014-03-28 08:03:03 +04:00
|
|
|
return;
|
2013-06-01 10:56:23 +04:00
|
|
|
}
|
|
|
|
// Initialize the object, if it implements nsIDOMGlobalPropertyInitializer.
|
|
|
|
nsCOMPtr<nsIDOMGlobalPropertyInitializer> gpi =
|
|
|
|
do_QueryInterface(implISupports);
|
|
|
|
if (gpi) {
|
|
|
|
JS::Rooted<JS::Value> initReturn(aCx);
|
2014-03-28 08:03:03 +04:00
|
|
|
nsresult rv = gpi->Init(aWindow, &initReturn);
|
2013-06-01 10:56:23 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-03-28 08:03:03 +04:00
|
|
|
return;
|
2013-06-01 10:56:23 +04:00
|
|
|
}
|
2013-09-27 03:46:42 +04:00
|
|
|
// With JS-implemented WebIDL, the return value of init() is not used to determine
|
|
|
|
// if init() failed, so init() should only return undefined. Any kind of permission
|
|
|
|
// or pref checking must happen by adding an attribute to the WebIDL interface.
|
|
|
|
if (!initReturn.isUndefined()) {
|
|
|
|
MOZ_ASSERT(false, "The init() method for JS-implemented WebIDL should not return anything");
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2013-06-01 10:56:23 +04:00
|
|
|
}
|
|
|
|
// Extract the JS implementation from the XPCOM object.
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> implWrapped =
|
|
|
|
do_QueryInterface(implISupports);
|
|
|
|
MOZ_ASSERT(implWrapped, "Failed to get wrapped JS from XPCOM component.");
|
|
|
|
if (!implWrapped) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2014-03-28 08:03:03 +04:00
|
|
|
return;
|
2013-06-01 10:56:23 +04:00
|
|
|
}
|
|
|
|
aObject.set(implWrapped->GetJSObject());
|
|
|
|
if (!aObject) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 09:18:35 +04:00
|
|
|
bool
|
|
|
|
NonVoidByteStringToJsval(JSContext *cx, const nsACString &str,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
// ByteStrings are not UTF-8 encoded.
|
|
|
|
JSString* jsStr = JS_NewStringCopyN(cx, str.Data(), str.Length());
|
|
|
|
|
|
|
|
if (!jsStr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
rval.setString(jsStr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConvertJSValueToByteString(JSContext* cx, JS::Handle<JS::Value> v,
|
|
|
|
JS::MutableHandle<JS::Value> pval, bool nullable,
|
|
|
|
nsACString& result)
|
|
|
|
{
|
2013-09-30 18:29:10 +04:00
|
|
|
JS::Rooted<JSString*> s(cx);
|
2013-06-13 09:18:35 +04:00
|
|
|
if (v.isString()) {
|
|
|
|
s = v.toString();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (nullable && v.isNullOrUndefined()) {
|
|
|
|
result.SetIsVoid(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
s = JS::ToString(cx, v);
|
2013-06-13 09:18:35 +04:00
|
|
|
if (!s) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pval.set(JS::StringValue(s)); // Root the new string.
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t length;
|
|
|
|
const jschar *chars = JS_GetStringCharsZAndLength(cx, s, &length);
|
|
|
|
if (!chars) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conversion from Javascript string to ByteString is only valid if all
|
|
|
|
// characters < 256.
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
if (chars[i] > 255) {
|
|
|
|
// The largest unsigned 64 bit number (18,446,744,073,709,551,615) has
|
|
|
|
// 20 digits, plus one more for the null terminator.
|
|
|
|
char index[21];
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(sizeof(size_t) <= 8, "index array too small");
|
2013-06-13 09:18:35 +04:00
|
|
|
PR_snprintf(index, sizeof(index), "%d", i);
|
|
|
|
// A jschar is 16 bits long. The biggest unsigned 16 bit
|
|
|
|
// number (65,535) has 5 digits, plus one more for the null
|
|
|
|
// terminator.
|
|
|
|
char badChar[6];
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(sizeof(jschar) <= 2, "badChar array too small");
|
2013-06-13 09:18:35 +04:00
|
|
|
PR_snprintf(badChar, sizeof(badChar), "%d", chars[i]);
|
|
|
|
ThrowErrorMessage(cx, MSG_INVALID_BYTESTRING, index, badChar);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= UINT32_MAX) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
result.SetCapacity(length+1);
|
|
|
|
JS_EncodeStringToBuffer(cx, s, result.BeginWriting(), length);
|
|
|
|
result.BeginWriting()[length] = '\0';
|
|
|
|
result.SetLength(length);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:38:17 +04:00
|
|
|
bool
|
|
|
|
IsInPrivilegedApp(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
using mozilla::dom::workers::GetWorkerPrivateFromContext;
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return GetWorkerPrivateFromContext(aCx)->IsInPrivilegedApp();
|
|
|
|
}
|
|
|
|
|
2014-05-13 13:58:00 +04:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aObj);
|
2014-02-05 22:38:17 +04:00
|
|
|
uint16_t appStatus = principal->GetAppStatus();
|
|
|
|
return (appStatus == nsIPrincipal::APP_STATUS_CERTIFIED ||
|
2014-05-09 01:16:00 +04:00
|
|
|
appStatus == nsIPrincipal::APP_STATUS_PRIVILEGED) ||
|
|
|
|
Preferences::GetBool("dom.ignore_webidl_scope_checks", false);
|
2014-02-05 22:38:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsInCertifiedApp(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
using mozilla::dom::workers::GetWorkerPrivateFromContext;
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return GetWorkerPrivateFromContext(aCx)->IsInCertifiedApp();
|
|
|
|
}
|
|
|
|
|
2014-05-13 13:58:00 +04:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aObj);
|
2014-05-09 01:16:00 +04:00
|
|
|
return principal->GetAppStatus() == nsIPrincipal::APP_STATUS_CERTIFIED ||
|
|
|
|
Preferences::GetBool("dom.ignore_webidl_scope_checks", false);
|
2014-02-05 22:38:17 +04:00
|
|
|
}
|
|
|
|
|
2014-04-08 22:48:37 +04:00
|
|
|
#ifdef DEBUG
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
2014-04-08 22:48:37 +04:00
|
|
|
VerifyTraceProtoAndIfaceCacheCalled(JSTracer *trc, void **thingp,
|
|
|
|
JSGCTraceKind kind)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
2014-04-08 22:48:37 +04:00
|
|
|
// We don't do anything here, we only want to verify that
|
|
|
|
// TraceProtoAndIfaceCache was called.
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2014-04-08 22:48:37 +04:00
|
|
|
#endif
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2013-11-19 10:05:42 +04:00
|
|
|
void
|
|
|
|
FinalizeGlobal(JSFreeOp* aFreeOp, JSObject* aObj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(aObj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
mozilla::dom::DestroyProtoAndIfaceCache(aObj);
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
bool
|
|
|
|
ResolveGlobal(JSContext* aCx, JS::Handle<JSObject*> aObj,
|
2014-04-26 01:11:02 +04:00
|
|
|
JS::Handle<jsid> aId, JS::MutableHandle<JSObject*> aObjp)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
|
|
|
bool resolved;
|
|
|
|
if (!JS_ResolveStandardClass(aCx, aObj, aId, &resolved)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aObjp.set(resolved ? aObj.get() : nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EnumerateGlobal(JSContext* aCx, JS::Handle<JSObject*> aObj)
|
|
|
|
{
|
|
|
|
return JS_EnumerateStandardClasses(aCx, aObj);
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:56:14 +04:00
|
|
|
bool
|
|
|
|
GenericBindingGetter(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
prototypes::ID protoID = static_cast<prototypes::ID>(info->protoID);
|
|
|
|
if (!args.thisv().isObject()) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
MSG_GETTER_THIS_DOES_NOT_IMPLEMENT_INTERFACE,
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
void* self;
|
|
|
|
{
|
|
|
|
nsresult rv = UnwrapObject<void>(obj, self, protoID, info->depth);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
GetInvalidThisErrorForGetter(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO),
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::Getter);
|
|
|
|
JSJitGetterOp getter = info->getter;
|
2014-05-15 21:26:23 +04:00
|
|
|
bool ok = getter(cx, obj, self, JSJitGetterCallArgs(args));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ok) {
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ok;
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GenericBindingSetter(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
prototypes::ID protoID = static_cast<prototypes::ID>(info->protoID);
|
|
|
|
if (!args.thisv().isObject()) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
MSG_SETTER_THIS_DOES_NOT_IMPLEMENT_INTERFACE,
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
void* self;
|
|
|
|
{
|
|
|
|
nsresult rv = UnwrapObject<void>(obj, self, protoID, info->depth);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
GetInvalidThisErrorForSetter(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO),
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (args.length() == 0) {
|
|
|
|
return ThrowNoSetterArg(cx, protoID);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::Setter);
|
|
|
|
JSJitSetterOp setter = info->setter;
|
|
|
|
if (!setter(cx, obj, self, JSJitSetterCallArgs(args))) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-15 21:26:23 +04:00
|
|
|
args.rval().setUndefined();
|
|
|
|
#ifdef DEBUG
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
#endif
|
2014-02-08 00:56:14 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GenericBindingMethod(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
prototypes::ID protoID = static_cast<prototypes::ID>(info->protoID);
|
|
|
|
if (!args.thisv().isObject()) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
MSG_METHOD_THIS_DOES_NOT_IMPLEMENT_INTERFACE,
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
void* self;
|
|
|
|
{
|
|
|
|
nsresult rv = UnwrapObject<void>(obj, self, protoID, info->depth);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
GetInvalidThisErrorForMethod(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO),
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::Method);
|
|
|
|
JSJitMethodOp method = info->method;
|
2014-05-15 21:26:23 +04:00
|
|
|
bool ok = method(cx, obj, self, JSJitMethodCallArgs(args));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ok) {
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ok;
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
|
2014-03-21 20:18:24 +04:00
|
|
|
bool
|
|
|
|
GenericPromiseReturningBindingMethod(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
// Make sure to save the callee before someone maybe messes with rval().
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> callee(cx, &args.callee());
|
|
|
|
|
|
|
|
// We could invoke GenericBindingMethod here, but that involves an
|
|
|
|
// extra call. Manually inline it instead.
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
prototypes::ID protoID = static_cast<prototypes::ID>(info->protoID);
|
|
|
|
if (!args.thisv().isObject()) {
|
|
|
|
ThrowInvalidThis(cx, args,
|
|
|
|
MSG_METHOD_THIS_DOES_NOT_IMPLEMENT_INTERFACE,
|
|
|
|
protoID);
|
|
|
|
return ConvertExceptionToPromise(cx, xpc::XrayAwareCalleeGlobal(callee),
|
|
|
|
args.rval());
|
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
void* self;
|
|
|
|
{
|
|
|
|
nsresult rv = UnwrapObject<void>(obj, self, protoID, info->depth);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ThrowInvalidThis(cx, args,
|
|
|
|
GetInvalidThisErrorForMethod(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO),
|
|
|
|
protoID);
|
|
|
|
return ConvertExceptionToPromise(cx, xpc::XrayAwareCalleeGlobal(callee),
|
|
|
|
args.rval());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::Method);
|
|
|
|
JSJitMethodOp method = info->method;
|
|
|
|
bool ok = method(cx, obj, self, JSJitMethodCallArgs(args));
|
|
|
|
if (ok) {
|
2014-05-15 21:26:23 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
#endif
|
2014-03-21 20:18:24 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 21:26:23 +04:00
|
|
|
// Promise-returning methods always return objects
|
|
|
|
MOZ_ASSERT(info->returnType() == JSVAL_TYPE_OBJECT);
|
2014-03-21 20:18:24 +04:00
|
|
|
return ConvertExceptionToPromise(cx, xpc::XrayAwareCalleeGlobal(callee),
|
|
|
|
args.rval());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StaticMethodPromiseWrapper(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
// Make sure to save the callee before someone maybe messes with rval().
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> callee(cx, &args.callee());
|
|
|
|
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
MOZ_ASSERT(info);
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::StaticMethod);
|
|
|
|
|
|
|
|
bool ok = info->staticMethod(cx, argc, vp);
|
|
|
|
if (ok) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-21 20:18:24 +04:00
|
|
|
return ConvertExceptionToPromise(cx, xpc::XrayAwareCalleeGlobal(callee),
|
|
|
|
args.rval());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConvertExceptionToPromise(JSContext* cx,
|
|
|
|
JSObject* promiseScope,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
GlobalObject global(cx, promiseScope);
|
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exn(cx);
|
|
|
|
if (!JS_GetPendingException(cx, &exn)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
ErrorResult rv;
|
|
|
|
nsRefPtr<Promise> promise = Promise::Reject(global, cx, exn, rv);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
// We just give up. Make sure to not leak memory on the
|
|
|
|
// ErrorResult, but then just put the original exception back.
|
|
|
|
ThrowMethodFailedWithDetails(cx, rv, "", "");
|
|
|
|
JS_SetPendingException(cx, exn);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:27:19 +04:00
|
|
|
return WrapNewBindingObject(cx, promise, rval);
|
2014-03-21 20:18:24 +04:00
|
|
|
}
|
|
|
|
|
2014-04-08 22:48:37 +04:00
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
CreateGlobalOptions<nsGlobalWindow>::TraceGlobal(JSTracer* aTrc, JSObject* aObj)
|
|
|
|
{
|
|
|
|
mozilla::dom::TraceProtoAndIfaceCache(aTrc, aObj);
|
2014-05-20 23:44:49 +04:00
|
|
|
|
|
|
|
// We might be called from a GC during the creation of a global, before we've
|
|
|
|
// been able to set up the compartment private or the XPCWrappedNativeScope,
|
|
|
|
// so we need to null-check those.
|
|
|
|
xpc::CompartmentPrivate* compartmentPrivate = xpc::GetCompartmentPrivate(aObj);
|
|
|
|
if (compartmentPrivate && compartmentPrivate->scope) {
|
|
|
|
compartmentPrivate->scope->TraceSelf(aTrc);
|
|
|
|
}
|
2014-04-08 22:48:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
CreateGlobalOptions<nsGlobalWindow>::PostCreateGlobal(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGlobal)
|
|
|
|
{
|
|
|
|
return XPCWrappedNativeScope::GetNewOrUsed(aCx, aGlobal);
|
|
|
|
}
|
|
|
|
|
2014-05-15 21:26:23 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
AssertReturnTypeMatchesJitinfo(const JSJitInfo* aJitInfo,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
switch (aJitInfo->returnType()) {
|
|
|
|
case JSVAL_TYPE_UNKNOWN:
|
|
|
|
// Any value is good.
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_DOUBLE:
|
|
|
|
// The value could actually be an int32 value as well.
|
|
|
|
MOZ_ASSERT(aValue.isNumber());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_INT32:
|
|
|
|
MOZ_ASSERT(aValue.isInt32());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_UNDEFINED:
|
|
|
|
MOZ_ASSERT(aValue.isUndefined());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_BOOLEAN:
|
|
|
|
MOZ_ASSERT(aValue.isBoolean());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_STRING:
|
|
|
|
MOZ_ASSERT(aValue.isString());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_NULL:
|
|
|
|
MOZ_ASSERT(aValue.isNull());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_OBJECT:
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Someone messed up their jitinfo type.
|
|
|
|
MOZ_ASSERT(false, "Unexpected JSValueType stored in jitinfo");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|