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 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 result.modules.append(lines); | 390 result.modules.append(lines); |
391 | 391 |
392 is_debugger = IsDebuggerFile(source) | 392 is_debugger = IsDebuggerFile(source) |
393 result.is_debugger_id.append(is_debugger); | 393 result.is_debugger_id.append(is_debugger); |
394 | 394 |
395 name = os.path.basename(source)[:-3] | 395 name = os.path.basename(source)[:-3] |
396 result.names.append(name if not is_debugger else name[:-9]); | 396 result.names.append(name if not is_debugger else name[:-9]); |
397 return result | 397 return result |
398 | 398 |
399 | 399 |
400 def BuildMetadata(sources, source_bytes, native_type, omit): | 400 def BuildMetadata(sources, source_bytes, native_type): |
401 """Build the meta data required to generate a libaries file. | 401 """Build the meta data required to generate a libaries file. |
402 | 402 |
403 Args: | 403 Args: |
404 sources: A Sources instance with the prepared sources. | 404 sources: A Sources instance with the prepared sources. |
405 source_bytes: A list of source bytes. | 405 source_bytes: A list of source bytes. |
406 (The concatenation of all sources; might be compressed.) | 406 (The concatenation of all sources; might be compressed.) |
407 native_type: The parameter for the NativesCollection template. | 407 native_type: The parameter for the NativesCollection template. |
408 omit: bool, whether we should omit the sources in the output. | |
409 | 408 |
410 Returns: | 409 Returns: |
411 A dictionary for use with HEADER_TEMPLATE. | 410 A dictionary for use with HEADER_TEMPLATE. |
412 """ | 411 """ |
413 total_length = len(source_bytes) | 412 total_length = len(source_bytes) |
414 raw_sources = "".join(sources.modules) | 413 raw_sources = "".join(sources.modules) |
415 | 414 |
416 # The sources are expected to be ASCII-only. | 415 # The sources are expected to be ASCII-only. |
417 assert not filter(lambda value: ord(value) >= 128, raw_sources) | 416 assert not filter(lambda value: ord(value) >= 128, raw_sources) |
418 | 417 |
(...skipping 12 matching lines...) Expand all Loading... |
431 "offset": offset, | 430 "offset": offset, |
432 "raw_length": len(sources.modules[i]), | 431 "raw_length": len(sources.modules[i]), |
433 } | 432 } |
434 get_index_cases.append(GET_INDEX_CASE % d) | 433 get_index_cases.append(GET_INDEX_CASE % d) |
435 get_script_name_cases.append(GET_SCRIPT_NAME_CASE % d) | 434 get_script_name_cases.append(GET_SCRIPT_NAME_CASE % d) |
436 get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % d) | 435 get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % d) |
437 offset += len(sources.modules[i]) | 436 offset += len(sources.modules[i]) |
438 assert offset == len(raw_sources) | 437 assert offset == len(raw_sources) |
439 | 438 |
440 # If we have the raw sources we can declare them accordingly. | 439 # If we have the raw sources we can declare them accordingly. |
441 have_raw_sources = source_bytes == raw_sources and not omit | 440 have_raw_sources = source_bytes == raw_sources |
442 raw_sources_declaration = (RAW_SOURCES_DECLARATION | 441 raw_sources_declaration = (RAW_SOURCES_DECLARATION |
443 if have_raw_sources else RAW_SOURCES_COMPRESSION_DECLARATION) | 442 if have_raw_sources else RAW_SOURCES_COMPRESSION_DECLARATION) |
444 | 443 |
445 metadata = { | 444 metadata = { |
446 "builtin_count": len(sources.modules), | 445 "builtin_count": len(sources.modules), |
447 "debugger_count": sum(sources.is_debugger_id), | 446 "debugger_count": sum(sources.is_debugger_id), |
448 "sources_declaration": SOURCES_DECLARATION % ToCArray(source_bytes), | 447 "sources_declaration": SOURCES_DECLARATION % ToCArray(source_bytes), |
449 "sources_data": ToCArray(source_bytes) if not omit else "", | |
450 "raw_sources_declaration": raw_sources_declaration, | 448 "raw_sources_declaration": raw_sources_declaration, |
451 "raw_total_length": sum(map(len, sources.modules)), | 449 "raw_total_length": sum(map(len, sources.modules)), |
452 "total_length": total_length, | 450 "total_length": total_length, |
453 "get_index_cases": "".join(get_index_cases), | 451 "get_index_cases": "".join(get_index_cases), |
454 "get_raw_script_source_cases": "".join(get_raw_script_source_cases), | 452 "get_raw_script_source_cases": "".join(get_raw_script_source_cases), |
455 "get_script_name_cases": "".join(get_script_name_cases), | 453 "get_script_name_cases": "".join(get_script_name_cases), |
456 "type": native_type, | 454 "type": native_type, |
457 } | 455 } |
458 return metadata | 456 return metadata |
459 | 457 |
(...skipping 10 matching lines...) Expand all Loading... |
470 """ | 468 """ |
471 sources_bytes = "".join(sources.modules) | 469 sources_bytes = "".join(sources.modules) |
472 if compression_type == "off": | 470 if compression_type == "off": |
473 return sources_bytes | 471 return sources_bytes |
474 elif compression_type == "bz2": | 472 elif compression_type == "bz2": |
475 return bz2.compress(sources_bytes) | 473 return bz2.compress(sources_bytes) |
476 else: | 474 else: |
477 raise Error("Unknown compression type %s." % compression_type) | 475 raise Error("Unknown compression type %s." % compression_type) |
478 | 476 |
479 | 477 |
480 def JS2C(source, target, native_type, compression_type, raw_file, omit): | 478 def PutInt(blob_file, value): |
| 479 assert(value >= 0 and value < (1 << 20)) |
| 480 size = 1 if (value < 1 << 6) else (2 if (value < 1 << 14) else 3) |
| 481 value_with_length = (value << 2) | size |
| 482 |
| 483 byte_sequence = bytearray() |
| 484 for i in xrange(size): |
| 485 byte_sequence.append(value_with_length & 255) |
| 486 value_with_length >>= 8; |
| 487 blob_file.write(byte_sequence) |
| 488 |
| 489 |
| 490 def PutStr(blob_file, value): |
| 491 PutInt(blob_file, len(value)); |
| 492 blob_file.write(value); |
| 493 |
| 494 |
| 495 def WriteStartupBlob(sources, startup_blob): |
| 496 """Write a startup blob, as expected by V8 Initialize ... |
| 497 TODO(vogelheim): Add proper method name. |
| 498 |
| 499 Args: |
| 500 sources: A Sources instance with the prepared sources. |
| 501 startup_blob_file: Name of file to write the blob to. |
| 502 """ |
| 503 output = open(startup_blob, "wb") |
| 504 |
| 505 debug_sources = sum(sources.is_debugger_id); |
| 506 PutInt(output, debug_sources) |
| 507 for i in xrange(debug_sources): |
| 508 PutStr(output, sources.names[i]); |
| 509 PutStr(output, sources.modules[i]); |
| 510 |
| 511 PutInt(output, len(sources.names) - debug_sources) |
| 512 for i in xrange(debug_sources, len(sources.names)): |
| 513 PutStr(output, sources.names[i]); |
| 514 PutStr(output, sources.modules[i]); |
| 515 |
| 516 output.close() |
| 517 |
| 518 |
| 519 def JS2C(source, target, native_type, compression_type, raw_file, startup_blob): |
481 sources = PrepareSources(source) | 520 sources = PrepareSources(source) |
482 sources_bytes = CompressMaybe(sources, compression_type) | 521 sources_bytes = CompressMaybe(sources, compression_type) |
483 metadata = BuildMetadata(sources, sources_bytes, native_type, omit) | 522 metadata = BuildMetadata(sources, sources_bytes, native_type) |
484 | 523 |
485 # Optionally emit raw file. | 524 # Optionally emit raw file. |
486 if raw_file: | 525 if raw_file: |
487 output = open(raw_file, "w") | 526 output = open(raw_file, "w") |
488 output.write(sources_bytes) | 527 output.write(sources_bytes) |
489 output.close() | 528 output.close() |
490 | 529 |
| 530 if startup_blob: |
| 531 WriteStartupBlob(sources, startup_blob); |
| 532 |
491 # Emit resulting source file. | 533 # Emit resulting source file. |
492 output = open(target, "w") | 534 output = open(target, "w") |
493 output.write(HEADER_TEMPLATE % metadata) | 535 output.write(HEADER_TEMPLATE % metadata) |
494 output.close() | 536 output.close() |
495 | 537 |
496 | 538 |
497 def main(): | 539 def main(): |
498 parser = optparse.OptionParser() | 540 parser = optparse.OptionParser() |
499 parser.add_option("--raw", action="store", | 541 parser.add_option("--raw", action="store", |
500 help="file to write the processed sources array to.") | 542 help="file to write the processed sources array to.") |
501 parser.add_option("--omit", dest="omit", action="store_true", | 543 parser.add_option("--startup_blob", action="store", |
502 help="Omit the raw sources from the generated code.") | 544 help="file to write the startup blob to.") |
503 parser.set_usage("""js2c out.cc type compression sources.js ... | 545 parser.set_usage("""js2c out.cc type compression sources.js ... |
504 out.cc: C code to be generated. | 546 out.cc: C code to be generated. |
505 type: type parameter for NativesCollection template. | 547 type: type parameter for NativesCollection template. |
506 compression: type of compression used. [off|bz2] | 548 compression: type of compression used. [off|bz2] |
507 sources.js: JS internal sources or macros.py.""") | 549 sources.js: JS internal sources or macros.py.""") |
508 (options, args) = parser.parse_args() | 550 (options, args) = parser.parse_args() |
509 | 551 |
510 JS2C(args[3:], args[0], args[1], args[2], options.raw, options.omit) | 552 JS2C(args[3:], args[0], args[1], args[2], options.raw, options.startup_blob) |
511 | 553 |
512 | 554 |
513 if __name__ == "__main__": | 555 if __name__ == "__main__": |
514 main() | 556 main() |
OLD | NEW |