3 # Copyright 2012 the V8 project authors. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 # This is a utility for converting JavaScript source code into C-style
31 # char arrays. It is used for embedded JavaScript code in the V8
34 import os, re, sys, string
41 class Error(Exception):
42 def __init__(self, msg):
43 Exception.__init__(self, msg)
46 def ToCArray(byte_sequence):
48 for chr in byte_sequence:
49 result.append(str(ord(chr)))
50 joined = ", ".join(result)
51 return textwrap.fill(joined, 80)
54 def RemoveCommentsAndTrailingWhitespace(lines):
55 lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments
56 lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
57 lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace
61 def ReadFile(filename):
62 file = open(filename, "rt")
70 EVAL_PATTERN = re.compile(r'\beval\s*\(')
71 WITH_PATTERN = re.compile(r'\bwith\s*\(')
74 # Because of simplified context setup, eval and with is not
75 # allowed in the natives files.
76 if EVAL_PATTERN.search(lines):
77 raise Error("Eval disallowed in natives.")
78 if WITH_PATTERN.search(lines):
79 raise Error("With statements disallowed in natives.")
81 # Pass lines through unchanged.
85 def ExpandConstants(lines, constants):
86 for key, value in constants:
87 lines = key.sub(str(value), lines)
91 def ExpandMacroDefinition(lines, pos, name_pattern, macro, expander):
92 pattern_match = name_pattern.search(lines, pos)
93 while pattern_match is not None:
94 # Scan over the arguments
96 start = pattern_match.start()
97 end = pattern_match.end()
98 assert lines[end - 1] == '('
100 arg_index = [0] # Wrap state into array, to work around Python "scoping"
103 # Remember to expand recursively in the arguments
104 replacement = expander(str.strip())
105 mapping[macro.args[arg_index[0]]] = replacement
107 while end < len(lines) and height > 0:
108 # We don't count commas at higher nesting levels.
109 if lines[end] == ',' and height == 1:
110 add_arg(lines[last_match:end])
112 elif lines[end] in ['(', '{', '[']:
114 elif lines[end] in [')', '}', ']']:
117 # Remember to add the last match.
118 add_arg(lines[last_match:end-1])
119 result = macro.expand(mapping)
120 # Replace the occurrence of the macro with the expansion
121 lines = lines[:start] + result + lines[end:]
122 pattern_match = name_pattern.search(lines, start + len(result))
125 def ExpandMacros(lines, macros):
126 # We allow macros to depend on the previously declared macros, but
127 # we don't allow self-dependecies or recursion.
128 for name_pattern, macro in reversed(macros):
130 return ExpandMacros(s, macros)
131 lines = ExpandMacroDefinition(lines, 0, name_pattern, macro, expander)
135 def __init__(self, args, body):
138 def expand(self, mapping):
140 for key, value in mapping.items():
141 result = result.replace(key, value)
145 def __init__(self, args, fun):
148 def expand(self, mapping):
150 for arg in self.args:
151 args.append(mapping[arg])
152 return str(self.fun(*args))
154 CONST_PATTERN = re.compile(r'^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$')
155 MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
156 PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
159 def ReadMacros(lines):
162 for line in lines.split('\n'):
163 hash = line.find('#')
164 if hash != -1: line = line[:hash]
166 if len(line) is 0: continue
167 const_match = CONST_PATTERN.match(line)
169 name = const_match.group(1)
170 value = const_match.group(2).strip()
171 constants.append((re.compile("\\b%s\\b" % name), value))
173 macro_match = MACRO_PATTERN.match(line)
175 name = macro_match.group(1)
176 args = [match.strip() for match in macro_match.group(2).split(',')]
177 body = macro_match.group(3).strip()
178 macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
180 python_match = PYTHON_MACRO_PATTERN.match(line)
182 name = python_match.group(1)
183 args = [match.strip() for match in python_match.group(2).split(',')]
184 body = python_match.group(3).strip()
185 fun = eval("lambda " + ",".join(args) + ': ' + body)
186 macros.append((re.compile("\\b%s\\(" % name), PythonMacro(args, fun)))
188 raise Error("Illegal line: " + line)
189 return (constants, macros)
191 INLINE_MACRO_PATTERN = re.compile(r'macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*\n')
192 INLINE_MACRO_END_PATTERN = re.compile(r'endmacro\s*\n')
194 def ExpandInlineMacros(lines):
197 macro_match = INLINE_MACRO_PATTERN.search(lines, pos)
198 if macro_match is None:
201 name = macro_match.group(1)
202 args = [match.strip() for match in macro_match.group(2).split(',')]
203 end_macro_match = INLINE_MACRO_END_PATTERN.search(lines, macro_match.end());
204 if end_macro_match is None:
205 raise Error("Macro %s unclosed" % name)
206 body = lines[macro_match.end():end_macro_match.start()]
208 # remove macro definition
209 lines = lines[:macro_match.start()] + lines[end_macro_match.end():]
210 name_pattern = re.compile("\\b%s\\(" % name)
211 macro = TextMacro(args, body)
213 # advance position to where the macro defintion was
214 pos = macro_match.start()
218 lines = ExpandMacroDefinition(lines, pos, name_pattern, macro, non_expander)
221 HEADER_TEMPLATE = """\
222 // Copyright 2011 Google Inc. All Rights Reserved.
224 // This file was generated from .js source files by GYP. If you
225 // want to make changes to this file you should either change the
226 // javascript source files or the GYP script.
235 %(sources_declaration)s\
237 %(raw_sources_declaration)s\
240 int NativesCollection<%(type)s>::GetBuiltinsCount() {
241 return %(builtin_count)i;
245 int NativesCollection<%(type)s>::GetDebuggerCount() {
246 return %(debugger_count)i;
250 int NativesCollection<%(type)s>::GetIndex(const char* name) {
256 int NativesCollection<%(type)s>::GetRawScriptsSize() {
257 return %(raw_total_length)i;
261 Vector<const char> NativesCollection<%(type)s>::GetRawScriptSource(int index) {
262 %(get_raw_script_source_cases)s\
263 return Vector<const char>("", 0);
267 Vector<const char> NativesCollection<%(type)s>::GetScriptName(int index) {
268 %(get_script_name_cases)s\
269 return Vector<const char>("", 0);
273 Vector<const byte> NativesCollection<%(type)s>::GetScriptsSource() {
274 return Vector<const byte>(sources, %(total_length)i);
278 void NativesCollection<%(type)s>::SetRawScriptsSource(Vector<const char> raw_source) {
279 ASSERT(%(raw_total_length)i == raw_source.length());
280 raw_sources = raw_source.start();
287 SOURCES_DECLARATION = """\
288 static const byte sources[] = { %s };
292 RAW_SOURCES_COMPRESSION_DECLARATION = """\
293 static const char* raw_sources = NULL;
297 RAW_SOURCES_DECLARATION = """\
298 static const char* raw_sources = reinterpret_cast<const char*>(sources);
302 GET_INDEX_CASE = """\
303 if (strcmp(name, "%(id)s") == 0) return %(i)i;
307 GET_RAW_SCRIPT_SOURCE_CASE = """\
308 if (index == %(i)i) return Vector<const char>(raw_sources + %(offset)i, %(raw_length)i);
312 GET_SCRIPT_NAME_CASE = """\
313 if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
317 def BuildFilterChain(macro_filename):
318 """Build the chain of filter functions to be applied to the sources.
321 macro_filename: Name of the macro file, if any.
324 A function (string -> string) that reads a source file and processes it.
326 filter_chain = [ReadFile]
329 (consts, macros) = ReadMacros(ReadFile(macro_filename))
330 filter_chain.append(lambda l: ExpandConstants(l, consts))
331 filter_chain.append(lambda l: ExpandInlineMacros(l))
332 filter_chain.append(lambda l: ExpandMacros(l, macros))
334 filter_chain.extend([
335 RemoveCommentsAndTrailingWhitespace,
337 jsmin.JavaScriptMinifier().JSMinify
341 return lambda x: f2(f1(x))
343 return reduce(chain, filter_chain)
350 self.is_debugger_id = []
353 def IsDebuggerFile(filename):
354 return filename.endswith("-debugger.js")
356 def IsMacroFile(filename):
357 return filename.endswith("macros.py")
360 def PrepareSources(source_files):
361 """Read, prepare and assemble the list of source files.
364 sources: List of Javascript-ish source files. A file named macros.py
365 will be treated as a list of macros.
368 An instance of Sources.
371 macro_files = filter(IsMacroFile, source_files)
372 assert len(macro_files) in [0, 1]
374 source_files.remove(macro_files[0])
375 macro_file = macro_files[0]
377 filters = BuildFilterChain(macro_file)
379 # Sort 'debugger' sources first.
380 source_files = sorted(source_files,
381 lambda l,r: IsDebuggerFile(r) - IsDebuggerFile(l))
384 for source in source_files:
386 lines = filters(source)
388 raise Error("In file %s:\n%s" % (source, str(e)))
390 result.modules.append(lines);
392 is_debugger = IsDebuggerFile(source)
393 result.is_debugger_id.append(is_debugger);
395 name = os.path.basename(source)[:-3]
396 result.names.append(name if not is_debugger else name[:-9]);
400 def BuildMetadata(sources, source_bytes, native_type, omit):
401 """Build the meta data required to generate a libaries file.
404 sources: A Sources instance with the prepared sources.
405 source_bytes: A list of source bytes.
406 (The concatenation of all sources; might be compressed.)
407 native_type: The parameter for the NativesCollection template.
408 omit: bool, whether we should omit the sources in the output.
411 A dictionary for use with HEADER_TEMPLATE.
413 total_length = len(source_bytes)
414 raw_sources = "".join(sources.modules)
416 # The sources are expected to be ASCII-only.
417 assert not filter(lambda value: ord(value) >= 128, raw_sources)
419 # Loop over modules and build up indices into the source blob:
421 get_script_name_cases = []
422 get_raw_script_source_cases = []
424 for i in xrange(len(sources.modules)):
425 native_name = "native %s.js" % sources.names[i]
428 "id": sources.names[i],
430 "length": len(native_name),
432 "raw_length": len(sources.modules[i]),
434 get_index_cases.append(GET_INDEX_CASE % d)
435 get_script_name_cases.append(GET_SCRIPT_NAME_CASE % d)
436 get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % d)
437 offset += len(sources.modules[i])
438 assert offset == len(raw_sources)
440 # If we have the raw sources we can declare them accordingly.
441 have_raw_sources = source_bytes == raw_sources and not omit
442 raw_sources_declaration = (RAW_SOURCES_DECLARATION
443 if have_raw_sources else RAW_SOURCES_COMPRESSION_DECLARATION)
446 "builtin_count": len(sources.modules),
447 "debugger_count": sum(sources.is_debugger_id),
448 "sources_declaration": SOURCES_DECLARATION % ToCArray(source_bytes),
449 "sources_data": ToCArray(source_bytes) if not omit else "",
450 "raw_sources_declaration": raw_sources_declaration,
451 "raw_total_length": sum(map(len, sources.modules)),
452 "total_length": total_length,
453 "get_index_cases": "".join(get_index_cases),
454 "get_raw_script_source_cases": "".join(get_raw_script_source_cases),
455 "get_script_name_cases": "".join(get_script_name_cases),
461 def CompressMaybe(sources, compression_type):
462 """Take the prepared sources and generate a sequence of bytes.
465 sources: A Sources instance with the prepared sourced.
466 compression_type: string, describing the desired compression.
471 sources_bytes = "".join(sources.modules)
472 if compression_type == "off":
474 elif compression_type == "bz2":
475 return bz2.compress(sources_bytes)
477 raise Error("Unknown compression type %s." % compression_type)
480 def JS2C(source, target, native_type, compression_type, raw_file, omit):
481 sources = PrepareSources(source)
482 sources_bytes = CompressMaybe(sources, compression_type)
483 metadata = BuildMetadata(sources, sources_bytes, native_type, omit)
485 # Optionally emit raw file.
487 output = open(raw_file, "w")
488 output.write(sources_bytes)
491 # Emit resulting source file.
492 output = open(target, "w")
493 output.write(HEADER_TEMPLATE % metadata)
498 parser = optparse.OptionParser()
499 parser.add_option("--raw", action="store",
500 help="file to write the processed sources array to.")
501 parser.add_option("--omit", dest="omit", action="store_true",
502 help="Omit the raw sources from the generated code.")
503 parser.set_usage("""js2c out.cc type compression sources.js ...
504 out.cc: C code to be generated.
505 type: type parameter for NativesCollection template.
506 compression: type of compression used. [off|bz2]
507 sources.js: JS internal sources or macros.py.""")
508 (options, args) = parser.parse_args()
510 JS2C(args[3:], args[0], args[1], args[2], options.raw, options.omit)
513 if __name__ == "__main__":