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 ExpandMacros(lines, macros):
120 # We allow macros to depend on the previously declared macros, but
121 # we don't allow self-dependecies or recursion.
122 for name_pattern, macro in reversed(macros):
123 pattern_match = name_pattern.search(lines, 0)
124 while pattern_match is not None:
125 # Scan over the arguments
127 start = pattern_match.start()
128 end = pattern_match.end()
129 assert lines[end - 1] == '('
131 arg_index = [0] # Wrap state into array, to work around Python "scoping"
134 # Remember to expand recursively in the arguments
135 replacement = ExpandMacros(str.strip(), macros)
136 mapping[macro.args[arg_index[0]]] = replacement
138 while end < len(lines) and height > 0:
139 # We don't count commas at higher nesting levels.
140 if lines[end] == ',' and height == 1:
141 add_arg(lines[last_match:end])
143 elif lines[end] in ['(', '{', '[']:
145 elif lines[end] in [')', '}', ']']:
148 # Remember to add the last match.
149 add_arg(lines[last_match:end-1])
150 result = macro.expand(mapping)
151 # Replace the occurrence of the macro with the expansion
152 lines = lines[:start] + result + lines[end:]
153 pattern_match = name_pattern.search(lines, start + len(result))
157 def __init__(self, args, body):
160 def expand(self, mapping):
162 for key, value in mapping.items():
163 result = result.replace(key, value)
167 def __init__(self, args, fun):
170 def expand(self, mapping):
172 for arg in self.args:
173 args.append(mapping[arg])
174 return str(self.fun(*args))
176 CONST_PATTERN = re.compile(r'^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$')
177 MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
178 PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
181 def ReadMacros(lines):
185 hash = line.find('#')
186 if hash != -1: line = line[:hash]
188 if len(line) is 0: continue
189 const_match = CONST_PATTERN.match(line)
191 name = const_match.group(1)
192 value = const_match.group(2).strip()
193 constants.append((re.compile("\\b%s\\b" % name), value))
195 macro_match = MACRO_PATTERN.match(line)
197 name = macro_match.group(1)
198 args = [match.strip() for match in macro_match.group(2).split(',')]
199 body = macro_match.group(3).strip()
200 macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
202 python_match = PYTHON_MACRO_PATTERN.match(line)
204 name = python_match.group(1)
205 args = [match.strip() for match in python_match.group(2).split(',')]
206 body = python_match.group(3).strip()
207 fun = eval("lambda " + ",".join(args) + ': ' + body)
208 macros.append((re.compile("\\b%s\\(" % name), PythonMacro(args, fun)))
210 raise ("Illegal line: " + line)
211 return (constants, macros)
214 HEADER_TEMPLATE = """\
215 // Copyright 2011 Google Inc. All Rights Reserved.
217 // This file was generated from .js source files by GYP. If you
218 // want to make changes to this file you should either change the
219 // javascript source files or the GYP script.
228 static const byte sources[] = { %(sources_data)s };
230 %(raw_sources_declaration)s\
233 int NativesCollection<%(type)s>::GetBuiltinsCount() {
234 return %(builtin_count)i;
238 int NativesCollection<%(type)s>::GetDebuggerCount() {
239 return %(debugger_count)i;
243 int NativesCollection<%(type)s>::GetIndex(const char* name) {
249 int NativesCollection<%(type)s>::GetRawScriptsSize() {
250 return %(raw_total_length)i;
254 Vector<const char> NativesCollection<%(type)s>::GetRawScriptSource(int index) {
255 %(get_raw_script_source_cases)s\
256 return Vector<const char>("", 0);
260 Vector<const char> NativesCollection<%(type)s>::GetScriptName(int index) {
261 %(get_script_name_cases)s\
262 return Vector<const char>("", 0);
266 Vector<const byte> NativesCollection<%(type)s>::GetScriptsSource() {
267 return Vector<const byte>(sources, %(total_length)i);
271 void NativesCollection<%(type)s>::SetRawScriptsSource(Vector<const char> raw_source) {
272 ASSERT(%(raw_total_length)i == raw_source.length());
273 raw_sources = raw_source.start();
281 RAW_SOURCES_COMPRESSION_DECLARATION = """\
282 static const char* raw_sources = NULL;
286 RAW_SOURCES_DECLARATION = """\
287 static const char* raw_sources = reinterpret_cast<const char*>(sources);
291 GET_INDEX_CASE = """\
292 if (strcmp(name, "%(id)s") == 0) return %(i)i;
296 GET_RAW_SCRIPT_SOURCE_CASE = """\
297 if (index == %(i)i) return Vector<const char>(raw_sources + %(offset)i, %(raw_length)i);
301 GET_SCRIPT_NAME_CASE = """\
302 if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
305 def JS2C(source, target, env):
309 # Locate the macros file name.
313 if 'macros.py' == (os.path.split(str(s))[1]):
314 (consts, macros) = ReadMacros(ReadLines(str(s)))
318 minifier = jsmin.JavaScriptMinifier()
322 for module in modules:
323 filename = str(module)
324 debugger = filename.endswith('-debugger.js')
325 lines = ReadFile(filename)
326 lines = ExpandConstants(lines, consts)
327 lines = ExpandMacros(lines, macros)
328 Validate(lines, filename)
329 lines = minifier.JSMinify(lines)
330 id = (os.path.split(filename)[1])[:-3]
331 if debugger: id = id[:-9]
332 raw_length = len(lines)
334 debugger_ids.append((id, raw_length, module_offset))
336 ids.append((id, raw_length, module_offset))
337 all_sources.append(lines)
338 module_offset += raw_length
339 total_length = raw_total_length = module_offset
341 if env['COMPRESSION'] == 'off':
342 raw_sources_declaration = RAW_SOURCES_DECLARATION
343 sources_data = ToCAsciiArray("".join(all_sources))
345 raw_sources_declaration = RAW_SOURCES_COMPRESSION_DECLARATION
346 if env['COMPRESSION'] == 'bz2':
347 all_sources = bz2.compress("".join(all_sources))
348 total_length = len(all_sources)
349 sources_data = ToCArray(all_sources)
351 # Build debugger support functions
352 get_index_cases = [ ]
353 get_raw_script_source_cases = [ ]
354 get_script_name_cases = [ ]
357 for (id, raw_length, module_offset) in debugger_ids + ids:
358 native_name = "native %s.js" % id
359 get_index_cases.append(GET_INDEX_CASE % { 'id': id, 'i': i })
360 get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % {
361 'offset': module_offset,
362 'raw_length': raw_length,
365 get_script_name_cases.append(GET_SCRIPT_NAME_CASE % {
367 'length': len(native_name),
373 output = open(str(target[0]), "w")
374 output.write(HEADER_TEMPLATE % {
375 'builtin_count': len(ids) + len(debugger_ids),
376 'debugger_count': len(debugger_ids),
377 'sources_data': sources_data,
378 'raw_sources_declaration': raw_sources_declaration,
379 'raw_total_length': raw_total_length,
380 'total_length': total_length,
381 'get_index_cases': "".join(get_index_cases),
382 'get_raw_script_source_cases': "".join(get_raw_script_source_cases),
383 'get_script_name_cases': "".join(get_script_name_cases),
389 natives = sys.argv[1]
391 compression = sys.argv[3]
392 source_files = sys.argv[4:]
393 JS2C(source_files, [natives], { 'TYPE': type, 'COMPRESSION': compression })
395 if __name__ == "__main__":