3 # Copyright 2006-2008 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
35 from os.path import dirname
40 sys.path.append(dirname(__file__) + "/../deps/v8/tools");
44 def ToCArray(filename, lines):
50 if chr == "\n" or chr == "\r":
57 print 'non-ascii value ' + filename + ':' + str(row) + ':' + str(col)
60 result.append(str(value))
62 return ", ".join(result)
65 def CompressScript(lines, do_jsmin):
66 # If we're not expecting this code to be user visible, we can run it through
67 # a more aggressive minifier.
69 minifier = JavaScriptMinifier()
70 return minifier.JSMinify(lines)
72 # Remove stuff from the source that we don't want to appear when
73 # people print the source code using Function.prototype.toString().
74 # Note that we could easily compress the scripts mode but don't
75 # since we want it to remain readable.
76 #lines = re.sub('//.*\n', '\n', lines) # end-of-line comments
77 #lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
78 #lines = re.sub('\s+\n+', '\n', lines) # trailing whitespace
82 def ReadFile(filename):
83 file = open(filename, "rt")
91 def ReadLines(filename):
93 for line in open(filename, "rt"):
95 line = line[:line.index('#')]
102 def LoadConfigFrom(name):
104 config = ConfigParser.ConfigParser()
109 def ParseValue(string):
110 string = string.strip()
111 if string.startswith('[') and string.endswith(']'):
112 return string.lstrip('[').rstrip(']').split()
117 def ExpandConstants(lines, constants):
118 for key, value in constants.items():
119 lines = lines.replace(key, str(value))
123 def ExpandMacros(lines, macros):
124 for name, macro in macros.items():
125 start = lines.find(name + '(', 0)
127 # Scan over the arguments
128 assert lines[start + len(name)] == '('
130 end = start + len(name) + 1
135 # Remember to expand recursively in the arguments
136 replacement = ExpandMacros(str.strip(), macros)
137 mapping[macro.args[arg_index]] = 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 start = lines.find(name + '(', end)
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('^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$')
177 MACRO_PATTERN = re.compile('^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
178 PYTHON_MACRO_PATTERN = re.compile('^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
180 def ReadMacros(lines):
184 hash = line.find('#')
185 if hash != -1: line = line[:hash]
187 if len(line) is 0: continue
188 const_match = CONST_PATTERN.match(line)
190 name = const_match.group(1)
191 value = const_match.group(2).strip()
192 constants[name] = value
194 macro_match = MACRO_PATTERN.match(line)
196 name = macro_match.group(1)
197 args = map(string.strip, macro_match.group(2).split(','))
198 body = macro_match.group(3).strip()
199 macros[name] = TextMacro(args, body)
201 python_match = PYTHON_MACRO_PATTERN.match(line)
203 name = python_match.group(1)
204 args = map(string.strip, python_match.group(2).split(','))
205 body = python_match.group(3).strip()
206 fun = eval("lambda " + ",".join(args) + ': ' + body)
207 macros[name] = PythonMacro(args, fun)
209 raise ("Illegal line: " + line)
210 return (constants, macros)
213 HEADER_TEMPLATE = """\
214 #ifndef node_natives_h
215 #define node_natives_h
225 static const struct _native natives[] = {
229 { NULL, NULL } /* sentinel */
238 NATIVE_DECLARATION = """\
239 { "%(id)s", %(id)s_native },
242 SOURCE_DECLARATION = """\
243 const char %(id)s_native[] = { %(data)s };
247 GET_DELAY_INDEX_CASE = """\
248 if (strcmp(name, "%(id)s") == 0) return %(i)i;
252 GET_DELAY_SCRIPT_SOURCE_CASE = """\
253 if (index == %(i)i) return Vector<const char>(%(id)s, %(length)i);
257 GET_DELAY_SCRIPT_NAME_CASE = """\
258 if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
261 def JS2C(source, target):
265 # Locate the macros file name.
270 if 'macros.py' == (os.path.split(str(s))[1]):
271 (consts, macros) = ReadMacros(ReadLines(str(s)))
275 # Build source code lines
277 source_lines_empty = []
282 delay = str(s).endswith('-delay.js')
283 lines = ReadFile(str(s))
284 do_jsmin = lines.find('// jsminify this file, js2c: jsmin') != -1
286 lines = ExpandConstants(lines, consts)
287 lines = ExpandMacros(lines, macros)
288 lines = CompressScript(lines, do_jsmin)
289 data = ToCArray(s, lines)
290 id = (os.path.split(str(s))[1])[:-3]
291 if delay: id = id[:-6]
293 delay_ids.append((id, len(lines)))
295 ids.append((id, len(lines)))
296 source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data })
297 source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 })
298 native_lines.append(NATIVE_DECLARATION % { 'id': id })
300 # Build delay support functions
301 get_index_cases = [ ]
302 get_script_source_cases = [ ]
303 get_script_name_cases = [ ]
306 for (id, length) in delay_ids:
307 native_name = "native %s.js" % id
308 get_index_cases.append(GET_DELAY_INDEX_CASE % { 'id': id, 'i': i })
309 get_script_source_cases.append(GET_DELAY_SCRIPT_SOURCE_CASE % {
314 get_script_name_cases.append(GET_DELAY_SCRIPT_NAME_CASE % {
316 'length': len(native_name),
321 for (id, length) in ids:
322 native_name = "native %s.js" % id
323 get_index_cases.append(GET_DELAY_INDEX_CASE % { 'id': id, 'i': i })
324 get_script_source_cases.append(GET_DELAY_SCRIPT_SOURCE_CASE % {
329 get_script_name_cases.append(GET_DELAY_SCRIPT_NAME_CASE % {
331 'length': len(native_name),
337 output = open(str(target[0]), "w")
338 output.write(HEADER_TEMPLATE % {
339 'builtin_count': len(ids) + len(delay_ids),
340 'delay_count': len(delay_ids),
341 'source_lines': "\n".join(source_lines),
342 'native_lines': "\n".join(native_lines),
343 'get_index_cases': "".join(get_index_cases),
344 'get_script_source_cases': "".join(get_script_source_cases),
345 'get_script_name_cases': "".join(get_script_name_cases)
350 output = open(str(target[1]), "w")
351 output.write(HEADER_TEMPLATE % {
352 'builtin_count': len(ids) + len(delay_ids),
353 'delay_count': len(delay_ids),
354 'source_lines': "\n".join(source_lines_empty),
355 'get_index_cases': "".join(get_index_cases),
356 'get_script_source_cases': "".join(get_script_source_cases),
357 'get_script_name_cases': "".join(get_script_name_cases)
362 natives = sys.argv[1]
363 source_files = sys.argv[2:]
364 JS2C(source_files, [natives])
366 if __name__ == "__main__":