コード例 #1
0
def BuildFilterChain(macro_filename):
    """Build the chain of filter functions to be applied to the sources.

  Args:
    macro_filename: Name of the macro file, if any.

  Returns:
    A function (string -> string) that reads a source file and processes it.
  """
    filter_chain = [ReadFile]

    if macro_filename:
        (consts, macros) = ReadMacros(ReadFile(macro_filename))
        filter_chain.append(lambda l: ExpandConstants(l, consts))
        filter_chain.append(lambda l: ExpandMacros(l, macros))

    filter_chain.extend([
        RemoveCommentsAndTrailingWhitespace, ExpandInlineMacros,
        ExpandInlineConstants, Validate,
        jsmin.JavaScriptMinifier().JSMinify
    ])

    def chain(f1, f2):
        return lambda x: f2(f1(x))

    return reduce(chain, filter_chain)
コード例 #2
0
def JS2C(source, target, env):
    ids = []
    debugger_ids = []
    modules = []
    # Locate the macros file name.
    consts = []
    macros = []
    for s in source:
        if 'macros.py' == (os.path.split(str(s))[1]):
            (consts, macros) = ReadMacros(ReadLines(str(s)))
        else:
            modules.append(s)

    minifier = jsmin.JavaScriptMinifier()

    module_offset = 0
    all_sources = []
    for module in modules:
        filename = str(module)
        debugger = filename.endswith('-debugger.js')
        lines = ReadFile(filename)
        lines = ExpandConstants(lines, consts)
        lines = ExpandMacros(lines, macros)
        Validate(lines, filename)
        lines = minifier.JSMinify(lines)
        id = (os.path.split(filename)[1])[:-3]
        if debugger: id = id[:-9]
        raw_length = len(lines)
        if debugger:
            debugger_ids.append((id, raw_length, module_offset))
        else:
            ids.append((id, raw_length, module_offset))
        all_sources.append(lines)
        module_offset += raw_length
    total_length = raw_total_length = module_offset

    if env['COMPRESSION'] == 'off':
        raw_sources_declaration = RAW_SOURCES_DECLARATION
        sources_data = ToCAsciiArray("".join(all_sources))
    else:
        raw_sources_declaration = RAW_SOURCES_COMPRESSION_DECLARATION
        if env['COMPRESSION'] == 'bz2':
            all_sources = bz2.compress("".join(all_sources))
        total_length = len(all_sources)
        sources_data = ToCArray(all_sources)

    # Build debugger support functions
    get_index_cases = []
    get_raw_script_source_cases = []
    get_script_name_cases = []

    i = 0
    for (id, raw_length, module_offset) in debugger_ids + ids:
        native_name = "native %s.js" % id
        get_index_cases.append(GET_INDEX_CASE % {'id': id, 'i': i})
        get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % {
            'offset': module_offset,
            'raw_length': raw_length,
            'i': i
        })
        get_script_name_cases.append(GET_SCRIPT_NAME_CASE % {
            'name': native_name,
            'length': len(native_name),
            'i': i
        })
        i = i + 1

    # Emit result
    output = open(str(target[0]), "w")
    output.write(
        HEADER_TEMPLATE % {
            'builtin_count': len(ids) + len(debugger_ids),
            'debugger_count': len(debugger_ids),
            'sources_data': sources_data,
            'raw_sources_declaration': raw_sources_declaration,
            'raw_total_length': raw_total_length,
            'total_length': total_length,
            'get_index_cases': "".join(get_index_cases),
            'get_raw_script_source_cases':
            "".join(get_raw_script_source_cases),
            'get_script_name_cases': "".join(get_script_name_cases),
            'type': env['TYPE']
        })
    output.close()
コード例 #3
0
ファイル: js2c.py プロジェクト: node-lib/v8-1
def JS2C(source, target, env):
    ids = []
    delay_ids = []
    modules = []
    # Locate the macros file name.
    consts = {}
    macros = {}
    for s in source:
        if 'macros.py' == (os.path.split(str(s))[1]):
            (consts, macros) = ReadMacros(ReadLines(str(s)))
        else:
            modules.append(s)

    # Build source code lines
    source_lines = []

    minifier = jsmin.JavaScriptMinifier()

    source_lines_empty = []
    for module in modules:
        filename = str(module)
        delay = filename.endswith('-delay.js')
        lines = ReadFile(filename)
        lines = ExpandConstants(lines, consts)
        lines = ExpandMacros(lines, macros)
        Validate(lines, filename)
        lines = minifier.JSMinify(lines)
        data = ToCArray(lines)
        id = (os.path.split(filename)[1])[:-3]
        if delay: id = id[:-6]
        if delay:
            delay_ids.append((id, len(lines)))
        else:
            ids.append((id, len(lines)))
        source_lines.append(SOURCE_DECLARATION % {'id': id, 'data': data})
        source_lines_empty.append(SOURCE_DECLARATION % {
            'id': id,
            'data': data
        })

    # Build delay support functions
    get_index_cases = []
    get_script_source_cases = []
    get_script_name_cases = []

    i = 0
    for (id, length) in delay_ids:
        native_name = "native %s.js" % id
        get_index_cases.append(GET_DELAY_INDEX_CASE % {'id': id, 'i': i})
        get_script_source_cases.append(GET_DELAY_SCRIPT_SOURCE_CASE % {
            'id': id,
            'length': length,
            'i': i
        })
        get_script_name_cases.append(GET_DELAY_SCRIPT_NAME_CASE % {
            'name': native_name,
            'length': len(native_name),
            'i': i
        })
        i = i + 1

    for (id, length) in ids:
        native_name = "native %s.js" % id
        get_index_cases.append(GET_DELAY_INDEX_CASE % {'id': id, 'i': i})
        get_script_source_cases.append(GET_DELAY_SCRIPT_SOURCE_CASE % {
            'id': id,
            'length': length,
            'i': i
        })
        get_script_name_cases.append(GET_DELAY_SCRIPT_NAME_CASE % {
            'name': native_name,
            'length': len(native_name),
            'i': i
        })
        i = i + 1

    # Emit result
    output = open(str(target[0]), "w")
    output.write(
        HEADER_TEMPLATE % {
            'builtin_count': len(ids) + len(delay_ids),
            'delay_count': len(delay_ids),
            'source_lines': "\n".join(source_lines),
            'get_index_cases': "".join(get_index_cases),
            'get_script_source_cases': "".join(get_script_source_cases),
            'get_script_name_cases': "".join(get_script_name_cases),
            'type': env['TYPE']
        })
    output.close()

    if len(target) > 1:
        output = open(str(target[1]), "w")
        output.write(
            HEADER_TEMPLATE % {
                'builtin_count': len(ids) + len(delay_ids),
                'delay_count': len(delay_ids),
                'source_lines': "\n".join(source_lines_empty),
                'get_index_cases': "".join(get_index_cases),
                'get_script_source_cases': "".join(get_script_source_cases),
                'get_script_name_cases': "".join(get_script_name_cases),
                'type': env['TYPE']
            })
        output.close()