Bug 672814 - Part a: Move nsJSRuntime::ParseVersion to nsContentUtils; r=bz
authorMs2ger <ms2ger@gmail.com>
Sat, 04 Aug 2012 09:43:59 +0200
changeset 101332 39877da5d9dd2cdfc13bdac3d8f9c055424716cc
parent 101331 c6ae36a7f8a9282b894e281c3335cd5e9485fc52
child 101333 a246a4b88efb3806a61615c2363877de0ce6f7f2
push id23230
push userMs2ger@gmail.com
push dateSat, 04 Aug 2012 07:45:45 +0000
treeherdermozilla-central@20fc34efd733 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs672814
milestone17.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 672814 - Part a: Move nsJSRuntime::ParseVersion to nsContentUtils; r=bz
content/base/public/nsContentUtils.h
content/base/src/nsContentUtils.cpp
content/base/src/nsScriptLoader.cpp
content/xul/document/src/nsXULContentSink.cpp
dom/base/nsIScriptRuntime.h
dom/base/nsJSEnvironment.cpp
dom/base/nsJSEnvironment.h
--- a/content/base/public/nsContentUtils.h
+++ b/content/base/public/nsContentUtils.h
@@ -2014,16 +2014,22 @@ public:
                                       nsAString& aValue);
 
   /**
    * Returns true if the language name is a version of JavaScript and
    * false otherwise
    */
   static bool IsJavaScriptLanguage(const nsString& aName, PRUint32 *aVerFlags);
 
+  /**
+   * Returns the JSVersion for a string of the form '1.n', n = 0, ..., 8, and
+   * JSVERSION_UNKNOWN for other strings.
+   */
+  static JSVersion ParseJavascriptVersion(const nsAString& aVersionStr);
+
   static void SplitMimeType(const nsAString& aValue, nsString& aType,
                             nsString& aParams);
 
   /**
    * Function checks if the user is idle.
    * 
    * @param aRequestedIdleTimeInMS    The idle observer's requested idle time.
    * @param aUserIsIdle               boolean indicating if the user 
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -840,16 +840,38 @@ nsContentUtils::IsJavaScriptLanguage(con
 
   if (version == JSVERSION_UNKNOWN) {
     return false;
   }
   *aFlags = version;
   return true;
 }
 
+JSVersion
+nsContentUtils::ParseJavascriptVersion(const nsAString& aVersionStr)
+{
+  if (aVersionStr.Length() != 3 || aVersionStr[0] != '1' ||
+      aVersionStr[1] != '.') {
+    return JSVERSION_UNKNOWN;
+  }
+
+  switch (aVersionStr[2]) {
+  case '0': return JSVERSION_1_0;
+  case '1': return JSVERSION_1_1;
+  case '2': return JSVERSION_1_2;
+  case '3': return JSVERSION_1_3;
+  case '4': return JSVERSION_1_4;
+  case '5': return JSVERSION_1_5;
+  case '6': return JSVERSION_1_6;
+  case '7': return JSVERSION_1_7;
+  case '8': return JSVERSION_1_8;
+  default:  return JSVERSION_UNKNOWN;
+  }
+}
+
 void
 nsContentUtils::SplitMimeType(const nsAString& aValue, nsString& aType,
                               nsString& aParams)
 {
   aType.Truncate();
   aParams.Truncate();
   PRInt32 semiIndex = aValue.FindChar(PRUnichar(';'));
   if (-1 != semiIndex) {
--- a/content/base/src/nsScriptLoader.cpp
+++ b/content/base/src/nsScriptLoader.cpp
@@ -389,17 +389,17 @@ nsScriptLoader::ProcessScriptElement(nsI
 
   // If scripts aren't enabled in the current context, there's no
   // point in going on.
   if (!context || !context->GetScriptsEnabled()) {
     return false;
   }
 
   PRUint32 typeID = nsIProgrammingLanguage::JAVASCRIPT;
-  PRUint32 version = 0;
+  JSVersion version = JSVERSION_DEFAULT;
   nsAutoString language, type, src;
   nsresult rv = NS_OK;
 
   // Check the type attribute to determine language and version.
   // If type exists, it trumps the deprecated 'language='
   aElement->GetScriptType(type);
   if (!type.IsEmpty()) {
     nsContentTypeParser parser(type);
@@ -432,71 +432,57 @@ nsScriptLoader::ProcessScriptElement(nsI
     if (!isJavaScript) {
       typeID = nsIProgrammingLanguage::UNKNOWN;
     }
 
     if (typeID != nsIProgrammingLanguage::UNKNOWN) {
       // Get the version string, and ensure the language supports it.
       nsAutoString versionName;
       rv = parser.GetParameter("version", versionName);
-      if (NS_FAILED(rv)) {
-        // no version attribute - version remains 0.
-        if (rv != NS_ERROR_INVALID_ARG)
-          return false;
-      } else {
-        nsCOMPtr<nsIScriptRuntime> runtime;
-        rv = NS_GetJSRuntime(getter_AddRefs(runtime));
-        if (NS_FAILED(rv)) {
-          NS_ERROR("Failed to locate the language with this ID");
-          return false;
-        }
-        rv = runtime->ParseVersion(versionName, &version);
-        if (NS_FAILED(rv)) {
-          NS_WARNING("This script language version is not supported - ignored");
-          typeID = nsIProgrammingLanguage::UNKNOWN;
-        }
+
+      if (NS_SUCCEEDED(rv)) {
+        version = nsContentUtils::ParseJavascriptVersion(versionName);
+      } else if (rv != NS_ERROR_INVALID_ARG) {
+        return false;
       }
     }
 
     // Some js specifics yet to be abstracted.
     if (typeID == nsIProgrammingLanguage::JAVASCRIPT) {
       nsAutoString value;
       rv = parser.GetParameter("e4x", value);
       if (NS_FAILED(rv)) {
         if (rv != NS_ERROR_INVALID_ARG)
           return false;
       } else {
         if (value.Length() == 1 && value[0] == '1')
           // This happens in about 2 web pages. Enable E4X no matter what JS
           // version number was selected.  We do this by turning on the "moar
           // XML" version bit.  This is OK even if version has
           // JSVERSION_UNKNOWN (-1).
-          version = js::VersionSetMoarXML(JSVersion(version), true);
+          version = js::VersionSetMoarXML(version, true);
       }
     }
   } else {
     // no 'type=' element
     // "language" is a deprecated attribute of HTML, so we check it only for
     // HTML script elements.
     if (scriptContent->IsHTML()) {
       scriptContent->GetAttr(kNameSpaceID_None, nsGkAtoms::language, language);
       if (!language.IsEmpty()) {
-        if (nsContentUtils::IsJavaScriptLanguage(language, &version))
-          typeID = nsIProgrammingLanguage::JAVASCRIPT;
-        else
-          typeID = nsIProgrammingLanguage::UNKNOWN;
         // IE, Opera, etc. do not respect language version, so neither should
         // we at this late date in the browser wars saga.  Note that this change
         // affects HTML but not XUL or SVG (but note also that XUL has its own
         // code to check nsContentUtils::IsJavaScriptLanguage -- that's probably
         // a separate bug, one we may not be able to fix short of XUL2).  See
         // bug 255895 (https://bugzilla.mozilla.org/show_bug.cgi?id=255895).
-        NS_ASSERTION(JSVERSION_DEFAULT == 0,
-                     "We rely on all languages having 0 as a version default");
-        version = 0;
+        PRUint32 dummy;
+        if (!nsContentUtils::IsJavaScriptLanguage(language, &dummy)) {
+          return false;
+        }
       }
     }
   }
 
   // If we don't know the language, we don't know how to evaluate
   if (typeID == nsIProgrammingLanguage::UNKNOWN) {
     return false;
   }
--- a/content/xul/document/src/nsXULContentSink.cpp
+++ b/content/xul/document/src/nsXULContentSink.cpp
@@ -893,30 +893,21 @@ XULContentSinkImpl::OpenScript(const PRU
           } else {
               langID = nsIProgrammingLanguage::UNKNOWN;
           }
 
           if (langID != nsIProgrammingLanguage::UNKNOWN) {
             // Get the version string, and ensure the language supports it.
             nsAutoString versionName;
             rv = parser.GetParameter("version", versionName);
-            if (NS_FAILED(rv)) {
-              if (rv != NS_ERROR_INVALID_ARG)
-                return rv;
-              // no version specified - version remains the default.
-            } else {
-              nsCOMPtr<nsIScriptRuntime> runtime;
-              rv = NS_GetJSRuntime(getter_AddRefs(runtime));
-              if (NS_FAILED(rv))
-                return rv;
-              rv = runtime->ParseVersion(versionName, &version);
-              if (NS_FAILED(rv)) {
-                NS_WARNING("This script language version is not supported - ignored");
-                langID = nsIProgrammingLanguage::UNKNOWN;
-              }
+
+            if (NS_SUCCEEDED(rv)) {
+              version = nsContentUtils::ParseJavascriptVersion(versionName);
+            } else if (rv != NS_ERROR_INVALID_ARG) {
+              return rv;
             }
           }
           // Some js specifics yet to be abstracted.
           if (langID == nsIProgrammingLanguage::JAVASCRIPT) {
               // By default scripts in XUL documents have E4X turned on. This
               // is still OK if version is JSVERSION_UNKNOWN (-1),
               version = js::VersionSetMoarXML(JSVersion(version), true);
 
--- a/dom/base/nsIScriptRuntime.h
+++ b/dom/base/nsIScriptRuntime.h
@@ -3,36 +3,29 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsIScriptRuntime_h__
 #define nsIScriptRuntime_h__
 
 #include "nsIScriptContext.h"
 
 #define NS_ISCRIPTRUNTIME_IID \
-{ 0xb146580f, 0x55f7, 0x4d97, \
-  { 0x8a, 0xbb, 0x4a, 0x50, 0xb0, 0xa8, 0x04, 0x97 } }
+{ 0xfa30d7a8, 0x7f0a, 0x437a, \
+  { 0xa1, 0x0c, 0xc2, 0xbe, 0xa3, 0xdb, 0x4f, 0x4b } }
 
 /**
  * A singleton language environment for an application.  Responsible for
  * initializing and cleaning up the global language environment, and a factory
  * for language contexts
  */
 class nsIScriptRuntime : public nsISupports
 {
 public:
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTRUNTIME_IID)
 
-  /* Parses a "version string" for the language into a bit-mask used by
-   * the language implementation.  If the specified version is not supported
-   * an error should be returned.  If the specified version is blank, a default
-   * version should be assumed
-   */
-  virtual nsresult ParseVersion(const nsString &aVersionStr, PRUint32 *verFlags) = 0;
-  
   /* Factory for a new context for this language */
   virtual already_AddRefed<nsIScriptContext> CreateContext() = 0;
   
   /* Memory managment for script objects returned from various
    * nsIScriptContext methods.  These are identical to those in
    * nsIScriptContext, but are useful when a script context is not known.
    */
   virtual nsresult DropScriptObject(void *object) = 0;
--- a/dom/base/nsJSEnvironment.cpp
+++ b/dom/base/nsJSEnvironment.cpp
@@ -3653,39 +3653,16 @@ NS_IMPL_RELEASE(nsJSRuntime)
 
 already_AddRefed<nsIScriptContext>
 nsJSRuntime::CreateContext()
 {
   nsCOMPtr<nsIScriptContext> scriptContext = new nsJSContext(sRuntime);
   return scriptContext.forget();
 }
 
-nsresult
-nsJSRuntime::ParseVersion(const nsString &aVersionStr, PRUint32 *flags)
-{
-    NS_PRECONDITION(flags, "Null flags param?");
-    JSVersion jsVersion = JSVERSION_UNKNOWN;
-    if (aVersionStr.Length() != 3 || aVersionStr[0] != '1' || aVersionStr[1] != '.')
-        jsVersion = JSVERSION_UNKNOWN;
-    else switch (aVersionStr[2]) {
-        case '0': jsVersion = JSVERSION_1_0; break;
-        case '1': jsVersion = JSVERSION_1_1; break;
-        case '2': jsVersion = JSVERSION_1_2; break;
-        case '3': jsVersion = JSVERSION_1_3; break;
-        case '4': jsVersion = JSVERSION_1_4; break;
-        case '5': jsVersion = JSVERSION_1_5; break;
-        case '6': jsVersion = JSVERSION_1_6; break;
-        case '7': jsVersion = JSVERSION_1_7; break;
-        case '8': jsVersion = JSVERSION_1_8; break;
-        default:  jsVersion = JSVERSION_UNKNOWN;
-    }
-    *flags = (PRUint32)jsVersion;
-    return NS_OK;
-}
-
 //static
 void
 nsJSRuntime::Startup()
 {
   // initialize all our statics, so that we can restart XPCOM
   sGCTimer = sFullGCTimer = sCCTimer = nullptr;
   sCCLockedOut = false;
   sCCLockedOutTime = 0;
--- a/dom/base/nsJSEnvironment.h
+++ b/dom/base/nsJSEnvironment.h
@@ -312,18 +312,16 @@ public:
   static JSRuntime *sRuntime;
 
 public:
   // nsISupports
   NS_DECL_ISUPPORTS
 
   virtual already_AddRefed<nsIScriptContext> CreateContext();
 
-  virtual nsresult ParseVersion(const nsString &aVersionStr, PRUint32 *flags);
-
   virtual nsresult DropScriptObject(void *object);
   virtual nsresult HoldScriptObject(void *object);
   
   static void Startup();
   static void Shutdown();
   // Setup all the statics etc - safe to call multiple times after Startup()
   static nsresult Init();
   // Get the NameSpaceManager, creating if necessary