blob: 8211ec5880249e1ccaea1c5f5add65da7347801c [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001#!/usr/bin/env python
2#
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
6# met:
7#
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.
17#
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.
29
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
32# library.
33
34import os, re, sys, string
35import jsmin
36
37
38def ToCArray(lines):
39 result = []
40 for chr in lines:
41 value = ord(chr)
42 assert value < 128
43 result.append(str(value))
44 result.append("0")
45 return ", ".join(result)
46
47
48def RemoveCommentsAndTrailingWhitespace(lines):
49 lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments
50 lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
51 lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace
52 return lines
53
54
55def ReadFile(filename):
56 file = open(filename, "rt")
57 try:
58 lines = file.read()
59 finally:
60 file.close()
61 return lines
62
63
64def ReadLines(filename):
65 result = []
66 for line in open(filename, "rt"):
67 if '#' in line:
68 line = line[:line.index('#')]
69 line = line.strip()
70 if len(line) > 0:
71 result.append(line)
72 return result
73
74
75def LoadConfigFrom(name):
76 import ConfigParser
77 config = ConfigParser.ConfigParser()
78 config.read(name)
79 return config
80
81
82def ParseValue(string):
83 string = string.strip()
84 if string.startswith('[') and string.endswith(']'):
85 return string.lstrip('[').rstrip(']').split()
86 else:
87 return string
88
89
90EVAL_PATTERN = re.compile(r'\beval\s*\(');
91WITH_PATTERN = re.compile(r'\bwith\s*\(');
92
93
94def Validate(lines, file):
95 lines = RemoveCommentsAndTrailingWhitespace(lines)
96 # Because of simplified context setup, eval and with is not
97 # allowed in the natives files.
98 eval_match = EVAL_PATTERN.search(lines)
99 if eval_match:
100 raise ("Eval disallowed in natives: %s" % file)
101 with_match = WITH_PATTERN.search(lines)
102 if with_match:
103 raise ("With statements disallowed in natives: %s" % file)
104
105
106def ExpandConstants(lines, constants):
Leon Clarkeac952652010-07-15 11:15:24 +0100107 for key, value in constants:
108 lines = key.sub(str(value), lines)
Steve Blocka7e24c12009-10-30 11:49:00 +0000109 return lines
110
111
112def ExpandMacros(lines, macros):
Leon Clarkeac952652010-07-15 11:15:24 +0100113 # We allow macros to depend on the previously declared macros, but
114 # we don't allow self-dependecies or recursion.
115 for name_pattern, macro in reversed(macros):
116 pattern_match = name_pattern.search(lines, 0)
117 while pattern_match is not None:
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 # Scan over the arguments
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 height = 1
Leon Clarkeac952652010-07-15 11:15:24 +0100120 start = pattern_match.start()
121 end = pattern_match.end()
122 assert lines[end - 1] == '('
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 last_match = end
124 arg_index = 0
125 mapping = { }
126 def add_arg(str):
127 # Remember to expand recursively in the arguments
128 replacement = ExpandMacros(str.strip(), macros)
129 mapping[macro.args[arg_index]] = replacement
130 while end < len(lines) and height > 0:
131 # We don't count commas at higher nesting levels.
132 if lines[end] == ',' and height == 1:
133 add_arg(lines[last_match:end])
134 last_match = end + 1
135 elif lines[end] in ['(', '{', '[']:
136 height = height + 1
137 elif lines[end] in [')', '}', ']']:
138 height = height - 1
139 end = end + 1
140 # Remember to add the last match.
141 add_arg(lines[last_match:end-1])
142 result = macro.expand(mapping)
143 # Replace the occurrence of the macro with the expansion
144 lines = lines[:start] + result + lines[end:]
Leon Clarkeac952652010-07-15 11:15:24 +0100145 pattern_match = name_pattern.search(lines, start + len(result))
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 return lines
147
148class TextMacro:
149 def __init__(self, args, body):
150 self.args = args
151 self.body = body
152 def expand(self, mapping):
153 result = self.body
154 for key, value in mapping.items():
155 result = result.replace(key, value)
156 return result
157
158class PythonMacro:
159 def __init__(self, args, fun):
160 self.args = args
161 self.fun = fun
162 def expand(self, mapping):
163 args = []
164 for arg in self.args:
165 args.append(mapping[arg])
166 return str(self.fun(*args))
167
168CONST_PATTERN = re.compile(r'^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$')
169MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
170PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
171
Leon Clarkeac952652010-07-15 11:15:24 +0100172
Steve Blocka7e24c12009-10-30 11:49:00 +0000173def ReadMacros(lines):
Leon Clarkeac952652010-07-15 11:15:24 +0100174 constants = []
175 macros = []
Steve Blocka7e24c12009-10-30 11:49:00 +0000176 for line in lines:
177 hash = line.find('#')
178 if hash != -1: line = line[:hash]
179 line = line.strip()
180 if len(line) is 0: continue
181 const_match = CONST_PATTERN.match(line)
182 if const_match:
183 name = const_match.group(1)
184 value = const_match.group(2).strip()
Leon Clarkeac952652010-07-15 11:15:24 +0100185 constants.append((re.compile("\\b%s\\b" % name), value))
Steve Blocka7e24c12009-10-30 11:49:00 +0000186 else:
187 macro_match = MACRO_PATTERN.match(line)
188 if macro_match:
189 name = macro_match.group(1)
190 args = map(string.strip, macro_match.group(2).split(','))
191 body = macro_match.group(3).strip()
Leon Clarkeac952652010-07-15 11:15:24 +0100192 macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 else:
194 python_match = PYTHON_MACRO_PATTERN.match(line)
195 if python_match:
196 name = python_match.group(1)
197 args = map(string.strip, python_match.group(2).split(','))
198 body = python_match.group(3).strip()
199 fun = eval("lambda " + ",".join(args) + ': ' + body)
Leon Clarkeac952652010-07-15 11:15:24 +0100200 macros.append((re.compile("\\b%s\\(" % name), PythonMacro(args, fun)))
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 else:
202 raise ("Illegal line: " + line)
203 return (constants, macros)
204
205
206HEADER_TEMPLATE = """\
Ben Murdoch257744e2011-11-30 15:57:28 +0000207// Copyright 2011 Google Inc. All Rights Reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +0000208
209// This file was generated from .js source files by SCons. If you
210// want to make changes to this file you should either change the
211// javascript source files or the SConstruct script.
212
213#include "v8.h"
214#include "natives.h"
215
216namespace v8 {
217namespace internal {
218
219%(source_lines)s\
220
221 template <>
222 int NativesCollection<%(type)s>::GetBuiltinsCount() {
223 return %(builtin_count)i;
224 }
225
226 template <>
Andrei Popescu31002712010-02-23 13:46:05 +0000227 int NativesCollection<%(type)s>::GetDebuggerCount() {
228 return %(debugger_count)i;
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 }
230
231 template <>
232 int NativesCollection<%(type)s>::GetIndex(const char* name) {
233%(get_index_cases)s\
234 return -1;
235 }
236
237 template <>
238 Vector<const char> NativesCollection<%(type)s>::GetScriptSource(int index) {
239%(get_script_source_cases)s\
240 return Vector<const char>("", 0);
241 }
242
243 template <>
244 Vector<const char> NativesCollection<%(type)s>::GetScriptName(int index) {
245%(get_script_name_cases)s\
246 return Vector<const char>("", 0);
247 }
248
249} // internal
250} // v8
251"""
252
253
254SOURCE_DECLARATION = """\
255 static const char %(id)s[] = { %(data)s };
256"""
257
258
Andrei Popescu31002712010-02-23 13:46:05 +0000259GET_DEBUGGER_INDEX_CASE = """\
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 if (strcmp(name, "%(id)s") == 0) return %(i)i;
261"""
262
263
Andrei Popescu31002712010-02-23 13:46:05 +0000264GET_DEBUGGER_SCRIPT_SOURCE_CASE = """\
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 if (index == %(i)i) return Vector<const char>(%(id)s, %(length)i);
266"""
267
268
Andrei Popescu31002712010-02-23 13:46:05 +0000269GET_DEBUGGER_SCRIPT_NAME_CASE = """\
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
271"""
272
273def JS2C(source, target, env):
274 ids = []
Andrei Popescu31002712010-02-23 13:46:05 +0000275 debugger_ids = []
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 modules = []
277 # Locate the macros file name.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100278 consts = []
279 macros = []
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 for s in source:
281 if 'macros.py' == (os.path.split(str(s))[1]):
282 (consts, macros) = ReadMacros(ReadLines(str(s)))
283 else:
284 modules.append(s)
285
286 # Build source code lines
287 source_lines = [ ]
288
289 minifier = jsmin.JavaScriptMinifier()
290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 for module in modules:
292 filename = str(module)
Andrei Popescu31002712010-02-23 13:46:05 +0000293 debugger = filename.endswith('-debugger.js')
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 lines = ReadFile(filename)
295 lines = ExpandConstants(lines, consts)
296 lines = ExpandMacros(lines, macros)
297 Validate(lines, filename)
298 lines = minifier.JSMinify(lines)
299 data = ToCArray(lines)
300 id = (os.path.split(filename)[1])[:-3]
Andrei Popescu31002712010-02-23 13:46:05 +0000301 if debugger: id = id[:-9]
302 if debugger:
303 debugger_ids.append((id, len(lines)))
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 else:
305 ids.append((id, len(lines)))
306 source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data })
Steve Blocka7e24c12009-10-30 11:49:00 +0000307
Andrei Popescu31002712010-02-23 13:46:05 +0000308 # Build debugger support functions
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 get_index_cases = [ ]
310 get_script_source_cases = [ ]
311 get_script_name_cases = [ ]
312
313 i = 0
Andrei Popescu31002712010-02-23 13:46:05 +0000314 for (id, length) in debugger_ids:
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 native_name = "native %s.js" % id
Andrei Popescu31002712010-02-23 13:46:05 +0000316 get_index_cases.append(GET_DEBUGGER_INDEX_CASE % { 'id': id, 'i': i })
317 get_script_source_cases.append(GET_DEBUGGER_SCRIPT_SOURCE_CASE % {
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 'id': id,
319 'length': length,
320 'i': i
321 })
Andrei Popescu31002712010-02-23 13:46:05 +0000322 get_script_name_cases.append(GET_DEBUGGER_SCRIPT_NAME_CASE % {
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 'name': native_name,
324 'length': len(native_name),
325 'i': i
326 });
327 i = i + 1
328
329 for (id, length) in ids:
330 native_name = "native %s.js" % id
Andrei Popescu31002712010-02-23 13:46:05 +0000331 get_index_cases.append(GET_DEBUGGER_INDEX_CASE % { 'id': id, 'i': i })
332 get_script_source_cases.append(GET_DEBUGGER_SCRIPT_SOURCE_CASE % {
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 'id': id,
334 'length': length,
335 'i': i
336 })
Andrei Popescu31002712010-02-23 13:46:05 +0000337 get_script_name_cases.append(GET_DEBUGGER_SCRIPT_NAME_CASE % {
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 'name': native_name,
339 'length': len(native_name),
340 'i': i
341 });
342 i = i + 1
343
344 # Emit result
345 output = open(str(target[0]), "w")
346 output.write(HEADER_TEMPLATE % {
Andrei Popescu31002712010-02-23 13:46:05 +0000347 'builtin_count': len(ids) + len(debugger_ids),
348 'debugger_count': len(debugger_ids),
Steve Blocka7e24c12009-10-30 11:49:00 +0000349 'source_lines': "\n".join(source_lines),
350 'get_index_cases': "".join(get_index_cases),
351 'get_script_source_cases': "".join(get_script_source_cases),
352 'get_script_name_cases': "".join(get_script_name_cases),
353 'type': env['TYPE']
354 })
355 output.close()
356
Steve Blocka7e24c12009-10-30 11:49:00 +0000357def main():
358 natives = sys.argv[1]
Ben Murdoch257744e2011-11-30 15:57:28 +0000359 type = sys.argv[2]
360 source_files = sys.argv[3:]
361 JS2C(source_files, [natives], { 'TYPE': type })
Steve Blocka7e24c12009-10-30 11:49:00 +0000362
363if __name__ == "__main__":
364 main()