| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2006-2008 the V8 project authors. All rights reserved. | 3 # Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
| 6 # met: | 6 # met: |
| 7 # | 7 # |
| 8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 return ", ".join(result) | 45 return ", ".join(result) |
| 46 | 46 |
| 47 | 47 |
| 48 def RemoveCommentsAndTrailingWhitespace(lines): | 48 def RemoveCommentsAndTrailingWhitespace(lines): |
| 49 lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments | 49 lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments |
| 50 lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments. | 50 lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments. |
| 51 lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace | 51 lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace |
| 52 return lines | 52 return lines |
| 53 | 53 |
| 54 | 54 |
| 55 def CompressScript(lines, do_jsmin): | |
| 56 # If we're not expecting this code to be user visible, we can run it through | |
| 57 # a more aggressive minifier. | |
| 58 if do_jsmin: | |
| 59 return jsmin.jsmin(lines) | |
| 60 | |
| 61 # Remove stuff from the source that we don't want to appear when | |
| 62 # people print the source code using Function.prototype.toString(). | |
| 63 # Note that we could easily compress the scripts mode but don't | |
| 64 # since we want it to remain readable. | |
| 65 lines = RemoveCommentsAndTrailingWhitespace(lines) | |
| 66 return lines | |
| 67 | |
| 68 | |
| 69 def ReadFile(filename): | 55 def ReadFile(filename): |
| 70 file = open(filename, "rt") | 56 file = open(filename, "rt") |
| 71 try: | 57 try: |
| 72 lines = file.read() | 58 lines = file.read() |
| 73 finally: | 59 finally: |
| 74 file.close() | 60 file.close() |
| 75 return lines | 61 return lines |
| 76 | 62 |
| 77 | 63 |
| 78 def ReadLines(filename): | 64 def ReadLines(filename): |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 consts = {} | 274 consts = {} |
| 289 macros = {} | 275 macros = {} |
| 290 for s in source: | 276 for s in source: |
| 291 if 'macros.py' == (os.path.split(str(s))[1]): | 277 if 'macros.py' == (os.path.split(str(s))[1]): |
| 292 (consts, macros) = ReadMacros(ReadLines(str(s))) | 278 (consts, macros) = ReadMacros(ReadLines(str(s))) |
| 293 else: | 279 else: |
| 294 modules.append(s) | 280 modules.append(s) |
| 295 | 281 |
| 296 # Build source code lines | 282 # Build source code lines |
| 297 source_lines = [ ] | 283 source_lines = [ ] |
| 284 |
| 285 minifier = jsmin.JavaScriptMinifier() |
| 286 |
| 298 source_lines_empty = [] | 287 source_lines_empty = [] |
| 299 for module in modules: | 288 for module in modules: |
| 300 filename = str(module) | 289 filename = str(module) |
| 301 delay = filename.endswith('-delay.js') | 290 delay = filename.endswith('-delay.js') |
| 302 lines = ReadFile(filename) | 291 lines = ReadFile(filename) |
| 303 do_jsmin = lines.find('// jsminify this file, js2c: jsmin') != -1 | |
| 304 lines = ExpandConstants(lines, consts) | 292 lines = ExpandConstants(lines, consts) |
| 305 lines = ExpandMacros(lines, macros) | 293 lines = ExpandMacros(lines, macros) |
| 306 Validate(lines, filename) | 294 Validate(lines, filename) |
| 307 lines = CompressScript(lines, do_jsmin) | 295 lines = minifier.JSMinify(lines) |
| 308 data = ToCArray(lines) | 296 data = ToCArray(lines) |
| 309 id = (os.path.split(filename)[1])[:-3] | 297 id = (os.path.split(filename)[1])[:-3] |
| 310 if delay: id = id[:-6] | 298 if delay: id = id[:-6] |
| 311 if delay: | 299 if delay: |
| 312 delay_ids.append((id, len(lines))) | 300 delay_ids.append((id, len(lines))) |
| 313 else: | 301 else: |
| 314 ids.append((id, len(lines))) | 302 ids.append((id, len(lines))) |
| 315 source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data }) | 303 source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data }) |
| 316 source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 }) | 304 source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 }) |
| 317 | 305 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 | 367 |
| 380 def main(): | 368 def main(): |
| 381 natives = sys.argv[1] | 369 natives = sys.argv[1] |
| 382 natives_empty = sys.argv[2] | 370 natives_empty = sys.argv[2] |
| 383 type = sys.argv[3] | 371 type = sys.argv[3] |
| 384 source_files = sys.argv[4:] | 372 source_files = sys.argv[4:] |
| 385 JS2C(source_files, [natives, natives_empty], { 'TYPE': type }) | 373 JS2C(source_files, [natives, natives_empty], { 'TYPE': type }) |
| 386 | 374 |
| 387 if __name__ == "__main__": | 375 if __name__ == "__main__": |
| 388 main() | 376 main() |
| OLD | NEW |