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
39 def ToCAsciiArray(lines):
44 result.append(str(value))
45 return ", ".join(result)
51 result.append(str(ord(chr)))
52 return ", ".join(result)
55 def RemoveCommentsAndTrailingWhitespace(lines):
56 lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments
57 lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
58 lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace
62 def ReadFile(filename):
63 file = open(filename, "rt")
71 def ReadLines(filename):
73 for line in open(filename, "rt"):
75 line = line[:line.index('#')]
82 def LoadConfigFrom(name):
84 config = ConfigParser.ConfigParser()
89 def ParseValue(string):
90 string = string.strip()
91 if string.startswith('[') and string.endswith(']'):
92 return string.lstrip('[').rstrip(']').split()
97 EVAL_PATTERN = re.compile(r'\beval\s*\(')
98 WITH_PATTERN = re.compile(r'\bwith\s*\(')
101 def Validate(lines, file):
102 lines = RemoveCommentsAndTrailingWhitespace(lines)
103 # Because of simplified context setup, eval and with is not
104 # allowed in the natives files.
105 eval_match = EVAL_PATTERN.search(lines)
107 raise ("Eval disallowed in natives: %s" % file)
108 with_match = WITH_PATTERN.search(lines)
110 raise ("With statements disallowed in natives: %s" % file)
113 def ExpandConstants(lines, constants):
114 for key, value in constants:
115 lines = key.sub(str(value), lines)
119 def ExpandMacroDefinition(lines, pos, name_pattern, macro, expander):
120 pattern_match = name_pattern.search(lines, pos)
121 while pattern_match is not None:
122 # Scan over the arguments
124 start = pattern_match.start()
125 end = pattern_match.end()
126 assert lines[end - 1] == '('
128 arg_index = [0] # Wrap state into array, to work around Python "scoping"
131 # Remember to expand recursively in the arguments
132 replacement = expander(str.strip())
133 mapping[macro.args[arg_index[0]]] = replacement
135 while end < len(lines) and height > 0:
136 # We don't count commas at higher nesting levels.
137 if lines[end] == ',' and height == 1:
138 add_arg(lines[last_match:end])
140 elif lines[end] in ['(', '{', '[']:
142 elif lines[end] in [')', '}', ']']:
145 # Remember to add the last match.
146 add_arg(lines[last_match:end-1])
147 result = macro.expand(mapping)
148 # Replace the occurrence of the macro with the expansion
149 lines = lines[:start] + result + lines[end:]
150 pattern_match = name_pattern.search(lines, start + len(result))
153 def ExpandMacros(lines, macros):
154 # We allow macros to depend on the previously declared macros, but
155 # we don't allow self-dependecies or recursion.
156 for name_pattern, macro in reversed(macros):
158 return ExpandMacros(s, macros)
159 lines = ExpandMacroDefinition(lines, 0, name_pattern, macro, expander)
163 def __init__(self, args, body):
166 def expand(self, mapping):
168 for key, value in mapping.items():
169 result = result.replace(key, value)
173 def __init__(self, args, fun):
176 def expand(self, mapping):
178 for arg in self.args:
179 args.append(mapping[arg])
180 return str(self.fun(*args))
182 CONST_PATTERN = re.compile(r'^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$')
183 MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
184 PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
187 def ReadMacros(lines):
191 hash = line.find('#')
192 if hash != -1: line = line[:hash]
194 if len(line) is 0: continue
195 const_match = CONST_PATTERN.match(line)
197 name = const_match.group(1)
198 value = const_match.group(2).strip()
199 constants.append((re.compile("\\b%s\\b" % name), value))
201 macro_match = MACRO_PATTERN.match(line)
203 name = macro_match.group(1)
204 args = [match.strip() for match in macro_match.group(2).split(',')]
205 body = macro_match.group(3).strip()
206 macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
208 python_match = PYTHON_MACRO_PATTERN.match(line)
210 name = python_match.group(1)
211 args = [match.strip() for match in python_match.group(2).split(',')]
212 body = python_match.group(3).strip()
213 fun = eval("lambda " + ",".join(args) + ': ' + body)
214 macros.append((re.compile("\\b%s\\(" % name), PythonMacro(args, fun)))
216 raise ("Illegal line: " + line)
217 return (constants, macros)
219 INLINE_MACRO_PATTERN = re.compile(r'macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*\n')
220 INLINE_MACRO_END_PATTERN = re.compile(r'endmacro\s*\n')
222 def ExpandInlineMacros(lines, filename):
225 macro_match = INLINE_MACRO_PATTERN.search(lines, pos)
226 if macro_match is None:
229 name = macro_match.group(1)
230 args = [match.strip() for match in macro_match.group(2).split(',')]
231 end_macro_match = INLINE_MACRO_END_PATTERN.search(lines, macro_match.end());
232 if end_macro_match is None:
233 raise ("Macro %s unclosed in %s" % (name, filename))
234 body = lines[macro_match.end():end_macro_match.start()]
236 # remove macro definition
237 lines = lines[:macro_match.start()] + lines[end_macro_match.end():]
238 name_pattern = re.compile("\\b%s\\(" % name)
239 macro = TextMacro(args, body)
241 # advance position to where the macro defintion was
242 pos = macro_match.start()
246 lines = ExpandMacroDefinition(lines, pos, name_pattern, macro, non_expander)
248 HEADER_TEMPLATE = """\
249 // Copyright 2011 Google Inc. All Rights Reserved.
251 // This file was generated from .js source files by GYP. If you
252 // want to make changes to this file you should either change the
253 // javascript source files or the GYP script.
262 static const byte sources[] = { %(sources_data)s };
264 %(raw_sources_declaration)s\
267 int NativesCollection<%(type)s>::GetBuiltinsCount() {
268 return %(builtin_count)i;
272 int NativesCollection<%(type)s>::GetDebuggerCount() {
273 return %(debugger_count)i;
277 int NativesCollection<%(type)s>::GetIndex(const char* name) {
283 int NativesCollection<%(type)s>::GetRawScriptsSize() {
284 return %(raw_total_length)i;
288 Vector<const char> NativesCollection<%(type)s>::GetRawScriptSource(int index) {
289 %(get_raw_script_source_cases)s\
290 return Vector<const char>("", 0);
294 Vector<const char> NativesCollection<%(type)s>::GetScriptName(int index) {
295 %(get_script_name_cases)s\
296 return Vector<const char>("", 0);
300 Vector<const byte> NativesCollection<%(type)s>::GetScriptsSource() {
301 return Vector<const byte>(sources, %(total_length)i);
305 void NativesCollection<%(type)s>::SetRawScriptsSource(Vector<const char> raw_source) {
306 ASSERT(%(raw_total_length)i == raw_source.length());
307 raw_sources = raw_source.start();
315 RAW_SOURCES_COMPRESSION_DECLARATION = """\
316 static const char* raw_sources = NULL;
320 RAW_SOURCES_DECLARATION = """\
321 static const char* raw_sources = reinterpret_cast<const char*>(sources);
325 GET_INDEX_CASE = """\
326 if (strcmp(name, "%(id)s") == 0) return %(i)i;
330 GET_RAW_SCRIPT_SOURCE_CASE = """\
331 if (index == %(i)i) return Vector<const char>(raw_sources + %(offset)i, %(raw_length)i);
335 GET_SCRIPT_NAME_CASE = """\
336 if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
339 def JS2C(source, target, env):
343 # Locate the macros file name.
347 if 'macros.py' == (os.path.split(str(s))[1]):
348 (consts, macros) = ReadMacros(ReadLines(str(s)))
352 minifier = jsmin.JavaScriptMinifier()
356 for module in modules:
357 filename = str(module)
358 debugger = filename.endswith('-debugger.js')
359 lines = ReadFile(filename)
360 lines = ExpandConstants(lines, consts)
361 lines = ExpandInlineMacros(lines, filename)
362 lines = ExpandMacros(lines, macros)
363 lines = RemoveCommentsAndTrailingWhitespace(lines)
364 Validate(lines, filename)
365 lines = minifier.JSMinify(lines)
366 id = (os.path.split(filename)[1])[:-3]
367 if debugger: id = id[:-9]
368 raw_length = len(lines)
370 debugger_ids.append((id, raw_length, module_offset))
372 ids.append((id, raw_length, module_offset))
373 all_sources.append(lines)
374 module_offset += raw_length
375 total_length = raw_total_length = module_offset
377 if env['COMPRESSION'] == 'off':
378 raw_sources_declaration = RAW_SOURCES_DECLARATION
379 sources_data = ToCAsciiArray("".join(all_sources))
381 raw_sources_declaration = RAW_SOURCES_COMPRESSION_DECLARATION
382 if env['COMPRESSION'] == 'bz2':
383 all_sources = bz2.compress("".join(all_sources))
384 total_length = len(all_sources)
385 sources_data = ToCArray(all_sources)
387 # Build debugger support functions
388 get_index_cases = [ ]
389 get_raw_script_source_cases = [ ]
390 get_script_name_cases = [ ]
393 for (id, raw_length, module_offset) in debugger_ids + ids:
394 native_name = "native %s.js" % id
395 get_index_cases.append(GET_INDEX_CASE % { 'id': id, 'i': i })
396 get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % {
397 'offset': module_offset,
398 'raw_length': raw_length,
401 get_script_name_cases.append(GET_SCRIPT_NAME_CASE % {
403 'length': len(native_name),
409 output = open(str(target[0]), "w")
410 output.write(HEADER_TEMPLATE % {
411 'builtin_count': len(ids) + len(debugger_ids),
412 'debugger_count': len(debugger_ids),
413 'sources_data': sources_data,
414 'raw_sources_declaration': raw_sources_declaration,
415 'raw_total_length': raw_total_length,
416 'total_length': total_length,
417 'get_index_cases': "".join(get_index_cases),
418 'get_raw_script_source_cases': "".join(get_raw_script_source_cases),
419 'get_script_name_cases': "".join(get_script_name_cases),
425 natives = sys.argv[1]
427 compression = sys.argv[3]
428 source_files = sys.argv[4:]
429 JS2C(source_files, [natives], { 'TYPE': type, 'COMPRESSION': compression })
431 if __name__ == "__main__":