2008-08-13 09:32:07 +00:00
|
|
|
#!/usr/bin/env python
|
2008-09-03 19:15:48 +00:00
|
|
|
#
|
2012-05-21 10:19:28 +00:00
|
|
|
# Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are
|
|
|
|
# met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above
|
|
|
|
# copyright notice, this list of conditions and the following
|
|
|
|
# disclaimer in the documentation and/or other materials provided
|
|
|
|
# with the distribution.
|
|
|
|
# * Neither the name of Google Inc. nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products derived
|
|
|
|
# from this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
# This is a utility for converting JavaScript source code into C-style
|
|
|
|
# char arrays. It is used for embedded JavaScript code in the V8
|
|
|
|
# library.
|
|
|
|
|
|
|
|
import os, re, sys, string
|
2014-04-10 14:53:32 +00:00
|
|
|
import optparse
|
2009-01-28 10:15:11 +00:00
|
|
|
import jsmin
|
2011-06-06 20:47:30 +00:00
|
|
|
import bz2
|
2014-04-10 14:53:32 +00:00
|
|
|
import textwrap
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
class Error(Exception):
|
|
|
|
def __init__(self, msg):
|
|
|
|
Exception.__init__(self, msg)
|
2011-06-06 20:47:30 +00:00
|
|
|
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
def ToCArray(byte_sequence):
|
2011-06-06 20:47:30 +00:00
|
|
|
result = []
|
2014-04-10 14:53:32 +00:00
|
|
|
for chr in byte_sequence:
|
2011-06-06 20:47:30 +00:00
|
|
|
result.append(str(ord(chr)))
|
2014-04-10 14:53:32 +00:00
|
|
|
joined = ", ".join(result)
|
|
|
|
return textwrap.fill(joined, 80)
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-08-31 15:02:54 +00:00
|
|
|
def RemoveCommentsAndTrailingWhitespace(lines):
|
|
|
|
lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments
|
|
|
|
lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
|
|
|
|
lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def ReadFile(filename):
|
|
|
|
file = open(filename, "rt")
|
|
|
|
try:
|
|
|
|
lines = file.read()
|
|
|
|
finally:
|
|
|
|
file.close()
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
EVAL_PATTERN = re.compile(r'\beval\s*\(')
|
|
|
|
WITH_PATTERN = re.compile(r'\bwith\s*\(')
|
2009-08-31 15:02:54 +00:00
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
def Validate(lines):
|
2009-08-31 15:02:54 +00:00
|
|
|
# Because of simplified context setup, eval and with is not
|
|
|
|
# allowed in the natives files.
|
2014-04-10 14:53:32 +00:00
|
|
|
if EVAL_PATTERN.search(lines):
|
|
|
|
raise Error("Eval disallowed in natives.")
|
|
|
|
if WITH_PATTERN.search(lines):
|
|
|
|
raise Error("With statements disallowed in natives.")
|
|
|
|
|
|
|
|
# Pass lines through unchanged.
|
|
|
|
return lines
|
2009-08-31 15:02:54 +00:00
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def ExpandConstants(lines, constants):
|
2010-07-05 14:28:37 +00:00
|
|
|
for key, value in constants:
|
|
|
|
lines = key.sub(str(value), lines)
|
2008-07-03 15:10:15 +00:00
|
|
|
return lines
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2013-11-05 14:08:03 +00:00
|
|
|
def ExpandMacroDefinition(lines, pos, name_pattern, macro, expander):
|
|
|
|
pattern_match = name_pattern.search(lines, pos)
|
|
|
|
while pattern_match is not None:
|
|
|
|
# Scan over the arguments
|
|
|
|
height = 1
|
|
|
|
start = pattern_match.start()
|
|
|
|
end = pattern_match.end()
|
|
|
|
assert lines[end - 1] == '('
|
|
|
|
last_match = end
|
|
|
|
arg_index = [0] # Wrap state into array, to work around Python "scoping"
|
|
|
|
mapping = { }
|
|
|
|
def add_arg(str):
|
|
|
|
# Remember to expand recursively in the arguments
|
|
|
|
replacement = expander(str.strip())
|
|
|
|
mapping[macro.args[arg_index[0]]] = replacement
|
|
|
|
arg_index[0] += 1
|
|
|
|
while end < len(lines) and height > 0:
|
|
|
|
# We don't count commas at higher nesting levels.
|
|
|
|
if lines[end] == ',' and height == 1:
|
|
|
|
add_arg(lines[last_match:end])
|
|
|
|
last_match = end + 1
|
|
|
|
elif lines[end] in ['(', '{', '[']:
|
|
|
|
height = height + 1
|
|
|
|
elif lines[end] in [')', '}', ']']:
|
|
|
|
height = height - 1
|
|
|
|
end = end + 1
|
|
|
|
# Remember to add the last match.
|
|
|
|
add_arg(lines[last_match:end-1])
|
|
|
|
result = macro.expand(mapping)
|
|
|
|
# Replace the occurrence of the macro with the expansion
|
|
|
|
lines = lines[:start] + result + lines[end:]
|
|
|
|
pattern_match = name_pattern.search(lines, start + len(result))
|
|
|
|
return lines
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def ExpandMacros(lines, macros):
|
2010-07-05 14:28:37 +00:00
|
|
|
# We allow macros to depend on the previously declared macros, but
|
|
|
|
# we don't allow self-dependecies or recursion.
|
|
|
|
for name_pattern, macro in reversed(macros):
|
2013-11-05 14:08:03 +00:00
|
|
|
def expander(s):
|
|
|
|
return ExpandMacros(s, macros)
|
|
|
|
lines = ExpandMacroDefinition(lines, 0, name_pattern, macro, expander)
|
2008-07-03 15:10:15 +00:00
|
|
|
return lines
|
|
|
|
|
|
|
|
class TextMacro:
|
|
|
|
def __init__(self, args, body):
|
|
|
|
self.args = args
|
|
|
|
self.body = body
|
|
|
|
def expand(self, mapping):
|
|
|
|
result = self.body
|
|
|
|
for key, value in mapping.items():
|
|
|
|
result = result.replace(key, value)
|
|
|
|
return result
|
|
|
|
|
|
|
|
class PythonMacro:
|
|
|
|
def __init__(self, args, fun):
|
|
|
|
self.args = args
|
|
|
|
self.fun = fun
|
|
|
|
def expand(self, mapping):
|
|
|
|
args = []
|
|
|
|
for arg in self.args:
|
|
|
|
args.append(mapping[arg])
|
|
|
|
return str(self.fun(*args))
|
|
|
|
|
2009-08-31 15:02:54 +00:00
|
|
|
CONST_PATTERN = re.compile(r'^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$')
|
|
|
|
MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
|
|
|
|
PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-07-05 14:28:37 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def ReadMacros(lines):
|
2010-07-05 14:28:37 +00:00
|
|
|
constants = []
|
|
|
|
macros = []
|
2014-04-10 14:53:32 +00:00
|
|
|
for line in lines.split('\n'):
|
2008-07-03 15:10:15 +00:00
|
|
|
hash = line.find('#')
|
|
|
|
if hash != -1: line = line[:hash]
|
|
|
|
line = line.strip()
|
|
|
|
if len(line) is 0: continue
|
|
|
|
const_match = CONST_PATTERN.match(line)
|
|
|
|
if const_match:
|
|
|
|
name = const_match.group(1)
|
|
|
|
value = const_match.group(2).strip()
|
2010-07-05 14:28:37 +00:00
|
|
|
constants.append((re.compile("\\b%s\\b" % name), value))
|
2008-07-03 15:10:15 +00:00
|
|
|
else:
|
|
|
|
macro_match = MACRO_PATTERN.match(line)
|
|
|
|
if macro_match:
|
|
|
|
name = macro_match.group(1)
|
2012-05-21 10:19:28 +00:00
|
|
|
args = [match.strip() for match in macro_match.group(2).split(',')]
|
2008-07-03 15:10:15 +00:00
|
|
|
body = macro_match.group(3).strip()
|
2010-07-05 14:28:37 +00:00
|
|
|
macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
|
2008-07-03 15:10:15 +00:00
|
|
|
else:
|
|
|
|
python_match = PYTHON_MACRO_PATTERN.match(line)
|
|
|
|
if python_match:
|
|
|
|
name = python_match.group(1)
|
2012-05-21 10:19:28 +00:00
|
|
|
args = [match.strip() for match in python_match.group(2).split(',')]
|
2008-07-03 15:10:15 +00:00
|
|
|
body = python_match.group(3).strip()
|
|
|
|
fun = eval("lambda " + ",".join(args) + ': ' + body)
|
2010-07-05 14:28:37 +00:00
|
|
|
macros.append((re.compile("\\b%s\\(" % name), PythonMacro(args, fun)))
|
2008-07-03 15:10:15 +00:00
|
|
|
else:
|
2014-04-10 14:53:32 +00:00
|
|
|
raise Error("Illegal line: " + line)
|
2008-07-03 15:10:15 +00:00
|
|
|
return (constants, macros)
|
|
|
|
|
2013-11-05 14:08:03 +00:00
|
|
|
INLINE_MACRO_PATTERN = re.compile(r'macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*\n')
|
|
|
|
INLINE_MACRO_END_PATTERN = re.compile(r'endmacro\s*\n')
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
def ExpandInlineMacros(lines):
|
2013-11-05 14:08:03 +00:00
|
|
|
pos = 0
|
|
|
|
while True:
|
|
|
|
macro_match = INLINE_MACRO_PATTERN.search(lines, pos)
|
|
|
|
if macro_match is None:
|
|
|
|
# no more macros
|
|
|
|
return lines
|
|
|
|
name = macro_match.group(1)
|
|
|
|
args = [match.strip() for match in macro_match.group(2).split(',')]
|
|
|
|
end_macro_match = INLINE_MACRO_END_PATTERN.search(lines, macro_match.end());
|
|
|
|
if end_macro_match is None:
|
2014-04-10 14:53:32 +00:00
|
|
|
raise Error("Macro %s unclosed" % name)
|
2013-11-05 14:08:03 +00:00
|
|
|
body = lines[macro_match.end():end_macro_match.start()]
|
|
|
|
|
|
|
|
# remove macro definition
|
|
|
|
lines = lines[:macro_match.start()] + lines[end_macro_match.end():]
|
|
|
|
name_pattern = re.compile("\\b%s\\(" % name)
|
|
|
|
macro = TextMacro(args, body)
|
|
|
|
|
|
|
|
# advance position to where the macro defintion was
|
|
|
|
pos = macro_match.start()
|
|
|
|
|
|
|
|
def non_expander(s):
|
|
|
|
return s
|
|
|
|
lines = ExpandMacroDefinition(lines, pos, name_pattern, macro, non_expander)
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
|
2014-08-06 11:39:39 +00:00
|
|
|
INLINE_CONSTANT_PATTERN = re.compile(r'const\s+([a-zA-Z0-9_]+)\s*=\s*([^;\n]+)[;\n]')
|
|
|
|
|
|
|
|
def ExpandInlineConstants(lines):
|
|
|
|
pos = 0
|
|
|
|
while True:
|
|
|
|
const_match = INLINE_CONSTANT_PATTERN.search(lines, pos)
|
|
|
|
if const_match is None:
|
|
|
|
# no more constants
|
|
|
|
return lines
|
|
|
|
name = const_match.group(1)
|
|
|
|
replacement = const_match.group(2)
|
|
|
|
name_pattern = re.compile("\\b%s\\b" % name)
|
|
|
|
|
|
|
|
# remove constant definition and replace
|
|
|
|
lines = (lines[:const_match.start()] +
|
|
|
|
re.sub(name_pattern, replacement, lines[const_match.end():]))
|
|
|
|
|
|
|
|
# advance position to where the constant defintion was
|
|
|
|
pos = const_match.start()
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
HEADER_TEMPLATE = """\
|
2011-04-15 12:31:03 +00:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-23 08:58:23 +00:00
|
|
|
// This file was generated from .js source files by GYP. If you
|
2008-07-03 15:10:15 +00:00
|
|
|
// want to make changes to this file you should either change the
|
2013-04-23 08:58:23 +00:00
|
|
|
// javascript source files or the GYP script.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
|
|
|
#include "src/natives.h"
|
|
|
|
#include "src/utils.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
%(sources_declaration)s\
|
2011-06-06 20:47:30 +00:00
|
|
|
|
|
|
|
%(raw_sources_declaration)s\
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
|
|
|
int NativesCollection<%(type)s>::GetBuiltinsCount() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return %(builtin_count)i;
|
|
|
|
}
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
2010-03-23 11:40:38 +00:00
|
|
|
int NativesCollection<%(type)s>::GetDebuggerCount() {
|
|
|
|
return %(debugger_count)i;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
|
|
|
int NativesCollection<%(type)s>::GetIndex(const char* name) {
|
2008-07-03 15:10:15 +00:00
|
|
|
%(get_index_cases)s\
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
2011-06-06 20:47:30 +00:00
|
|
|
int NativesCollection<%(type)s>::GetRawScriptsSize() {
|
|
|
|
return %(raw_total_length)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
Vector<const char> NativesCollection<%(type)s>::GetRawScriptSource(int index) {
|
|
|
|
%(get_raw_script_source_cases)s\
|
2008-07-03 15:10:15 +00:00
|
|
|
return Vector<const char>("", 0);
|
|
|
|
}
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
|
|
|
Vector<const char> NativesCollection<%(type)s>::GetScriptName(int index) {
|
2008-07-03 15:10:15 +00:00
|
|
|
%(get_script_name_cases)s\
|
|
|
|
return Vector<const char>("", 0);
|
|
|
|
}
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
template <>
|
|
|
|
Vector<const byte> NativesCollection<%(type)s>::GetScriptsSource() {
|
|
|
|
return Vector<const byte>(sources, %(total_length)i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void NativesCollection<%(type)s>::SetRawScriptsSource(Vector<const char> raw_source) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(%(raw_total_length)i == raw_source.length());
|
2011-06-06 20:47:30 +00:00
|
|
|
raw_sources = raw_source.start();
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} // internal
|
|
|
|
} // v8
|
|
|
|
"""
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
SOURCES_DECLARATION = """\
|
|
|
|
static const byte sources[] = { %s };
|
|
|
|
"""
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
RAW_SOURCES_COMPRESSION_DECLARATION = """\
|
|
|
|
static const char* raw_sources = NULL;
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
RAW_SOURCES_DECLARATION = """\
|
|
|
|
static const char* raw_sources = reinterpret_cast<const char*>(sources);
|
2008-07-03 15:10:15 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
GET_INDEX_CASE = """\
|
2008-07-03 15:10:15 +00:00
|
|
|
if (strcmp(name, "%(id)s") == 0) return %(i)i;
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
GET_RAW_SCRIPT_SOURCE_CASE = """\
|
|
|
|
if (index == %(i)i) return Vector<const char>(raw_sources + %(offset)i, %(raw_length)i);
|
2008-07-03 15:10:15 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
GET_SCRIPT_NAME_CASE = """\
|
2008-07-03 15:10:15 +00:00
|
|
|
if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
|
|
|
|
"""
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
|
|
|
|
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,
|
2014-08-06 11:39:39 +00:00
|
|
|
ExpandInlineConstants,
|
2014-04-10 14:53:32 +00:00
|
|
|
Validate,
|
|
|
|
jsmin.JavaScriptMinifier().JSMinify
|
|
|
|
])
|
|
|
|
|
|
|
|
def chain(f1, f2):
|
|
|
|
return lambda x: f2(f1(x))
|
|
|
|
|
|
|
|
return reduce(chain, filter_chain)
|
|
|
|
|
|
|
|
|
|
|
|
class Sources:
|
|
|
|
def __init__(self):
|
|
|
|
self.names = []
|
|
|
|
self.modules = []
|
|
|
|
self.is_debugger_id = []
|
|
|
|
|
|
|
|
|
|
|
|
def IsDebuggerFile(filename):
|
|
|
|
return filename.endswith("-debugger.js")
|
|
|
|
|
|
|
|
def IsMacroFile(filename):
|
|
|
|
return filename.endswith("macros.py")
|
|
|
|
|
|
|
|
|
|
|
|
def PrepareSources(source_files):
|
|
|
|
"""Read, prepare and assemble the list of source files.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sources: List of Javascript-ish source files. A file named macros.py
|
|
|
|
will be treated as a list of macros.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
An instance of Sources.
|
|
|
|
"""
|
|
|
|
macro_file = None
|
|
|
|
macro_files = filter(IsMacroFile, source_files)
|
|
|
|
assert len(macro_files) in [0, 1]
|
|
|
|
if macro_files:
|
|
|
|
source_files.remove(macro_files[0])
|
|
|
|
macro_file = macro_files[0]
|
|
|
|
|
|
|
|
filters = BuildFilterChain(macro_file)
|
|
|
|
|
|
|
|
# Sort 'debugger' sources first.
|
|
|
|
source_files = sorted(source_files,
|
|
|
|
lambda l,r: IsDebuggerFile(r) - IsDebuggerFile(l))
|
|
|
|
|
|
|
|
result = Sources()
|
|
|
|
for source in source_files:
|
|
|
|
try:
|
|
|
|
lines = filters(source)
|
|
|
|
except Error as e:
|
|
|
|
raise Error("In file %s:\n%s" % (source, str(e)))
|
|
|
|
|
|
|
|
result.modules.append(lines);
|
|
|
|
|
|
|
|
is_debugger = IsDebuggerFile(source)
|
|
|
|
result.is_debugger_id.append(is_debugger);
|
|
|
|
|
|
|
|
name = os.path.basename(source)[:-3]
|
|
|
|
result.names.append(name if not is_debugger else name[:-9]);
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
def BuildMetadata(sources, source_bytes, native_type):
|
2014-04-10 14:53:32 +00:00
|
|
|
"""Build the meta data required to generate a libaries file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sources: A Sources instance with the prepared sources.
|
|
|
|
source_bytes: A list of source bytes.
|
|
|
|
(The concatenation of all sources; might be compressed.)
|
|
|
|
native_type: The parameter for the NativesCollection template.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary for use with HEADER_TEMPLATE.
|
|
|
|
"""
|
|
|
|
total_length = len(source_bytes)
|
|
|
|
raw_sources = "".join(sources.modules)
|
|
|
|
|
|
|
|
# The sources are expected to be ASCII-only.
|
|
|
|
assert not filter(lambda value: ord(value) >= 128, raw_sources)
|
|
|
|
|
|
|
|
# Loop over modules and build up indices into the source blob:
|
|
|
|
get_index_cases = []
|
|
|
|
get_script_name_cases = []
|
|
|
|
get_raw_script_source_cases = []
|
|
|
|
offset = 0
|
|
|
|
for i in xrange(len(sources.modules)):
|
|
|
|
native_name = "native %s.js" % sources.names[i]
|
|
|
|
d = {
|
|
|
|
"i": i,
|
|
|
|
"id": sources.names[i],
|
|
|
|
"name": native_name,
|
|
|
|
"length": len(native_name),
|
|
|
|
"offset": offset,
|
|
|
|
"raw_length": len(sources.modules[i]),
|
|
|
|
}
|
|
|
|
get_index_cases.append(GET_INDEX_CASE % d)
|
|
|
|
get_script_name_cases.append(GET_SCRIPT_NAME_CASE % d)
|
|
|
|
get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % d)
|
|
|
|
offset += len(sources.modules[i])
|
|
|
|
assert offset == len(raw_sources)
|
|
|
|
|
|
|
|
# If we have the raw sources we can declare them accordingly.
|
2014-06-23 13:52:17 +00:00
|
|
|
have_raw_sources = source_bytes == raw_sources
|
2014-04-10 14:53:32 +00:00
|
|
|
raw_sources_declaration = (RAW_SOURCES_DECLARATION
|
|
|
|
if have_raw_sources else RAW_SOURCES_COMPRESSION_DECLARATION)
|
|
|
|
|
|
|
|
metadata = {
|
|
|
|
"builtin_count": len(sources.modules),
|
|
|
|
"debugger_count": sum(sources.is_debugger_id),
|
|
|
|
"sources_declaration": SOURCES_DECLARATION % ToCArray(source_bytes),
|
|
|
|
"raw_sources_declaration": raw_sources_declaration,
|
|
|
|
"raw_total_length": sum(map(len, sources.modules)),
|
|
|
|
"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": native_type,
|
|
|
|
}
|
|
|
|
return metadata
|
|
|
|
|
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
def PutInt(blob_file, value):
|
2014-10-04 10:30:24 +00:00
|
|
|
assert(value >= 0 and value < (1 << 28))
|
|
|
|
if (value < 1 << 6):
|
|
|
|
size = 1
|
|
|
|
elif (value < 1 << 14):
|
|
|
|
size = 2
|
|
|
|
elif (value < 1 << 22):
|
|
|
|
size = 3
|
|
|
|
else:
|
|
|
|
size = 4
|
|
|
|
value_with_length = (value << 2) | (size - 1)
|
2014-06-23 13:52:17 +00:00
|
|
|
|
|
|
|
byte_sequence = bytearray()
|
|
|
|
for i in xrange(size):
|
|
|
|
byte_sequence.append(value_with_length & 255)
|
|
|
|
value_with_length >>= 8;
|
|
|
|
blob_file.write(byte_sequence)
|
|
|
|
|
|
|
|
|
|
|
|
def PutStr(blob_file, value):
|
|
|
|
PutInt(blob_file, len(value));
|
|
|
|
blob_file.write(value);
|
|
|
|
|
|
|
|
|
|
|
|
def WriteStartupBlob(sources, startup_blob):
|
|
|
|
"""Write a startup blob, as expected by V8 Initialize ...
|
|
|
|
TODO(vogelheim): Add proper method name.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sources: A Sources instance with the prepared sources.
|
|
|
|
startup_blob_file: Name of file to write the blob to.
|
|
|
|
"""
|
|
|
|
output = open(startup_blob, "wb")
|
|
|
|
|
|
|
|
debug_sources = sum(sources.is_debugger_id);
|
|
|
|
PutInt(output, debug_sources)
|
|
|
|
for i in xrange(debug_sources):
|
|
|
|
PutStr(output, sources.names[i]);
|
|
|
|
PutStr(output, sources.modules[i]);
|
|
|
|
|
|
|
|
PutInt(output, len(sources.names) - debug_sources)
|
|
|
|
for i in xrange(debug_sources, len(sources.names)):
|
|
|
|
PutStr(output, sources.names[i]);
|
|
|
|
PutStr(output, sources.modules[i]);
|
|
|
|
|
|
|
|
output.close()
|
|
|
|
|
|
|
|
|
2014-11-21 12:45:10 +00:00
|
|
|
def JS2C(source, target, native_type, raw_file, startup_blob):
|
2014-04-10 14:53:32 +00:00
|
|
|
sources = PrepareSources(source)
|
2014-11-21 12:45:10 +00:00
|
|
|
sources_bytes = "".join(sources.modules)
|
2014-06-23 13:52:17 +00:00
|
|
|
metadata = BuildMetadata(sources, sources_bytes, native_type)
|
2014-04-10 14:53:32 +00:00
|
|
|
|
|
|
|
# Optionally emit raw file.
|
|
|
|
if raw_file:
|
|
|
|
output = open(raw_file, "w")
|
|
|
|
output.write(sources_bytes)
|
|
|
|
output.close()
|
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
if startup_blob:
|
|
|
|
WriteStartupBlob(sources, startup_blob);
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
# Emit resulting source file.
|
|
|
|
output = open(target, "w")
|
|
|
|
output.write(HEADER_TEMPLATE % metadata)
|
2008-07-03 15:10:15 +00:00
|
|
|
output.close()
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def main():
|
2014-04-10 14:53:32 +00:00
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option("--raw", action="store",
|
2014-06-23 13:52:17 +00:00
|
|
|
help="file to write the processed sources array to.")
|
|
|
|
parser.add_option("--startup_blob", action="store",
|
|
|
|
help="file to write the startup blob to.")
|
2014-11-21 12:45:10 +00:00
|
|
|
parser.set_usage("""js2c out.cc type sources.js ...
|
2014-04-10 14:53:32 +00:00
|
|
|
out.cc: C code to be generated.
|
|
|
|
type: type parameter for NativesCollection template.
|
|
|
|
sources.js: JS internal sources or macros.py.""")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2014-11-21 12:45:10 +00:00
|
|
|
JS2C(args[2:], args[0], args[1], options.raw, options.startup_blob)
|
2014-04-10 14:53:32 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|