Bug 847000 - JSMSG_CANT_CONVERT_TO mapped to 35_TO in self-hosted code. r=tschneidereit
authorNorbert Lindenberg <mozilladev@lindenbergsoftware.com>
Fri, 01 Mar 2013 23:57:00 +0100
changeset 130017 b6fe70978ee5011e6b0d3db3daf0e97af76a246c
parent 130016 1d0f2cba284af9bde2a79ff3da0b57a06bc93a73
child 130018 18c3ffc28f4a80eeaa1cb9c9fd020f800de060f6
push idunknown
push userunknown
push dateunknown
reviewerstschneidereit
bugs847000
milestone22.0a1
Bug 847000 - JSMSG_CANT_CONVERT_TO mapped to 35_TO in self-hosted code. r=tschneidereit
js/src/builtin/embedjs.py
--- a/js/src/builtin/embedjs.py
+++ b/js/src/builtin/embedjs.py
@@ -8,36 +8,41 @@
 # It expects error messages in the JS code to be referenced by their C enum
 # keys as literals.
 
 from __future__ import with_statement
 import re, sys, os, js2c, fileinput
 
 def replaceErrorMsgs(source_files, messages_file, output_file):
     messages = buildMessagesTable(messages_file)
+    # For cases where one key is a prefix of another, we need to check
+    # for the longer one first. Using a reverse-sorted key list ensures that.
+    message_keys = messages.keys()
+    message_keys.sort(reverse=True)
     with open(output_file, 'w') as output:
         if len(source_files) == 0:
             return
         for line in fileinput.input(source_files):
-            output.write(replaceMessages(line if line[-1] == '\n' else line + '\n', messages))
+            line = line if line[-1] == '\n' else line + '\n'
+            output.write(replaceMessages(line, messages, message_keys))
 
 def buildMessagesTable(messages_file):
     table = {}
     pattern = re.compile(r"MSG_DEF\(([\w_]+),\s*(\d+)")
     for line in fileinput.input(messages_file):
         match = pattern.match(line)
         if match:
             table[match.group(1)] = match.group(2)
     return table
 
-def replaceMessages(line, messages):
+def replaceMessages(line, messages, message_keys):
     if not 'JSMSG_' in line:
         return line
-    for message_str, message_num in messages.iteritems():
-        line = line.replace(message_str, message_num)
+    for key in message_keys:
+        line = line.replace(key, messages[key])
     return line
 
 def main():
     debug = sys.argv[1] == '-d'
     if debug:
         sys.argv.pop(1)
     output_file = sys.argv[1]
     messages_file = sys.argv[2]