| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2012 the V8 project authors. All rights reserved. | 3 # Copyright 2012 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 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 "raw_total_length": sum(map(len, sources.modules)), | 471 "raw_total_length": sum(map(len, sources.modules)), |
| 472 "total_length": total_length, | 472 "total_length": total_length, |
| 473 "get_index_cases": "".join(get_index_cases), | 473 "get_index_cases": "".join(get_index_cases), |
| 474 "get_raw_script_source_cases": "".join(get_raw_script_source_cases), | 474 "get_raw_script_source_cases": "".join(get_raw_script_source_cases), |
| 475 "get_script_name_cases": "".join(get_script_name_cases), | 475 "get_script_name_cases": "".join(get_script_name_cases), |
| 476 "type": native_type, | 476 "type": native_type, |
| 477 } | 477 } |
| 478 return metadata | 478 return metadata |
| 479 | 479 |
| 480 | 480 |
| 481 def CompressMaybe(sources, compression_type): | |
| 482 """Take the prepared sources and generate a sequence of bytes. | |
| 483 | |
| 484 Args: | |
| 485 sources: A Sources instance with the prepared sourced. | |
| 486 compression_type: string, describing the desired compression. | |
| 487 | |
| 488 Returns: | |
| 489 A sequence of bytes. | |
| 490 """ | |
| 491 sources_bytes = "".join(sources.modules) | |
| 492 if compression_type == "off": | |
| 493 return sources_bytes | |
| 494 elif compression_type == "bz2": | |
| 495 return bz2.compress(sources_bytes) | |
| 496 else: | |
| 497 raise Error("Unknown compression type %s." % compression_type) | |
| 498 | |
| 499 | |
| 500 def PutInt(blob_file, value): | 481 def PutInt(blob_file, value): |
| 501 assert(value >= 0 and value < (1 << 28)) | 482 assert(value >= 0 and value < (1 << 28)) |
| 502 if (value < 1 << 6): | 483 if (value < 1 << 6): |
| 503 size = 1 | 484 size = 1 |
| 504 elif (value < 1 << 14): | 485 elif (value < 1 << 14): |
| 505 size = 2 | 486 size = 2 |
| 506 elif (value < 1 << 22): | 487 elif (value < 1 << 22): |
| 507 size = 3 | 488 size = 3 |
| 508 else: | 489 else: |
| 509 size = 4 | 490 size = 4 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 538 PutStr(output, sources.modules[i]); | 519 PutStr(output, sources.modules[i]); |
| 539 | 520 |
| 540 PutInt(output, len(sources.names) - debug_sources) | 521 PutInt(output, len(sources.names) - debug_sources) |
| 541 for i in xrange(debug_sources, len(sources.names)): | 522 for i in xrange(debug_sources, len(sources.names)): |
| 542 PutStr(output, sources.names[i]); | 523 PutStr(output, sources.names[i]); |
| 543 PutStr(output, sources.modules[i]); | 524 PutStr(output, sources.modules[i]); |
| 544 | 525 |
| 545 output.close() | 526 output.close() |
| 546 | 527 |
| 547 | 528 |
| 548 def JS2C(source, target, native_type, compression_type, raw_file, startup_blob): | 529 def JS2C(source, target, native_type, raw_file, startup_blob): |
| 549 sources = PrepareSources(source) | 530 sources = PrepareSources(source) |
| 550 sources_bytes = CompressMaybe(sources, compression_type) | 531 sources_bytes = "".join(sources.modules) |
| 551 metadata = BuildMetadata(sources, sources_bytes, native_type) | 532 metadata = BuildMetadata(sources, sources_bytes, native_type) |
| 552 | 533 |
| 553 # Optionally emit raw file. | 534 # Optionally emit raw file. |
| 554 if raw_file: | 535 if raw_file: |
| 555 output = open(raw_file, "w") | 536 output = open(raw_file, "w") |
| 556 output.write(sources_bytes) | 537 output.write(sources_bytes) |
| 557 output.close() | 538 output.close() |
| 558 | 539 |
| 559 if startup_blob: | 540 if startup_blob: |
| 560 WriteStartupBlob(sources, startup_blob); | 541 WriteStartupBlob(sources, startup_blob); |
| 561 | 542 |
| 562 # Emit resulting source file. | 543 # Emit resulting source file. |
| 563 output = open(target, "w") | 544 output = open(target, "w") |
| 564 output.write(HEADER_TEMPLATE % metadata) | 545 output.write(HEADER_TEMPLATE % metadata) |
| 565 output.close() | 546 output.close() |
| 566 | 547 |
| 567 | 548 |
| 568 def main(): | 549 def main(): |
| 569 parser = optparse.OptionParser() | 550 parser = optparse.OptionParser() |
| 570 parser.add_option("--raw", action="store", | 551 parser.add_option("--raw", action="store", |
| 571 help="file to write the processed sources array to.") | 552 help="file to write the processed sources array to.") |
| 572 parser.add_option("--startup_blob", action="store", | 553 parser.add_option("--startup_blob", action="store", |
| 573 help="file to write the startup blob to.") | 554 help="file to write the startup blob to.") |
| 574 parser.set_usage("""js2c out.cc type compression sources.js ... | 555 parser.set_usage("""js2c out.cc type sources.js ... |
| 575 out.cc: C code to be generated. | 556 out.cc: C code to be generated. |
| 576 type: type parameter for NativesCollection template. | 557 type: type parameter for NativesCollection template. |
| 577 compression: type of compression used. [off|bz2] | |
| 578 sources.js: JS internal sources or macros.py.""") | 558 sources.js: JS internal sources or macros.py.""") |
| 579 (options, args) = parser.parse_args() | 559 (options, args) = parser.parse_args() |
| 580 | 560 |
| 581 JS2C(args[3:], args[0], args[1], args[2], options.raw, options.startup_blob) | 561 JS2C(args[2:], args[0], args[1], options.raw, options.startup_blob) |
| 582 | 562 |
| 583 | 563 |
| 584 if __name__ == "__main__": | 564 if __name__ == "__main__": |
| 585 main() | 565 main() |
| OLD | NEW |