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