Bug 446132 - Allocate nsJSONWriter on the stack, r=sayrer sr=jst

This commit is contained in:
Benjamin Smedberg 2008-08-11 15:06:27 -04:00
parent 2ffbc6aee8
commit 73948e4e37
2 changed files with 9 additions and 14 deletions

View File

@ -87,22 +87,19 @@ nsJSON::Encode(nsAString &aJSON)
// This function should only be called from JS.
nsresult rv;
nsAutoPtr<nsJSONWriter> writer(new nsJSONWriter());
if (!writer)
return NS_ERROR_OUT_OF_MEMORY;
rv = EncodeInternal(writer);
nsJSONWriter writer;
rv = EncodeInternal(&writer);
// FIXME: bug 408838. Get exception types sorted out
if (NS_SUCCEEDED(rv) || rv == NS_ERROR_INVALID_ARG) {
rv = NS_OK;
// if we didn't consume anything, it's not JSON, so return null
if (!writer->DidWrite()) {
if (!writer.DidWrite()) {
aJSON.Truncate();
aJSON.SetIsVoid(PR_TRUE);
} else {
writer->FlushBuffer();
aJSON.Append(writer->mOutputString);
writer.FlushBuffer();
aJSON.Append(writer.mOutputString);
}
}
@ -172,13 +169,11 @@ nsJSON::EncodeToStream(nsIOutputStream *aStream,
NS_ENSURE_SUCCESS(rv, rv);
}
nsAutoPtr<nsJSONWriter> writer(new nsJSONWriter(bufferedStream));
if (!writer)
return NS_ERROR_OUT_OF_MEMORY;
rv = writer->SetCharset(aCharset);
nsJSONWriter writer(bufferedStream);
rv = writer.SetCharset(aCharset);
NS_ENSURE_SUCCESS(rv, rv);
rv = EncodeInternal(writer);
rv = EncodeInternal(&writer);
NS_ENSURE_SUCCESS(rv, rv);
rv = bufferedStream->Flush();

View File

@ -55,7 +55,7 @@ class nsIURI;
#define JSON_MAX_DEPTH 2048
#define JSON_PARSER_BUFSIZE 1024
class nsJSONWriter
class NS_STACK_CLASS nsJSONWriter
{
public:
nsJSONWriter();