js/src/builtin/embedjs.py
author Gregory Szorc <gps@mozilla.com>
Wed, 28 Jan 2015 13:37:00 -0800
branchCAL180_20121114_RELBRANCH
changeset 171639 31ec81b5d7bbeac985e50bc15742c82752457e7b
parent 108068 2f83e74b38ff702f57ce22df899f531c9b252ec0
child 114788 5f1d0cfe99ace6c6383bd98230ea2ee986471c51
permissions -rw-r--r--
Close old release branch CAL180_20121114_RELBRANCH

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# This utility converts JS files containing self-hosted builtins into a C
# header file that can be embedded into SpiderMonkey.
#
# 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)
    with open(output_file, 'w') as output:
        if len(source_files) == 0:
            return
        for line in fileinput.input(source_files):
            output.write(replaceMessages(line, messages))

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):
    if not 'JSMSG_' in line:
        return line
    for message_str, message_num in messages.iteritems():
        line = line.replace(message_str, message_num)
    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]
    macros_file = sys.argv[3]
    source_files = sys.argv[4:]
    combined_file = 'combined.js'
    replaceErrorMsgs(source_files, messages_file, combined_file)
    js2c.JS2C([combined_file, macros_file], [output_file], { 'TYPE': 'CORE', 'COMPRESSION': 'off', 'DEBUG':debug })
    os.remove(combined_file)

if __name__ == "__main__":
    main()