1332245 - Move nsScriptError from js/xpconnect to dom/bindings.

This commit is contained in:
Fedor 2019-09-05 20:06:03 +03:00
parent f4a5088c68
commit 8a0e23aadb
20 changed files with 336 additions and 83 deletions

View File

@ -185,6 +185,7 @@
@RESPATH@/components/dom.xpt
@RESPATH@/components/dom_apps.xpt
@RESPATH@/components/dom_base.xpt
@RESPATH@/components/dom_bindings.xpt
@RESPATH@/components/dom_system.xpt
@RESPATH@/components/dom_canvas.xpt
@RESPATH@/components/dom_core.xpt

View File

@ -6,6 +6,12 @@
TEST_DIRS += ['test']
XPIDL_SOURCES += [
'nsIScriptError.idl'
]
XPIDL_MODULE = 'dom_bindings'
EXPORTS.ipc += [
'ErrorIPCUtils.h',
]
@ -91,6 +97,8 @@ UNIFIED_SOURCES += [
'DOMJSProxyHandler.cpp',
'Exceptions.cpp',
'IterableIterator.cpp',
'nsScriptError.cpp',
'nsScriptErrorWithStack.cpp',
'SimpleGlobalObject.cpp',
'ToJSValue.cpp',
'WebIDLGlobalNameHash.cpp',

View File

@ -5,15 +5,15 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* nsIScriptError implementation. Defined here, lacking a JS-specific
* place to put XPCOM things.
* nsIScriptError implementation.
*/
#include "xpcprivate.h"
#include "nsScriptError.h"
#include "jsprf.h"
#include "MainThreadUtils.h"
#include "mozilla/Assertions.h"
#include "nsGlobalWindow.h"
#include "nsNetUtil.h"
#include "nsPIDOMWindow.h"
#include "nsILoadContext.h"
#include "nsIDocShell.h"

View File

@ -0,0 +1,86 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_dom_nsScriptError_h
#define mozilla_dom_nsScriptError_h
#include "mozilla/Atomics.h"
#include <stdint.h>
#include "jsapi.h"
#include "js/RootingAPI.h"
#include "nsIScriptError.h"
#include "nsString.h"
// Definition of nsScriptError..
class nsScriptErrorBase : public nsIScriptError {
public:
nsScriptErrorBase();
NS_DECL_NSICONSOLEMESSAGE
NS_DECL_NSISCRIPTERROR
protected:
virtual ~nsScriptErrorBase();
void
InitializeOnMainThread();
nsString mMessage;
nsString mMessageName;
nsString mSourceName;
uint32_t mLineNumber;
nsString mSourceLine;
uint32_t mColumnNumber;
uint32_t mFlags;
nsCString mCategory;
// mOuterWindowID is set on the main thread from InitializeOnMainThread().
uint64_t mOuterWindowID;
uint64_t mInnerWindowID;
int64_t mTimeStamp;
// mInitializedOnMainThread and mIsFromPrivateWindow are set on the main
// thread from InitializeOnMainThread().
mozilla::Atomic<bool> mInitializedOnMainThread;
bool mIsFromPrivateWindow;
};
class nsScriptError final : public nsScriptErrorBase {
public:
nsScriptError() {}
NS_DECL_THREADSAFE_ISUPPORTS
private:
virtual ~nsScriptError() {}
};
class nsScriptErrorWithStack : public nsScriptErrorBase {
public:
explicit nsScriptErrorWithStack(JS::HandleObject);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsScriptErrorWithStack)
NS_IMETHOD Init(const nsAString& message,
const nsAString& sourceName,
const nsAString& sourceLine,
uint32_t lineNumber,
uint32_t columnNumber,
uint32_t flags,
const char* category) override;
NS_IMETHOD GetStack(JS::MutableHandleValue) override;
NS_IMETHOD ToString(nsACString& aResult) override;
private:
virtual ~nsScriptErrorWithStack();
// Complete stackframe where the error happened.
// Must be SavedFrame object.
JS::Heap<JSObject*> mStack;
};
#endif /* mozilla_dom_nsScriptError_h */

View File

@ -10,12 +10,14 @@
* that can store a SavedFrame stack trace object.
*/
#include "xpcprivate.h"
#include "nsScriptError.h"
#include "MainThreadUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsGlobalWindow.h"
#include "nsCycleCollectionParticipant.h"
using namespace mozilla::dom;
namespace {

View File

@ -9,6 +9,7 @@
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsJSUtils.h"
#include "nsIScriptError.h"
#include "jsfriendapi.h"
#include "jswrapper.h"
#include "js/Proxy.h"

View File

@ -0,0 +1,124 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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/. */
#include "vm/ErrorReporting.h"
#include "mozilla/Move.h"
#include <stdarg.h>
#include "jscntxt.h"
#include "jsexn.h"
using mozilla::Move;
using JS::UniqueTwoByteChars;
void
CallWarningReporter(JSContext* cx, JSErrorReport* reportp)
{
MOZ_ASSERT(reportp);
MOZ_ASSERT(JSREPORT_IS_WARNING(reportp->flags));
if (JS::WarningReporter warningReporter = cx->runtime()->warningReporter)
warningReporter(cx, reportp);
}
void
CompileError::throwError(JSContext* cx)
{
if (JSREPORT_IS_WARNING(flags)) {
CallWarningReporter(cx, this);
return;
}
// If there's a runtime exception type associated with this error
// number, set that as the pending exception. For errors occuring at
// compile time, this is very likely to be a JSEXN_SYNTAXERR.
//
// If an exception is thrown but not caught, the JSREPORT_EXCEPTION
// flag will be set in report.flags. Proper behavior for an error
// reporter is to ignore a report with this flag for all but top-level
// compilation errors. The exception will remain pending, and so long
// as the non-top-level "load", "eval", or "compile" native function
// returns false, the top-level reporter will eventually receive the
// uncaught exception report.
ErrorToException(cx, this, nullptr, nullptr);
}
bool
ReportCompileWarning(JSContext* cx, ErrorMetadata&& metadata, UniquePtr<JSErrorNotes> notes,
unsigned flags, unsigned errorNumber, va_list args)
{
// On the main thread, report the error immediately. When compiling off
// thread, save the error so that the thread finishing the parse can report
// it later.
CompileError tempErr;
CompileError* err = &tempErr;
if (!cx->isJSContext() && !cx->addPendingCompileError(&err)) {
return false;
}
err->notes = Move(notes);
err->flags = flags;
err->errorNumber = errorNumber;
err->filename = metadata.filename;
err->lineno = metadata.lineNumber;
err->column = metadata.columnNumber;
err->isMuted = metadata.isMuted;
if (UniqueTwoByteChars lineOfContext = Move(metadata.lineOfContext))
err->initOwnedLinebuf(lineOfContext.release(), metadata.lineLength, metadata.tokenOffset);
if (!ExpandErrorArgumentsVA(cx, GetErrorMessage, nullptr, errorNumber,
nullptr, ArgumentsAreLatin1, err, args))
{
return false;
}
if (cx->isJSContext()) {
err->throwError(cx->asJSContext());
}
return true;
}
void
ReportCompileError(JSContext* cx, ErrorMetadata&& metadata, UniquePtr<JSErrorNotes> notes,
unsigned flags, unsigned errorNumber, va_list args)
{
// On the main thread, report the error immediately. When compiling off
// thread, save the error so that the thread finishing the parse can report
// it later.
CompileError tempErr;
CompileError* err = &tempErr;
if (!cx->isJSContext() && !cx->addPendingCompileError(&err)) {
return;
}
err->notes = Move(notes);
err->flags = flags;
err->errorNumber = errorNumber;
err->filename = metadata.filename;
err->lineno = metadata.lineNumber;
err->column = metadata.columnNumber;
err->isMuted = metadata.isMuted;
if (UniqueTwoByteChars lineOfContext = Move(metadata.lineOfContext))
err->initOwnedLinebuf(lineOfContext.release(), metadata.lineLength, metadata.tokenOffset);
if (!ExpandErrorArgumentsVA(cx, GetErrorMessage, nullptr, errorNumber,
nullptr, ArgumentsAreLatin1, err, args))
{
return;
}
if (cx->isJSContext()) {
err->throwError(cx->asJSContext());
}
}

View File

@ -0,0 +1,91 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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/. */
#ifndef vm_ErrorReporting_h
#define vm_ErrorReporting_h
#include "mozilla/Move.h"
#include <stdarg.h>
#include "jsapi.h" // for JSErrorNotes, JSErrorReport
#include "js/UniquePtr.h" // for UniquePtr
#include "js/Utility.h" // for UniqueTwoByteChars
struct JSContext;
namespace js {
/**
* Metadata for a compilation error (or warning) at a particular offset, or at
* no offset (i.e. with respect to a script overall).
*/
struct ErrorMetadata
{
// The file/URL where the error occurred.
const char* filename;
// The line and column numbers where the error occurred. If the error
// is with respect to the entire script and not with respect to a
// particular location, these will both be zero.
uint32_t lineNumber;
uint32_t columnNumber;
// If the error occurs at a particular location, context surrounding the
// location of the error: the line that contained the error, or a small
// portion of it if the line is long.
//
// This information is provided on a best-effort basis: code populating
// ErrorMetadata instances isn't obligated to supply this.
JS::UniqueTwoByteChars lineOfContext;
// If |lineOfContext| is non-null, its length.
size_t lineLength;
// If |lineOfContext| is non-null, the offset within it of the token that
// triggered the error.
size_t tokenOffset;
// Whether the error is "muted" because it derives from a cross-origin
// load. See the comment in TransitiveCompileOptions in jsapi.h for
// details.
bool isMuted;
};
class CompileError : public JSErrorReport
{
public:
void throwError(JSContext* cx);
};
/** Send a JSErrorReport to the warningReporter callback. */
extern void
CallWarningReporter(JSContext* cx, JSErrorReport* report);
/**
* Report a compile error during script processing prior to execution of the
* script.
*/
extern void
ReportCompileError(ErrorMetadata&& metadata, UniquePtr<JSErrorNotes> notes,
unsigned flags, unsigned errorNumber, va_list args);
/**
* Report a compile warning during script processing prior to execution of the
* script. Returns true if the warning was successfully reported, false if an
* error occurred.
*
* This function DOES NOT respect an existing werror option. If the caller
* wishes such option to be respected, it must do so itself.
*/
extern MOZ_MUST_USE bool
ReportCompileWarning(JSContext* cx, ErrorMetadata&& metadata, UniquePtr<JSErrorNotes> notes,
unsigned flags, unsigned errorNumber, va_list args);
} // namespace js
#endif /* vm_ErrorReporting_h */

View File

@ -7,7 +7,6 @@
XPIDL_SOURCES += [
'mozIJSSubScriptLoader.idl',
'nsIAddonInterposition.idl',
'nsIScriptError.idl',
'nsIXPConnect.idl',
'nsIXPCScriptable.idl',
'xpccomponents.idl',

View File

@ -34,9 +34,11 @@
#include "nsDOMClassInfo.h"
#include "ShimInterfaceInfo.h"
#include "nsIAddonInterposition.h"
#include "nsIScriptError.h"
#include "nsISimpleEnumerator.h"
#include "nsPIDOMWindow.h"
#include "nsGlobalWindow.h"
#include "nsScriptError.h"
using namespace mozilla;
using namespace JS;

View File

@ -11,9 +11,11 @@
#include "xpcprivate.h"
#include "nsIAtom.h"
#include "nsIScriptError.h"
#include "nsWrapperCache.h"
#include "nsJSUtils.h"
#include "nsQueryObject.h"
#include "nsScriptError.h"
#include "WrapperFactory.h"
#include "nsWrapperCacheInlines.h"

View File

@ -23,7 +23,6 @@
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSID)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIXPConnect,
nsXPConnect::GetSingleton)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptError)
NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSComponentLoader)
NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSSubScriptLoader)
@ -31,14 +30,12 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSSubScriptLoader)
NS_DEFINE_NAMED_CID(NS_JS_ID_CID);
NS_DEFINE_NAMED_CID(NS_XPCONNECT_CID);
NS_DEFINE_NAMED_CID(NS_XPCEXCEPTION_CID);
NS_DEFINE_NAMED_CID(NS_SCRIPTERROR_CID);
NS_DEFINE_NAMED_CID(MOZJSCOMPONENTLOADER_CID);
NS_DEFINE_NAMED_CID(MOZ_JSSUBSCRIPTLOADER_CID);
#define XPCONNECT_CIDENTRIES \
{ &kNS_JS_ID_CID, false, nullptr, nsJSIDConstructor }, \
{ &kNS_XPCONNECT_CID, false, nullptr, nsIXPConnectConstructor }, \
{ &kNS_SCRIPTERROR_CID, false, nullptr, nsScriptErrorConstructor }, \
{ &kMOZJSCOMPONENTLOADER_CID, false, nullptr, mozJSComponentLoaderConstructor },\
{ &kMOZ_JSSUBSCRIPTLOADER_CID, false, nullptr, mozJSSubScriptLoaderConstructor },
@ -46,7 +43,6 @@ NS_DEFINE_NAMED_CID(MOZ_JSSUBSCRIPTLOADER_CID);
{ XPC_ID_CONTRACTID, &kNS_JS_ID_CID }, \
{ XPC_XPCONNECT_CONTRACTID, &kNS_XPCONNECT_CID }, \
{ XPC_CONTEXT_STACK_CONTRACTID, &kNS_XPCONNECT_CID }, \
{ NS_SCRIPTERROR_CONTRACTID, &kNS_SCRIPTERROR_CID }, \
{ MOZJSCOMPONENTLOADER_CONTRACTID, &kMOZJSCOMPONENTLOADER_CID }, \
{ MOZJSSUBSCRIPTLOADER_CONTRACTID, &kMOZ_JSSUBSCRIPTLOADER_CID },

View File

@ -10,6 +10,7 @@
#include "jsprf.h"
#include "nsArrayEnumerator.h"
#include "nsContentUtils.h"
#include "nsIScriptError.h"
#include "nsWrapperCache.h"
#include "AccessCheck.h"
#include "nsJSUtils.h"

View File

@ -11,6 +11,7 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/XPTInterfaceInfoManager.h"
#include "nsIScriptError.h"
#include "nsPrintfCString.h"
using namespace JS;

View File

@ -14,8 +14,6 @@ EXPORTS += [
UNIFIED_SOURCES += [
'ExportHelpers.cpp',
'nsScriptError.cpp',
'nsScriptErrorWithStack.cpp',
'nsXPConnect.cpp',
'Sandbox.cpp',
'XPCCallContext.cpp',
@ -58,6 +56,7 @@ LOCAL_INCLUDES += [
'../wrappers',
'/caps',
'/dom/base',
'/dom/bindings',
'/dom/html',
'/dom/svg',
'/dom/workers',

View File

@ -33,7 +33,9 @@
#include "nsIObjectOutputStream.h"
#include "nsScriptSecurityManager.h"
#include "nsIPermissionManager.h"
#include "nsIScriptError.h"
#include "nsContentUtils.h"
#include "nsScriptError.h"
#include "jsfriendapi.h"
using namespace mozilla;

View File

@ -128,7 +128,6 @@
#include "MainThreadUtils.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsIException.h"
#include "nsVariant.h"
@ -2552,77 +2551,6 @@ extern char*
xpc_PrintJSStack(JSContext* cx, bool showArgs, bool showLocals,
bool showThisProps);
/***************************************************************************/
// Definition of nsScriptError, defined here because we lack a place to put
// XPCOM objects associated with the JavaScript engine.
class nsScriptErrorBase : public nsIScriptError {
public:
nsScriptErrorBase();
// TODO - do something reasonable on getting null from these babies.
NS_DECL_NSICONSOLEMESSAGE
NS_DECL_NSISCRIPTERROR
protected:
virtual ~nsScriptErrorBase();
void
InitializeOnMainThread();
nsString mMessage;
nsString mMessageName;
nsString mSourceName;
uint32_t mLineNumber;
nsString mSourceLine;
uint32_t mColumnNumber;
uint32_t mFlags;
nsCString mCategory;
// mOuterWindowID is set on the main thread from InitializeOnMainThread().
uint64_t mOuterWindowID;
uint64_t mInnerWindowID;
int64_t mTimeStamp;
// mInitializedOnMainThread and mIsFromPrivateWindow are set on the main
// thread from InitializeOnMainThread().
mozilla::Atomic<bool> mInitializedOnMainThread;
bool mIsFromPrivateWindow;
};
class nsScriptError final : public nsScriptErrorBase {
public:
nsScriptError() {}
NS_DECL_THREADSAFE_ISUPPORTS
private:
virtual ~nsScriptError() {}
};
class nsScriptErrorWithStack : public nsScriptErrorBase {
public:
explicit nsScriptErrorWithStack(JS::HandleObject);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsScriptErrorWithStack)
NS_IMETHOD Init(const nsAString& message,
const nsAString& sourceName,
const nsAString& sourceLine,
uint32_t lineNumber,
uint32_t columnNumber,
uint32_t flags,
const char* category) override;
NS_IMETHOD GetStack(JS::MutableHandleValue) override;
NS_IMETHOD ToString(nsACString& aResult) override;
private:
virtual ~nsScriptErrorWithStack();
// Complete stackframe where the error happened.
// Must be SavedFrame object.
JS::Heap<JSObject*> mStack;
};
/******************************************************************************
* Handles pre/post script processing.
*/

View File

@ -31,6 +31,7 @@ LOCAL_INCLUDES += [
'/docshell/base',
'/dom/audiochannel',
'/dom/base',
'/dom/bindings',
'/dom/canvas',
'/dom/filesystem',
'/dom/geolocation',

View File

@ -35,6 +35,7 @@
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsIScriptNameSpaceManager.h"
#include "nsIScriptError.h"
#include "nsISelection.h"
#include "nsCaret.h"
#include "nsPlainTextSerializer.h"
@ -194,6 +195,8 @@ static void Shutdown();
#include "mozilla/dom/PresentationDeviceManager.h"
#include "mozilla/dom/PresentationTCPSessionTransport.h"
#include "nsScriptError.h"
#include "mozilla/TextInputProcessor.h"
using namespace mozilla;
@ -565,6 +568,8 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(UDPSocketChild)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(GeckoMediaPluginService, GeckoMediaPluginService::GetGeckoMediaPluginService)
#endif
NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptError)
#ifdef ACCESSIBILITY
#include "xpcAccessibilityService.h"
@ -725,6 +730,8 @@ NS_DEFINE_NAMED_CID(PRESENTATION_TCP_SESSION_TRANSPORT_CID);
NS_DEFINE_NAMED_CID(TEXT_INPUT_PROCESSOR_CID);
NS_DEFINE_NAMED_CID(NS_SCRIPTERROR_CID);
static nsresult
CreateWindowCommandTableConstructor(nsISupports *aOuter,
REFNSIID aIID, void **aResult)
@ -985,6 +992,7 @@ static const mozilla::Module::CIDEntry kLayoutCIDs[] = {
{ &kPRESENTATION_DEVICE_MANAGER_CID, false, nullptr, PresentationDeviceManagerConstructor },
{ &kPRESENTATION_TCP_SESSION_TRANSPORT_CID, false, nullptr, PresentationTCPSessionTransportConstructor },
{ &kTEXT_INPUT_PROCESSOR_CID, false, nullptr, TextInputProcessorConstructor },
{ &kNS_SCRIPTERROR_CID, false, nullptr, nsScriptErrorConstructor },
{ nullptr }
};
@ -1118,6 +1126,7 @@ static const mozilla::Module::ContractIDEntry kLayoutContracts[] = {
{ PRESENTATION_DEVICE_MANAGER_CONTRACTID, &kPRESENTATION_DEVICE_MANAGER_CID },
{ PRESENTATION_TCP_SESSION_TRANSPORT_CONTRACTID, &kPRESENTATION_TCP_SESSION_TRANSPORT_CID },
{ "@mozilla.org/text-input-processor;1", &kTEXT_INPUT_PROCESSOR_CID },
{ NS_SCRIPTERROR_CONTRACTID, &kNS_SCRIPTERROR_CID },
{ nullptr }
};