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): | 400 def BuildMetadata(sources, source_bytes, native_type, omit): |
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. |
408 | 409 |
409 Returns: | 410 Returns: |
410 A dictionary for use with HEADER_TEMPLATE. | 411 A dictionary for use with HEADER_TEMPLATE. |
411 """ | 412 """ |
412 total_length = len(source_bytes) | 413 total_length = len(source_bytes) |
413 raw_sources = "".join(sources.modules) | 414 raw_sources = "".join(sources.modules) |
414 | 415 |
415 # The sources are expected to be ASCII-only. | 416 # The sources are expected to be ASCII-only. |
416 assert not filter(lambda value: ord(value) >= 128, raw_sources) | 417 assert not filter(lambda value: ord(value) >= 128, raw_sources) |
417 | 418 |
(...skipping 12 matching lines...) Expand all Loading... |
430 "offset": offset, | 431 "offset": offset, |
431 "raw_length": len(sources.modules[i]), | 432 "raw_length": len(sources.modules[i]), |
432 } | 433 } |
433 get_index_cases.append(GET_INDEX_CASE % d) | 434 get_index_cases.append(GET_INDEX_CASE % d) |
434 get_script_name_cases.append(GET_SCRIPT_NAME_CASE % d) | 435 get_script_name_cases.append(GET_SCRIPT_NAME_CASE % d) |
435 get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % d) | 436 get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % d) |
436 offset += len(sources.modules[i]) | 437 offset += len(sources.modules[i]) |
437 assert offset == len(raw_sources) | 438 assert offset == len(raw_sources) |
438 | 439 |
439 # If we have the raw sources we can declare them accordingly. | 440 # If we have the raw sources we can declare them accordingly. |
440 have_raw_sources = source_bytes == raw_sources | 441 have_raw_sources = source_bytes == raw_sources and not omit |
441 raw_sources_declaration = (RAW_SOURCES_DECLARATION | 442 raw_sources_declaration = (RAW_SOURCES_DECLARATION |
442 if have_raw_sources else RAW_SOURCES_COMPRESSION_DECLARATION) | 443 if have_raw_sources else RAW_SOURCES_COMPRESSION_DECLARATION) |
443 | 444 |
444 metadata = { | 445 metadata = { |
445 "builtin_count": len(sources.modules), | 446 "builtin_count": len(sources.modules), |
446 "debugger_count": sum(sources.is_debugger_id), | 447 "debugger_count": sum(sources.is_debugger_id), |
447 "sources_declaration": SOURCES_DECLARATION % ToCArray(source_bytes), | 448 "sources_declaration": SOURCES_DECLARATION % ToCArray(source_bytes), |
| 449 "sources_data": ToCArray(source_bytes) if not omit else "", |
448 "raw_sources_declaration": raw_sources_declaration, | 450 "raw_sources_declaration": raw_sources_declaration, |
449 "raw_total_length": sum(map(len, sources.modules)), | 451 "raw_total_length": sum(map(len, sources.modules)), |
450 "total_length": total_length, | 452 "total_length": total_length, |
451 "get_index_cases": "".join(get_index_cases), | 453 "get_index_cases": "".join(get_index_cases), |
452 "get_raw_script_source_cases": "".join(get_raw_script_source_cases), | 454 "get_raw_script_source_cases": "".join(get_raw_script_source_cases), |
453 "get_script_name_cases": "".join(get_script_name_cases), | 455 "get_script_name_cases": "".join(get_script_name_cases), |
454 "type": native_type, | 456 "type": native_type, |
455 } | 457 } |
456 return metadata | 458 return metadata |
457 | 459 |
(...skipping 10 matching lines...) Expand all Loading... |
468 """ | 470 """ |
469 sources_bytes = "".join(sources.modules) | 471 sources_bytes = "".join(sources.modules) |
470 if compression_type == "off": | 472 if compression_type == "off": |
471 return sources_bytes | 473 return sources_bytes |
472 elif compression_type == "bz2": | 474 elif compression_type == "bz2": |
473 return bz2.compress(sources_bytes) | 475 return bz2.compress(sources_bytes) |
474 else: | 476 else: |
475 raise Error("Unknown compression type %s." % compression_type) | 477 raise Error("Unknown compression type %s." % compression_type) |
476 | 478 |
477 | 479 |
478 def PutInt(blob_file, value): | 480 def JS2C(source, target, native_type, compression_type, raw_file, omit): |
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): | |
520 sources = PrepareSources(source) | 481 sources = PrepareSources(source) |
521 sources_bytes = CompressMaybe(sources, compression_type) | 482 sources_bytes = CompressMaybe(sources, compression_type) |
522 metadata = BuildMetadata(sources, sources_bytes, native_type) | 483 metadata = BuildMetadata(sources, sources_bytes, native_type, omit) |
523 | 484 |
524 # Optionally emit raw file. | 485 # Optionally emit raw file. |
525 if raw_file: | 486 if raw_file: |
526 output = open(raw_file, "w") | 487 output = open(raw_file, "w") |
527 output.write(sources_bytes) | 488 output.write(sources_bytes) |
528 output.close() | 489 output.close() |
529 | 490 |
530 if startup_blob: | |
531 WriteStartupBlob(sources, startup_blob); | |
532 | |
533 # Emit resulting source file. | 491 # Emit resulting source file. |
534 output = open(target, "w") | 492 output = open(target, "w") |
535 output.write(HEADER_TEMPLATE % metadata) | 493 output.write(HEADER_TEMPLATE % metadata) |
536 output.close() | 494 output.close() |
537 | 495 |
538 | 496 |
539 def main(): | 497 def main(): |
540 parser = optparse.OptionParser() | 498 parser = optparse.OptionParser() |
541 parser.add_option("--raw", action="store", | 499 parser.add_option("--raw", action="store", |
542 help="file to write the processed sources array to.") | 500 help="file to write the processed sources array to.") |
543 parser.add_option("--startup_blob", action="store", | 501 parser.add_option("--omit", dest="omit", action="store_true", |
544 help="file to write the startup blob to.") | 502 help="Omit the raw sources from the generated code.") |
545 parser.set_usage("""js2c out.cc type compression sources.js ... | 503 parser.set_usage("""js2c out.cc type compression sources.js ... |
546 out.cc: C code to be generated. | 504 out.cc: C code to be generated. |
547 type: type parameter for NativesCollection template. | 505 type: type parameter for NativesCollection template. |
548 compression: type of compression used. [off|bz2] | 506 compression: type of compression used. [off|bz2] |
549 sources.js: JS internal sources or macros.py.""") | 507 sources.js: JS internal sources or macros.py.""") |
550 (options, args) = parser.parse_args() | 508 (options, args) = parser.parse_args() |
551 | 509 |
552 JS2C(args[3:], args[0], args[1], args[2], options.raw, options.startup_blob) | 510 JS2C(args[3:], args[0], args[1], args[2], options.raw, options.omit) |
553 | 511 |
554 | 512 |
555 if __name__ == "__main__": | 513 if __name__ == "__main__": |
556 main() | 514 main() |
OLD | NEW |