Bug 612642 - 'JS base64 code needs to be updated for removal of JS_GetStringBytesZ'. r=jorendorff.

This commit is contained in:
Ben Turner 2010-12-08 14:36:10 -08:00
Родитель 19fcb656b4
Коммит 7a49c258b1
7 изменённых файлов: 83 добавлений и 141 удалений

Просмотреть файл

@ -367,39 +367,8 @@ nsDOMWorkerFunctions::AtoB(JSContext* aCx,
return JS_FALSE;
}
JSString* str = JS_ValueToString(aCx, JS_ARGV(aCx, aVp)[0]);
if (!str) {
NS_ASSERTION(JS_IsExceptionPending(aCx), "Need to set an exception!");
return JS_FALSE;
}
size_t len = JS_GetStringEncodingLength(aCx, str);
if (len == size_t(-1))
return JS_FALSE;
JSUint32 alloc_len = (len + 1) * sizeof(char);
char *buffer = static_cast<char *>(nsMemory::Alloc(alloc_len));
if (!buffer)
return JS_FALSE;
JS_EncodeStringToBuffer(str, buffer, len);
buffer[len] = '\0';
nsDependentCString string(buffer, len);
nsCAutoString result;
if (NS_FAILED(nsXPConnect::Base64Decode(string, result))) {
JS_ReportError(aCx, "Failed to decode base64 string!");
return JS_FALSE;
}
str = JS_NewStringCopyN(aCx, result.get(), result.Length());
if (!str) {
return JS_FALSE;
}
JS_SET_RVAL(aCx, aVp, STRING_TO_JSVAL(str));
return JS_TRUE;
return nsXPConnect::Base64Decode(aCx, JS_ARGV(aCx, aVp)[0],
&JS_RVAL(aCx, aVp));
}
JSBool
@ -419,39 +388,8 @@ nsDOMWorkerFunctions::BtoA(JSContext* aCx,
return JS_FALSE;
}
JSString* str = JS_ValueToString(aCx, JS_ARGV(aCx, aVp)[0]);
if (!str) {
NS_ASSERTION(JS_IsExceptionPending(aCx), "Need to set an exception!");
return JS_FALSE;
}
size_t len = JS_GetStringEncodingLength(aCx, str);
if (len == size_t(-1))
return JS_FALSE;
JSUint32 alloc_len = (len + 1) * sizeof(char);
char *buffer = static_cast<char *>(nsMemory::Alloc(alloc_len));
if (!buffer)
return JS_FALSE;
JS_EncodeStringToBuffer(str, buffer, len);
buffer[len] = '\0';
nsDependentCString string(buffer, len);
nsCAutoString result;
if (NS_FAILED(nsXPConnect::Base64Encode(string, result))) {
JS_ReportError(aCx, "Failed to encode base64 data!");
return JS_FALSE;
}
str = JS_NewStringCopyN(aCx, result.get(), result.Length());
if (!str) {
return JS_FALSE;
}
JS_SET_RVAL(aCx, aVp, STRING_TO_JSVAL(str));
return JS_TRUE;
return nsXPConnect::Base64Encode(aCx, JS_ARGV(aCx, aVp)[0],
&JS_RVAL(aCx, aVp));
}
JSBool

Просмотреть файл

@ -1,7 +1,4 @@
var data = [ -1, 0, 1, 1.5, undefined, true, false ];
// XXXbent window.atob treats |null| as the empty string, whereas worker.atob
// and the js component loader treat it as the string 'null'. Meh.
var data = [ -1, 0, 1, 1.5, null, undefined, true, false ];
var str = "";
for (var i = 0; i < 30; i++) {

Просмотреть файл

@ -221,36 +221,7 @@ Atob(JSContext *cx, uintN argc, jsval *vp)
if (!argc)
return JS_TRUE;
JSString *str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
if (!str)
return JS_FALSE;
size_t len = JS_GetStringEncodingLength(cx, str);
if (len == size_t(-1))
return JS_FALSE;
JSUint32 alloc_len = (len + 1) * sizeof(char);
char *buffer = static_cast<char *>(nsMemory::Alloc(alloc_len));
if (!buffer)
return JS_FALSE;
JS_EncodeStringToBuffer(str, buffer, len);
buffer[len] = '\0';
nsDependentCString string(buffer, JS_GetStringLength(str));
nsCAutoString result;
if (NS_FAILED(nsXPConnect::Base64Decode(string, result))) {
JS_ReportError(cx, "Failed to decode base64 string!");
return JS_FALSE;
}
str = JS_NewStringCopyN(cx, result.get(), result.Length());
if (!str)
return JS_FALSE;
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(str));
return JS_TRUE;
return nsXPConnect::Base64Decode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
}
static JSBool
@ -259,36 +230,7 @@ Btoa(JSContext *cx, uintN argc, jsval *vp)
if (!argc)
return JS_TRUE;
JSString *str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
if (!str)
return JS_FALSE;
size_t len = JS_GetStringEncodingLength(cx, str);
if (len == size_t(-1))
return JS_FALSE;
JSUint32 alloc_len = (len + 1) * sizeof(char);
char *buffer = static_cast<char *>(nsMemory::Alloc(alloc_len));
if (!buffer)
return JS_FALSE;
JS_EncodeStringToBuffer(str, buffer, len);
buffer[len] = '\0';
nsDependentCString data(buffer, len);
nsCAutoString result;
if (NS_FAILED(nsXPConnect::Base64Encode(data, result))) {
JS_ReportError(cx, "Failed to encode base64 data!");
return JS_FALSE;
}
str = JS_NewStringCopyN(cx, result.get(), result.Length());
if (!str)
return JS_FALSE;
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(str));
return JS_TRUE;
return nsXPConnect::Base64Encode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
}
static JSFunctionSpec gGlobalFun[] = {

Просмотреть файл

@ -64,6 +64,8 @@
#include "jsdIDebuggerService.h"
#include "xpcquickstubs.h"
NS_IMPL_THREADSAFE_ISUPPORTS6(nsXPConnect,
nsIXPConnect,
nsISupportsWeakReference,
@ -2615,8 +2617,7 @@ nsXPConnect::GetCaller(JSContext **aJSContext, JSObject **aObj)
// static
nsresult
nsXPConnect::Base64Encode(const nsACString &aBinaryData,
nsACString &aString)
nsXPConnect::Base64Encode(const nsACString &aBinaryData, nsACString &aString)
{
// Check for overflow.
if(aBinaryData.Length() > (PR_UINT32_MAX / 4) * 3)
@ -2645,8 +2646,7 @@ nsXPConnect::Base64Encode(const nsACString &aBinaryData,
// static
nsresult
nsXPConnect::Base64Encode(const nsAString &aString,
nsAString &aBinaryData)
nsXPConnect::Base64Encode(const nsAString &aString, nsAString &aBinaryData)
{
NS_LossyConvertUTF16toASCII string(aString);
nsCAutoString binaryData;
@ -2660,10 +2660,37 @@ nsXPConnect::Base64Encode(const nsAString &aString,
return rv;
}
// static
JSBool
nsXPConnect::Base64Encode(JSContext *cx, jsval val, jsval *out)
{
NS_ASSERTION(cx, "Null context!");
NS_ASSERTION(out, "Null jsval pointer!");
jsval root = val;
xpc_qsACString encodedString(cx, root, &root, xpc_qsACString::eNull,
xpc_qsACString::eStringify);
if(!encodedString.IsValid())
return JS_FALSE;
nsCAutoString result;
if(NS_FAILED(nsXPConnect::Base64Encode(encodedString, result)))
{
JS_ReportError(cx, "Failed to encode base64 data!");
return JS_FALSE;
}
JSString *str = JS_NewStringCopyN(cx, result.get(), result.Length());
if (!str)
return JS_FALSE;
*out = STRING_TO_JSVAL(str);
return JS_TRUE;
}
// static
nsresult
nsXPConnect::Base64Decode(const nsACString &aString,
nsACString &aBinaryData)
nsXPConnect::Base64Decode(const nsACString &aString, nsACString &aBinaryData)
{
// Check for overflow.
if(aString.Length() > PR_UINT32_MAX / 3)
@ -2700,8 +2727,7 @@ nsXPConnect::Base64Decode(const nsACString &aString,
// static
nsresult
nsXPConnect::Base64Decode(const nsAString &aBinaryData,
nsAString &aString)
nsXPConnect::Base64Decode(const nsAString &aBinaryData, nsAString &aString)
{
NS_LossyConvertUTF16toASCII binaryData(aBinaryData);
nsCAutoString string;
@ -2715,6 +2741,34 @@ nsXPConnect::Base64Decode(const nsAString &aBinaryData,
return rv;
}
// static
JSBool
nsXPConnect::Base64Decode(JSContext *cx, jsval val, jsval *out)
{
NS_ASSERTION(cx, "Null context!");
NS_ASSERTION(out, "Null jsval pointer!");
jsval root = val;
xpc_qsACString encodedString(cx, root, &root, xpc_qsACString::eNull,
xpc_qsACString::eNull);
if(!encodedString.IsValid())
return JS_FALSE;
nsCAutoString result;
if(NS_FAILED(nsXPConnect::Base64Decode(encodedString, result)))
{
JS_ReportError(cx, "Failed to decode base64 string!");
return JS_FALSE;
}
JSString *str = JS_NewStringCopyN(cx, result.get(), result.Length());
if(!str)
return JS_FALSE;
*out = STRING_TO_JSVAL(str);
return JS_TRUE;
}
NS_IMETHODIMP
nsXPConnect::SetDebugModeWhenPossible(PRBool mode)
{

Просмотреть файл

@ -570,12 +570,18 @@ public:
static nsresult Base64Encode(const nsAString &aString,
nsAString &aBinaryData);
// If this returns JS_FALSE then an exception will be set on cx.
static JSBool Base64Encode(JSContext *cx, jsval val, jsval *out);
static nsresult Base64Decode(const nsACString &aBinaryData,
nsACString &aString);
static nsresult Base64Decode(const nsAString &aBinaryData,
nsAString &aString);
// If this returns JS_FALSE then an exception will be set on cx.
static JSBool Base64Decode(JSContext *cx, jsval val, jsval *out);
// nsCycleCollectionParticipant
NS_IMETHOD RootAndUnlinkJSObjects(void *p);
NS_IMETHOD Unlink(void *p);

Просмотреть файл

@ -709,11 +709,14 @@ xpc_qsDOMString::xpc_qsDOMString(JSContext *cx, jsval v, jsval *pval,
mValid = JS_TRUE;
}
xpc_qsACString::xpc_qsACString(JSContext *cx, jsval v, jsval *pval)
xpc_qsACString::xpc_qsACString(JSContext *cx, jsval v, jsval *pval,
StringificationBehavior nullBehavior,
StringificationBehavior undefinedBehavior)
{
typedef implementation_type::char_traits traits;
// From the T_CSTRING case in XPCConvert::JSData2Native.
JSString *s = InitOrStringify<traits>(cx, v, pval, eNull, eNull);
JSString *s = InitOrStringify<traits>(cx, v, pval, nullBehavior,
undefinedBehavior);
if (!s)
return;

Просмотреть файл

@ -439,7 +439,9 @@ public:
class xpc_qsACString : public xpc_qsBasicString<nsACString, nsCString>
{
public:
xpc_qsACString(JSContext *cx, jsval v, jsval *pval);
xpc_qsACString(JSContext *cx, jsval v, jsval *pval,
StringificationBehavior nullBehavior = eNull,
StringificationBehavior undefinedBehavior = eNull);
};
/**