Bug 958816 - Make strings in nsIStackFrame API sane, r=bz

This commit is contained in:
Andrea Marchesini 2014-01-17 15:24:03 +01:00
parent 940cdd21fd
commit ab79c85864
14 changed files with 212 additions and 302 deletions

View File

@ -94,16 +94,14 @@ class BlobURLsReporter MOZ_FINAL : public nsIMemoryReporter
}
for (uint32_t i = 0; i < maxFrames && frame; ++i) {
nsAutoCString fileNameEscaped;
char* fileName = nullptr;
nsCString fileName;
int32_t lineNumber = 0;
frame->GetFilename(&fileName);
frame->GetFilename(fileName);
frame->GetLineNumber(&lineNumber);
if (fileName != nullptr && fileName[0] != '\0') {
if (!fileName.IsEmpty()) {
stack += "js(";
fileNameEscaped = fileName;
if (!origin.IsEmpty()) {
// Make the file name root-relative for conciseness if possible.
const char* originData;
@ -111,14 +109,14 @@ class BlobURLsReporter MOZ_FINAL : public nsIMemoryReporter
originLen = origin.GetData(&originData);
// If fileName starts with origin + "/", cut up to that "/".
if (strlen(fileName) >= originLen + 1 &&
memcmp(fileName, originData, originLen) == 0 &&
if (fileName.Length() >= originLen + 1 &&
memcmp(fileName.get(), originData, originLen) == 0 &&
fileName[originLen] == '/') {
fileNameEscaped.Cut(0, originLen);
fileName.Cut(0, originLen);
}
}
fileNameEscaped.ReplaceChar('/', '\\');
stack += fileNameEscaped;
fileName.ReplaceChar('/', '\\');
stack += fileName;
if (lineNumber > 0) {
stack += ", line=";
stack.AppendInt(lineNumber);

View File

@ -29,11 +29,11 @@ DOMError::DOMError(nsPIDOMWindow* aWindow)
DOMError::DOMError(nsPIDOMWindow* aWindow, nsresult aValue)
: mWindow(aWindow)
{
const char *name, *message;
NS_GetNameAndMessageForDOMNSResult(aValue, &name, &message);
nsCString name, message;
NS_GetNameAndMessageForDOMNSResult(aValue, name, message);
mName = NS_ConvertASCIItoUTF16(name);
mMessage = NS_ConvertASCIItoUTF16(message);
CopyUTF8toUTF16(name, mName);
CopyUTF8toUTF16(message, mMessage);
SetIsDOMBinding();
}

View File

@ -89,17 +89,19 @@ static const struct ResultStruct
static void
NSResultToNameAndMessage(nsresult aNSResult,
const char** aName,
const char** aMessage,
nsCString& aName,
nsCString& aMessage,
uint16_t* aCode)
{
*aName = nullptr;
*aMessage = nullptr;
aName.Truncate();
aMessage.Truncate();
*aCode = 0;
for (uint32_t idx = 0; idx < ArrayLength(sDOMErrorMsgMap); idx++) {
if (aNSResult == sDOMErrorMsgMap[idx].mNSResult) {
*aName = sDOMErrorMsgMap[idx].mName;
*aMessage = sDOMErrorMsgMap[idx].mMessage;
aName.Rebind(sDOMErrorMsgMap[idx].mName,
strlen(sDOMErrorMsgMap[idx].mName));
aMessage.Rebind(sDOMErrorMsgMap[idx].mMessage,
strlen(sDOMErrorMsgMap[idx].mMessage));
*aCode = sDOMErrorMsgMap[idx].mCode;
return;
}
@ -111,17 +113,17 @@ NSResultToNameAndMessage(nsresult aNSResult,
}
nsresult
NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName,
const char** aMessage, uint16_t* aCode)
NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, nsACString& aName,
nsACString& aMessage, uint16_t* aCode)
{
const char* name = nullptr;
const char* message = nullptr;
nsCString name;
nsCString message;
uint16_t code = 0;
NSResultToNameAndMessage(aNSResult, &name, &message, &code);
NSResultToNameAndMessage(aNSResult, name, message, &code);
if (name && message) {
*aName = name;
*aMessage = message;
if (!name.IsEmpty() && !message.IsEmpty()) {
aName = name;
aMessage = message;
if (aCode) {
*aCode = code;
}
@ -170,19 +172,13 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CI_INTERFACE_GETTER1(Exception, nsIXPCException)
Exception::Exception(const char *aMessage,
Exception::Exception(const nsACString& aMessage,
nsresult aResult,
const char *aName,
const nsACString& aName,
nsIStackFrame *aLocation,
nsISupports *aData)
: mMessage(nullptr),
mResult(NS_OK),
mName(nullptr),
mLocation(nullptr),
mData(nullptr),
mFilename(nullptr),
: mResult(NS_OK),
mLineNumber(0),
mInner(nullptr),
mInitialized(false),
mHoldingJSVal(false)
{
@ -235,10 +231,7 @@ Exception::Exception(const char *aMessage,
}
Exception::Exception()
: mMessage(nullptr),
mResult(NS_OK),
mName(nullptr),
mFilename(nullptr),
: mResult(NS_OK),
mLineNumber(-1),
mInitialized(false),
mHoldingJSVal(false)
@ -247,19 +240,6 @@ Exception::Exception()
Exception::~Exception()
{
if (mMessage) {
nsMemory::Free(mMessage);
mMessage = nullptr;
}
if (mName) {
nsMemory::Free(mName);
mName = nullptr;
}
if (mFilename) {
nsMemory::Free(mFilename);
mFilename = nullptr;
}
if (mHoldingJSVal) {
MOZ_ASSERT(NS_IsMainThread());
@ -296,20 +276,13 @@ Exception::StowJSVal(JS::Value& aVp)
}
}
/* readonly attribute string message; */
/* readonly attribute AUTF8String message; */
NS_IMETHODIMP
Exception::GetMessageMoz(char** aMessage)
Exception::GetMessageMoz(nsACString& aMessage)
{
NS_ENSURE_ARG_POINTER(aMessage);
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
if (mMessage) {
*aMessage =
(char*) nsMemory::Clone(mMessage, sizeof(char)*(strlen(mMessage)+1));
} else {
*aMessage = nullptr;
}
aMessage.Assign(mMessage);
return NS_OK;
}
@ -324,30 +297,31 @@ Exception::GetResult(nsresult* aResult)
return NS_OK;
}
/* readonly attribute string name; */
/* readonly attribute AUTF8String name; */
NS_IMETHODIMP
Exception::GetName(char** aName)
Exception::GetName(nsACString& aName)
{
NS_ENSURE_ARG_POINTER(aName);
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
const char* name = mName;
if (!name) {
nsXPCException::NameAndFormatForNSResult(mResult, &name, nullptr);
}
if (name) {
*aName = (char*) nsMemory::Clone(name, sizeof(char)*(strlen(name)+1));
if (!mName.IsEmpty()) {
aName.Assign(mName);
} else {
*aName = nullptr;
aName.Truncate();
const char* name = nullptr;
nsXPCException::NameAndFormatForNSResult(mResult, &name, nullptr);
if (name) {
aName.Assign(name);
}
}
return NS_OK;
}
/* readonly attribute string filename; */
/* readonly attribute AUTF8String filename; */
NS_IMETHODIMP
Exception::GetFilename(char** aFilename)
Exception::GetFilename(nsACString& aFilename)
{
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
@ -355,7 +329,8 @@ Exception::GetFilename(char** aFilename)
return mLocation->GetFilename(aFilename);
}
XPC_STRING_GETTER_BODY(aFilename, mFilename);
aFilename.Assign(mFilename);
return NS_OK;
}
/* readonly attribute uint32_t lineNumber; */
@ -423,11 +398,10 @@ Exception::GetInner(nsIException** aException)
return NS_OK;
}
/* string toString (); */
/* AUTF8String toString (); */
NS_IMETHODIMP
Exception::ToString(char **_retval)
Exception::ToString(nsACString& _retval)
{
NS_ENSURE_ARG_POINTER(_retval);
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
static const char defaultMsg[] = "<no message>";
@ -435,18 +409,21 @@ Exception::ToString(char **_retval)
static const char format[] =
"[Exception... \"%s\" nsresult: \"0x%x (%s)\" location: \"%s\" data: %s]";
char* indicatedLocation = nullptr;
nsCString location;
if (mLocation) {
// we need to free this if it does not fail
nsresult rv = mLocation->ToString(&indicatedLocation);
nsresult rv = mLocation->ToString(location);
NS_ENSURE_SUCCESS(rv, rv);
}
const char* msg = mMessage ? mMessage : nullptr;
const char* location = indicatedLocation ?
indicatedLocation : defaultLocation;
const char* resultName = mName;
if (location.IsEmpty()) {
location.Assign(defaultLocation);
}
const char* msg = mMessage.IsEmpty() ? nullptr : mMessage.get();
const char* resultName = mName.IsEmpty() ? nullptr: mName.get();
if (!resultName &&
!nsXPCException::NameAndFormatForNSResult(mResult, &resultName,
(!msg) ? &msg : nullptr)) {
@ -457,36 +434,24 @@ Exception::ToString(char **_retval)
}
const char* data = mData ? "yes" : "no";
char* temp = JS_smprintf(format, msg, mResult, resultName, location, data);
if (indicatedLocation) {
nsMemory::Free(indicatedLocation);
}
char* final = nullptr;
if (temp) {
final = (char*) nsMemory::Clone(temp, sizeof(char)*(strlen(temp)+1));
JS_smprintf_free(temp);
}
*_retval = final;
return final ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
_retval.Truncate();
_retval.AppendPrintf(format, msg, mResult, resultName,
location.get(), data);
return NS_OK;
}
/* void initialize (in string aMessage, in nsresult aResult, in string aName, in nsIStackFrame aLocation, in nsISupports aData, in nsIException aInner); */
/* void initialize (in AUTF8String aMessage, in nsresult aResult,
* in AUTF8String aName, in nsIStackFrame aLocation,
* in nsISupports aData, in nsIException aInner); */
NS_IMETHODIMP
Exception::Initialize(const char *aMessage, nsresult aResult, const char *aName, nsIStackFrame *aLocation, nsISupports *aData, nsIException *aInner)
Exception::Initialize(const nsACString& aMessage, nsresult aResult,
const nsACString& aName, nsIStackFrame *aLocation,
nsISupports *aData, nsIException *aInner)
{
NS_ENSURE_FALSE(mInitialized, NS_ERROR_ALREADY_INITIALIZED);
if (aMessage) {
mMessage =
(char*) nsMemory::Clone(aMessage, sizeof(char)*(strlen(aMessage)+1));
}
if (aName) {
mName = (char*) nsMemory::Clone(aName, sizeof(char)*(strlen(aName)+1));
}
mMessage = aMessage;
mName = aName;
mResult = aResult;
if (aLocation) {
@ -516,14 +481,13 @@ Exception::WrapObject(JSContext* cx, JS::Handle<JSObject*> scope)
void
Exception::GetMessageMoz(nsString& retval)
{
char* str = nullptr;
nsCString str;
#ifdef DEBUG
DebugOnly<nsresult> rv =
#endif
GetMessageMoz(&str);
GetMessageMoz(str);
MOZ_ASSERT(NS_SUCCEEDED(rv));
CopyUTF8toUTF16(str, retval);
nsMemory::Free(str);
}
uint32_t
@ -535,27 +499,25 @@ Exception::Result() const
void
Exception::GetName(nsString& retval)
{
char* str = nullptr;
nsCString str;
#ifdef DEBUG
DebugOnly<nsresult> rv =
#endif
GetName(&str);
GetName(str);
MOZ_ASSERT(NS_SUCCEEDED(rv));
CopyUTF8toUTF16(str, retval);
nsMemory::Free(str);
}
void
Exception::GetFilename(nsString& retval)
{
char* str = nullptr;
nsCString str;
#ifdef DEBUG
DebugOnly<nsresult> rv =
#endif
GetFilename(&str);
GetFilename(str);
MOZ_ASSERT(NS_SUCCEEDED(rv));
CopyUTF8toUTF16(str, retval);
nsMemory::Free(str);
}
uint32_t
@ -602,14 +564,13 @@ Exception::GetData() const
void
Exception::Stringify(nsString& retval)
{
char* str = nullptr;
nsCString str;
#ifdef DEBUG
DebugOnly<nsresult> rv =
#endif
ToString(&str);
ToString(str);
MOZ_ASSERT(NS_SUCCEEDED(rv));
CopyUTF8toUTF16(str, retval);
nsMemory::Free(str);
}
NS_IMPL_ADDREF_INHERITED(DOMException, Exception)
@ -618,9 +579,9 @@ NS_INTERFACE_MAP_BEGIN(DOMException)
NS_INTERFACE_MAP_ENTRY(nsIDOMDOMException)
NS_INTERFACE_MAP_END_INHERITING(Exception)
DOMException::DOMException(nsresult aRv, const char* aMessage,
const char* aName, uint16_t aCode)
: Exception(nullptr, aRv, nullptr, nullptr, nullptr),
DOMException::DOMException(nsresult aRv, const nsACString& aMessage,
const nsACString& aName, uint16_t aCode)
: Exception(EmptyCString(), aRv, EmptyCString(), nullptr, nullptr),
mName(aName),
mMessage(aMessage),
mCode(aCode)
@ -647,9 +608,9 @@ DOMException::GetCode(uint16_t* aCode)
}
NS_IMETHODIMP
DOMException::ToString(char **aReturn)
DOMException::ToString(nsACString& aReturn)
{
*aReturn = nullptr;
aReturn.Truncate();
static const char defaultMsg[] = "<no message>";
static const char defaultLocation[] = "<unknown>";
@ -660,9 +621,8 @@ DOMException::ToString(char **aReturn)
nsAutoCString location;
if (mInner) {
nsXPIDLCString filename;
mInner->GetFilename(getter_Copies(filename));
nsCString filename;
mInner->GetFilename(filename);
if (!filename.IsEmpty()) {
uint32_t line_nr = 0;
@ -681,13 +641,13 @@ DOMException::ToString(char **aReturn)
location = defaultLocation;
}
const char* msg = mMessage ? mMessage : defaultMsg;
const char* resultName = mName ? mName : defaultName;
const char* msg = !mMessage.IsEmpty() ? mMessage.get() : defaultMsg;
const char* resultName = !mName.IsEmpty() ? mName.get() : defaultName;
*aReturn = PR_smprintf(format, msg, mCode, mResult, resultName,
location.get());
aReturn.AppendPrintf(format, msg, mCode, mResult, resultName,
location.get());
return *aReturn ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
void
@ -711,10 +671,10 @@ DOMException::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
/* static */already_AddRefed<DOMException>
DOMException::Create(nsresult aRv)
{
const char* name;
const char* message;
nsCString name;
nsCString message;
uint16_t code;
NSResultToNameAndMessage(aRv, &name, &message, &code);
NSResultToNameAndMessage(aRv, name, message, &code);
nsRefPtr<DOMException> inst =
new DOMException(aRv, message, name, code);
return inst.forget();

View File

@ -21,13 +21,14 @@
#include "nsIDOMDOMException.h"
#include "nsWrapperCache.h"
#include "xpcexception.h"
#include "nsString.h"
class nsIStackFrame;
class nsString;
nsresult
NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName,
const char** aMessage,
NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, nsACString& aName,
nsACString& aMessage,
uint16_t* aCode = nullptr);
namespace mozilla {
@ -85,21 +86,21 @@ public:
// XPCOM factory ctor.
Exception();
Exception(const char *aMessage,
Exception(const nsACString& aMessage,
nsresult aResult,
const char *aName,
const nsACString& aName,
nsIStackFrame *aLocation,
nsISupports *aData);
protected:
virtual ~Exception();
char* mMessage;
nsCString mMessage;
nsresult mResult;
char* mName;
nsCString mName;
nsCOMPtr<nsIStackFrame> mLocation;
nsCOMPtr<nsISupports> mData;
char* mFilename;
nsCString mFilename;
int mLineNumber;
nsCOMPtr<nsIException> mInner;
bool mInitialized;
@ -117,14 +118,14 @@ class DOMException : public Exception,
public nsIDOMDOMException
{
public:
DOMException(nsresult aRv, const char* aMessage,
const char* aName, uint16_t aCode);
DOMException(nsresult aRv, const nsACString& aMessage,
const nsACString& aName, uint16_t aCode);
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIDOMDOMEXCEPTION
// nsIException overrides
NS_IMETHOD ToString(char **aReturn) MOZ_OVERRIDE;
NS_IMETHOD ToString(nsACString& aReturn) MOZ_OVERRIDE;
// nsWrapperCache overrides
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
@ -145,9 +146,8 @@ protected:
virtual ~DOMException() {}
// Intentionally shadow the nsXPCException version.
const char* mName;
const char* mMessage;
nsCString mName;
nsCString mMessage;
uint16_t mCode;
};

View File

@ -147,7 +147,8 @@ Throw(JSContext* aCx, nsresult aRv, const char* aMessage)
// If not, use the default.
if (!finalException) {
finalException = new Exception(aMessage, aRv, nullptr, nullptr, nullptr);
finalException = new Exception(nsCString(aMessage), aRv,
EmptyCString(), nullptr, nullptr);
}
MOZ_ASSERT(finalException);
@ -292,16 +293,14 @@ private:
return mLanguage == nsIProgrammingLanguage::JAVASCRIPT;
}
const char* GetFilename();
const char* GetFunname();
int32_t GetLineno();
nsRefPtr<StackDescriptionOwner> mStackDescription;
nsCOMPtr<nsIStackFrame> mCaller;
// Cached values
char* mFilename;
char* mFunname;
nsCString mFilename;
nsCString mFunname;
int32_t mLineno;
uint32_t mLanguage;
@ -315,9 +314,7 @@ private:
JSStackFrame::JSStackFrame(StackDescriptionOwner* aStackDescription,
size_t aIndex)
: mFilename(nullptr),
mFunname(nullptr),
mLineno(0)
: mLineno(0)
{
if (aStackDescription && aIndex < aStackDescription->NumFrames()) {
mStackDescription = aStackDescription;
@ -340,12 +337,6 @@ JSStackFrame::JSStackFrame(StackDescriptionOwner* aStackDescription,
JSStackFrame::~JSStackFrame()
{
if (mFilename) {
nsMemory::Free(mFilename);
}
if (mFunname) {
nsMemory::Free(mFunname);
}
}
NS_IMPL_CYCLE_COLLECTION_2(JSStackFrame, mStackDescription, mCaller)
@ -366,22 +357,22 @@ NS_IMETHODIMP JSStackFrame::GetLanguage(uint32_t* aLanguage)
}
/* readonly attribute string languageName; */
NS_IMETHODIMP JSStackFrame::GetLanguageName(char** aLanguageName)
NS_IMETHODIMP JSStackFrame::GetLanguageName(nsACString& aLanguageName)
{
static const char js[] = "JavaScript";
static const char cpp[] = "C++";
if (IsJSFrame()) {
*aLanguageName = (char*) nsMemory::Clone(js, sizeof(js));
aLanguageName.AssignASCII(js);
} else {
*aLanguageName = (char*) nsMemory::Clone(cpp, sizeof(cpp));
aLanguageName.AssignASCII(cpp);
}
return NS_OK;
}
const char*
JSStackFrame::GetFilename()
/* readonly attribute string filename; */
NS_IMETHODIMP JSStackFrame::GetFilename(nsACString& aFilename)
{
if (!mFilenameInitialized) {
JS::FrameDescription& desc = mStackDescription->FrameAt(mIndex);
@ -393,34 +384,22 @@ JSStackFrame::GetFilename()
JSAutoCompartment ac(cx, desc.script());
const char* filename = JS_GetScriptFilename(cx, desc.script());
if (filename) {
mFilename =
(char*)nsMemory::Clone(filename, sizeof(char)*(strlen(filename)+1));
mFilename.Assign(filename);
}
}
mFilenameInitialized = true;
}
return mFilename;
}
/* readonly attribute string filename; */
NS_IMETHODIMP JSStackFrame::GetFilename(char** aFilename)
{
NS_ENSURE_ARG_POINTER(aFilename);
const char* filename = GetFilename();
if (filename) {
*aFilename = (char*) nsMemory::Clone(filename,
sizeof(char)*(strlen(filename)+1));
if (mFilename.IsEmpty()) {
aFilename.SetIsVoid(true);
} else {
*aFilename = nullptr;
aFilename.Assign(mFilename);
}
return NS_OK;
}
const char*
JSStackFrame::GetFunname()
/* readonly attribute string name; */
NS_IMETHODIMP JSStackFrame::GetName(nsACString& aFunction)
{
if (!mFunnameInitialized) {
JS::FrameDescription& desc = mStackDescription->FrameAt(mIndex);
@ -433,33 +412,19 @@ JSStackFrame::GetFunname()
if (funid) {
size_t length = JS_GetStringEncodingLength(cx, funid);
if (length != size_t(-1)) {
mFunname = static_cast<char *>(nsMemory::Alloc(length + 1));
if (mFunname) {
JS_EncodeStringToBuffer(cx, funid, mFunname, length);
mFunname[length] = '\0';
}
mFunname.SetLength(uint32_t(length));
JS_EncodeStringToBuffer(cx, funid, mFunname.BeginWriting(), length);
}
}
}
mFunnameInitialized = true;
}
return mFunname;
}
/* readonly attribute string name; */
NS_IMETHODIMP JSStackFrame::GetName(char** aFunction)
{
NS_ENSURE_ARG_POINTER(aFunction);
const char* funname = GetFunname();
if (funname) {
*aFunction = (char*) nsMemory::Clone(funname,
sizeof(char)*(strlen(funname)+1));
if (mFunname.IsEmpty()) {
aFunction.SetIsVoid(true);
} else {
*aFunction = nullptr;
aFunction.Assign(mFunname);
}
return NS_OK;
}
@ -482,10 +447,10 @@ NS_IMETHODIMP JSStackFrame::GetLineNumber(int32_t* aLineNumber)
return NS_OK;
}
/* readonly attribute string sourceLine; */
NS_IMETHODIMP JSStackFrame::GetSourceLine(char** aSourceLine)
/* readonly attribute AUTF8String sourceLine; */
NS_IMETHODIMP JSStackFrame::GetSourceLine(nsACString& aSourceLine)
{
*aSourceLine = nullptr;
aSourceLine.Truncate();
return NS_OK;
}
@ -500,26 +465,31 @@ NS_IMETHODIMP JSStackFrame::GetCaller(nsIStackFrame** aCaller)
return NS_OK;
}
/* string toString (); */
NS_IMETHODIMP JSStackFrame::ToString(char** _retval)
/* AUTF8String toString (); */
NS_IMETHODIMP JSStackFrame::ToString(nsACString& _retval)
{
_retval.Truncate();
const char* frametype = IsJSFrame() ? "JS" : "native";
const char* filename = GetFilename();
if (!filename) {
filename = "<unknown filename>";
nsCString filename;
nsresult rv = GetFilename(filename);
NS_ENSURE_SUCCESS(rv, rv);
if (filename.IsEmpty()) {
filename.AssignASCII("<unknown filename>");
}
const char* funname = GetFunname();
if (!funname) {
funname = "<TOP_LEVEL>";
nsCString funname;
rv = GetName(funname);
NS_ENSURE_SUCCESS(rv, rv);
if (funname.IsEmpty()) {
funname.AssignASCII("<TOP_LEVEL>");
}
static const char format[] = "%s frame :: %s :: %s :: line %d";
int len = sizeof(char)*
(strlen(frametype) + strlen(filename) + strlen(funname)) +
sizeof(format) + 3 * sizeof(mLineno);
char* buf = (char*) nsMemory::Alloc(len);
JS_snprintf(buf, len, format, frametype, filename, funname, GetLineno());
*_retval = buf;
_retval.AppendPrintf(format, frametype, filename.get(),
funname.get(), GetLineno());
return NS_OK;
}
@ -550,17 +520,8 @@ JSStackFrame::CreateStackFrameLocation(uint32_t aLanguage,
self->mLanguage = aLanguage;
self->mLineno = aLineNumber;
if (aFilename) {
self->mFilename =
(char*)nsMemory::Clone(aFilename, sizeof(char)*(strlen(aFilename)+1));
}
if (aFunctionName) {
self->mFunname =
(char*)nsMemory::Clone(aFunctionName,
sizeof(char)*(strlen(aFunctionName)+1));
}
self->mFilename = aFilename;
self->mFunname = aFunctionName;
self->mCaller = aCaller;

View File

@ -153,7 +153,7 @@ interface nsIXPCComponents_Utils : nsISupports
[implicit_jscontext,optional_argc]
jsval evalInSandbox(in AString source, in jsval sandbox,
[optional] in jsval version,
[optional] in jsval filename,
[optional] in AUTF8String filename,
[optional] in long lineNo);
/*

View File

@ -12,9 +12,9 @@ interface nsIXPCException : nsIException
{
// inherits methods from nsIException
void initialize(in string aMessage,
void initialize(in AUTF8String aMessage,
in nsresult aResult,
in string aName,
in AUTF8String aName,
in nsIStackFrame aLocation,
in nsISupports aData,
in nsIException aInner);

View File

@ -1515,7 +1515,7 @@ AssembleSandboxMemoryReporterName(JSContext *cx, nsCString &sandboxName)
if (frame) {
nsCString location;
int32_t lineNumber = 0;
frame->GetFilename(getter_Copies(location));
frame->GetFilename(location);
frame->GetLineNumber(&lineNumber);
sandboxName.AppendLiteral(" (from: ");
@ -1634,7 +1634,7 @@ ContextHolder::~ContextHolder()
nsresult
xpc::EvalInSandbox(JSContext *cx, HandleObject sandboxArg, const nsAString& source,
const char *filename, int32_t lineNo,
const nsACString& filename, int32_t lineNo,
JSVersion jsVersion, bool returnStringOnly, MutableHandleValue rval)
{
JS_AbortIfWrongThread(JS_GetRuntime(cx));
@ -1653,10 +1653,11 @@ xpc::EvalInSandbox(JSContext *cx, HandleObject sandboxArg, const nsAString& sour
NS_ENSURE_TRUE(prin, NS_ERROR_FAILURE);
nsAutoCString filenameBuf;
if (!filename) {
if (!filename.IsVoid()) {
filenameBuf.Assign(filename);
} else {
// Default to the spec of the principal.
nsJSPrincipals::get(prin)->GetScriptLocation(filenameBuf);
filename = filenameBuf.get();
lineNo = 1;
}
@ -1680,7 +1681,7 @@ xpc::EvalInSandbox(JSContext *cx, HandleObject sandboxArg, const nsAString& sour
JS::CompileOptions options(sandcx);
options.setPrincipals(nsJSPrincipals::get(prin))
.setFileAndLine(filename, lineNo);
.setFileAndLine(filenameBuf.get(), lineNo);
if (jsVersion != JSVERSION_DEFAULT)
options.setVersion(jsVersion);
JS::RootedObject rootedSandbox(sandcx, sandbox);

View File

@ -1861,8 +1861,10 @@ nsXPCComponents_Exception::CallOrConstruct(nsIXPConnectWrappedNative *wrapper,
if (!parser.parse(args))
return ThrowAndFail(NS_ERROR_XPC_BAD_CONVERT_JS, cx, _retval);
nsCOMPtr<nsIException> e = new Exception(parser.eMsg, parser.eResult,
nullptr, parser.eStack,
nsCOMPtr<nsIException> e = new Exception(nsCString(parser.eMsg),
parser.eResult,
EmptyCString(),
parser.eStack,
parser.eData);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
@ -2582,10 +2584,10 @@ nsXPCComponents_Utils::ReportError(HandleValue error, JSContext *cx)
nsXPConnect *xpc = nsXPConnect::XPConnect();
xpc->GetCurrentJSStack(getter_AddRefs(frame));
nsXPIDLCString fileName;
nsCString fileName;
int32_t lineNo = 0;
if (frame) {
frame->GetFilename(getter_Copies(fileName));
frame->GetFilename(fileName);
frame->GetLineNumber(&lineNo);
}
@ -2595,8 +2597,8 @@ nsXPCComponents_Utils::ReportError(HandleValue error, JSContext *cx)
nsresult rv = scripterr->InitWithWindowID(
nsDependentString(static_cast<const char16_t *>(msgchars)),
NS_ConvertUTF8toUTF16(fileName),
EmptyString(), lineNo, 0, 0, "XPConnect JavaScript", innerWindowID);
NS_ConvertUTF8toUTF16(fileName), EmptyString(), lineNo, 0, 0,
"XPConnect JavaScript", innerWindowID);
NS_ENSURE_SUCCESS(rv, NS_OK);
console->LogMessage(scripterr);
@ -2608,7 +2610,7 @@ NS_IMETHODIMP
nsXPCComponents_Utils::EvalInSandbox(const nsAString& source,
HandleValue sandboxVal,
HandleValue version,
HandleValue filenameVal,
const nsACString& filenameArg,
int32_t lineNumber,
JSContext *cx,
uint8_t optionalArgc,
@ -2642,17 +2644,10 @@ nsXPCComponents_Utils::EvalInSandbox(const nsAString& source,
}
// Optional fourth and fifth arguments: filename and line number.
nsXPIDLCString filename;
int32_t lineNo = (optionalArgc >= 3) ? lineNumber : 1;
if (optionalArgc >= 2) {
JSString *filenameStr = ToString(cx, filenameVal);
if (!filenameStr)
return NS_ERROR_INVALID_ARG;
JSAutoByteString filenameBytes;
if (!filenameBytes.encodeLatin1(cx, filenameStr))
return NS_ERROR_INVALID_ARG;
filename = filenameBytes.ptr();
nsCString filename;
if (!filenameArg.IsVoid()) {
filename.Assign(filenameArg);
} else {
// Get the current source info from xpc.
nsresult rv;
@ -2662,12 +2657,12 @@ nsXPCComponents_Utils::EvalInSandbox(const nsAString& source,
nsCOMPtr<nsIStackFrame> frame;
xpc->GetCurrentJSStack(getter_AddRefs(frame));
if (frame) {
frame->GetFilename(getter_Copies(filename));
frame->GetFilename(filename);
frame->GetLineNumber(&lineNo);
}
}
return xpc::EvalInSandbox(cx, sandbox, source, filename.get(), lineNo,
return xpc::EvalInSandbox(cx, sandbox, source, filename, lineNo,
jsVersion, false, retval);
}

View File

@ -1070,7 +1070,6 @@ XPCConvert::ConstructException(nsresult rv, const char* message,
static const char format[] = "\'%s\' when calling method: [%s::%s]";
const char * msg = message;
char* sz = nullptr;
nsXPIDLString xmsg;
nsAutoCString sxmsg;
@ -1084,19 +1083,18 @@ XPCConvert::ConstructException(nsresult rv, const char* message,
if (!msg)
if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &msg) || ! msg)
msg = "<error>";
if (ifaceName && methodName)
msg = sz = JS_smprintf(format, msg, ifaceName, methodName);
nsRefPtr<Exception> e = new Exception(msg, rv, nullptr, nullptr, data);
nsCString msgStr(msg);
if (ifaceName && methodName)
msgStr.AppendPrintf(format, msg, ifaceName, methodName);
nsRefPtr<Exception> e = new Exception(msgStr, rv, EmptyCString(), nullptr, data);
if (cx && jsExceptionPtr) {
e->StowJSVal(*jsExceptionPtr);
}
e.forget(exceptn);
if (sz)
JS_smprintf_free(sz);
return NS_OK;
}

View File

@ -899,11 +899,11 @@ nsXPCWrappedJSClass::CheckForException(XPCCallContext & ccx,
fputs(line, stdout);
fputs(preamble, stdout);
char* text;
if (NS_SUCCEEDED(xpc_exception->ToString(&text)) && text) {
fputs(text, stdout);
nsCString text;
if (NS_SUCCEEDED(xpc_exception->ToString(text)) &&
!text.IsEmpty()) {
fputs(text.get(), stdout);
fputs("\n", stdout);
nsMemory::Free(text);
} else
fputs(cant_get_text, stdout);
fputs(line, stdout);
@ -926,17 +926,13 @@ nsXPCWrappedJSClass::CheckForException(XPCCallContext & ccx,
// try to cook one up.
scriptError = do_CreateInstance(XPC_SCRIPT_ERROR_CONTRACTID);
if (nullptr != scriptError) {
char* exn_string;
rv = xpc_exception->ToString(&exn_string);
nsCString newMessage;
rv = xpc_exception->ToString(newMessage);
if (NS_SUCCEEDED(rv)) {
// use toString on the exception as the message
NS_ConvertASCIItoUTF16 newMessage(exn_string);
nsMemory::Free((void *) exn_string);
// try to get filename, lineno from the first
// stack frame location.
int32_t lineNumber = 0;
nsXPIDLCString sourceName;
nsCString sourceName;
nsCOMPtr<nsIStackFrame> location;
xpc_exception->
@ -946,11 +942,11 @@ nsXPCWrappedJSClass::CheckForException(XPCCallContext & ccx,
location->GetLineNumber(&lineNumber);
// get a filename.
rv = location->GetFilename(getter_Copies(sourceName));
rv = location->GetFilename(sourceName);
}
rv = scriptError->InitWithWindowID(newMessage,
NS_ConvertASCIItoUTF16(sourceName),
rv = scriptError->InitWithWindowID(NS_ConvertUTF8toUTF16(newMessage),
NS_ConvertUTF8toUTF16(sourceName),
EmptyString(),
lineNumber, 0, 0,
"XPConnect JavaScript",

View File

@ -898,9 +898,14 @@ nsXPConnect::EvalInSandboxObject(const nsAString& source, const char *filename,
return NS_ERROR_INVALID_ARG;
RootedObject sandbox(cx, sandboxArg);
return EvalInSandbox(cx, sandbox, source, filename ? filename :
"x-bogus://XPConnect/Sandbox", 1, JSVERSION_DEFAULT,
returnStringOnly, rval);
nsCString filenameStr;
if (filename) {
filenameStr.Assign(filename);
} else {
filenameStr = NS_LITERAL_CSTRING("x-bogus://XPConnect/Sandbox");
}
return EvalInSandbox(cx, sandbox, source, filenameStr, 1,
JSVERSION_DEFAULT, returnStringOnly, rval);
}
/* nsIXPConnectJSObjectHolder getWrappedNativePrototype (in JSContextPtr aJSContext, in JSObjectPtr aScope, in nsIClassInfo aClassInfo); */

View File

@ -3527,7 +3527,7 @@ CreateSandboxObject(JSContext *cx, JS::MutableHandleValue vp, nsISupports *prinO
// result, and cx->exception will be empty.
nsresult
EvalInSandbox(JSContext *cx, JS::HandleObject sandbox, const nsAString& source,
const char *filename, int32_t lineNo,
const nsACString& filename, int32_t lineNo,
JSVersion jsVersion, bool returnStringOnly,
JS::MutableHandleValue rval);

View File

@ -10,35 +10,31 @@
#include "nsISupports.idl"
// XXX - most "string"s in this file should probably move to Unicode
// so may as well use AStrings...
[scriptable, uuid(91d82105-7c62-4f8b-9779-154277c0ee90)]
interface nsIStackFrame : nsISupports
{
// see nsIProgrammingLanguage for list of language consts
readonly attribute uint32_t language;
readonly attribute string languageName;
readonly attribute string filename;
readonly attribute string name;
readonly attribute AUTF8String languageName;
readonly attribute AUTF8String filename;
readonly attribute AUTF8String name;
// Valid line numbers begin at '1'. '0' indicates unknown.
readonly attribute int32_t lineNumber;
readonly attribute string sourceLine;
readonly attribute AUTF8String sourceLine;
readonly attribute nsIStackFrame caller;
string toString();
AUTF8String toString();
};
[scriptable, uuid(F3A8D3B4-C424-4edc-8BF6-8974C983BA78)]
interface nsIException : nsISupports
{
// A custom message set by the thrower.
[binaryname(MessageMoz)] readonly attribute string message;
[binaryname(MessageMoz)] readonly attribute AUTF8String message;
// The nsresult associated with this exception.
readonly attribute nsresult result;
// The name of the error code (ie, a string repr of |result|)
readonly attribute string name;
readonly attribute AUTF8String name;
// Filename location. This is the location that caused the
// error, which may or may not be a source file location.
@ -48,7 +44,7 @@ interface nsIException : nsISupports
// etc.
// null indicates "no data"
readonly attribute string filename;
readonly attribute AUTF8String filename;
// Valid line numbers begin at '1'. '0' indicates unknown.
readonly attribute uint32_t lineNumber;
// Valid column numbers begin at 0.
@ -64,5 +60,5 @@ interface nsIException : nsISupports
readonly attribute nsISupports data;
// A generic formatter - make it suitable to print, etc.
string toString();
AUTF8String toString();
};