2012-03-31 08:42:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
#include <algorithm>
|
2012-07-18 14:36:08 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-05-02 22:38:19 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2012-12-15 03:58:45 +04:00
|
|
|
|
2012-05-03 08:35:38 +04:00
|
|
|
#include "BindingUtils.h"
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
#include "AccessCheck.h"
|
2012-09-26 18:17:46 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIXPConnect.h"
|
2012-05-22 17:46:20 +04:00
|
|
|
#include "WrapperFactory.h"
|
2012-05-18 12:29:40 +04:00
|
|
|
#include "xpcprivate.h"
|
|
|
|
#include "XPCQuickStubs.h"
|
2012-09-26 18:17:46 +04:00
|
|
|
#include "XrayWrapper.h"
|
2013-05-02 22:38:19 +04:00
|
|
|
#include "jsfriendapi.h"
|
2012-05-18 12:29:40 +04:00
|
|
|
|
2013-01-07 03:55:14 +04:00
|
|
|
#include "mozilla/dom/HTMLObjectElement.h"
|
|
|
|
#include "mozilla/dom/HTMLObjectElementBinding.h"
|
2013-03-17 18:42:05 +04:00
|
|
|
#include "mozilla/dom/HTMLSharedObjectElement.h"
|
|
|
|
#include "mozilla/dom/HTMLEmbedElementBinding.h"
|
|
|
|
#include "mozilla/dom/HTMLAppletElementBinding.h"
|
2013-01-07 03:55:14 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2012-07-18 14:36:08 +04:00
|
|
|
JSErrorFormatString ErrorFormatString[] = {
|
|
|
|
#define MSG_DEF(_name, _argc, _str) \
|
|
|
|
{ _str, _argc, JSEXN_TYPEERR },
|
|
|
|
#include "mozilla/dom/Errors.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
};
|
|
|
|
|
|
|
|
const JSErrorFormatString*
|
|
|
|
GetErrorMessage(void* aUserRef, const char* aLocale,
|
|
|
|
const unsigned aErrorNumber)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aErrorNumber < ArrayLength(ErrorFormatString));
|
|
|
|
return &ErrorFormatString[aErrorNumber];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThrowErrorMessage(JSContext* aCx, const ErrNum aErrorNumber, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, aErrorNumber);
|
|
|
|
JS_ReportErrorNumberVA(aCx, GetErrorMessage, NULL,
|
|
|
|
static_cast<const unsigned>(aErrorNumber), ap);
|
|
|
|
va_end(ap);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
} // namespace dom
|
|
|
|
|
|
|
|
struct ErrorResult::Message {
|
|
|
|
nsTArray<nsString> mArgs;
|
|
|
|
dom::ErrNum mErrorNumber;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ThrowTypeError(const dom::ErrNum errorNumber, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, errorNumber);
|
2013-02-19 20:54:40 +04:00
|
|
|
if (IsJSException()) {
|
|
|
|
// We have rooted our mJSException, and we don't have the info
|
|
|
|
// needed to unroot here, so just bail.
|
|
|
|
va_end(ap);
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"Ignoring ThrowTypeError call because we have a JS exception");
|
|
|
|
return;
|
|
|
|
}
|
2012-11-07 03:23:14 +04:00
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
}
|
|
|
|
mResult = NS_ERROR_TYPE_ERR;
|
|
|
|
Message* message = new Message();
|
|
|
|
message->mErrorNumber = errorNumber;
|
|
|
|
uint16_t argCount =
|
|
|
|
dom::GetErrorMessage(nullptr, nullptr, errorNumber)->argCount;
|
|
|
|
MOZ_ASSERT(argCount <= 10);
|
|
|
|
argCount = std::min<uint16_t>(argCount, 10);
|
|
|
|
while (argCount--) {
|
|
|
|
message->mArgs.AppendElement(*va_arg(ap, nsString*));
|
|
|
|
}
|
|
|
|
mMessage = message;
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ReportTypeError(JSContext* aCx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMessage, "ReportTypeError() can be called only once");
|
|
|
|
|
|
|
|
Message* message = mMessage;
|
|
|
|
const uint32_t argCount = message->mArgs.Length();
|
|
|
|
const jschar* args[11];
|
|
|
|
for (uint32_t i = 0; i < argCount; ++i) {
|
|
|
|
args[i] = message->mArgs.ElementAt(i).get();
|
|
|
|
}
|
|
|
|
args[argCount] = nullptr;
|
|
|
|
|
|
|
|
JS_ReportErrorNumberUCArray(aCx, dom::GetErrorMessage, nullptr,
|
|
|
|
static_cast<const unsigned>(message->mErrorNumber),
|
|
|
|
argCount > 0 ? args : nullptr);
|
|
|
|
|
2013-02-19 17:30:28 +04:00
|
|
|
ClearMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ClearMessage()
|
|
|
|
{
|
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
mMessage = nullptr;
|
|
|
|
}
|
2012-11-07 03:23:14 +04:00
|
|
|
}
|
|
|
|
|
2013-02-19 20:54:40 +04:00
|
|
|
void
|
2013-04-11 22:31:06 +04:00
|
|
|
ErrorResult::ThrowJSException(JSContext* cx, JS::Handle<JS::Value> exn)
|
2013-02-19 20:54:40 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to throw a JS exception?");
|
|
|
|
|
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
}
|
|
|
|
|
2013-04-09 01:05:22 +04:00
|
|
|
// Make sure mJSException is initialized _before_ we try to root it. But
|
|
|
|
// don't set it to exn yet, because we don't want to do that until after we
|
|
|
|
// root.
|
|
|
|
mJSException = JS::UndefinedValue();
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!JS_AddNamedValueRoot(cx, &mJSException, "ErrorResult::mJSException")) {
|
|
|
|
// 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?");
|
|
|
|
if (JS_WrapValue(cx, &mJSException)) {
|
|
|
|
JS_SetPendingException(cx, mJSException);
|
|
|
|
}
|
|
|
|
// If JS_WrapValue failed, not much we can do about it... No matter
|
|
|
|
// what, go ahead and unroot mJSException.
|
|
|
|
JS_RemoveValueRoot(cx, &mJSException);
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2012-05-09 14:00:48 +04:00
|
|
|
bool
|
2013-05-08 06:34:56 +04:00
|
|
|
DefineConstants(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const ConstantSpec* cs)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
for (; cs->name; ++cs) {
|
|
|
|
JSBool ok =
|
|
|
|
JS_DefineProperty(cx, obj, cs->name, cs->value, NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-23 20:44:48 +04:00
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const JSFunctionSpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return JS_DefineFunctions(cx, obj, spec);
|
|
|
|
}
|
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const JSPropertySpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return JS_DefineProperties(cx, obj, spec);
|
|
|
|
}
|
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const ConstantSpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return DefineConstants(cx, obj, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
DefinePrefable(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const Prefable<T>* props)
|
2012-05-23 20:44:48 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(props);
|
|
|
|
MOZ_ASSERT(props->specs);
|
|
|
|
do {
|
|
|
|
// Define if enabled
|
2013-02-19 20:54:40 +04:00
|
|
|
if (props->isEnabled(cx, obj)) {
|
2012-05-23 20:44:48 +04:00
|
|
|
if (!Define(cx, obj, props->specs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((++props)->specs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-25 00:10:49 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
DefineUnforgeableAttributes(JSContext* cx, JS::Handle<JSObject*> obj,
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSPropertySpec>* props)
|
2012-10-25 00:10:49 +04:00
|
|
|
{
|
|
|
|
return DefinePrefable(cx, obj, props);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-02 17:28:17 +04:00
|
|
|
// We should use JSFunction objects for interface objects, but we need a custom
|
|
|
|
// hasInstance hook because we have new interface objects on prototype chains of
|
|
|
|
// old (XPConnect-based) bindings. Because Function.prototype.toString throws if
|
|
|
|
// passed a non-Function object we also need to provide our own toString method
|
|
|
|
// for interface objects.
|
|
|
|
|
|
|
|
enum {
|
|
|
|
TOSTRING_CLASS_RESERVED_SLOT = 0,
|
|
|
|
TOSTRING_NAME_RESERVED_SLOT = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
InterfaceObjectToString(JSContext* cx, unsigned argc, JS::Value *vp)
|
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> callee(cx, JSVAL_TO_OBJECT(JS_CALLEE(cx, vp)));
|
2012-05-02 17:28:17 +04:00
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, JS_THIS_OBJECT(cx, vp));
|
2012-05-02 17:28:17 +04:00
|
|
|
if (!obj) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_CONVERT_TO,
|
|
|
|
"null", "object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-03 03:05:37 +04:00
|
|
|
JS::Value v = js::GetFunctionNativeReserved(callee,
|
|
|
|
TOSTRING_CLASS_RESERVED_SLOT);
|
2012-05-02 17:28:17 +04:00
|
|
|
JSClass* clasp = static_cast<JSClass*>(JSVAL_TO_PRIVATE(v));
|
|
|
|
|
|
|
|
v = js::GetFunctionNativeReserved(callee, TOSTRING_NAME_RESERVED_SLOT);
|
|
|
|
JSString* jsname = static_cast<JSString*>(JSVAL_TO_STRING(v));
|
|
|
|
size_t length;
|
|
|
|
const jschar* name = JS_GetInternedStringCharsAndLength(jsname, &length);
|
|
|
|
|
|
|
|
if (js::GetObjectJSClass(obj) != clasp) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_INCOMPATIBLE_PROTO,
|
|
|
|
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');
|
|
|
|
str.AppendLiteral("}");
|
|
|
|
|
|
|
|
return xpc::NonVoidStringToJsval(cx, str, vp);
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
JSBool
|
|
|
|
Constructor(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JSObject* callee = JSVAL_TO_OBJECT(JS_CALLEE(cx, vp));
|
|
|
|
const JS::Value& v =
|
|
|
|
js::GetFunctionNativeReserved(callee,
|
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT);
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* nativeHolder =
|
|
|
|
static_cast<const JSNativeHolder*>(v.toPrivate());
|
2012-10-09 22:50:27 +04:00
|
|
|
return (nativeHolder->mNative)(cx, argc, vp);
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:51:15 +04:00
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateConstructor(JSContext* cx, JS::Handle<JSObject*> global, const char* name,
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* nativeHolder, unsigned ctorNargs)
|
|
|
|
{
|
|
|
|
JSFunction* fun = js::NewFunctionWithReserved(cx, Constructor, ctorNargs,
|
|
|
|
JSFUN_CONSTRUCTOR, global,
|
|
|
|
name);
|
|
|
|
if (!fun) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* constructor = JS_GetFunctionObject(fun);
|
|
|
|
js::SetFunctionNativeReserved(constructor,
|
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT,
|
|
|
|
js::PrivateValue(const_cast<JSNativeHolder*>(nativeHolder)));
|
|
|
|
return constructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-05-02 07:44:12 +04:00
|
|
|
DefineConstructor(JSContext* cx, JS::Handle<JSObject*> global, const char* name,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> constructor)
|
2013-01-22 14:51:15 +04:00
|
|
|
{
|
|
|
|
JSBool alreadyDefined;
|
|
|
|
if (!JS_AlreadyHasOwnProperty(cx, global, name, &alreadyDefined)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is Enumerable: False per spec.
|
|
|
|
return alreadyDefined ||
|
|
|
|
JS_DefineProperty(cx, global, name, OBJECT_TO_JSVAL(constructor),
|
|
|
|
nullptr, nullptr, 0);
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfaceObject(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 22:38:19 +04:00
|
|
|
JS::Handle<JSObject*> constructorProto,
|
2012-10-09 22:50:27 +04:00
|
|
|
JSClass* constructorClass,
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* constructorNative,
|
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> proto,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2012-03-31 08:42:20 +04:00
|
|
|
const char* name)
|
|
|
|
{
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> constructor(cx);
|
2012-05-03 08:35:38 +04:00
|
|
|
if (constructorClass) {
|
2013-05-02 22:38:19 +04:00
|
|
|
MOZ_ASSERT(constructorProto);
|
2012-12-07 00:21:18 +04:00
|
|
|
constructor = JS_NewObject(cx, constructorClass, constructorProto, global);
|
2012-05-03 08:35:38 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(constructorNative);
|
2013-05-02 22:38:19 +04:00
|
|
|
MOZ_ASSERT(constructorProto == JS_GetFunctionPrototype(cx, global));
|
2013-01-22 14:51:15 +04:00
|
|
|
constructor = CreateConstructor(cx, global, name, constructorNative,
|
|
|
|
ctorNargs);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
if (!constructor) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-02 22:38:19 +04:00
|
|
|
if (constructorClass) {
|
2012-12-07 00:21:18 +04:00
|
|
|
// Have to shadow Function.prototype.toString, since that throws
|
|
|
|
// on things that are not js::FunctionClass.
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSFunction*> toString(cx,
|
|
|
|
js::DefineFunctionWithReserved(cx, constructor,
|
|
|
|
"toString",
|
|
|
|
InterfaceObjectToString,
|
|
|
|
0, 0));
|
2012-05-02 17:28:17 +04:00
|
|
|
if (!toString) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString *str = ::JS_InternString(cx, name);
|
|
|
|
if (!str) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-05-02 07:44:11 +04:00
|
|
|
JSObject* toStringObj = JS_GetFunctionObject(toString);
|
|
|
|
js::SetFunctionNativeReserved(toStringObj, TOSTRING_CLASS_RESERVED_SLOT,
|
|
|
|
PRIVATE_TO_JSVAL(constructorClass));
|
|
|
|
|
2012-05-02 17:28:17 +04:00
|
|
|
js::SetFunctionNativeReserved(toStringObj, TOSTRING_NAME_RESERVED_SLOT,
|
|
|
|
STRING_TO_JSVAL(str));
|
2012-12-14 23:10:49 +04:00
|
|
|
|
|
|
|
if (!JS_DefineProperty(cx, constructor, "length", JS::Int32Value(ctorNargs),
|
|
|
|
nullptr, nullptr, JSPROP_READONLY | JSPROP_PERMANENT)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-02 17:28:17 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties) {
|
|
|
|
if (properties->staticMethods &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->staticMethods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (properties->staticAttributes &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->staticAttributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties->constants &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->constants)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties) {
|
|
|
|
if (chromeOnlyProperties->staticMethods &&
|
|
|
|
!DefinePrefable(cx, constructor, chromeOnlyProperties->staticMethods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (chromeOnlyProperties->staticAttributes &&
|
|
|
|
!DefinePrefable(cx, constructor,
|
|
|
|
chromeOnlyProperties->staticAttributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (chromeOnlyProperties->constants &&
|
|
|
|
!DefinePrefable(cx, constructor, chromeOnlyProperties->constants)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (proto && !JS_LinkConstructorAndPrototype(cx, constructor, proto)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:51:15 +04:00
|
|
|
if (!DefineConstructor(cx, global, name, constructor)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (namedConstructors) {
|
|
|
|
int namedConstructorSlot = DOM_INTERFACE_SLOTS_BASE;
|
|
|
|
while (namedConstructors->mName) {
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> namedConstructor(cx,
|
|
|
|
CreateConstructor(cx, global, namedConstructors->mName,
|
|
|
|
&namedConstructors->mHolder,
|
|
|
|
namedConstructors->mNargs));
|
2013-01-22 14:51:15 +04:00
|
|
|
if (!namedConstructor ||
|
|
|
|
!JS_DefineProperty(cx, namedConstructor, "prototype",
|
|
|
|
JS::ObjectValue(*proto), JS_PropertyStub,
|
|
|
|
JS_StrictPropertyStub,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY) ||
|
|
|
|
!DefineConstructor(cx, global, namedConstructors->mName,
|
|
|
|
namedConstructor)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
js::SetReservedSlot(constructor, namedConstructorSlot++,
|
|
|
|
JS::ObjectValue(*namedConstructor));
|
|
|
|
++namedConstructors;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:09:41 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
DefineWebIDLBindingPropertiesOnXPCProto(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> proto,
|
|
|
|
const NativeProperties* properties)
|
2012-11-22 15:09:41 +04:00
|
|
|
{
|
|
|
|
if (properties->methods &&
|
|
|
|
!DefinePrefable(cx, proto, properties->methods)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties->attributes &&
|
|
|
|
!DefinePrefable(cx, proto, properties->attributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfacePrototypeObject(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> parentProto,
|
|
|
|
JSClass* protoClass,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> ourProto(cx,
|
|
|
|
JS_NewObjectWithUniqueType(cx, protoClass, parentProto, global));
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!ourProto) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties) {
|
|
|
|
if (properties->methods &&
|
|
|
|
!DefinePrefable(cx, ourProto, properties->methods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties->attributes &&
|
|
|
|
!DefinePrefable(cx, ourProto, properties->attributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties->constants &&
|
|
|
|
!DefinePrefable(cx, ourProto, properties->constants)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (chromeOnlyProperties) {
|
|
|
|
if (chromeOnlyProperties->methods &&
|
|
|
|
!DefinePrefable(cx, ourProto, chromeOnlyProperties->methods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties->attributes &&
|
|
|
|
!DefinePrefable(cx, ourProto, chromeOnlyProperties->attributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties->constants &&
|
|
|
|
!DefinePrefable(cx, ourProto, chromeOnlyProperties->constants)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ourProto;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
void
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfaceObjects(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> protoProto,
|
2012-10-09 22:50:27 +04:00
|
|
|
JSClass* protoClass, JSObject** protoCache,
|
2013-05-02 22:38:19 +04:00
|
|
|
JS::Handle<JSObject*> constructorProto,
|
2013-01-22 14:51:15 +04:00
|
|
|
JSClass* constructorClass, const JSNativeHolder* constructor,
|
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
|
|
|
JSObject** constructorCache, const DOMClass* domClass,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
|
|
|
const char* name)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-05-03 08:35:38 +04:00
|
|
|
MOZ_ASSERT(protoClass || constructorClass || constructor,
|
|
|
|
"Need at least one class or a constructor!");
|
2012-10-09 22:50:05 +04:00
|
|
|
MOZ_ASSERT(!((properties &&
|
|
|
|
(properties->methods || properties->attributes)) ||
|
|
|
|
(chromeOnlyProperties &&
|
|
|
|
(chromeOnlyProperties->methods ||
|
|
|
|
chromeOnlyProperties->attributes))) || protoClass,
|
2012-03-31 08:42:20 +04:00
|
|
|
"Methods or properties but no protoClass!");
|
2012-10-19 11:34:28 +04:00
|
|
|
MOZ_ASSERT(!((properties &&
|
|
|
|
(properties->staticMethods || properties->staticAttributes)) ||
|
|
|
|
(chromeOnlyProperties &&
|
|
|
|
(chromeOnlyProperties->staticMethods ||
|
|
|
|
chromeOnlyProperties->staticAttributes))) ||
|
2012-10-09 22:50:05 +04:00
|
|
|
constructorClass || constructor,
|
2012-05-03 08:35:38 +04:00
|
|
|
"Static methods but no constructorClass or constructor!");
|
|
|
|
MOZ_ASSERT(bool(name) == bool(constructorClass || constructor),
|
2012-03-31 08:42:20 +04:00
|
|
|
"Must have name precisely when we have an interface object");
|
2012-05-03 08:35:38 +04:00
|
|
|
MOZ_ASSERT(!constructorClass || !constructor);
|
2012-10-09 22:50:27 +04:00
|
|
|
MOZ_ASSERT(!protoClass == !protoCache,
|
|
|
|
"If, and only if, there is an interface prototype object we need "
|
|
|
|
"to cache it");
|
|
|
|
MOZ_ASSERT(!(constructorClass || constructor) == !constructorCache,
|
|
|
|
"If, and only if, there is an interface object we need to cache "
|
|
|
|
"it");
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (protoClass) {
|
2013-05-02 07:44:11 +04:00
|
|
|
proto =
|
|
|
|
CreateInterfacePrototypeObject(cx, global, protoProto, protoClass,
|
|
|
|
properties, chromeOnlyProperties);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!proto) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-08-08 09:26:18 +04:00
|
|
|
|
|
|
|
js::SetReservedSlot(proto, DOM_PROTO_INSTANCE_CLASS_SLOT,
|
2012-05-22 17:46:20 +04:00
|
|
|
JS::PrivateValue(const_cast<DOMClass*>(domClass)));
|
2012-10-09 22:50:27 +04:00
|
|
|
|
|
|
|
*protoCache = proto;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-05-02 07:44:11 +04:00
|
|
|
MOZ_ASSERT(!proto);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* interface;
|
2012-05-03 08:35:38 +04:00
|
|
|
if (constructorClass || constructor) {
|
2013-05-02 22:38:19 +04:00
|
|
|
interface = CreateInterfaceObject(cx, global, constructorProto,
|
|
|
|
constructorClass, constructor,
|
2013-01-22 14:51:15 +04:00
|
|
|
ctorNargs, namedConstructors, proto,
|
|
|
|
properties, chromeOnlyProperties, name);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!interface) {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (protoCache) {
|
|
|
|
// If we fail we need to make sure to clear the value of protoCache we
|
|
|
|
// set above.
|
|
|
|
*protoCache = nullptr;
|
|
|
|
}
|
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
*constructorCache = interface;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:20:04 +04:00
|
|
|
bool
|
|
|
|
NativeInterface2JSObjectAndThrowIfFailed(JSContext* aCx,
|
2013-04-30 01:33:42 +04:00
|
|
|
JS::Handle<JSObject*> aScope,
|
2012-03-31 08:42:20 +04:00
|
|
|
JS::Value* aRetval,
|
|
|
|
xpcObjectHelper& aHelper,
|
|
|
|
const nsIID* aIID,
|
|
|
|
bool aAllowNativeWrapper)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-11-27 13:20:04 +04:00
|
|
|
XPCLazyCallContext lccx(JS_CALLER, aCx, aScope);
|
|
|
|
if (!XPCConvert::NativeInterface2JSObject(lccx, aRetval, NULL, aHelper, aIID,
|
2012-03-31 08:42:20 +04:00
|
|
|
NULL, aAllowNativeWrapper, &rv)) {
|
|
|
|
// 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)) {
|
|
|
|
Throw<true>(aCx, NS_FAILED(rv) ? rv : NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-09 22:59:02 +04:00
|
|
|
bool
|
|
|
|
TryPreserveWrapper(JSObject* obj)
|
|
|
|
{
|
|
|
|
nsISupports* native;
|
|
|
|
if (UnwrapDOMObjectToISupports(obj, native)) {
|
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(native, &cache);
|
|
|
|
if (cache) {
|
|
|
|
nsContentUtils::PreserveWrapper(native, cache);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this DOMClass is not cycle collected, then it isn't wrappercached,
|
|
|
|
// so it does not need to be preserved. If it is cycle collected, then
|
|
|
|
// we can't tell if it is wrappercached or not, so we just return false.
|
|
|
|
const DOMClass* domClass = GetDOMClass(obj);
|
|
|
|
return domClass && !domClass->mParticipant;
|
|
|
|
}
|
|
|
|
|
2012-08-08 09:26:18 +04:00
|
|
|
// Can only be called with the immediate prototype of the instance object. Can
|
|
|
|
// only be called on the prototype of an object known to be a DOM instance.
|
|
|
|
JSBool
|
|
|
|
InstanceClassHasProtoAtDepth(JSHandleObject protoObject, uint32_t protoID,
|
|
|
|
uint32_t depth)
|
|
|
|
{
|
2012-05-22 17:46:20 +04:00
|
|
|
const DOMClass* domClass = static_cast<DOMClass*>(
|
2012-08-08 09:26:18 +04:00
|
|
|
js::GetReservedSlot(protoObject, DOM_PROTO_INSTANCE_CLASS_SLOT).toPrivate());
|
2012-08-08 09:57:07 +04:00
|
|
|
return (uint32_t)domClass->mInterfaceChain[depth] == protoID;
|
2012-08-08 09:26:18 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
// Only set allowNativeWrapper to false if you really know you need it, if in
|
|
|
|
// doubt use true. Setting it to false disables security wrappers.
|
|
|
|
bool
|
2013-04-30 01:33:42 +04:00
|
|
|
XPCOMObjectToJsval(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
xpcObjectHelper& helper, const nsIID* iid,
|
|
|
|
bool allowNativeWrapper, JS::Value* rval)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-11-27 13:20:04 +04:00
|
|
|
if (!NativeInterface2JSObjectAndThrowIfFailed(cx, scope, rval, helper, iid,
|
2012-03-31 08:42:20 +04:00
|
|
|
allowNativeWrapper)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
JSObject* jsobj = JSVAL_TO_OBJECT(*rval);
|
|
|
|
if (jsobj && !js::GetObjectParent(jsobj))
|
|
|
|
NS_ASSERTION(js::GetObjectClass(jsobj)->flags & JSCLASS_IS_GLOBAL,
|
|
|
|
"Why did we recreate this wrapper?");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-04 06:22:16 +04:00
|
|
|
bool
|
2013-04-30 01:33:42 +04:00
|
|
|
VariantToJsval(JSContext* aCx, JS::Handle<JSObject*> aScope,
|
|
|
|
nsIVariant* aVariant, JS::Value* aRetval)
|
2013-04-04 06:22:16 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
XPCLazyCallContext lccx(JS_CALLER, aCx, aScope);
|
|
|
|
if (!XPCVariant::VariantDataToJS(lccx, aVariant, &rv, aRetval)) {
|
|
|
|
// Does it throw? Who knows
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
|
|
|
Throw<true>(aCx, NS_FAILED(rv) ? rv : NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
JSBool
|
|
|
|
QueryInterface(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JS::Value> thisv(cx, JS_THIS(cx, vp));
|
2013-04-30 01:33:42 +04:00
|
|
|
if (thisv.isNull())
|
2012-03-31 08:42:20 +04:00
|
|
|
return false;
|
|
|
|
|
2012-06-12 17:44:21 +04:00
|
|
|
// Get the object. It might be a security wrapper, in which case we do a checked
|
|
|
|
// unwrap.
|
2013-04-30 01:33:42 +04:00
|
|
|
JS::Rooted<JSObject*> origObj(cx, &thisv.toObject());
|
2013-04-11 22:50:18 +04:00
|
|
|
JSObject* obj = js::CheckedUnwrap(origObj);
|
2012-11-14 21:56:26 +04:00
|
|
|
if (!obj) {
|
|
|
|
JS_ReportError(cx, "Permission denied to access object");
|
2012-06-12 17:44:21 +04:00
|
|
|
return false;
|
2012-11-14 21:56:26 +04:00
|
|
|
}
|
2012-06-12 17:44:21 +04:00
|
|
|
|
2012-05-22 17:46:20 +04:00
|
|
|
nsISupports* native;
|
|
|
|
if (!UnwrapDOMObjectToISupports(obj, native)) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return Throw<true>(cx, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
return Throw<true>(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value* argv = JS_ARGV(cx, vp);
|
|
|
|
if (!argv[0].isObject()) {
|
|
|
|
return Throw<true>(cx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:51:55 +04:00
|
|
|
nsIJSID* iid;
|
2013-05-30 07:15:31 +04:00
|
|
|
SelfRef iidRef;
|
2013-01-29 21:51:55 +04:00
|
|
|
if (NS_FAILED(xpc_qsUnwrapArg<nsIJSID>(cx, argv[0], &iid, &iidRef.ptr,
|
2012-03-31 08:42:20 +04:00
|
|
|
&argv[0]))) {
|
|
|
|
return Throw<true>(cx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(iid);
|
|
|
|
|
|
|
|
if (iid->GetID()->Equals(NS_GET_IID(nsIClassInfo))) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIClassInfo> ci = do_QueryInterface(native, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return Throw<true>(cx, rv);
|
|
|
|
}
|
|
|
|
|
2012-06-12 17:44:21 +04:00
|
|
|
return WrapObject(cx, origObj, ci, &NS_GET_IID(nsIClassInfo), vp);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-06-12 17:44:21 +04:00
|
|
|
|
2013-01-14 14:29:49 +04:00
|
|
|
nsCOMPtr<nsISupports> unused;
|
|
|
|
nsresult rv = native->QueryInterface(*iid->GetID(), getter_AddRefs(unused));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return Throw<true>(cx, rv);
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
*vp = thisv;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-03 08:35:38 +04:00
|
|
|
JSBool
|
|
|
|
ThrowingConstructor(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2012-08-04 11:44:00 +04:00
|
|
|
return ThrowErrorMessage(cx, MSG_ILLEGAL_CONSTRUCTOR);
|
2012-05-03 08:35:38 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
inline const NativePropertyHooks*
|
2013-05-04 03:29:08 +04:00
|
|
|
GetNativePropertyHooks(JSContext *cx, JS::Handle<JSObject*> obj,
|
|
|
|
DOMObjectType& type)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
2012-12-07 00:21:19 +04:00
|
|
|
const DOMClass* domClass = GetDOMClass(obj);
|
|
|
|
if (domClass) {
|
2012-10-09 22:50:27 +04:00
|
|
|
type = eInstance;
|
|
|
|
return domClass->mNativeHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JS_ObjectIsFunction(cx, obj)) {
|
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
type = eInterface;
|
|
|
|
const JS::Value& v =
|
|
|
|
js::GetFunctionNativeReserved(obj,
|
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT);
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* nativeHolder =
|
|
|
|
static_cast<const JSNativeHolder*>(v.toPrivate());
|
2012-10-09 22:50:27 +04:00
|
|
|
return nativeHolder->mPropertyHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(obj)));
|
|
|
|
const DOMIfaceAndProtoJSClass* ifaceAndProtoJSClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(js::GetObjectClass(obj));
|
|
|
|
type = ifaceAndProtoJSClass->mType;
|
|
|
|
return ifaceAndProtoJSClass->mNativeHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveOwnProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-01-04 01:31:36 +04:00
|
|
|
JSPropertyDescriptor* desc, unsigned flags)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks *nativePropertyHooks =
|
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
|
|
|
|
|
|
|
return type != eInstance || !nativePropertyHooks->mResolveOwnProperty ||
|
2013-01-04 01:31:36 +04:00
|
|
|
nativePropertyHooks->mResolveOwnProperty(cx, wrapper, obj, id, desc,
|
|
|
|
flags);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveAttribute(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSPropertySpec>* attributes, jsid* attributeIds,
|
|
|
|
const JSPropertySpec* attributeSpecs, JSPropertyDescriptor* desc)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
for (; attributes->specs; ++attributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (attributes->isEnabled(cx, obj)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = attributes->specs - attributeSpecs;
|
|
|
|
for ( ; attributeIds[i] != JSID_VOID; ++i) {
|
|
|
|
if (id == attributeIds[i]) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSPropertySpec& attrSpec = attributeSpecs[i];
|
2012-10-09 22:50:27 +04:00
|
|
|
// Because of centralization, we need to make sure we fault in the
|
|
|
|
// JitInfos as well. At present, until the JSAPI changes, the easiest
|
|
|
|
// way to do this is wrap them up as functions ourselves.
|
|
|
|
desc->attrs = attrSpec.flags & ~JSPROP_NATIVE_ACCESSORS;
|
|
|
|
// They all have getters, so we can just make it.
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> global(cx, JS_GetGlobalForObject(cx, wrapper));
|
|
|
|
JS::Rooted<JSFunction*> fun(cx,
|
|
|
|
JS_NewFunction(cx, (JSNative)attrSpec.getter.op,
|
|
|
|
0, 0, global, nullptr));
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
|
|
|
SET_JITINFO(fun, attrSpec.getter.info);
|
|
|
|
JSObject *funobj = JS_GetFunctionObject(fun);
|
|
|
|
desc->getter = js::CastAsJSPropertyOp(funobj);
|
|
|
|
desc->attrs |= JSPROP_GETTER;
|
|
|
|
if (attrSpec.setter.op) {
|
|
|
|
// We have a setter! Make it.
|
|
|
|
fun = JS_NewFunction(cx, (JSNative)attrSpec.setter.op, 1, 0,
|
|
|
|
global, nullptr);
|
|
|
|
if (!fun)
|
|
|
|
return false;
|
|
|
|
SET_JITINFO(fun, attrSpec.setter.info);
|
|
|
|
funobj = JS_GetFunctionObject(fun);
|
|
|
|
desc->setter = js::CastAsJSStrictPropertyOp(funobj);
|
|
|
|
desc->attrs |= JSPROP_SETTER;
|
|
|
|
} else {
|
|
|
|
desc->setter = nullptr;
|
|
|
|
}
|
|
|
|
desc->obj = wrapper;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2012-10-09 22:50:27 +04:00
|
|
|
JSPropertyDescriptor* desc, DOMObjectType type,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 15:52:07 +04:00
|
|
|
{
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* methods;
|
2012-10-09 22:50:27 +04:00
|
|
|
jsid* methodIds;
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSFunctionSpec* methodsSpecs;
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
methods = nativeProperties->staticMethods;
|
|
|
|
methodIds = nativeProperties->staticMethodIds;
|
|
|
|
methodsSpecs = nativeProperties->staticMethodsSpecs;
|
|
|
|
} else {
|
|
|
|
methods = nativeProperties->methods;
|
|
|
|
methodIds = nativeProperties->methodIds;
|
|
|
|
methodsSpecs = nativeProperties->methodsSpecs;
|
|
|
|
}
|
|
|
|
if (methods) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* method;
|
2012-10-09 22:50:27 +04:00
|
|
|
for (method = methods; method->specs; ++method) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (method->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2012-10-09 22:50:27 +04:00
|
|
|
size_t i = method->specs - methodsSpecs;
|
|
|
|
for ( ; methodIds[i] != JSID_VOID; ++i) {
|
|
|
|
if (id == methodIds[i]) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSFunctionSpec& methodSpec = methodsSpecs[i];
|
2012-10-09 22:50:05 +04:00
|
|
|
JSFunction *fun = JS_NewFunctionById(cx, methodSpec.call.op,
|
|
|
|
methodSpec.nargs, 0,
|
|
|
|
wrapper, id);
|
|
|
|
if (!fun) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SET_JITINFO(fun, methodSpec.call.info);
|
|
|
|
JSObject *funobj = JS_GetFunctionObject(fun);
|
|
|
|
desc->value.setObject(*funobj);
|
|
|
|
desc->attrs = methodSpec.flags;
|
|
|
|
desc->obj = wrapper;
|
|
|
|
desc->setter = nullptr;
|
|
|
|
desc->getter = nullptr;
|
|
|
|
return true;
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
if (nativeProperties->staticAttributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-19 11:34:28 +04:00
|
|
|
nativeProperties->staticAttributes,
|
|
|
|
nativeProperties->staticAttributeIds,
|
|
|
|
nativeProperties->staticAttributeSpecs, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc->obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (nativeProperties->attributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties->attributes,
|
|
|
|
nativeProperties->attributeIds,
|
|
|
|
nativeProperties->attributeSpecs, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc->obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nativeProperties->unforgeableAttributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties->unforgeableAttributes,
|
|
|
|
nativeProperties->unforgeableAttributeIds,
|
|
|
|
nativeProperties->unforgeableAttributeSpecs,
|
|
|
|
desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc->obj) {
|
|
|
|
return true;
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (nativeProperties->constants) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2012-10-09 22:50:05 +04:00
|
|
|
for (constant = nativeProperties->constants; constant->specs; ++constant) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (constant->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = constant->specs - nativeProperties->constantSpecs;
|
|
|
|
for ( ; nativeProperties->constantIds[i] != JSID_VOID; ++i) {
|
|
|
|
if (id == nativeProperties->constantIds[i]) {
|
|
|
|
desc->attrs = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT;
|
|
|
|
desc->obj = wrapper;
|
|
|
|
desc->value = nativeProperties->constantSpecs[i].value;
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
ResolvePrototypeOrConstructor(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2012-10-30 00:55:13 +04:00
|
|
|
size_t protoAndIfaceArrayIndex, unsigned attrs,
|
2012-10-09 22:50:27 +04:00
|
|
|
JSPropertyDescriptor* desc)
|
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> global(cx, js::GetGlobalForObjectCrossCompartment(obj));
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
JSObject** protoAndIfaceArray = GetProtoAndIfaceArray(global);
|
|
|
|
JSObject* protoOrIface = protoAndIfaceArray[protoAndIfaceArrayIndex];
|
|
|
|
if (!protoOrIface) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
desc->obj = wrapper;
|
|
|
|
desc->shortid = 0;
|
2012-10-30 00:55:13 +04:00
|
|
|
desc->attrs = attrs;
|
2012-10-09 22:50:27 +04:00
|
|
|
desc->getter = JS_PropertyStub;
|
|
|
|
desc->setter = JS_StrictPropertyStub;
|
|
|
|
desc->value = JS::ObjectValue(*protoOrIface);
|
|
|
|
}
|
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2012-10-09 22:50:27 +04:00
|
|
|
const NativePropertyHooks* nativePropertyHooks,
|
2013-05-04 03:29:08 +04:00
|
|
|
DOMObjectType type, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id,
|
2012-10-09 22:50:27 +04:00
|
|
|
JSPropertyDescriptor* desc)
|
2012-10-09 22:50:05 +04:00
|
|
|
{
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface && IdEquals(id, "prototype")) {
|
|
|
|
return nativePropertyHooks->mPrototypeID == prototypes::id::_ID_Count ||
|
|
|
|
ResolvePrototypeOrConstructor(cx, wrapper, obj,
|
|
|
|
nativePropertyHooks->mPrototypeID,
|
2012-10-30 00:55:13 +04:00
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY,
|
2012-10-09 22:50:27 +04:00
|
|
|
desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eInterfacePrototype && IdEquals(id, "constructor")) {
|
|
|
|
return nativePropertyHooks->mConstructorID == constructors::id::_ID_Count ||
|
|
|
|
ResolvePrototypeOrConstructor(cx, wrapper, obj,
|
|
|
|
nativePropertyHooks->mConstructorID,
|
2012-10-30 00:55:13 +04:00
|
|
|
0, desc);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
|
|
|
|
if (nativeProperties.regular &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayResolveProperty(cx, wrapper, obj, id, desc, type,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.regular)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!desc->obj &&
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.chromeOnly &&
|
2012-10-09 22:50:05 +04:00
|
|
|
xpc::AccessCheck::isChrome(js::GetObjectCompartment(wrapper)) &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayResolveProperty(cx, wrapper, obj, id, desc, type,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.chromeOnly)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-18 15:52:07 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveNativeProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id, JSPropertyDescriptor* desc)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks* nativePropertyHooks =
|
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
|
|
|
|
|
|
|
if (type == eInstance) {
|
|
|
|
// Force the type to be eInterfacePrototype, since we need to walk the
|
|
|
|
// prototype chain.
|
|
|
|
type = eInterfacePrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eInterfacePrototype) {
|
|
|
|
do {
|
|
|
|
if (!XrayResolveNativeProperty(cx, wrapper, nativePropertyHooks, type,
|
|
|
|
obj, id, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while ((nativePropertyHooks = nativePropertyHooks->mProtoHooks));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XrayResolveNativeProperty(cx, wrapper, nativePropertyHooks, type, obj,
|
|
|
|
id, desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayEnumerateAttributes(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSPropertySpec>* attributes,
|
|
|
|
jsid* attributeIds, const JSPropertySpec* attributeSpecs,
|
2012-11-05 20:58:03 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
for (; attributes->specs; ++attributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (attributes->isEnabled(cx, obj)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = attributes->specs - attributeSpecs;
|
|
|
|
for ( ; attributeIds[i] != JSID_VOID; ++i) {
|
2012-11-05 20:58:03 +04:00
|
|
|
if (((flags & JSITER_HIDDEN) ||
|
|
|
|
(attributeSpecs[i].flags & JSPROP_ENUMERATE)) &&
|
2012-10-09 22:50:27 +04:00
|
|
|
!props.append(attributeIds[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayEnumerateProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2013-02-19 20:54:40 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props,
|
2012-11-05 20:58:03 +04:00
|
|
|
DOMObjectType type,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 15:52:07 +04:00
|
|
|
{
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* methods;
|
2012-10-09 22:50:27 +04:00
|
|
|
jsid* methodIds;
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSFunctionSpec* methodsSpecs;
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
methods = nativeProperties->staticMethods;
|
|
|
|
methodIds = nativeProperties->staticMethodIds;
|
|
|
|
methodsSpecs = nativeProperties->staticMethodsSpecs;
|
|
|
|
} else {
|
|
|
|
methods = nativeProperties->methods;
|
|
|
|
methodIds = nativeProperties->methodIds;
|
|
|
|
methodsSpecs = nativeProperties->methodsSpecs;
|
|
|
|
}
|
|
|
|
if (methods) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* method;
|
2012-10-09 22:50:27 +04:00
|
|
|
for (method = methods; method->specs; ++method) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (method->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2012-10-09 22:50:27 +04:00
|
|
|
size_t i = method->specs - methodsSpecs;
|
|
|
|
for ( ; methodIds[i] != JSID_VOID; ++i) {
|
2012-11-05 20:58:03 +04:00
|
|
|
if (((flags & JSITER_HIDDEN) ||
|
|
|
|
(methodsSpecs[i].flags & JSPROP_ENUMERATE)) &&
|
2012-10-09 22:50:27 +04:00
|
|
|
!props.append(methodIds[i])) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
if (nativeProperties->staticAttributes &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->staticAttributes,
|
2012-10-19 11:34:28 +04:00
|
|
|
nativeProperties->staticAttributeIds,
|
|
|
|
nativeProperties->staticAttributeSpecs,
|
2012-11-05 20:58:03 +04:00
|
|
|
flags, props)) {
|
2012-10-19 11:34:28 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (nativeProperties->attributes &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->attributes,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties->attributeIds,
|
2012-11-05 20:58:03 +04:00
|
|
|
nativeProperties->attributeSpecs,
|
|
|
|
flags, props)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nativeProperties->unforgeableAttributes &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateAttributes(cx, wrapper, obj,
|
|
|
|
nativeProperties->unforgeableAttributes,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties->unforgeableAttributeIds,
|
|
|
|
nativeProperties->unforgeableAttributeSpecs,
|
2012-11-05 20:58:03 +04:00
|
|
|
flags, props)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (nativeProperties->constants) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2012-10-09 22:50:05 +04:00
|
|
|
for (constant = nativeProperties->constants; constant->specs; ++constant) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (constant->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = constant->specs - nativeProperties->constantSpecs;
|
|
|
|
for ( ; nativeProperties->constantIds[i] != JSID_VOID; ++i) {
|
|
|
|
if (!props.append(nativeProperties->constantIds[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayEnumerateNativeProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
2012-10-09 22:50:27 +04:00
|
|
|
const NativePropertyHooks* nativePropertyHooks,
|
2013-05-04 03:29:08 +04:00
|
|
|
DOMObjectType type, JS::Handle<JSObject*> obj,
|
2012-11-05 20:58:03 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:05 +04:00
|
|
|
{
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface &&
|
|
|
|
nativePropertyHooks->mPrototypeID != prototypes::id::_ID_Count &&
|
|
|
|
!AddStringToIDVector(cx, props, "prototype")) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterfacePrototype &&
|
|
|
|
nativePropertyHooks->mConstructorID != constructors::id::_ID_Count &&
|
2012-11-05 20:58:03 +04:00
|
|
|
(flags & JSITER_HIDDEN) &&
|
2012-10-09 22:50:27 +04:00
|
|
|
!AddStringToIDVector(cx, props, "constructor")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
|
|
|
|
if (nativeProperties.regular &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateProperties(cx, wrapper, obj, flags, props, type,
|
|
|
|
nativeProperties.regular)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeProperties.chromeOnly &&
|
2012-10-09 22:50:05 +04:00
|
|
|
xpc::AccessCheck::isChrome(js::GetObjectCompartment(wrapper)) &&
|
2013-02-19 20:54:40 +04:00
|
|
|
!XrayEnumerateProperties(cx, wrapper, obj, flags, props, type,
|
|
|
|
nativeProperties.chromeOnly)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayEnumerateProperties(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2012-11-05 20:58:03 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks* nativePropertyHooks =
|
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
|
|
|
|
|
|
|
if (type == eInstance) {
|
|
|
|
if (nativePropertyHooks->mEnumerateOwnProperties &&
|
|
|
|
!nativePropertyHooks->mEnumerateOwnProperties(cx, wrapper, obj,
|
|
|
|
props)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-05 20:58:03 +04:00
|
|
|
if (flags & JSITER_OWNONLY) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the type to be eInterfacePrototype, since we need to walk the
|
|
|
|
// prototype chain.
|
|
|
|
type = eInterfacePrototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eInterfacePrototype) {
|
|
|
|
do {
|
|
|
|
if (!XrayEnumerateNativeProperties(cx, wrapper, nativePropertyHooks, type,
|
2012-11-05 20:58:03 +04:00
|
|
|
obj, flags, props)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-05 20:58:03 +04:00
|
|
|
|
|
|
|
if (flags & JSITER_OWNONLY) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
} while ((nativePropertyHooks = nativePropertyHooks->mProtoHooks));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XrayEnumerateNativeProperties(cx, wrapper, nativePropertyHooks, type,
|
2012-11-05 20:58:03 +04:00
|
|
|
obj, flags, props);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NativePropertyHooks sWorkerNativePropertyHooks = {
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
|
|
|
},
|
|
|
|
prototypes::id::_ID_Count,
|
|
|
|
constructors::id::_ID_Count,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2012-05-22 17:46:20 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
GetPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id, bool* found,
|
2012-05-22 17:46:20 +04:00
|
|
|
JS::Value* vp)
|
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-05-05 11:03:14 +04:00
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
2012-09-04 03:42:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 17:46:20 +04:00
|
|
|
if (!proto) {
|
|
|
|
*found = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool hasProp;
|
|
|
|
if (!JS_HasPropertyById(cx, proto, id, &hasProp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*found = hasProp;
|
|
|
|
if (!hasProp || !vp) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_ForwardGetPropertyTo(cx, proto, id, proxy, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
HasPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
DOMProxyHandler* handler,
|
|
|
|
JS::Handle<jsid> id)
|
2012-05-22 17:46:20 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, proxy);
|
2012-08-22 05:42:53 +04:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-05-04 03:29:08 +04:00
|
|
|
if (xpc::WrapperFactory::IsXrayWrapper(obj)) {
|
|
|
|
obj = js::UncheckedUnwrap(obj);
|
|
|
|
ac.construct(cx, obj);
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
2013-05-04 03:29:08 +04:00
|
|
|
MOZ_ASSERT(js::IsProxy(obj) && js::GetProxyHandler(obj) == handler);
|
2012-05-22 17:46:20 +04:00
|
|
|
|
|
|
|
bool found;
|
|
|
|
// We ignore an error from GetPropertyOnPrototype.
|
2013-05-04 03:29:08 +04:00
|
|
|
return !GetPropertyOnPrototype(cx, obj, id, &found, NULL) || found;
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-05 20:59:23 +04:00
|
|
|
JSObject*
|
|
|
|
GetXrayExpandoChain(JSObject* obj)
|
|
|
|
{
|
2012-10-09 22:50:27 +04:00
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
JS::Value v;
|
|
|
|
if (IsDOMClass(clasp) || IsDOMIfaceAndProtoClass(clasp)) {
|
|
|
|
v = js::GetReservedSlot(obj, DOM_XRAY_EXPANDO_SLOT);
|
|
|
|
} else if (js::IsObjectProxyClass(clasp) || js::IsFunctionProxyClass(clasp)) {
|
|
|
|
MOZ_ASSERT(js::GetProxyHandler(obj)->family() == ProxyFamily());
|
|
|
|
v = js::GetProxyExtra(obj, JSPROXYSLOT_XRAY_EXPANDO);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
v = js::GetFunctionNativeReserved(obj, CONSTRUCTOR_XRAY_EXPANDO_SLOT);
|
|
|
|
}
|
2012-10-05 20:59:23 +04:00
|
|
|
return v.isUndefined() ? nullptr : &v.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetXrayExpandoChain(JSObject* obj, JSObject* chain)
|
|
|
|
{
|
|
|
|
JS::Value v = chain ? JS::ObjectValue(*chain) : JSVAL_VOID;
|
2012-10-09 22:50:27 +04:00
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
if (IsDOMClass(clasp) || IsDOMIfaceAndProtoClass(clasp)) {
|
|
|
|
js::SetReservedSlot(obj, DOM_XRAY_EXPANDO_SLOT, v);
|
|
|
|
} else if (js::IsObjectProxyClass(clasp) || js::IsFunctionProxyClass(clasp)) {
|
|
|
|
MOZ_ASSERT(js::GetProxyHandler(obj)->family() == ProxyFamily());
|
2012-10-05 20:59:23 +04:00
|
|
|
js::SetProxyExtra(obj, JSPROXYSLOT_XRAY_EXPANDO, v);
|
|
|
|
} else {
|
2012-10-09 22:50:27 +04:00
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
js::SetFunctionNativeReserved(obj, CONSTRUCTOR_XRAY_EXPANDO_SLOT, v);
|
2012-10-05 20:59:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 01:18:03 +04:00
|
|
|
bool
|
|
|
|
MainThreadDictionaryBase::ParseJSON(JSContext *aCx,
|
|
|
|
const nsAString& aJSON,
|
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2012-10-16 18:52:10 +04:00
|
|
|
{
|
2012-10-16 19:37:44 +04:00
|
|
|
if (aJSON.IsEmpty()) {
|
2013-05-08 01:18:03 +04:00
|
|
|
return true;
|
2012-10-16 18:52:10 +04:00
|
|
|
}
|
2013-05-08 01:18:03 +04:00
|
|
|
return JS_ParseJSON(aCx,
|
|
|
|
static_cast<const jschar*>(PromiseFlatString(aJSON).get()),
|
2013-05-29 12:29:39 +04:00
|
|
|
aJSON.Length(), aVal);
|
2012-10-16 18:52:10 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
static JSString*
|
|
|
|
ConcatJSString(JSContext* cx, const char* pre, JSString* str, const char* post)
|
|
|
|
{
|
|
|
|
if (!str) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSString*> preString(cx, JS_NewStringCopyN(cx, pre, strlen(pre)));
|
|
|
|
JS::Rooted<JSString*> postString(cx, JS_NewStringCopyN(cx, post, strlen(post)));
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!preString || !postString) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = JS_ConcatStrings(cx, preString, str);
|
|
|
|
if (!str) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_ConcatStrings(cx, str, postString);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
NativeToString(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, const char* pre,
|
2012-10-09 22:50:27 +04:00
|
|
|
const char* post, JS::Value* v)
|
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSPropertyDescriptor> toStringDesc(cx);
|
|
|
|
toStringDesc.object().set(nullptr);
|
|
|
|
toStringDesc.setAttributes(0);
|
|
|
|
toStringDesc.setShortId(0);
|
|
|
|
toStringDesc.setGetter(nullptr);
|
|
|
|
toStringDesc.setSetter(nullptr);
|
|
|
|
toStringDesc.value().set(JS::UndefinedValue());
|
|
|
|
JS::Rooted<jsid> id(cx,
|
|
|
|
nsXPConnect::GetRuntimeInstance()->GetStringID(XPCJSRuntime::IDX_TO_STRING));
|
|
|
|
if (!XrayResolveNativeProperty(cx, wrapper, obj, id,
|
|
|
|
toStringDesc.address())) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSString*> str(cx);
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
2013-05-04 03:29:08 +04:00
|
|
|
if (toStringDesc.object()) {
|
|
|
|
JS::Rooted<JS::Value> toString(cx, toStringDesc.value());
|
|
|
|
if (!JS_WrapValue(cx, toString.address())) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(JS_ObjectIsCallable(cx, &toString.toObject()));
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JS::Value> toStringResult(cx);
|
2012-10-09 22:50:27 +04:00
|
|
|
if (JS_CallFunctionValue(cx, obj, toString, 0, nullptr,
|
2013-05-04 03:29:08 +04:00
|
|
|
toStringResult.address())) {
|
2012-10-09 22:50:27 +04:00
|
|
|
str = toStringResult.toString();
|
|
|
|
} else {
|
|
|
|
str = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IsDOMProxy(obj)) {
|
2013-05-01 01:44:50 +04:00
|
|
|
str = JS_BasicObjectToString(cx, obj);
|
2012-11-05 20:58:03 +04:00
|
|
|
} else {
|
2013-02-01 18:33:58 +04:00
|
|
|
js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
if (IsDOMClass(clasp)) {
|
|
|
|
str = ConcatJSString(cx, "[object ",
|
|
|
|
JS_NewStringCopyZ(cx, clasp->name), "]");
|
|
|
|
} else if (IsDOMIfaceAndProtoClass(clasp)) {
|
|
|
|
const DOMIfaceAndProtoJSClass* ifaceAndProtoJSClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(clasp);
|
|
|
|
str = JS_NewStringCopyZ(cx, ifaceAndProtoJSClass->mToString);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
str = JS_DecompileFunction(cx, JS_GetObjectFunction(obj), 0);
|
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
str = ConcatJSString(cx, pre, str, post);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->setString(str);
|
|
|
|
return JS_WrapValue(cx, v);
|
|
|
|
}
|
|
|
|
|
2012-09-26 18:17:46 +04:00
|
|
|
// Dynamically ensure that two objects don't end up with the same reserved slot.
|
2013-04-12 07:20:18 +04:00
|
|
|
class MOZ_STACK_CLASS AutoCloneDOMObjectSlotGuard
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-05-04 03:29:08 +04:00
|
|
|
AutoCloneDOMObjectSlotGuard(JSContext* aCx, JSObject* aOld, JSObject* aNew)
|
|
|
|
: mOldReflector(aCx, aOld), mNewReflector(aCx, aNew)
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetReservedSlot(aOld, DOM_OBJECT_SLOT) ==
|
|
|
|
js::GetReservedSlot(aNew, DOM_OBJECT_SLOT));
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoCloneDOMObjectSlotGuard()
|
|
|
|
{
|
|
|
|
if (js::GetReservedSlot(mOldReflector, DOM_OBJECT_SLOT).toPrivate()) {
|
|
|
|
js::SetReservedSlot(mNewReflector, DOM_OBJECT_SLOT,
|
|
|
|
JS::PrivateValue(nullptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> mOldReflector;
|
|
|
|
JS::Rooted<JSObject*> mNewReflector;
|
2012-09-26 18:17:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
2013-04-15 21:32:55 +04:00
|
|
|
ReparentWrapper(JSContext* aCx, JS::HandleObject aObjArg)
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
2013-04-15 21:32:55 +04:00
|
|
|
// aObj is assigned to below, so needs to be re-rooted.
|
|
|
|
JS::RootedObject aObj(aCx, aObjArg);
|
2012-09-26 18:17:46 +04:00
|
|
|
const DOMClass* domClass = GetDOMClass(aObj);
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> oldParent(aCx, JS_GetParent(aObj));
|
|
|
|
JS::Rooted<JSObject*> newParent(aCx, domClass->mGetParent(aCx, aObj));
|
2012-09-26 18:17:46 +04:00
|
|
|
|
|
|
|
JSAutoCompartment oldAc(aCx, oldParent);
|
|
|
|
|
|
|
|
if (js::GetObjectCompartment(oldParent) ==
|
|
|
|
js::GetObjectCompartment(newParent)) {
|
|
|
|
if (!JS_SetParent(aCx, aObj, newParent)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports* native;
|
|
|
|
if (!UnwrapDOMObjectToISupports(aObj, native)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before proceeding, eagerly create any same-compartment security wrappers
|
|
|
|
// that the object might have. This forces us to take the 'WithWrapper' path
|
|
|
|
// while transplanting that handles this stuff correctly.
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> ww(aCx,
|
|
|
|
xpc::WrapperFactory::WrapForSameCompartment(aCx, aObj));
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!ww) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-04-16 21:02:57 +04:00
|
|
|
bool isProxy = js::IsProxy(aObj);
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> expandoObject(aCx);
|
2013-04-16 21:02:57 +04:00
|
|
|
if (isProxy) {
|
|
|
|
expandoObject = DOMProxyHandler::GetAndClearExpandoObject(aObj);
|
|
|
|
}
|
|
|
|
|
2012-09-26 18:17:46 +04:00
|
|
|
JSAutoCompartment newAc(aCx, newParent);
|
|
|
|
|
|
|
|
// First we clone the reflector. We get a copy of its properties and clone its
|
|
|
|
// expando chain. The only part that is dangerous here is that if we have to
|
|
|
|
// return early we must avoid ending up with two reflectors pointing to the
|
|
|
|
// same native. Other than that, the objects we create will just go away.
|
|
|
|
|
2013-05-02 07:44:12 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx,
|
|
|
|
js::GetGlobalForObjectCrossCompartment(newParent));
|
|
|
|
JS::Handle<JSObject*> proto = (domClass->mGetProto)(aCx, global);
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!proto) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> newobj(aCx, JS_CloneObject(aCx, aObj, proto, newParent));
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!newobj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
js::SetReservedSlot(newobj, DOM_OBJECT_SLOT,
|
|
|
|
js::GetReservedSlot(aObj, DOM_OBJECT_SLOT));
|
|
|
|
|
|
|
|
// At this point, both |aObj| and |newobj| point to the same native
|
|
|
|
// which is bad, because one of them will end up being finalized with a
|
|
|
|
// native it does not own. |cloneGuard| ensures that if we exit before
|
|
|
|
// clearing |aObj|'s reserved slot the reserved slot of |newobj| will be
|
|
|
|
// set to null. |aObj| will go away soon, because we swap it with
|
|
|
|
// another object during the transplant and let that object die.
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> propertyHolder(aCx);
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
AutoCloneDOMObjectSlotGuard cloneGuard(aCx, aObj, newobj);
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> copyFrom(aCx, isProxy ? expandoObject : aObj);
|
2013-04-16 21:02:57 +04:00
|
|
|
if (copyFrom) {
|
|
|
|
propertyHolder = JS_NewObjectWithGivenProto(aCx, nullptr, nullptr,
|
|
|
|
newParent);
|
|
|
|
if (!propertyHolder) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2013-04-16 21:02:57 +04:00
|
|
|
if (!JS_CopyPropertiesFrom(aCx, propertyHolder, copyFrom)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
propertyHolder = nullptr;
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expandos from other compartments are attached to the target JS object.
|
|
|
|
// Copy them over, and let the old ones die a natural death.
|
|
|
|
SetXrayExpandoChain(newobj, nullptr);
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(native, &cache);
|
|
|
|
if (ww != aObj) {
|
|
|
|
MOZ_ASSERT(cache->HasSystemOnlyWrapper());
|
|
|
|
|
|
|
|
JSObject *newwrapper =
|
|
|
|
xpc::WrapperFactory::WrapSOWObject(aCx, newobj);
|
|
|
|
if (!newwrapper) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, now we do the special object-plus-wrapper transplant.
|
|
|
|
ww = xpc::TransplantObjectWithWrapper(aCx, aObj, ww, newobj, newwrapper);
|
|
|
|
if (!ww) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
aObj = newobj;
|
|
|
|
SetSystemOnlyWrapperSlot(aObj, JS::ObjectValue(*ww));
|
|
|
|
} else {
|
|
|
|
aObj = xpc::TransplantObject(aCx, aObj, newobj);
|
|
|
|
if (!aObj) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool preserving = cache->PreservingWrapper();
|
|
|
|
cache->SetPreservingWrapper(false);
|
|
|
|
cache->SetWrapper(aObj);
|
|
|
|
cache->SetPreservingWrapper(preserving);
|
2013-04-16 21:02:57 +04:00
|
|
|
|
|
|
|
if (propertyHolder) {
|
|
|
|
JSObject* copyTo;
|
|
|
|
if (isProxy) {
|
|
|
|
copyTo = DOMProxyHandler::EnsureExpandoObject(aCx, aObj);
|
|
|
|
} else {
|
|
|
|
copyTo = aObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!copyTo || !JS_CopyPropertiesFrom(aCx, copyTo, propertyHolder)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
2013-03-17 18:42:05 +04:00
|
|
|
nsObjectLoadingContent* htmlobject;
|
2013-01-07 03:55:14 +04:00
|
|
|
nsresult rv = UnwrapObject<HTMLObjectElement>(aCx, aObj, htmlobject);
|
2013-03-17 18:42:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = UnwrapObject<prototypes::id::HTMLEmbedElement,
|
|
|
|
HTMLSharedObjectElement>(aCx, aObj, htmlobject);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = UnwrapObject<prototypes::id::HTMLAppletElement,
|
|
|
|
HTMLSharedObjectElement>(aCx, aObj, htmlobject);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
htmlobject = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (htmlobject) {
|
2013-01-07 03:55:14 +04:00
|
|
|
htmlobject->SetupProtoChain(aCx, aObj);
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
|
|
|
|
// Now we can just fix up the parent and return the wrapper
|
|
|
|
|
|
|
|
if (newParent && !JS_SetParent(aCx, aObj, newParent)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:07:49 +04:00
|
|
|
template<bool mainThread>
|
|
|
|
inline JSObject*
|
|
|
|
GetGlobalObject(JSContext* aCx, JSObject* aObject,
|
|
|
|
Maybe<JSAutoCompartment>& aAutoCompartment)
|
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, aObject);
|
|
|
|
if (js::IsWrapper(obj)) {
|
|
|
|
obj = js::CheckedUnwrap(obj, /* stopAtOuter = */ false);
|
|
|
|
if (!obj) {
|
2012-12-03 20:07:49 +04:00
|
|
|
Throw<mainThread>(aCx, NS_ERROR_XPC_SECURITY_MANAGER_VETO);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-04 03:29:08 +04:00
|
|
|
aAutoCompartment.construct(aCx, obj);
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
return JS_GetGlobalForObject(aCx, obj);
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalObject::GlobalObject(JSContext* aCx, JSObject* aObject)
|
|
|
|
: mGlobalJSObject(aCx)
|
|
|
|
{
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
mGlobalJSObject = GetGlobalObject<true>(aCx, aObject, ac);
|
|
|
|
if (!mGlobalJSObject) {
|
2013-05-08 23:50:58 +04:00
|
|
|
mGlobalObject = nullptr;
|
2012-12-03 20:07:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-30 00:16:04 +04:00
|
|
|
JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*mGlobalJSObject));
|
2012-12-03 20:07:49 +04:00
|
|
|
|
|
|
|
// Switch this to UnwrapDOMObjectToISupports once our global objects are
|
|
|
|
// using new bindings.
|
|
|
|
nsresult rv = xpc_qsUnwrapArg<nsISupports>(aCx, val, &mGlobalObject,
|
|
|
|
static_cast<nsISupports**>(getter_AddRefs(mGlobalObjectRef)),
|
2013-05-30 00:16:04 +04:00
|
|
|
val.address());
|
2012-12-03 20:07:49 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mGlobalObject = nullptr;
|
|
|
|
Throw<true>(aCx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerGlobalObject::WorkerGlobalObject(JSContext* aCx, JSObject* aObject)
|
|
|
|
: mGlobalJSObject(aCx),
|
|
|
|
mCx(aCx)
|
|
|
|
{
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
mGlobalJSObject = GetGlobalObject<false>(aCx, aObject, ac);
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:05:36 +04:00
|
|
|
JSBool
|
2013-05-04 03:29:08 +04:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-01-08 22:05:36 +04:00
|
|
|
JSBool* bp)
|
|
|
|
{
|
|
|
|
const DOMIfaceAndProtoJSClass* clasp =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(js::GetObjectClass(obj));
|
|
|
|
|
2013-04-11 22:50:18 +04:00
|
|
|
const DOMClass* domClass = GetDOMClass(js::UncheckedUnwrap(instance));
|
2013-01-08 22:05:36 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!domClass || clasp->mPrototypeID != prototypes::id::_ID_Count,
|
|
|
|
"Why do we have a hasInstance hook if we don't have a prototype "
|
|
|
|
"ID?");
|
|
|
|
|
2013-02-22 13:25:24 +04:00
|
|
|
if (domClass &&
|
|
|
|
domClass->mInterfaceChain[clasp->mDepth] == clasp->mPrototypeID) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JS::Value> protov(cx);
|
|
|
|
DebugOnly<bool> ok = JS_GetProperty(cx, obj, "prototype", protov.address());
|
2013-02-22 13:25:24 +04:00
|
|
|
MOZ_ASSERT(ok, "Someone messed with our prototype property?");
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> interfacePrototype(cx, &protov.toObject());
|
2013-02-22 13:25:24 +04:00
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(interfacePrototype)),
|
|
|
|
"Someone messed with our prototype property?");
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
|
|
|
if (!JS_GetPrototype(cx, instance, proto.address())) {
|
2013-02-22 13:25:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (proto) {
|
|
|
|
if (proto == interfacePrototype) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
if (!JS_GetPrototype(cx, proto, proto.address())) {
|
2013-02-22 13:25:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bp = false;
|
2013-01-08 22:05:36 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
InterfaceHasInstance(JSContext* cx, JSHandleObject obj, JSMutableHandleValue vp,
|
|
|
|
JSBool* bp)
|
|
|
|
{
|
|
|
|
if (!vp.isObject()) {
|
|
|
|
*bp = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> instanceObject(cx, &vp.toObject());
|
|
|
|
return InterfaceHasInstance(cx, obj, instanceObject, bp);
|
2013-01-08 22:05:36 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 23:50:58 +04:00
|
|
|
bool
|
2013-04-09 01:04:21 +04:00
|
|
|
ReportLenientThisUnwrappingFailure(JSContext* cx, JS::Handle<JSObject*> obj)
|
|
|
|
{
|
|
|
|
GlobalObject global(cx, obj);
|
2013-05-08 23:50:58 +04:00
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-09 01:04:21 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(global.Get());
|
|
|
|
if (window && window->GetDoc()) {
|
|
|
|
window->GetDoc()->WarnOnceAbout(nsIDocument::eLenientThis);
|
|
|
|
}
|
2013-05-08 23:50:58 +04:00
|
|
|
return true;
|
2013-04-09 01:04:21 +04:00
|
|
|
}
|
|
|
|
|
2013-05-30 07:15:31 +04:00
|
|
|
bool
|
|
|
|
RegisterForDeferredFinalization(DeferredFinalizeStartFunction start,
|
|
|
|
DeferredFinalizeFunction run)
|
|
|
|
{
|
|
|
|
XPCJSRuntime *rt = nsXPConnect::GetRuntimeInstance();
|
|
|
|
NS_ENSURE_TRUE(rt, false);
|
|
|
|
|
|
|
|
rt->RegisterDeferredFinalize(start, run);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-02 22:38:19 +04:00
|
|
|
// Date implementation methods
|
|
|
|
Date::Date() :
|
2013-05-02 00:55:13 +04:00
|
|
|
mMsecSinceEpoch(UnspecifiedNaN())
|
2013-05-02 22:38:19 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Date::IsUndefined() const
|
|
|
|
{
|
2013-05-02 00:55:13 +04:00
|
|
|
return IsNaN(mMsecSinceEpoch);
|
2013-05-02 22:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Date::SetTimeStamp(JSContext* cx, JSObject* objArg)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(cx, objArg);
|
|
|
|
MOZ_ASSERT(JS_ObjectIsDate(cx, obj));
|
|
|
|
|
|
|
|
obj = js::CheckedUnwrap(obj);
|
|
|
|
// This really sucks: even if JS_ObjectIsDate, CheckedUnwrap can _still_ fail
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mMsecSinceEpoch = js_DateGetMsecSinceEpoch(obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Date::ToDateObject(JSContext* cx, JS::Value* vp) const
|
|
|
|
{
|
|
|
|
JSObject* obj = JS_NewDateObjectMsec(cx, mMsecSinceEpoch);
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*vp = JS::ObjectValue(*obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-20 21:47:08 +04:00
|
|
|
bool
|
|
|
|
GetWindowForJSImplementedObject(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
nsPIDOMWindow** window)
|
|
|
|
{
|
|
|
|
// Be very careful to not get tricked here.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!xpc::AccessCheck::isChrome(js::GetObjectCompartment(obj))) {
|
|
|
|
NS_RUNTIMEABORT("Should have a chrome object here");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the content-side object.
|
|
|
|
JS::Rooted<JS::Value> domImplVal(cx);
|
|
|
|
if (!JS_GetProperty(cx, obj, "__DOM_IMPL__", domImplVal.address())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!domImplVal.isObject()) {
|
|
|
|
ThrowErrorMessage(cx, MSG_NOT_OBJECT);
|
|
|
|
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.
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(global.Get()));
|
|
|
|
win.forget(window);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|