2012-03-30 21:42:20 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-08-13 23:56:21 -07:00
|
|
|
#include "BindingUtils.h"
|
|
|
|
|
2012-11-06 15:23:14 -08:00
|
|
|
#include <algorithm>
|
2012-07-18 03:36:08 -07:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2013-08-01 16:45:17 -07:00
|
|
|
#include "JavaScriptParent.h"
|
|
|
|
|
2012-12-14 15:58:45 -08:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-05-02 11:38:19 -07:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2013-06-12 22:18:35 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-05-08 14:16:00 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-12-14 15:58:45 -08:00
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
#include "AccessCheck.h"
|
2013-05-31 23:56:23 -07:00
|
|
|
#include "jsfriendapi.h"
|
2013-10-03 05:34:47 -07:00
|
|
|
#include "js/OldDebugAPI.h"
|
2014-02-05 10:38:17 -08:00
|
|
|
#include "nsContentUtils.h"
|
2014-05-25 11:31:07 -07:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-05-31 23:56:23 -07:00
|
|
|
#include "nsIDOMGlobalPropertyInitializer.h"
|
2014-05-25 11:31:07 -07:00
|
|
|
#include "nsIPermissionManager.h"
|
2014-02-05 10:38:17 -08:00
|
|
|
#include "nsIPrincipal.h"
|
2012-09-26 07:17:46 -07:00
|
|
|
#include "nsIXPConnect.h"
|
2012-05-22 06:46:20 -07:00
|
|
|
#include "WrapperFactory.h"
|
2012-05-18 01:29:40 -07:00
|
|
|
#include "xpcprivate.h"
|
|
|
|
#include "XPCQuickStubs.h"
|
2012-09-26 07:17:46 -07:00
|
|
|
#include "XrayWrapper.h"
|
2013-07-04 06:24:49 -07:00
|
|
|
#include "nsPrintfCString.h"
|
2013-08-17 15:50:18 -07:00
|
|
|
#include "prprf.h"
|
2012-05-18 01:29:40 -07:00
|
|
|
|
2013-12-11 17:51:58 -08:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-09-03 05:01:53 -07:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
#include "mozilla/dom/DOMErrorBinding.h"
|
2013-01-06 15:55:14 -08:00
|
|
|
#include "mozilla/dom/HTMLObjectElement.h"
|
|
|
|
#include "mozilla/dom/HTMLObjectElementBinding.h"
|
2013-03-17 07:42:05 -07:00
|
|
|
#include "mozilla/dom/HTMLSharedObjectElement.h"
|
|
|
|
#include "mozilla/dom/HTMLEmbedElementBinding.h"
|
|
|
|
#include "mozilla/dom/HTMLAppletElementBinding.h"
|
2014-03-21 09:18:24 -07:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2013-09-08 20:29:21 -07:00
|
|
|
#include "WorkerPrivate.h"
|
2013-01-06 15:55:14 -08:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2012-07-18 03:36:08 -07: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 10:07:03 -07:00
|
|
|
JS_ReportErrorNumberVA(aCx, GetErrorMessage, nullptr,
|
2012-07-18 03:36:08 -07:00
|
|
|
static_cast<const unsigned>(aErrorNumber), ap);
|
|
|
|
va_end(ap);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-17 10:07:03 -07:00
|
|
|
bool
|
2013-06-17 13:31:13 -07:00
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
const char* aInterfaceName)
|
2013-06-17 10:07:03 -07:00
|
|
|
{
|
|
|
|
NS_ConvertASCIItoUTF16 ifaceName(aInterfaceName);
|
2013-06-17 13:31:13 -07: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 10:07:03 -07: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-17 13:31:13 -07:00
|
|
|
static_cast<const unsigned>(aErrorNumber),
|
2013-06-17 10:07:03 -07:00
|
|
|
JS_GetStringCharsZ(aCx, funcName),
|
|
|
|
ifaceName.get());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-07 12:56:14 -08:00
|
|
|
bool
|
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
prototypes::ID aProtoId)
|
|
|
|
{
|
|
|
|
return ThrowInvalidThis(aCx, aArgs, aErrorNumber,
|
2014-02-10 07:24:38 -08:00
|
|
|
NamesOfInterfacesWithProtos(aProtoId));
|
2014-02-07 12:56:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThrowNoSetterArg(JSContext* aCx, prototypes::ID aProtoId)
|
|
|
|
{
|
|
|
|
nsPrintfCString errorMessage("%s attribute setter",
|
2014-02-10 07:24:38 -08:00
|
|
|
NamesOfInterfacesWithProtos(aProtoId));
|
2014-02-07 12:56:14 -08:00
|
|
|
return ThrowErrorMessage(aCx, MSG_MISSING_ARGUMENTS, errorMessage.get());
|
|
|
|
}
|
|
|
|
|
2012-11-06 15:23:14 -08: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 08:54:40 -08: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-06 15:23:14 -08: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 05:30:28 -08:00
|
|
|
ClearMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ClearMessage()
|
|
|
|
{
|
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
mMessage = nullptr;
|
|
|
|
}
|
2012-11-06 15:23:14 -08:00
|
|
|
}
|
|
|
|
|
2013-02-19 08:54:40 -08:00
|
|
|
void
|
2013-04-11 11:31:06 -07:00
|
|
|
ErrorResult::ThrowJSException(JSContext* cx, JS::Handle<JS::Value> exn)
|
2013-02-19 08:54:40 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to throw a JS exception?");
|
|
|
|
|
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
}
|
|
|
|
|
2013-04-08 14:05:22 -07: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 01:47:57 -07:00
|
|
|
if (!js::AddRawValueRoot(cx, &mJSException, "ErrorResult::mJSException")) {
|
2013-02-19 08:54:40 -08: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 09:19:05 -07:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exception(cx, mJSException);
|
|
|
|
if (JS_WrapValue(cx, &exception)) {
|
2013-09-19 00:54:01 -07:00
|
|
|
JS_SetPendingException(cx, exception);
|
2013-02-19 08:54:40 -08:00
|
|
|
}
|
2013-10-26 09:19:05 -07:00
|
|
|
mJSException = exception;
|
2013-02-19 08:54:40 -08:00
|
|
|
// If JS_WrapValue failed, not much we can do about it... No matter
|
|
|
|
// what, go ahead and unroot mJSException.
|
2014-04-16 01:47:57 -07:00
|
|
|
js::RemoveRawValueRoot(cx, &mJSException);
|
2013-02-19 08:54:40 -08:00
|
|
|
}
|
|
|
|
|
2013-09-03 05:01:53 -07: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 04:51:16 -08:00
|
|
|
nsresult rv = UNWRAP_OBJECT(DOMError, &mJSException.toObject(), domError);
|
2013-09-03 05:01:53 -07: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 04:22:09 -07:00
|
|
|
JS_ReportError(aCx, "%hs", message.get());
|
2014-04-16 01:47:57 -07:00
|
|
|
js::RemoveRawValueRoot(aCx, &mJSException);
|
2014-02-12 23:38:36 -08:00
|
|
|
|
2013-09-03 05:01:53 -07:00
|
|
|
// We no longer have a useful exception but we do want to signal that an error
|
|
|
|
// occured.
|
|
|
|
mResult = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:41:21 -07: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 01:47:57 -07:00
|
|
|
js::RemoveRawValueRoot(cx, &mJSException);
|
2013-06-11 18:41:21 -07:00
|
|
|
mResult = NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-04 06:24:49 -07: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-06 15:23:14 -08:00
|
|
|
namespace dom {
|
|
|
|
|
2012-05-09 03:00:48 -07:00
|
|
|
bool
|
2013-05-07 19:34:56 -07:00
|
|
|
DefineConstants(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const ConstantSpec* cs)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-01-21 18:11:17 -08:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
2012-03-30 21:42:20 -07:00
|
|
|
for (; cs->name; ++cs) {
|
2014-01-21 18:11:17 -08:00
|
|
|
value = cs->value;
|
2013-08-08 15:53:04 -07:00
|
|
|
bool ok =
|
2014-01-21 18:11:17 -08:00
|
|
|
JS_DefineProperty(cx, obj, cs->name, value,
|
2012-03-30 21:42:20 -07:00
|
|
|
JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-23 09:44:48 -07:00
|
|
|
static inline bool
|
2013-05-07 19:34:56 -07:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const JSFunctionSpec* spec) {
|
2012-05-23 09:44:48 -07:00
|
|
|
return JS_DefineFunctions(cx, obj, spec);
|
|
|
|
}
|
|
|
|
static inline bool
|
2013-05-07 19:34:56 -07:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const JSPropertySpec* spec) {
|
2012-05-23 09:44:48 -07:00
|
|
|
return JS_DefineProperties(cx, obj, spec);
|
|
|
|
}
|
|
|
|
static inline bool
|
2013-05-07 19:34:56 -07:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const ConstantSpec* spec) {
|
2012-05-23 09:44:48 -07:00
|
|
|
return DefineConstants(cx, obj, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
DefinePrefable(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const Prefable<T>* props)
|
2012-05-23 09:44:48 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(props);
|
|
|
|
MOZ_ASSERT(props->specs);
|
|
|
|
do {
|
|
|
|
// Define if enabled
|
2013-02-19 08:54:40 -08:00
|
|
|
if (props->isEnabled(cx, obj)) {
|
2012-05-23 09:44:48 -07:00
|
|
|
if (!Define(cx, obj, props->specs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((++props)->specs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-04 06:02:10 -07:00
|
|
|
bool
|
|
|
|
DefineUnforgeableMethods(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const Prefable<const JSFunctionSpec>* props)
|
|
|
|
{
|
|
|
|
return DefinePrefable(cx, obj, props);
|
|
|
|
}
|
|
|
|
|
2012-10-24 13:10:49 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
DefineUnforgeableAttributes(JSContext* cx, JS::Handle<JSObject*> obj,
|
2013-04-02 17:20:38 -07:00
|
|
|
const Prefable<const JSPropertySpec>* props)
|
2012-10-24 13:10:49 -07:00
|
|
|
{
|
|
|
|
return DefinePrefable(cx, obj, props);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-02 06:28:17 -07: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-15 15:34:13 -07:00
|
|
|
static bool
|
2012-05-02 06:28:17 -07:00
|
|
|
InterfaceObjectToString(JSContext* cx, unsigned argc, JS::Value *vp)
|
|
|
|
{
|
2013-10-11 22:02:39 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> callee(cx, &args.callee());
|
2012-05-02 06:28:17 -07:00
|
|
|
|
2013-10-21 10:52:22 -07:00
|
|
|
if (!args.thisv().isObject()) {
|
2013-10-28 07:04:12 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_CANT_CONVERT_TO, "null", "object");
|
2012-05-02 06:28:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-02 16:05:37 -07:00
|
|
|
JS::Value v = js::GetFunctionNativeReserved(callee,
|
|
|
|
TOSTRING_CLASS_RESERVED_SLOT);
|
2013-10-15 15:34:13 -07:00
|
|
|
const JSClass* clasp = static_cast<const JSClass*>(v.toPrivate());
|
2012-05-02 06:28:17 -07:00
|
|
|
|
|
|
|
v = js::GetFunctionNativeReserved(callee, TOSTRING_NAME_RESERVED_SLOT);
|
2014-04-27 19:45:46 -07:00
|
|
|
JSString* jsname = static_cast<JSString*>(v.toString());
|
2012-05-02 06:28:17 -07:00
|
|
|
size_t length;
|
|
|
|
const jschar* name = JS_GetInternedStringCharsAndLength(jsname, &length);
|
|
|
|
|
2013-10-21 10:52:22 -07:00
|
|
|
if (js::GetObjectJSClass(&args.thisv().toObject()) != clasp) {
|
2013-10-28 07:04:12 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO,
|
2012-05-02 06:28:17 -07: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-21 20:48:51 -07:00
|
|
|
str.Append('}');
|
2012-05-02 06:28:17 -07:00
|
|
|
|
2013-10-11 22:02:39 -07:00
|
|
|
return xpc::NonVoidStringToJsval(cx, str, args.rval());
|
2012-05-02 06:28:17 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
bool
|
2012-10-09 11:50:27 -07:00
|
|
|
Constructor(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2014-03-19 10:05:04 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2012-10-09 11:50:27 -07:00
|
|
|
const JS::Value& v =
|
2014-03-19 10:05:04 -07:00
|
|
|
js::GetFunctionNativeReserved(&args.callee(),
|
2012-10-09 11:50:27 -07:00
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT);
|
2013-01-22 02:51:15 -08:00
|
|
|
const JSNativeHolder* nativeHolder =
|
|
|
|
static_cast<const JSNativeHolder*>(v.toPrivate());
|
2012-10-09 11:50:27 -07:00
|
|
|
return (nativeHolder->mNative)(cx, argc, vp);
|
|
|
|
}
|
|
|
|
|
2013-01-22 02:51:15 -08:00
|
|
|
static JSObject*
|
2013-05-01 20:44:12 -07:00
|
|
|
CreateConstructor(JSContext* cx, JS::Handle<JSObject*> global, const char* name,
|
2013-01-22 02:51:15 -08: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-01 20:44:12 -07:00
|
|
|
DefineConstructor(JSContext* cx, JS::Handle<JSObject*> global, const char* name,
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Handle<JSObject*> constructor)
|
2013-01-22 02:51:15 -08:00
|
|
|
{
|
2013-08-08 15:53:04 -07:00
|
|
|
bool alreadyDefined;
|
2013-01-22 02:51:15 -08:00
|
|
|
if (!JS_AlreadyHasOwnProperty(cx, global, name, &alreadyDefined)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is Enumerable: False per spec.
|
|
|
|
return alreadyDefined ||
|
2014-01-21 18:11:17 -08:00
|
|
|
JS_DefineProperty(cx, global, name, constructor, 0);
|
2013-01-22 02:51:15 -08:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
static JSObject*
|
2013-05-01 20:44:12 -07:00
|
|
|
CreateInterfaceObject(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 11:38:19 -07:00
|
|
|
JS::Handle<JSObject*> constructorProto,
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* constructorClass,
|
2013-01-22 02:51:15 -08:00
|
|
|
const JSNativeHolder* constructorNative,
|
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Handle<JSObject*> proto,
|
2012-10-09 11:50:05 -07:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2013-08-07 14:40:00 -07:00
|
|
|
const char* name, bool defineOnGlobal)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Rooted<JSObject*> constructor(cx);
|
2012-05-02 21:35:38 -07:00
|
|
|
if (constructorClass) {
|
2013-05-02 11:38:19 -07:00
|
|
|
MOZ_ASSERT(constructorProto);
|
2012-12-06 12:21:18 -08:00
|
|
|
constructor = JS_NewObject(cx, constructorClass, constructorProto, global);
|
2012-05-02 21:35:38 -07:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(constructorNative);
|
2013-05-02 11:38:19 -07:00
|
|
|
MOZ_ASSERT(constructorProto == JS_GetFunctionPrototype(cx, global));
|
2013-01-22 02:51:15 -08:00
|
|
|
constructor = CreateConstructor(cx, global, name, constructorNative,
|
|
|
|
ctorNargs);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
if (!constructor) {
|
2013-10-28 07:04:12 -07:00
|
|
|
return nullptr;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:38:19 -07:00
|
|
|
if (constructorClass) {
|
2012-12-06 12:21:18 -08:00
|
|
|
// Have to shadow Function.prototype.toString, since that throws
|
|
|
|
// on things that are not js::FunctionClass.
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Rooted<JSFunction*> toString(cx,
|
|
|
|
js::DefineFunctionWithReserved(cx, constructor,
|
|
|
|
"toString",
|
|
|
|
InterfaceObjectToString,
|
|
|
|
0, 0));
|
2012-05-02 06:28:17 -07:00
|
|
|
if (!toString) {
|
2013-10-28 07:04:12 -07:00
|
|
|
return nullptr;
|
2012-05-02 06:28:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSString *str = ::JS_InternString(cx, name);
|
|
|
|
if (!str) {
|
2013-10-28 07:04:12 -07:00
|
|
|
return nullptr;
|
2012-05-02 06:28:17 -07:00
|
|
|
}
|
2013-05-01 20:44:11 -07:00
|
|
|
JSObject* toStringObj = JS_GetFunctionObject(toString);
|
|
|
|
js::SetFunctionNativeReserved(toStringObj, TOSTRING_CLASS_RESERVED_SLOT,
|
2013-09-11 05:49:05 -07:00
|
|
|
PRIVATE_TO_JSVAL(const_cast<JSClass *>(constructorClass)));
|
2013-05-01 20:44:11 -07:00
|
|
|
|
2012-05-02 06:28:17 -07:00
|
|
|
js::SetFunctionNativeReserved(toStringObj, TOSTRING_NAME_RESERVED_SLOT,
|
|
|
|
STRING_TO_JSVAL(str));
|
2012-12-14 11:10:49 -08:00
|
|
|
|
2014-01-21 18:11:17 -08:00
|
|
|
if (!JS_DefineProperty(cx, constructor, "length", ctorNargs,
|
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT)) {
|
2013-10-28 07:04:12 -07:00
|
|
|
return nullptr;
|
2012-12-14 11:10:49 -08:00
|
|
|
}
|
2012-05-02 06:28:17 -07:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
if (properties) {
|
|
|
|
if (properties->staticMethods &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->staticMethods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-19 00:34:28 -07:00
|
|
|
if (properties->staticAttributes &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->staticAttributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:05 -07: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 00:34:28 -07:00
|
|
|
if (chromeOnlyProperties->staticAttributes &&
|
|
|
|
!DefinePrefable(cx, constructor,
|
|
|
|
chromeOnlyProperties->staticAttributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
if (chromeOnlyProperties->constants &&
|
|
|
|
!DefinePrefable(cx, constructor, chromeOnlyProperties->constants)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (proto && !JS_LinkConstructorAndPrototype(cx, constructor, proto)) {
|
2013-10-28 07:04:12 -07:00
|
|
|
return nullptr;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2013-08-07 14:40:00 -07:00
|
|
|
if (defineOnGlobal && !DefineConstructor(cx, global, name, constructor)) {
|
2013-01-22 02:51:15 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (namedConstructors) {
|
|
|
|
int namedConstructorSlot = DOM_INTERFACE_SLOTS_BASE;
|
|
|
|
while (namedConstructors->mName) {
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Rooted<JSObject*> namedConstructor(cx,
|
|
|
|
CreateConstructor(cx, global, namedConstructors->mName,
|
|
|
|
&namedConstructors->mHolder,
|
|
|
|
namedConstructors->mNargs));
|
2013-01-22 02:51:15 -08:00
|
|
|
if (!namedConstructor ||
|
|
|
|
!JS_DefineProperty(cx, namedConstructor, "prototype",
|
2014-01-21 18:11:17 -08:00
|
|
|
proto, JSPROP_PERMANENT | JSPROP_READONLY,
|
|
|
|
JS_PropertyStub, JS_StrictPropertyStub) ||
|
2013-08-07 14:40:00 -07:00
|
|
|
(defineOnGlobal &&
|
|
|
|
!DefineConstructor(cx, global, namedConstructors->mName,
|
|
|
|
namedConstructor))) {
|
2013-01-22 02:51:15 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
js::SetReservedSlot(constructor, namedConstructorSlot++,
|
|
|
|
JS::ObjectValue(*namedConstructor));
|
|
|
|
++namedConstructors;
|
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
}
|
|
|
|
|
2014-02-15 13:12:34 -08: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 03:09:41 -08:00
|
|
|
bool
|
2013-10-08 09:18:40 -07:00
|
|
|
DefineWebIDLBindingPropertiesOnXPCObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2014-02-15 13:12:34 -08:00
|
|
|
const NativeProperties* properties)
|
2012-11-22 03:09:41 -08:00
|
|
|
{
|
|
|
|
if (properties->methods &&
|
2013-10-08 09:18:40 -07:00
|
|
|
!DefinePrefable(cx, obj, properties->methods)) {
|
2012-11-22 03:09:41 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties->attributes &&
|
2013-10-08 09:18:40 -07:00
|
|
|
!DefinePrefable(cx, obj, properties->attributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-22 03:09:41 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
static JSObject*
|
2013-05-01 20:44:12 -07:00
|
|
|
CreateInterfacePrototypeObject(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Handle<JSObject*> parentProto,
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* protoClass,
|
2012-10-09 11:50:05 -07:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Rooted<JSObject*> ourProto(cx,
|
|
|
|
JS_NewObjectWithUniqueType(cx, protoClass, parentProto, global));
|
2014-02-15 13:12:34 -08:00
|
|
|
if (!ourProto ||
|
|
|
|
!DefineProperties(cx, ourProto, properties, chromeOnlyProperties)) {
|
2013-10-28 07:04:12 -07:00
|
|
|
return nullptr;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-15 13:12:34 -08:00
|
|
|
return ourProto;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DefineProperties(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties)
|
|
|
|
{
|
2012-10-09 11:50:05 -07:00
|
|
|
if (properties) {
|
|
|
|
if (properties->methods &&
|
2014-02-15 13:12:34 -08:00
|
|
|
!DefinePrefable(cx, obj, properties->methods)) {
|
|
|
|
return false;
|
2012-10-09 11:50:05 -07:00
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
if (properties->attributes &&
|
2014-02-15 13:12:34 -08:00
|
|
|
!DefinePrefable(cx, obj, properties->attributes)) {
|
|
|
|
return false;
|
2012-10-09 11:50:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (properties->constants &&
|
2014-02-15 13:12:34 -08:00
|
|
|
!DefinePrefable(cx, obj, properties->constants)) {
|
|
|
|
return false;
|
2012-10-09 11:50:05 -07:00
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
if (chromeOnlyProperties) {
|
|
|
|
if (chromeOnlyProperties->methods &&
|
2014-02-15 13:12:34 -08:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->methods)) {
|
|
|
|
return false;
|
2012-10-09 11:50:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties->attributes &&
|
2014-02-15 13:12:34 -08:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->attributes)) {
|
|
|
|
return false;
|
2012-10-09 11:50:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties->constants &&
|
2014-02-15 13:12:34 -08:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->constants)) {
|
|
|
|
return false;
|
2012-10-09 11:50:05 -07:00
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-15 13:12:34 -08:00
|
|
|
return true;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
void
|
2013-05-01 20:44:12 -07:00
|
|
|
CreateInterfaceObjects(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Handle<JSObject*> protoProto,
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* protoClass, JS::Heap<JSObject*>* protoCache,
|
2013-05-02 11:38:19 -07:00
|
|
|
JS::Handle<JSObject*> constructorProto,
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* constructorClass, const JSNativeHolder* constructor,
|
2013-01-22 02:51:15 -08:00
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
2014-05-19 13:37:59 -07:00
|
|
|
JS::Heap<JSObject*>* constructorCache,
|
2012-10-09 11:50:05 -07:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2013-08-07 14:40:00 -07:00
|
|
|
const char* name, bool defineOnGlobal)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2012-05-02 21:35:38 -07:00
|
|
|
MOZ_ASSERT(protoClass || constructorClass || constructor,
|
|
|
|
"Need at least one class or a constructor!");
|
2012-10-09 11:50:05 -07:00
|
|
|
MOZ_ASSERT(!((properties &&
|
|
|
|
(properties->methods || properties->attributes)) ||
|
|
|
|
(chromeOnlyProperties &&
|
|
|
|
(chromeOnlyProperties->methods ||
|
|
|
|
chromeOnlyProperties->attributes))) || protoClass,
|
2012-03-30 21:42:20 -07:00
|
|
|
"Methods or properties but no protoClass!");
|
2012-10-19 00:34:28 -07:00
|
|
|
MOZ_ASSERT(!((properties &&
|
|
|
|
(properties->staticMethods || properties->staticAttributes)) ||
|
|
|
|
(chromeOnlyProperties &&
|
|
|
|
(chromeOnlyProperties->staticMethods ||
|
|
|
|
chromeOnlyProperties->staticAttributes))) ||
|
2012-10-09 11:50:05 -07:00
|
|
|
constructorClass || constructor,
|
2012-05-02 21:35:38 -07:00
|
|
|
"Static methods but no constructorClass or constructor!");
|
|
|
|
MOZ_ASSERT(bool(name) == bool(constructorClass || constructor),
|
2012-03-30 21:42:20 -07:00
|
|
|
"Must have name precisely when we have an interface object");
|
2012-05-02 21:35:38 -07:00
|
|
|
MOZ_ASSERT(!constructorClass || !constructor);
|
2012-10-09 11:50:27 -07: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-30 21:42:20 -07:00
|
|
|
|
2013-05-01 20:44:11 -07:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2012-03-30 21:42:20 -07:00
|
|
|
if (protoClass) {
|
2013-05-01 20:44:11 -07:00
|
|
|
proto =
|
|
|
|
CreateInterfacePrototypeObject(cx, global, protoProto, protoClass,
|
|
|
|
properties, chromeOnlyProperties);
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!proto) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
2012-08-07 22:26:18 -07:00
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
*protoCache = proto;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
else {
|
2013-05-01 20:44:11 -07:00
|
|
|
MOZ_ASSERT(!proto);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* interface;
|
2012-05-02 21:35:38 -07:00
|
|
|
if (constructorClass || constructor) {
|
2013-05-02 11:38:19 -07:00
|
|
|
interface = CreateInterfaceObject(cx, global, constructorProto,
|
|
|
|
constructorClass, constructor,
|
2013-01-22 02:51:15 -08:00
|
|
|
ctorNargs, namedConstructors, proto,
|
2013-08-07 14:40:00 -07:00
|
|
|
properties, chromeOnlyProperties, name,
|
|
|
|
defineOnGlobal);
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!interface) {
|
2012-10-09 11:50:27 -07: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-30 21:42:20 -07:00
|
|
|
}
|
2012-10-09 11:50:27 -07:00
|
|
|
*constructorCache = interface;
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 01:20:04 -08:00
|
|
|
bool
|
|
|
|
NativeInterface2JSObjectAndThrowIfFailed(JSContext* aCx,
|
2013-04-29 14:33:42 -07:00
|
|
|
JS::Handle<JSObject*> aScope,
|
2013-10-11 22:02:39 -07:00
|
|
|
JS::MutableHandle<JS::Value> aRetval,
|
2012-03-30 21:42:20 -07:00
|
|
|
xpcObjectHelper& aHelper,
|
|
|
|
const nsIID* aIID,
|
|
|
|
bool aAllowNativeWrapper)
|
|
|
|
{
|
2014-04-08 15:27:18 -07:00
|
|
|
js::AssertSameCompartment(aCx, aScope);
|
2012-03-30 21:42:20 -07:00
|
|
|
nsresult rv;
|
2013-09-09 14:58:29 -07:00
|
|
|
// Inline some logic from XPCConvert::NativeInterfaceToJSObject that we need
|
|
|
|
// on all threads.
|
|
|
|
nsWrapperCache *cache = aHelper.GetWrapperCache();
|
|
|
|
|
|
|
|
if (cache && cache->IsDOMBinding()) {
|
2013-10-15 15:34:13 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, cache->GetWrapper());
|
2013-09-09 14:58:29 -07:00
|
|
|
if (!obj) {
|
2014-04-08 15:27:18 -07:00
|
|
|
obj = cache->WrapObject(aCx);
|
2013-09-09 14:58:29 -07:00
|
|
|
}
|
|
|
|
|
2013-10-15 17:02:23 -07:00
|
|
|
if (obj && aAllowNativeWrapper && !JS_WrapObject(aCx, &obj)) {
|
2013-09-09 14:58:29 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj) {
|
2013-10-11 22:02:39 -07:00
|
|
|
aRetval.setObject(*obj);
|
2013-09-09 14:58:29 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-10-28 07:04:12 -07:00
|
|
|
if (!XPCConvert::NativeInterface2JSObject(aRetval, nullptr, aHelper, aIID,
|
|
|
|
nullptr, aAllowNativeWrapper, &rv)) {
|
2012-03-30 21:42:20 -07: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-08 20:29:21 -07:00
|
|
|
Throw(aCx, NS_FAILED(rv) ? rv : NS_ERROR_UNEXPECTED);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-09 10:59:02 -08:00
|
|
|
bool
|
|
|
|
TryPreserveWrapper(JSObject* obj)
|
|
|
|
{
|
2013-07-01 00:14:36 -07:00
|
|
|
MOZ_ASSERT(IsDOMObject(obj));
|
|
|
|
|
2013-05-31 23:56:02 -07:00
|
|
|
if (nsISupports* native = UnwrapDOMObjectToISupports(obj)) {
|
2012-11-09 10:59:02 -08:00
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(native, &cache);
|
|
|
|
if (cache) {
|
2013-06-23 00:15:42 -07:00
|
|
|
cache->PreserveWrapper(native);
|
2012-11-09 10:59:02 -08: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.
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(obj);
|
2012-11-09 10:59:02 -08:00
|
|
|
return domClass && !domClass->mParticipant;
|
|
|
|
}
|
|
|
|
|
2014-05-19 13:37:59 -07:00
|
|
|
// Can only be called with a DOM JSClass.
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2014-05-19 13:37:59 -07:00
|
|
|
InstanceClassHasProtoAtDepth(const js::Class* clasp,
|
|
|
|
uint32_t protoID, uint32_t depth)
|
2012-08-07 22:26:18 -07:00
|
|
|
{
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* domClass = DOMJSClass::FromJSClass(clasp);
|
|
|
|
return static_cast<uint32_t>(domClass->mInterfaceChain[depth]) == protoID;
|
2012-08-07 22:26:18 -07:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
// Only set allowNativeWrapper to false if you really know you need it, if in
|
|
|
|
// doubt use true. Setting it to false disables security wrappers.
|
|
|
|
bool
|
2013-04-29 14:33:42 -07:00
|
|
|
XPCOMObjectToJsval(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
xpcObjectHelper& helper, const nsIID* iid,
|
2013-10-11 22:02:39 -07:00
|
|
|
bool allowNativeWrapper, JS::MutableHandle<JS::Value> rval)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2012-11-27 01:20:04 -08:00
|
|
|
if (!NativeInterface2JSObjectAndThrowIfFailed(cx, scope, rval, helper, iid,
|
2012-03-30 21:42:20 -07:00
|
|
|
allowNativeWrapper)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-10-15 15:34:13 -07:00
|
|
|
JSObject* jsobj = rval.toObjectOrNull();
|
2012-03-30 21:42:20 -07: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-03 19:22:16 -07:00
|
|
|
bool
|
2014-04-09 21:58:43 -07:00
|
|
|
VariantToJsval(JSContext* aCx, nsIVariant* aVariant,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval)
|
2013-04-03 19:22:16 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2013-06-04 21:11:19 -07:00
|
|
|
if (!XPCVariant::VariantDataToJS(aVariant, &rv, aRetval)) {
|
2013-04-03 19:22:16 -07:00
|
|
|
// Does it throw? Who knows
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
2013-09-08 20:29:21 -07:00
|
|
|
Throw(aCx, NS_FAILED(rv) ? rv : NS_ERROR_UNEXPECTED);
|
2013-04-03 19:22:16 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
bool
|
2012-03-30 21:42:20 -07:00
|
|
|
QueryInterface(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2013-06-07 19:45:46 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JS::Value> thisv(cx, JS_THIS(cx, vp));
|
2013-04-29 14:33:42 -07:00
|
|
|
if (thisv.isNull())
|
2012-03-30 21:42:20 -07:00
|
|
|
return false;
|
|
|
|
|
2012-06-12 06:44:21 -07:00
|
|
|
// Get the object. It might be a security wrapper, in which case we do a checked
|
|
|
|
// unwrap.
|
2013-04-29 14:33:42 -07:00
|
|
|
JS::Rooted<JSObject*> origObj(cx, &thisv.toObject());
|
2014-02-15 13:12:33 -08:00
|
|
|
JSObject* obj = js::CheckedUnwrap(origObj, /* stopAtOuter = */ false);
|
2012-11-14 09:56:26 -08:00
|
|
|
if (!obj) {
|
|
|
|
JS_ReportError(cx, "Permission denied to access object");
|
2012-06-12 06:44:21 -07:00
|
|
|
return false;
|
2012-11-14 09:56:26 -08:00
|
|
|
}
|
2012-06-12 06:44:21 -07:00
|
|
|
|
2014-02-15 13:12:33 -08: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-05-31 23:56:02 -07:00
|
|
|
if (!native) {
|
2013-09-08 20:29:21 -07:00
|
|
|
return Throw(cx, NS_ERROR_FAILURE);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 1) {
|
2013-09-08 20:29:21 -07:00
|
|
|
return Throw(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2013-10-15 15:34:13 -07:00
|
|
|
if (!args[0].isObject()) {
|
2013-09-08 20:29:21 -07:00
|
|
|
return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2013-01-29 09:51:55 -08:00
|
|
|
nsIJSID* iid;
|
2013-05-29 20:15:31 -07:00
|
|
|
SelfRef iidRef;
|
2013-10-11 22:02:39 -07:00
|
|
|
if (NS_FAILED(xpc_qsUnwrapArg<nsIJSID>(cx, args[0], &iid, &iidRef.ptr,
|
|
|
|
args[0]))) {
|
2013-09-08 20:29:21 -07:00
|
|
|
return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
2012-03-30 21:42:20 -07: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-08 20:29:21 -07:00
|
|
|
return Throw(cx, rv);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2014-04-09 21:58:43 -07:00
|
|
|
return WrapObject(cx, ci, &NS_GET_IID(nsIClassInfo), args.rval());
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
2012-06-12 06:44:21 -07:00
|
|
|
|
2013-01-14 02:29:49 -08:00
|
|
|
nsCOMPtr<nsISupports> unused;
|
|
|
|
nsresult rv = native->QueryInterface(*iid->GetID(), getter_AddRefs(unused));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-08 20:29:21 -07:00
|
|
|
return Throw(cx, rv);
|
2013-01-14 02:29:49 -08:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
*vp = thisv;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
2014-02-15 13:12:33 -08:00
|
|
|
GetInterfaceImpl(JSContext* aCx, nsIInterfaceRequestor* aRequestor,
|
2014-06-11 13:26:52 -07:00
|
|
|
nsWrapperCache* aCache, nsIJSID* aIID,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval, ErrorResult& aError)
|
2014-02-15 13:12:33 -08:00
|
|
|
{
|
|
|
|
const nsID* iid = aIID->GetID();
|
|
|
|
|
|
|
|
nsRefPtr<nsISupports> result;
|
|
|
|
aError = aRequestor->GetInterface(*iid, getter_AddRefs(result));
|
|
|
|
if (aError.Failed()) {
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2014-02-15 13:12:33 -08:00
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
if (!WrapObject(aCx, result, iid, aRetval)) {
|
2014-02-15 13:12:33 -08:00
|
|
|
aError.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
bool
|
2012-05-02 21:35:38 -07:00
|
|
|
ThrowingConstructor(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2012-08-04 00:44:00 -07:00
|
|
|
return ThrowErrorMessage(cx, MSG_ILLEGAL_CONSTRUCTOR);
|
2012-05-02 21:35:38 -07:00
|
|
|
}
|
|
|
|
|
2014-03-10 14:38:31 -07:00
|
|
|
bool
|
|
|
|
ThrowConstructorWithoutNew(JSContext* cx, const char* name)
|
|
|
|
{
|
|
|
|
return ThrowErrorMessage(cx, MSG_CONSTRUCTOR_WITHOUT_NEW, name);
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
inline const NativePropertyHooks*
|
2013-05-03 16:29:08 -07:00
|
|
|
GetNativePropertyHooks(JSContext *cx, JS::Handle<JSObject*> obj,
|
|
|
|
DOMObjectType& type)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(obj);
|
2012-12-06 12:21:19 -08:00
|
|
|
if (domClass) {
|
2012-10-09 11:50:27 -07: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 02:51:15 -08:00
|
|
|
const JSNativeHolder* nativeHolder =
|
|
|
|
static_cast<const JSNativeHolder*>(v.toPrivate());
|
2012-10-09 11:50:27 -07: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 11:38:29 -07: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 11:38:30 -07: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 11:50:27 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayResolveOwnProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2014-04-25 14:11:02 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks *nativePropertyHooks =
|
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
|
|
|
|
2013-09-25 11:38:29 -07:00
|
|
|
if (type != eInstance) {
|
2013-09-25 11:38:30 -07: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 11:38:29 -07: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-25 14:11:02 -07:00
|
|
|
nativePropertyHooks->mResolveOwnProperty(cx, wrapper, obj, id, desc);
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayResolveAttribute(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-04-02 17:20:38 -07:00
|
|
|
const Prefable<const JSPropertySpec>* attributes, jsid* attributeIds,
|
2013-08-12 04:09:14 -07:00
|
|
|
const JSPropertySpec* attributeSpecs, JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
|
|
|
for (; attributes->specs; ++attributes) {
|
2013-02-19 08:54:40 -08:00
|
|
|
if (attributes->isEnabled(cx, obj)) {
|
2012-10-09 11:50:27 -07: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-16 18:27:43 -08:00
|
|
|
for ( ; attributeIds[i] != JSID_VOID; ++i) {
|
2012-10-09 11:50:27 -07:00
|
|
|
if (id == attributeIds[i]) {
|
2013-04-02 17:20:38 -07:00
|
|
|
const JSPropertySpec& attrSpec = attributeSpecs[i];
|
2012-10-09 11:50:27 -07: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 04:09:14 -07:00
|
|
|
desc.setAttributes(attrSpec.flags & ~JSPROP_NATIVE_ACCESSORS);
|
2012-10-09 11:50:27 -07:00
|
|
|
// They all have getters, so we can just make it.
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSFunction*> fun(cx,
|
2014-01-10 09:11:31 -08:00
|
|
|
JS_NewFunctionById(cx, (JSNative)attrSpec.getter.propertyOp.op,
|
2014-03-04 10:05:07 -08:00
|
|
|
0, 0, wrapper, id));
|
2012-10-09 11:50:27 -07:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
2014-01-10 09:11:31 -08:00
|
|
|
SET_JITINFO(fun, attrSpec.getter.propertyOp.info);
|
2012-10-09 11:50:27 -07:00
|
|
|
JSObject *funobj = JS_GetFunctionObject(fun);
|
2013-08-12 04:09:14 -07:00
|
|
|
desc.setGetterObject(funobj);
|
|
|
|
desc.attributesRef() |= JSPROP_GETTER;
|
2014-01-10 09:11:31 -08:00
|
|
|
if (attrSpec.setter.propertyOp.op) {
|
2012-10-09 11:50:27 -07:00
|
|
|
// We have a setter! Make it.
|
2014-01-10 09:11:31 -08:00
|
|
|
fun = JS_NewFunctionById(cx, (JSNative)attrSpec.setter.propertyOp.op, 1, 0,
|
2014-03-04 10:05:07 -08:00
|
|
|
wrapper, id);
|
2012-10-09 11:50:27 -07:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
2014-01-10 09:11:31 -08:00
|
|
|
SET_JITINFO(fun, attrSpec.setter.propertyOp.info);
|
2012-10-09 11:50:27 -07:00
|
|
|
funobj = JS_GetFunctionObject(fun);
|
2013-08-12 04:09:14 -07:00
|
|
|
desc.setSetterObject(funobj);
|
|
|
|
desc.attributesRef() |= JSPROP_SETTER;
|
2012-10-09 11:50:27 -07:00
|
|
|
} else {
|
2013-08-12 04:09:14 -07:00
|
|
|
desc.setSetter(nullptr);
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
2013-08-12 04:09:14 -07:00
|
|
|
desc.object().set(wrapper);
|
2012-10-09 11:50:27 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-25 11:38:29 -07: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 11:50:05 -07:00
|
|
|
static bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayResolveProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-08-12 04:09:14 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc, DOMObjectType type,
|
2012-10-09 11:50:05 -07:00
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 04:52:07 -07:00
|
|
|
{
|
2013-04-02 17:20:38 -07:00
|
|
|
const Prefable<const JSFunctionSpec>* methods;
|
2012-10-09 11:50:27 -07:00
|
|
|
jsid* methodIds;
|
2013-04-02 17:20:38 -07:00
|
|
|
const JSFunctionSpec* methodsSpecs;
|
2012-10-09 11:50:27 -07: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-02 17:20:38 -07:00
|
|
|
const Prefable<const JSFunctionSpec>* method;
|
2012-10-09 11:50:27 -07:00
|
|
|
for (method = methods; method->specs; ++method) {
|
2013-02-19 08:54:40 -08:00
|
|
|
if (method->isEnabled(cx, obj)) {
|
2012-10-09 11:50:05 -07:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2012-10-09 11:50:27 -07:00
|
|
|
size_t i = method->specs - methodsSpecs;
|
2013-12-16 18:27:43 -08:00
|
|
|
for ( ; methodIds[i] != JSID_VOID; ++i) {
|
2012-10-09 11:50:27 -07:00
|
|
|
if (id == methodIds[i]) {
|
2013-04-02 17:20:38 -07:00
|
|
|
const JSFunctionSpec& methodSpec = methodsSpecs[i];
|
2013-09-26 07:11:38 -07: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 11:50:05 -07:00
|
|
|
}
|
|
|
|
JSObject *funobj = JS_GetFunctionObject(fun);
|
2013-08-12 04:09:14 -07:00
|
|
|
desc.value().setObject(*funobj);
|
|
|
|
desc.setAttributes(methodSpec.flags);
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.setSetter(nullptr);
|
|
|
|
desc.setGetter(nullptr);
|
2012-10-09 11:50:05 -07:00
|
|
|
return true;
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
2012-07-18 04:52:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 00:34:28 -07:00
|
|
|
if (type == eInterface) {
|
|
|
|
if (nativeProperties->staticAttributes) {
|
2013-02-19 08:54:40 -08:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-19 00:34:28 -07:00
|
|
|
nativeProperties->staticAttributes,
|
|
|
|
nativeProperties->staticAttributeIds,
|
|
|
|
nativeProperties->staticAttributeSpecs, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 04:09:14 -07:00
|
|
|
if (desc.object()) {
|
2012-10-19 00:34:28 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-10-09 11:50:27 -07:00
|
|
|
if (nativeProperties->attributes) {
|
2013-02-19 08:54:40 -08:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-09 11:50:27 -07:00
|
|
|
nativeProperties->attributes,
|
|
|
|
nativeProperties->attributeIds,
|
|
|
|
nativeProperties->attributeSpecs, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 04:09:14 -07:00
|
|
|
if (desc.object()) {
|
2013-09-25 15:22:33 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-07-18 04:52:07 -07:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
if (nativeProperties->constants) {
|
2013-04-02 17:20:38 -07:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2012-10-09 11:50:05 -07:00
|
|
|
for (constant = nativeProperties->constants; constant->specs; ++constant) {
|
2013-02-19 08:54:40 -08:00
|
|
|
if (constant->isEnabled(cx, obj)) {
|
2012-10-09 11:50:05 -07: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-16 18:27:43 -08:00
|
|
|
for ( ; nativeProperties->constantIds[i] != JSID_VOID; ++i) {
|
2012-10-09 11:50:05 -07:00
|
|
|
if (id == nativeProperties->constantIds[i]) {
|
2013-08-12 04:09:14 -07:00
|
|
|
desc.setAttributes(JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.value().set(nativeProperties->constantSpecs[i].value);
|
2012-10-09 11:50:05 -07:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-18 04:52:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
static bool
|
2013-05-03 16:29:08 -07:00
|
|
|
ResolvePrototypeOrConstructor(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2014-03-22 10:04:09 -07:00
|
|
|
size_t protoAndIfaceCacheIndex, unsigned attrs,
|
2013-08-12 04:09:14 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, js::GetGlobalForObjectCrossCompartment(obj));
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, global);
|
2014-03-22 10:04:09 -07:00
|
|
|
ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
|
2013-12-10 13:04:55 -08:00
|
|
|
JSObject* protoOrIface =
|
2014-03-22 10:04:09 -07:00
|
|
|
protoAndIfaceCache.EntrySlotIfExists(protoAndIfaceCacheIndex);
|
2012-10-09 11:50:27 -07:00
|
|
|
if (!protoOrIface) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 04:09:14 -07: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 11:50:27 -07:00
|
|
|
}
|
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
|
|
|
}
|
|
|
|
|
2013-09-25 11:38:30 -07:00
|
|
|
/* static */ bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2012-10-09 11:50:27 -07:00
|
|
|
const NativePropertyHooks* nativePropertyHooks,
|
2013-05-03 16:29:08 -07:00
|
|
|
DOMObjectType type, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id,
|
2013-08-12 04:09:14 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 11:50:05 -07:00
|
|
|
{
|
2012-10-09 11:50:27 -07:00
|
|
|
if (type == eInterface && IdEquals(id, "prototype")) {
|
|
|
|
return nativePropertyHooks->mPrototypeID == prototypes::id::_ID_Count ||
|
|
|
|
ResolvePrototypeOrConstructor(cx, wrapper, obj,
|
|
|
|
nativePropertyHooks->mPrototypeID,
|
2012-10-29 13:55:13 -07:00
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY,
|
2012-10-09 11:50:27 -07:00
|
|
|
desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eInterfacePrototype && IdEquals(id, "constructor")) {
|
|
|
|
return nativePropertyHooks->mConstructorID == constructors::id::_ID_Count ||
|
|
|
|
ResolvePrototypeOrConstructor(cx, wrapper, obj,
|
|
|
|
nativePropertyHooks->mConstructorID,
|
2012-10-29 13:55:13 -07:00
|
|
|
0, desc);
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
|
|
|
|
if (nativeProperties.regular &&
|
2013-02-19 08:54:40 -08:00
|
|
|
!XrayResolveProperty(cx, wrapper, obj, id, desc, type,
|
2012-10-09 11:50:27 -07:00
|
|
|
nativeProperties.regular)) {
|
2012-10-09 11:50:05 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-12 04:09:14 -07:00
|
|
|
if (!desc.object() &&
|
2012-10-09 11:50:27 -07:00
|
|
|
nativeProperties.chromeOnly &&
|
2012-10-09 11:50:05 -07:00
|
|
|
xpc::AccessCheck::isChrome(js::GetObjectCompartment(wrapper)) &&
|
2013-02-19 08:54:40 -08:00
|
|
|
!XrayResolveProperty(cx, wrapper, obj, id, desc, type,
|
2012-10-09 11:50:27 -07:00
|
|
|
nativeProperties.chromeOnly)) {
|
2012-10-09 11:50:05 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-18 04:52:07 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2013-08-12 04:09:14 -07:00
|
|
|
JS::Handle<jsid> id, JS::MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-09 11:50:27 -07: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 04:09:14 -07:00
|
|
|
if (desc.object()) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while ((nativePropertyHooks = nativePropertyHooks->mProtoHooks));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XrayResolveNativeProperty(cx, wrapper, nativePropertyHooks, type, obj,
|
|
|
|
id, desc);
|
|
|
|
}
|
|
|
|
|
2013-05-29 02:38:57 -07:00
|
|
|
bool
|
|
|
|
XrayDefineProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-04-30 10:29:40 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc, bool* defined)
|
2013-05-29 02:38:57 -07:00
|
|
|
{
|
|
|
|
if (!js::IsProxy(obj))
|
|
|
|
return true;
|
|
|
|
|
2014-06-27 04:44:08 -07:00
|
|
|
const DOMProxyHandler* handler = GetDOMProxyHandler(obj);
|
2013-05-29 02:38:57 -07:00
|
|
|
return handler->defineProperty(cx, wrapper, id, desc, defined);
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayEnumerateAttributes(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2013-04-02 17:20:38 -07:00
|
|
|
const Prefable<const JSPropertySpec>* attributes,
|
|
|
|
jsid* attributeIds, const JSPropertySpec* attributeSpecs,
|
2012-11-05 08:58:03 -08:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
|
|
|
for (; attributes->specs; ++attributes) {
|
2013-02-19 08:54:40 -08:00
|
|
|
if (attributes->isEnabled(cx, obj)) {
|
2012-10-09 11:50:27 -07: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-16 18:27:43 -08:00
|
|
|
for ( ; attributeIds[i] != JSID_VOID; ++i) {
|
2012-11-05 08:58:03 -08:00
|
|
|
if (((flags & JSITER_HIDDEN) ||
|
|
|
|
(attributeSpecs[i].flags & JSPROP_ENUMERATE)) &&
|
2012-10-09 11:50:27 -07:00
|
|
|
!props.append(attributeIds[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayEnumerateProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2013-02-19 08:54:40 -08:00
|
|
|
unsigned flags, JS::AutoIdVector& props,
|
2012-11-05 08:58:03 -08:00
|
|
|
DOMObjectType type,
|
2012-10-09 11:50:05 -07:00
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 04:52:07 -07:00
|
|
|
{
|
2013-04-02 17:20:38 -07:00
|
|
|
const Prefable<const JSFunctionSpec>* methods;
|
2012-10-09 11:50:27 -07:00
|
|
|
jsid* methodIds;
|
2013-04-02 17:20:38 -07:00
|
|
|
const JSFunctionSpec* methodsSpecs;
|
2012-10-09 11:50:27 -07: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-02 17:20:38 -07:00
|
|
|
const Prefable<const JSFunctionSpec>* method;
|
2012-10-09 11:50:27 -07:00
|
|
|
for (method = methods; method->specs; ++method) {
|
2013-02-19 08:54:40 -08:00
|
|
|
if (method->isEnabled(cx, obj)) {
|
2012-10-09 11:50:05 -07:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2012-10-09 11:50:27 -07:00
|
|
|
size_t i = method->specs - methodsSpecs;
|
2013-12-16 18:27:43 -08:00
|
|
|
for ( ; methodIds[i] != JSID_VOID; ++i) {
|
2012-11-05 08:58:03 -08:00
|
|
|
if (((flags & JSITER_HIDDEN) ||
|
|
|
|
(methodsSpecs[i].flags & JSPROP_ENUMERATE)) &&
|
2012-10-09 11:50:27 -07:00
|
|
|
!props.append(methodIds[i])) {
|
2012-10-09 11:50:05 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 04:52:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 00:34:28 -07:00
|
|
|
if (type == eInterface) {
|
|
|
|
if (nativeProperties->staticAttributes &&
|
2013-02-19 08:54:40 -08:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->staticAttributes,
|
2012-10-19 00:34:28 -07:00
|
|
|
nativeProperties->staticAttributeIds,
|
|
|
|
nativeProperties->staticAttributeSpecs,
|
2012-11-05 08:58:03 -08:00
|
|
|
flags, props)) {
|
2012-10-19 00:34:28 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2012-10-09 11:50:27 -07:00
|
|
|
if (nativeProperties->attributes &&
|
2013-02-19 08:54:40 -08:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->attributes,
|
2012-10-09 11:50:27 -07:00
|
|
|
nativeProperties->attributeIds,
|
2012-11-05 08:58:03 -08:00
|
|
|
nativeProperties->attributeSpecs,
|
|
|
|
flags, props)) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nativeProperties->unforgeableAttributes &&
|
2013-02-19 08:54:40 -08:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->unforgeableAttributes,
|
2012-10-09 11:50:27 -07:00
|
|
|
nativeProperties->unforgeableAttributeIds,
|
|
|
|
nativeProperties->unforgeableAttributeSpecs,
|
2012-11-05 08:58:03 -08:00
|
|
|
flags, props)) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
2012-07-18 04:52:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
if (nativeProperties->constants) {
|
2013-04-02 17:20:38 -07:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2012-10-09 11:50:05 -07:00
|
|
|
for (constant = nativeProperties->constants; constant->specs; ++constant) {
|
2013-02-19 08:54:40 -08:00
|
|
|
if (constant->isEnabled(cx, obj)) {
|
2012-10-09 11:50:05 -07: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-16 18:27:43 -08:00
|
|
|
for ( ; nativeProperties->constantIds[i] != JSID_VOID; ++i) {
|
2012-10-09 11:50:05 -07:00
|
|
|
if (!props.append(nativeProperties->constantIds[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 04:52:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:05 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayEnumerateNativeProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2012-10-09 11:50:27 -07:00
|
|
|
const NativePropertyHooks* nativePropertyHooks,
|
2013-05-03 16:29:08 -07:00
|
|
|
DOMObjectType type, JS::Handle<JSObject*> obj,
|
2012-11-05 08:58:03 -08:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 11:50:05 -07:00
|
|
|
{
|
2012-10-09 11:50:27 -07:00
|
|
|
if (type == eInterface &&
|
|
|
|
nativePropertyHooks->mPrototypeID != prototypes::id::_ID_Count &&
|
|
|
|
!AddStringToIDVector(cx, props, "prototype")) {
|
2012-10-09 11:50:05 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
if (type == eInterfacePrototype &&
|
|
|
|
nativePropertyHooks->mConstructorID != constructors::id::_ID_Count &&
|
2012-11-05 08:58:03 -08:00
|
|
|
(flags & JSITER_HIDDEN) &&
|
2012-10-09 11:50:27 -07:00
|
|
|
!AddStringToIDVector(cx, props, "constructor")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
|
|
|
|
if (nativeProperties.regular &&
|
2013-02-19 08:54:40 -08:00
|
|
|
!XrayEnumerateProperties(cx, wrapper, obj, flags, props, type,
|
|
|
|
nativeProperties.regular)) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeProperties.chromeOnly &&
|
2012-10-09 11:50:05 -07:00
|
|
|
xpc::AccessCheck::isChrome(js::GetObjectCompartment(wrapper)) &&
|
2013-02-19 08:54:40 -08:00
|
|
|
!XrayEnumerateProperties(cx, wrapper, obj, flags, props, type,
|
|
|
|
nativeProperties.chromeOnly)) {
|
2012-10-09 11:50:05 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
XrayEnumerateProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2012-11-05 08:58:03 -08:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
|
|
|
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 08:58:03 -08:00
|
|
|
if (flags & JSITER_OWNONLY) {
|
2012-10-09 11:50:27 -07: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 08:58:03 -08:00
|
|
|
obj, flags, props)) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-05 08:58:03 -08:00
|
|
|
|
|
|
|
if (flags & JSITER_OWNONLY) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-09 11:50:27 -07:00
|
|
|
} while ((nativePropertyHooks = nativePropertyHooks->mProtoHooks));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XrayEnumerateNativeProperties(cx, wrapper, nativePropertyHooks, type,
|
2012-11-05 08:58:03 -08:00
|
|
|
obj, flags, props);
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NativePropertyHooks sWorkerNativePropertyHooks = {
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
|
|
|
},
|
|
|
|
prototypes::id::_ID_Count,
|
|
|
|
constructors::id::_ID_Count,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2012-05-22 06:46:20 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
GetPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id, bool* found,
|
2012-05-22 06:46:20 -07:00
|
|
|
JS::Value* vp)
|
|
|
|
{
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-05-05 00:03:14 -07:00
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
2012-09-03 16:42:10 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 06:46:20 -07:00
|
|
|
if (!proto) {
|
|
|
|
*found = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProp;
|
2012-05-22 06:46:20 -07:00
|
|
|
if (!JS_HasPropertyById(cx, proto, id, &hasProp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*found = hasProp;
|
|
|
|
if (!hasProp || !vp) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-26 02:00:38 -07:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
if (!JS_ForwardGetPropertyTo(cx, proto, id, proxy, &value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = value;
|
|
|
|
return true;
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
HasPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id)
|
2012-05-22 06:46:20 -07:00
|
|
|
{
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, proxy);
|
2012-08-21 18:42:53 -07:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-05-03 16:29:08 -07:00
|
|
|
if (xpc::WrapperFactory::IsXrayWrapper(obj)) {
|
|
|
|
obj = js::UncheckedUnwrap(obj);
|
|
|
|
ac.construct(cx, obj);
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool found;
|
2013-07-09 07:45:13 -07: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-11 22:02:39 -07:00
|
|
|
if (!xpc::NonVoidStringToJsval(cx, names[i], &v)) {
|
2013-07-09 07:45:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<jsid> id(cx);
|
2014-01-17 10:08:51 -08:00
|
|
|
if (!JS_ValueToId(cx, v, &id)) {
|
2013-07-09 07:45:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shadowPrototypeProperties || !HasPropertyOnPrototype(cx, proxy, id)) {
|
|
|
|
if (!props.append(id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-05-22 06:46:20 -07:00
|
|
|
}
|
|
|
|
|
2013-05-07 14:18:03 -07:00
|
|
|
bool
|
2014-02-06 18:08:28 -08:00
|
|
|
DictionaryBase::ParseJSON(JSContext* aCx,
|
|
|
|
const nsAString& aJSON,
|
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2012-10-16 07:52:10 -07:00
|
|
|
{
|
2012-10-16 08:37:44 -07:00
|
|
|
if (aJSON.IsEmpty()) {
|
2013-05-07 14:18:03 -07:00
|
|
|
return true;
|
2012-10-16 07:52:10 -07:00
|
|
|
}
|
2013-05-07 14:18:03 -07:00
|
|
|
return JS_ParseJSON(aCx,
|
|
|
|
static_cast<const jschar*>(PromiseFlatString(aJSON).get()),
|
2013-05-29 01:29:39 -07:00
|
|
|
aJSON.Length(), aVal);
|
2012-10-16 07:52:10 -07:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
static JSString*
|
2013-09-16 18:33:40 -07:00
|
|
|
ConcatJSString(JSContext* cx, const char* pre, JS::Handle<JSString*> str, const char* post)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
|
|
|
if (!str) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:29:08 -07: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 11:50:27 -07:00
|
|
|
if (!preString || !postString) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-16 18:33:40 -07:00
|
|
|
preString = JS_ConcatStrings(cx, preString, str);
|
|
|
|
if (!preString) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-16 18:33:40 -07:00
|
|
|
return JS_ConcatStrings(cx, preString, postString);
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
NativeToString(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2014-07-07 13:11:24 -07:00
|
|
|
JS::Handle<JSObject*> obj,
|
2013-10-26 09:19:05 -07:00
|
|
|
JS::MutableHandle<JS::Value> v)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
2013-05-03 16:29:08 -07: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 04:09:14 -07:00
|
|
|
if (!XrayResolveNativeProperty(cx, wrapper, obj, id, &toStringDesc)) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSString*> str(cx);
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
2013-05-03 16:29:08 -07:00
|
|
|
if (toStringDesc.object()) {
|
|
|
|
JS::Rooted<JS::Value> toString(cx, toStringDesc.value());
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(cx, &toString)) {
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(JS_ObjectIsCallable(cx, &toString.toObject()));
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JS::Value> toStringResult(cx);
|
2014-02-12 23:38:36 -08:00
|
|
|
if (JS_CallFunctionValue(cx, obj, toString, JS::HandleValueArray::empty(),
|
|
|
|
&toStringResult)) {
|
2012-10-09 11:50:27 -07:00
|
|
|
str = toStringResult.toString();
|
|
|
|
} else {
|
|
|
|
str = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-01 00:29:52 -08: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 08:58:03 -08:00
|
|
|
} else {
|
2014-02-01 00:29:52 -08:00
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
JS::Rooted<JSFunction*> fun(cx, JS_GetObjectFunction(obj));
|
|
|
|
str = JS_DecompileFunction(cx, fun, 0);
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-26 09:19:05 -07:00
|
|
|
v.setString(str);
|
2012-10-09 11:50:27 -07:00
|
|
|
return JS_WrapValue(cx, v);
|
|
|
|
}
|
|
|
|
|
2012-09-26 07:17:46 -07:00
|
|
|
// Dynamically ensure that two objects don't end up with the same reserved slot.
|
2013-04-11 20:20:18 -07:00
|
|
|
class MOZ_STACK_CLASS AutoCloneDOMObjectSlotGuard
|
2012-09-26 07:17:46 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-05-03 16:29:08 -07:00
|
|
|
AutoCloneDOMObjectSlotGuard(JSContext* aCx, JSObject* aOld, JSObject* aNew)
|
|
|
|
: mOldReflector(aCx, aOld), mNewReflector(aCx, aNew)
|
2012-09-26 07:17:46 -07: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-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> mOldReflector;
|
|
|
|
JS::Rooted<JSObject*> mNewReflector;
|
2012-09-26 07:17:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
2013-10-15 15:34:13 -07:00
|
|
|
ReparentWrapper(JSContext* aCx, JS::Handle<JSObject*> aObjArg)
|
2012-09-26 07:17:46 -07:00
|
|
|
{
|
2014-04-08 15:27:15 -07:00
|
|
|
js::AssertSameCompartment(aCx, aObjArg);
|
|
|
|
|
2014-03-06 15:26:17 -08: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-15 15:34:13 -07:00
|
|
|
JS::Rooted<JSObject*> aObj(aCx, aObjArg);
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(aObj);
|
2012-09-26 07:17:46 -07:00
|
|
|
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> oldParent(aCx, JS_GetParent(aObj));
|
|
|
|
JS::Rooted<JSObject*> newParent(aCx, domClass->mGetParent(aCx, aObj));
|
2012-09-26 07:17:46 -07:00
|
|
|
|
|
|
|
JSAutoCompartment oldAc(aCx, oldParent);
|
|
|
|
|
2013-10-23 05:02:42 -07:00
|
|
|
JSCompartment* oldCompartment = js::GetObjectCompartment(oldParent);
|
|
|
|
JSCompartment* newCompartment = js::GetObjectCompartment(newParent);
|
|
|
|
if (oldCompartment == newCompartment) {
|
2012-09-26 07:17:46 -07:00
|
|
|
if (!JS_SetParent(aCx, aObj, newParent)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-23 05:02:42 -07:00
|
|
|
// Telemetry.
|
|
|
|
xpc::RecordDonatedNode(oldCompartment);
|
|
|
|
xpc::RecordAdoptedNode(newCompartment);
|
|
|
|
|
2013-05-31 23:56:02 -07:00
|
|
|
nsISupports* native = UnwrapDOMObjectToISupports(aObj);
|
|
|
|
if (!native) {
|
2012-09-26 07:17:46 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-16 10:02:57 -07:00
|
|
|
bool isProxy = js::IsProxy(aObj);
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> expandoObject(aCx);
|
2013-04-16 10:02:57 -07:00
|
|
|
if (isProxy) {
|
|
|
|
expandoObject = DOMProxyHandler::GetAndClearExpandoObject(aObj);
|
|
|
|
}
|
|
|
|
|
2012-09-26 07:17:46 -07: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-01 20:44:12 -07:00
|
|
|
JS::Rooted<JSObject*> global(aCx,
|
|
|
|
js::GetGlobalForObjectCrossCompartment(newParent));
|
|
|
|
JS::Handle<JSObject*> proto = (domClass->mGetProto)(aCx, global);
|
2012-09-26 07:17:46 -07:00
|
|
|
if (!proto) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> newobj(aCx, JS_CloneObject(aCx, aObj, proto, newParent));
|
2012-09-26 07:17:46 -07: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-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> propertyHolder(aCx);
|
2012-09-26 07:17:46 -07:00
|
|
|
{
|
2013-05-03 16:29:08 -07:00
|
|
|
AutoCloneDOMObjectSlotGuard cloneGuard(aCx, aObj, newobj);
|
2012-09-26 07:17:46 -07:00
|
|
|
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> copyFrom(aCx, isProxy ? expandoObject : aObj);
|
2013-04-16 10:02:57 -07:00
|
|
|
if (copyFrom) {
|
2014-01-16 09:48:58 -08:00
|
|
|
propertyHolder = JS_NewObjectWithGivenProto(aCx, nullptr, JS::NullPtr(),
|
2013-04-16 10:02:57 -07:00
|
|
|
newParent);
|
|
|
|
if (!propertyHolder) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-09-26 07:17:46 -07:00
|
|
|
|
2013-04-16 10:02:57 -07:00
|
|
|
if (!JS_CopyPropertiesFrom(aCx, propertyHolder, copyFrom)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
propertyHolder = nullptr;
|
2012-09-26 07:17:46 -07: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 09:35:45 -07:00
|
|
|
aObj = xpc::TransplantObject(aCx, aObj, newobj);
|
|
|
|
if (!aObj) {
|
2013-10-17 01:00:02 -07:00
|
|
|
MOZ_CRASH();
|
2012-09-26 07:17:46 -07:00
|
|
|
}
|
|
|
|
|
2014-03-19 09:35:45 -07:00
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(native, &cache);
|
2012-09-26 07:17:46 -07:00
|
|
|
bool preserving = cache->PreservingWrapper();
|
|
|
|
cache->SetPreservingWrapper(false);
|
|
|
|
cache->SetWrapper(aObj);
|
|
|
|
cache->SetPreservingWrapper(preserving);
|
2013-04-16 10:02:57 -07:00
|
|
|
|
|
|
|
if (propertyHolder) {
|
2014-03-17 09:17:58 -07:00
|
|
|
JS::Rooted<JSObject*> copyTo(aCx);
|
2013-04-16 10:02:57 -07:00
|
|
|
if (isProxy) {
|
|
|
|
copyTo = DOMProxyHandler::EnsureExpandoObject(aCx, aObj);
|
|
|
|
} else {
|
|
|
|
copyTo = aObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!copyTo || !JS_CopyPropertiesFrom(aCx, copyTo, propertyHolder)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2012-09-26 07:17:46 -07:00
|
|
|
}
|
|
|
|
|
2013-03-17 07:42:05 -07:00
|
|
|
nsObjectLoadingContent* htmlobject;
|
2013-11-21 04:51:16 -08:00
|
|
|
nsresult rv = UNWRAP_OBJECT(HTMLObjectElement, aObj, htmlobject);
|
2013-03-17 07:42:05 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = UnwrapObject<prototypes::id::HTMLEmbedElement,
|
2013-11-21 04:51:15 -08:00
|
|
|
HTMLSharedObjectElement>(aObj, htmlobject);
|
2013-03-17 07:42:05 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = UnwrapObject<prototypes::id::HTMLAppletElement,
|
2013-11-21 04:51:15 -08:00
|
|
|
HTMLSharedObjectElement>(aObj, htmlobject);
|
2013-03-17 07:42:05 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
htmlobject = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (htmlobject) {
|
2013-01-06 15:55:14 -08:00
|
|
|
htmlobject->SetupProtoChain(aCx, aObj);
|
|
|
|
}
|
2012-09-26 07:17:46 -07: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-22 22:17:08 -07:00
|
|
|
GlobalObject::GlobalObject(JSContext* aCx, JSObject* aObject)
|
|
|
|
: mGlobalJSObject(aCx),
|
|
|
|
mCx(aCx),
|
|
|
|
mGlobalObject(nullptr)
|
2012-12-03 08:07:49 -08:00
|
|
|
{
|
2014-06-16 11:08:00 -07:00
|
|
|
MOZ_ASSERT(mCx);
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, aObject);
|
|
|
|
if (js::IsWrapper(obj)) {
|
|
|
|
obj = js::CheckedUnwrap(obj, /* stopAtOuter = */ false);
|
|
|
|
if (!obj) {
|
2013-08-22 22:17:08 -07: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-08 20:29:21 -07:00
|
|
|
Throw(aCx, NS_ERROR_XPC_SECURITY_MANAGER_VETO);
|
2013-08-22 22:17:08 -07:00
|
|
|
return;
|
2012-12-03 08:07:49 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:19:43 -07:00
|
|
|
mGlobalJSObject = js::GetGlobalForObjectCrossCompartment(obj);
|
2012-12-03 08:07:49 -08:00
|
|
|
}
|
|
|
|
|
2013-08-22 22:17:08 -07:00
|
|
|
nsISupports*
|
|
|
|
GlobalObject::GetAsSupports() const
|
2012-12-03 08:07:49 -08:00
|
|
|
{
|
2013-08-22 22:17:08 -07:00
|
|
|
if (mGlobalObject) {
|
|
|
|
return mGlobalObject;
|
2012-12-03 08:07:49 -08:00
|
|
|
}
|
|
|
|
|
2014-02-25 13:34:55 -08:00
|
|
|
if (!NS_IsMainThread()) {
|
2014-03-06 13:05:11 -08:00
|
|
|
mGlobalObject = UnwrapDOMObjectToISupports(mGlobalJSObject);
|
|
|
|
return mGlobalObject;
|
2014-02-25 13:34:55 -08:00
|
|
|
}
|
|
|
|
|
2013-08-22 22:17:08 -07:00
|
|
|
JS::Rooted<JS::Value> val(mCx, JS::ObjectValue(*mGlobalJSObject));
|
2012-12-03 08:07:49 -08:00
|
|
|
|
|
|
|
// Switch this to UnwrapDOMObjectToISupports once our global objects are
|
|
|
|
// using new bindings.
|
2013-08-22 22:17:08 -07:00
|
|
|
nsresult rv = xpc_qsUnwrapArg<nsISupports>(mCx, val, &mGlobalObject,
|
2012-12-03 08:07:49 -08:00
|
|
|
static_cast<nsISupports**>(getter_AddRefs(mGlobalObjectRef)),
|
2013-10-11 22:02:39 -07:00
|
|
|
&val);
|
2012-12-03 08:07:49 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mGlobalObject = nullptr;
|
2013-09-08 20:29:21 -07:00
|
|
|
Throw(mCx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
2012-12-03 08:07:49 -08:00
|
|
|
}
|
|
|
|
|
2013-08-22 22:17:08 -07:00
|
|
|
return mGlobalObject;
|
2012-12-03 08:07:49 -08:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2013-05-03 16:29:08 -07:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool* bp)
|
2013-01-08 10:05:36 -08:00
|
|
|
{
|
|
|
|
const DOMIfaceAndProtoJSClass* clasp =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(js::GetObjectClass(obj));
|
|
|
|
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(js::UncheckedUnwrap(instance, /* stopAtOuter = */ false));
|
2013-01-08 10:05:36 -08: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 01:25:24 -08:00
|
|
|
if (domClass &&
|
|
|
|
domClass->mInterfaceChain[clasp->mDepth] == clasp->mPrototypeID) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-03 10:19:29 -07:00
|
|
|
JS::Rooted<JSObject*> unwrapped(cx, js::CheckedUnwrap(instance, true));
|
|
|
|
if (unwrapped && jsipc::IsCPOW(unwrapped)) {
|
2013-08-01 16:45:17 -07:00
|
|
|
bool boolp = false;
|
2014-07-03 10:19:29 -07:00
|
|
|
if (!jsipc::DOMInstanceOf(cx, unwrapped, clasp->mPrototypeID,
|
|
|
|
clasp->mDepth, &boolp)) {
|
2013-08-01 16:45:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*bp = boolp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JS::Value> protov(cx);
|
2013-07-26 02:00:38 -07:00
|
|
|
DebugOnly<bool> ok = JS_GetProperty(cx, obj, "prototype", &protov);
|
2013-02-22 01:25:24 -08:00
|
|
|
MOZ_ASSERT(ok, "Someone messed with our prototype property?");
|
|
|
|
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> interfacePrototype(cx, &protov.toObject());
|
2013-02-22 01:25:24 -08:00
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(interfacePrototype)),
|
|
|
|
"Someone messed with our prototype property?");
|
|
|
|
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-07-31 09:20:33 -07:00
|
|
|
if (!JS_GetPrototype(cx, instance, &proto)) {
|
2013-02-22 01:25:24 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (proto) {
|
|
|
|
if (proto == interfacePrototype) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-31 09:20:33 -07:00
|
|
|
if (!JS_GetPrototype(cx, proto, &proto)) {
|
2013-02-22 01:25:24 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bp = false;
|
2013-01-08 10:05:36 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2013-06-21 06:12:46 -07:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj, JS::MutableHandle<JS::Value> vp,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool* bp)
|
2013-01-08 10:05:36 -08:00
|
|
|
{
|
|
|
|
if (!vp.isObject()) {
|
|
|
|
*bp = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> instanceObject(cx, &vp.toObject());
|
|
|
|
return InterfaceHasInstance(cx, obj, instanceObject, bp);
|
2013-01-08 10:05:36 -08:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2013-08-01 16:45:17 -07:00
|
|
|
InterfaceHasInstance(JSContext* cx, int prototypeID, int depth,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool* bp)
|
2013-08-01 16:45:17 -07:00
|
|
|
{
|
2014-06-18 13:19:19 -07:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(js::UncheckedUnwrap(instance));
|
2013-08-01 16:45:17 -07:00
|
|
|
|
|
|
|
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 12:50:58 -07:00
|
|
|
bool
|
2013-09-25 11:38:30 -07:00
|
|
|
ReportLenientThisUnwrappingFailure(JSContext* cx, JSObject* obj)
|
2013-04-08 14:04:21 -07:00
|
|
|
{
|
2013-09-25 11:38:30 -07:00
|
|
|
JS::Rooted<JSObject*> rootedObj(cx, obj);
|
|
|
|
GlobalObject global(cx, rootedObj);
|
2013-05-08 12:50:58 -07:00
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(global.GetAsSupports());
|
2013-04-08 14:04:21 -07:00
|
|
|
if (window && window->GetDoc()) {
|
|
|
|
window->GetDoc()->WarnOnceAbout(nsIDocument::eLenientThis);
|
|
|
|
}
|
2013-05-08 12:50:58 -07:00
|
|
|
return true;
|
2013-04-08 14:04:21 -07:00
|
|
|
}
|
|
|
|
|
2013-05-20 10:47:08 -07: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 02:00:38 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "__DOM_IMPL__", &domImplVal)) {
|
2013-05-20 10:47:08 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!domImplVal.isObject()) {
|
2013-06-17 10:07:03 -07:00
|
|
|
ThrowErrorMessage(cx, MSG_NOT_OBJECT, "Value");
|
2013-05-20 10:47:08 -07: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-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(global.GetAsSupports()));
|
2013-05-20 10:47:08 -07:00
|
|
|
win.forget(window);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-31 23:56:23 -07:00
|
|
|
already_AddRefed<nsPIDOMWindow>
|
|
|
|
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
|
|
|
|
const GlobalObject& aGlobal,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Get the window to use as a parent and for initialization.
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-05-31 23:56:23 -07:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-27 21:03:03 -07: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-11 17:51:58 -08:00
|
|
|
// Make sure to divorce ourselves from the calling JS while creating and
|
2013-05-31 23:56:23 -07: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-11 17:51:58 -08:00
|
|
|
{
|
2014-04-14 20:27:00 -07:00
|
|
|
AutoNoJSAPI nojsapi;
|
2013-12-11 17:51:58 -08:00
|
|
|
|
2013-05-31 23:56:23 -07: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-27 21:03:03 -07:00
|
|
|
return;
|
2013-05-31 23:56:23 -07:00
|
|
|
}
|
|
|
|
// Initialize the object, if it implements nsIDOMGlobalPropertyInitializer.
|
|
|
|
nsCOMPtr<nsIDOMGlobalPropertyInitializer> gpi =
|
|
|
|
do_QueryInterface(implISupports);
|
|
|
|
if (gpi) {
|
|
|
|
JS::Rooted<JS::Value> initReturn(aCx);
|
2014-03-27 21:03:03 -07:00
|
|
|
nsresult rv = gpi->Init(aWindow, &initReturn);
|
2013-05-31 23:56:23 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-03-27 21:03:03 -07:00
|
|
|
return;
|
2013-05-31 23:56:23 -07:00
|
|
|
}
|
2013-09-26 16:46:42 -07: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-05-31 23:56:23 -07: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-27 21:03:03 -07:00
|
|
|
return;
|
2013-05-31 23:56:23 -07:00
|
|
|
}
|
|
|
|
aObject.set(implWrapped->GetJSObject());
|
|
|
|
if (!aObject) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 22:18:35 -07: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,
|
2014-07-03 04:05:03 -07:00
|
|
|
bool nullable, nsACString& result)
|
2013-06-12 22:18:35 -07:00
|
|
|
{
|
2013-09-30 07:29:10 -07:00
|
|
|
JS::Rooted<JSString*> s(cx);
|
2013-06-12 22:18:35 -07:00
|
|
|
if (v.isString()) {
|
|
|
|
s = v.toString();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (nullable && v.isNullOrUndefined()) {
|
|
|
|
result.SetIsVoid(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-16 04:31:36 -08:00
|
|
|
s = JS::ToString(cx, v);
|
2013-06-12 22:18:35 -07:00
|
|
|
if (!s) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 04:04:14 -07:00
|
|
|
// Conversion from Javascript string to ByteString is only valid if all
|
|
|
|
// characters < 256. This is always the case for Latin1 strings.
|
2013-06-12 22:18:35 -07:00
|
|
|
size_t length;
|
2014-07-03 04:05:03 -07:00
|
|
|
if (!js::StringHasLatin1Chars(s)) {
|
2014-07-02 04:04:14 -07:00
|
|
|
// ThrowErrorMessage can GC, so we first scan the string for bad chars
|
|
|
|
// and report the error outside the AutoCheckCannotGC scope.
|
|
|
|
bool foundBadChar = false;
|
|
|
|
size_t badCharIndex;
|
|
|
|
jschar badChar;
|
|
|
|
{
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
const jschar* chars = JS_GetTwoByteStringCharsAndLength(cx, nogc, s, &length);
|
|
|
|
if (!chars) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-12 22:18:35 -07:00
|
|
|
|
2014-07-02 04:04:14 -07:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
if (chars[i] > 255) {
|
|
|
|
badCharIndex = i;
|
|
|
|
badChar = chars[i];
|
|
|
|
foundBadChar = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundBadChar) {
|
|
|
|
MOZ_ASSERT(badCharIndex < length);
|
|
|
|
MOZ_ASSERT(badChar > 255);
|
2013-06-12 22:18:35 -07:00
|
|
|
// 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 10:59:53 -07:00
|
|
|
static_assert(sizeof(size_t) <= 8, "index array too small");
|
2014-07-02 04:04:14 -07:00
|
|
|
PR_snprintf(index, sizeof(index), "%d", badCharIndex);
|
2013-06-12 22:18:35 -07:00
|
|
|
// A jschar is 16 bits long. The biggest unsigned 16 bit
|
|
|
|
// number (65,535) has 5 digits, plus one more for the null
|
|
|
|
// terminator.
|
2014-07-02 04:04:14 -07:00
|
|
|
char badCharArray[6];
|
|
|
|
static_assert(sizeof(jschar) <= 2, "badCharArray too small");
|
|
|
|
PR_snprintf(badCharArray, sizeof(badCharArray), "%d", badChar);
|
|
|
|
ThrowErrorMessage(cx, MSG_INVALID_BYTESTRING, index, badCharArray);
|
2013-06-12 22:18:35 -07:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-02 04:04:14 -07:00
|
|
|
} else {
|
2014-07-03 04:05:03 -07:00
|
|
|
length = js::GetStringLength(s);
|
2013-06-12 22:18:35 -07:00
|
|
|
}
|
|
|
|
|
2014-07-03 01:03:56 -07:00
|
|
|
static_assert(js::MaxStringLength < UINT32_MAX,
|
|
|
|
"length+1 shouldn't overflow");
|
|
|
|
|
2013-06-12 22:18:35 -07:00
|
|
|
result.SetCapacity(length+1);
|
|
|
|
JS_EncodeStringToBuffer(cx, s, result.BeginWriting(), length);
|
|
|
|
result.BeginWriting()[length] = '\0';
|
|
|
|
result.SetLength(length);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-05 10:38:17 -08:00
|
|
|
bool
|
|
|
|
IsInPrivilegedApp(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
using mozilla::dom::workers::GetWorkerPrivateFromContext;
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return GetWorkerPrivateFromContext(aCx)->IsInPrivilegedApp();
|
|
|
|
}
|
|
|
|
|
2014-05-13 02:58:00 -07:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aObj);
|
2014-02-05 10:38:17 -08:00
|
|
|
uint16_t appStatus = principal->GetAppStatus();
|
|
|
|
return (appStatus == nsIPrincipal::APP_STATUS_CERTIFIED ||
|
2014-05-08 14:16:00 -07:00
|
|
|
appStatus == nsIPrincipal::APP_STATUS_PRIVILEGED) ||
|
|
|
|
Preferences::GetBool("dom.ignore_webidl_scope_checks", false);
|
2014-02-05 10:38:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsInCertifiedApp(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
using mozilla::dom::workers::GetWorkerPrivateFromContext;
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return GetWorkerPrivateFromContext(aCx)->IsInCertifiedApp();
|
|
|
|
}
|
|
|
|
|
2014-05-13 02:58:00 -07:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aObj);
|
2014-05-08 14:16:00 -07:00
|
|
|
return principal->GetAppStatus() == nsIPrincipal::APP_STATUS_CERTIFIED ||
|
|
|
|
Preferences::GetBool("dom.ignore_webidl_scope_checks", false);
|
2014-02-05 10:38:17 -08:00
|
|
|
}
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
#ifdef DEBUG
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
2014-04-08 11:48:37 -07:00
|
|
|
VerifyTraceProtoAndIfaceCacheCalled(JSTracer *trc, void **thingp,
|
|
|
|
JSGCTraceKind kind)
|
2013-11-05 06:16:26 -08:00
|
|
|
{
|
2014-04-08 11:48:37 -07:00
|
|
|
// We don't do anything here, we only want to verify that
|
|
|
|
// TraceProtoAndIfaceCache was called.
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
2014-04-08 11:48:37 -07:00
|
|
|
#endif
|
2013-11-05 06:16:26 -08:00
|
|
|
|
2013-11-18 22:05:42 -08:00
|
|
|
void
|
|
|
|
FinalizeGlobal(JSFreeOp* aFreeOp, JSObject* aObj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(aObj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
mozilla::dom::DestroyProtoAndIfaceCache(aObj);
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
bool
|
|
|
|
ResolveGlobal(JSContext* aCx, JS::Handle<JSObject*> aObj,
|
2014-04-25 14:11:02 -07:00
|
|
|
JS::Handle<jsid> aId, JS::MutableHandle<JSObject*> aObjp)
|
2013-11-05 06:16:26 -08:00
|
|
|
{
|
|
|
|
bool 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-05-25 11:31:07 -07:00
|
|
|
bool
|
|
|
|
CheckPermissions(JSContext* aCx, JSObject* aObj, const char* const aPermissions[])
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> rootedObj(aCx, aObj);
|
|
|
|
nsPIDOMWindow* window = xpc::WindowGlobalOrNull(rootedObj);
|
|
|
|
if (!window) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
|
|
|
|
NS_ENSURE_TRUE(permMgr, false);
|
|
|
|
|
|
|
|
do {
|
|
|
|
uint32_t permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
permMgr->TestPermissionFromWindow(window, *aPermissions, &permission);
|
|
|
|
if (permission == nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (*(++aPermissions));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-07 12:56:14 -08: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 10:26:23 -07:00
|
|
|
bool ok = getter(cx, obj, self, JSJitGetterCallArgs(args));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ok) {
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ok;
|
2014-02-07 12:56:14 -08: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 10:26:23 -07:00
|
|
|
args.rval().setUndefined();
|
|
|
|
#ifdef DEBUG
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
#endif
|
2014-02-07 12:56:14 -08: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 10:26:23 -07:00
|
|
|
bool ok = method(cx, obj, self, JSJitMethodCallArgs(args));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ok) {
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ok;
|
2014-02-07 12:56:14 -08:00
|
|
|
}
|
|
|
|
|
2014-03-21 09:18:24 -07: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 10:26:23 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
#endif
|
2014-03-21 09:18:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:26:23 -07:00
|
|
|
// Promise-returning methods always return objects
|
|
|
|
MOZ_ASSERT(info->returnType() == JSVAL_TYPE_OBJECT);
|
2014-03-21 09:18:24 -07: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 09:18:24 -07: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;
|
2014-06-16 09:52:00 -07:00
|
|
|
nsRefPtr<Promise> promise = Promise::Reject(global, exn, rv);
|
2014-03-21 09:18:24 -07:00
|
|
|
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-08 15:27:19 -07:00
|
|
|
return WrapNewBindingObject(cx, promise, rval);
|
2014-03-21 09:18:24 -07:00
|
|
|
}
|
|
|
|
|
2014-04-08 11:48:37 -07:00
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
CreateGlobalOptions<nsGlobalWindow>::TraceGlobal(JSTracer* aTrc, JSObject* aObj)
|
|
|
|
{
|
|
|
|
mozilla::dom::TraceProtoAndIfaceCache(aTrc, aObj);
|
2014-05-20 12:44:49 -07: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.
|
2014-07-03 09:40:52 -07:00
|
|
|
xpc::CompartmentPrivate* compartmentPrivate = xpc::CompartmentPrivate::Get(aObj);
|
2014-05-20 12:44:49 -07:00
|
|
|
if (compartmentPrivate && compartmentPrivate->scope) {
|
|
|
|
compartmentPrivate->scope->TraceSelf(aTrc);
|
|
|
|
}
|
2014-04-08 11:48:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
CreateGlobalOptions<nsGlobalWindow>::PostCreateGlobal(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGlobal)
|
|
|
|
{
|
2014-07-03 09:40:52 -07:00
|
|
|
// Invoking the XPCWrappedNativeScope constructor automatically hooks it
|
|
|
|
// up to the compartment of aGlobal.
|
|
|
|
(void) new XPCWrappedNativeScope(aCx, aGlobal);
|
|
|
|
return true;
|
2014-04-08 11:48:37 -07:00
|
|
|
}
|
|
|
|
|
2014-05-15 10:26:23 -07: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-30 21:42:20 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|