Bug 612642 - JS base64 code needs to be updated for removal of JS_GetStringBytesZ. Wallpaper patch for merging.
authorRobert Sayre <sayrer@gmail.com>
Tue, 16 Nov 2010 11:38:33 -0800
changeset 57825 53f4a2fc0abd8ce2f1a16f56d258256f9e9bdce5
parent 57824 98e10bbc80d3373d82b42871d5d8076a94164dc2
child 57826 f1a334334d13dd84a8171cf650af24a15f926c8e
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
bugs612642
milestone2.0b8pre
Bug 612642 - JS base64 code needs to be updated for removal of JS_GetStringBytesZ. Wallpaper patch for merging.
dom/src/threads/nsDOMWorker.cpp
js/src/xpconnect/loader/mozJSComponentLoader.cpp
--- a/dom/src/threads/nsDOMWorker.cpp
+++ b/dom/src/threads/nsDOMWorker.cpp
@@ -368,23 +368,29 @@ nsDOMWorkerFunctions::AtoB(JSContext* aC
   }
 
   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;
   }
 
-  // We want the bytes here, not the jschars.
-  const char* bytes = JS_GetStringBytesZ(aCx, str);
-  if (!bytes) {
-    return JS_FALSE;
-  }
+  size_t len = JS_GetStringEncodingLength(aCx, str);
+  if (len == size_t(-1))
+      return JS_FALSE;
 
-  nsDependentCString string(bytes, JS_GetStringLength(str));
+  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());
@@ -414,23 +420,29 @@ nsDOMWorkerFunctions::BtoA(JSContext* aC
   }
 
   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;
   }
 
-  // We want the bytes here, not the jschars.
-  const char* bytes = JS_GetStringBytesZ(aCx, str);
-  if (!bytes) {
-    return JS_FALSE;
-  }
+  size_t len = JS_GetStringEncodingLength(aCx, str);
+  if (len == size_t(-1))
+      return JS_FALSE;
 
-  nsDependentCString string(bytes, JS_GetStringLength(str));
+  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());
--- a/js/src/xpconnect/loader/mozJSComponentLoader.cpp
+++ b/js/src/xpconnect/loader/mozJSComponentLoader.cpp
@@ -220,21 +220,29 @@ Atob(JSContext *cx, uintN argc, jsval *v
 {
     if (!argc)
         return JS_TRUE;
 
     JSString *str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
     if (!str)
         return JS_FALSE;
 
-    const char* bytes = JS_GetStringBytesZ(cx, str);
-    if (!bytes)
+    size_t len = JS_GetStringEncodingLength(cx, str);
+    if (len == size_t(-1))
         return JS_FALSE;
 
-    nsDependentCString string(bytes, JS_GetStringLength(str));
+    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());
@@ -250,21 +258,29 @@ Btoa(JSContext *cx, uintN argc, jsval *v
 {
     if (!argc)
         return JS_TRUE;
 
     JSString *str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
     if (!str)
         return JS_FALSE;
 
-    const char* bytes = JS_GetStringBytesZ(cx, str);
-    if (!bytes)
+    size_t len = JS_GetStringEncodingLength(cx, str);
+    if (len == size_t(-1))
         return JS_FALSE;
 
-    nsDependentCString data(bytes, JS_GetStringLength(str));
+    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());