Skip to content

Commit b3de874

Browse files
committed
add support to Java 9 (module-info)
1 parent 570202a commit b3de874

File tree

3 files changed

+226
-2
lines changed

3 files changed

+226
-2
lines changed

src/constant-type.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ const ConstantType = {
1919
NAME_AND_TYPE: 12,
2020
METHOD_HANDLE: 15,
2121
METHOD_TYPE: 16,
22-
INVOKE_DYNAMIC: 18
22+
INVOKE_DYNAMIC: 18,
23+
MODULE: 19,
24+
PACKAGE: 20,
2325
};
2426

2527
module.exports = ConstantType;

src/java-class-reader.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,136 @@ class JavaClassFileReader {
591591
break;
592592
}
593593

594+
case 'ModuleMainClass':
595+
attribute.main_class_index = this.buf.readUint16();
596+
break;
597+
598+
case 'Module': {
599+
attribute.module_name_index = this.buf.readUint16();
600+
attribute.module_flags = this.buf.readUint16();
601+
attribute.module_version_index = this.buf.readUint16();
602+
603+
attribute.requires_count = this.buf.readUint16();
604+
attribute.requires = [];
605+
606+
let requires_count = attribute.requires_count;
607+
while (requires_count-- > 0) {
608+
attribute.requires.push({
609+
requires_index: this.buf.readUint16(),
610+
requires_flags: this.buf.readUint16(),
611+
requires_version_index: this.buf.readUint16()
612+
});
613+
}
614+
615+
attribute.exports_count = this.buf.readUint16();
616+
attribute.exports = [];
617+
618+
let exports_count = attribute.exports_count;
619+
while (exports_count-- > 0) {
620+
const entry = {
621+
exports_index: this.buf.readUint16(),
622+
exports_flags: this.buf.readUint16(),
623+
exports_to_count: this.buf.readUint16(),
624+
exports_to_index: []
625+
};
626+
627+
let exports_to_count = entry.exports_to_count;
628+
while (exports_to_count-- > 0) {
629+
entry.exports_to_index.push(this.buf.readUint16());
630+
}
631+
632+
attribute.exports.push(entry);
633+
}
634+
635+
attribute.opens_count = this.buf.readUint16();
636+
attribute.opens = [];
637+
638+
let opens_count = attribute.opens_count;
639+
while (opens_count-- > 0) {
640+
const entry = {
641+
opens_index: this.buf.readUint16(),
642+
opens_flags: this.buf.readUint16(),
643+
opens_to_count: this.buf.readUint16(),
644+
opens_to_index: []
645+
};
646+
647+
let opens_to_count = entry.opens_to_count;
648+
while (opens_to_count-- > 0) {
649+
entry.opens_to_index.push(this.buf.readUint16());
650+
}
651+
652+
attribute.opens.push(entry);
653+
}
654+
655+
attribute.uses_count = this.buf.readUint16();
656+
attribute.uses_index = [];
657+
658+
let uses_count = attribute.uses_count;
659+
while (uses_count-- > 0) {
660+
attribute.uses_index.push(this.buf.readUint16());
661+
}
662+
663+
attribute.provides_count = this.buf.readUint16();
664+
attribute.provides = [];
665+
666+
let provides_count = attribute.provides_count;
667+
while (provides_count-- > 0) {
668+
const entry = {
669+
provides_index: this.buf.readUint16(),
670+
provides_with_count: this.buf.readUint16(),
671+
provides_with_index: []
672+
};
673+
674+
let provides_with_count = entry.provides_with_count;
675+
while (provides_with_count-- > 0) {
676+
entry.provides_with_index.push(this.buf.readUint16());
677+
}
678+
679+
attribute.provides.push(entry);
680+
}
681+
break;
682+
}
683+
684+
case 'ModulePackages': {
685+
attribute.package_count = this.buf.readUint16();
686+
attribute.package_index = [];
687+
688+
let package_count = attribute.package_count;
689+
while (package_count-- > 0) {
690+
attribute.package_index.push(this.buf.readUint16());
691+
}
692+
break;
693+
}
694+
695+
/* Not specified in JVMS 9 */
696+
case 'ModuleTarget':
697+
attribute.target_platform_index = this.buf.readUint16();
698+
break;
699+
700+
case 'ModuleHashes': {
701+
attribute.algorithm_index = this.buf.readUint16();
702+
attribute.hashes_table_length = this.buf.readUint16();
703+
attribute.hashes_table = [];
704+
705+
let hashes_table_length = attribute.hashes_table_length;
706+
while (hashes_table_length-- > 0) {
707+
const entry = {
708+
module_name_index: this.buf.readUint16(),
709+
hash_length: this.buf.readUint16(),
710+
hash: []
711+
};
712+
713+
let hash_length = entry.hash_length;
714+
while (hash_length-- > 0) {
715+
entry.hash.push(this.buf.readUInt8());
716+
}
717+
718+
attribute.hashes_table.push(entry);
719+
}
720+
break;
721+
}
722+
/* -- */
723+
594724
default:
595725
throw Error(`Unexpected attributeName: ${attributeName}`);
596726
}
@@ -745,6 +875,8 @@ class JavaClassFileReader {
745875
cp_info.low_bytes = this.buf.readUint32();
746876
break;
747877

878+
case ConstantType.PACKAGE:
879+
case ConstantType.MODULE:
748880
case ConstantType.CLASS:
749881
cp_info.name_index = this.buf.readUInt16();
750882
break;

src/java-class-writer.js

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,94 @@ class JavaClassFileWriter {
450450
}
451451
break;
452452

453+
case 'Module': {
454+
this.buf.writeUint16(attribute_info.module_name_index);
455+
this.buf.writeUint16(attribute_info.module_flags);
456+
this.buf.writeUint16(attribute_info.module_version_index);
457+
458+
this.buf.writeUint16(attribute_info.requires_count);
459+
for (var i = 0; i < attribute_info.requires_count; i++) {
460+
const entry = attribute_info.requires[i];
461+
462+
this.buf.writeUint16(entry.requires_index);
463+
this.buf.writeUint16(entry.requires_flags);
464+
this.buf.writeUint16(entry.requires_version_index);
465+
}
466+
467+
this.buf.writeUint16(attribute_info.exports_count);
468+
for (var i = 0; i < attribute_info.exports_count; i++) {
469+
const entry = attribute_info.exports[i];
470+
471+
this.buf.writeUint16(entry.exports_index);
472+
this.buf.writeUint16(entry.exports_flags);
473+
this.buf.writeUint16(entry.exports_to_count);
474+
475+
for (var j = 0; j < entry.exports_to_count; j++) {
476+
this.buf.writeUint16(entry.exports_to_index[j]);
477+
}
478+
}
479+
480+
this.buf.writeUint16(attribute_info.opens_count);
481+
for (var i = 0; i < attribute_info.opens_count; i++) {
482+
const entry = attribute_info.opens[i];
483+
484+
this.buf.writeUint16(entry.opens_index);
485+
this.buf.writeUint16(entry.opens_flags);
486+
this.buf.writeUint16(entry.opens_to_count);
487+
488+
for (var j = 0; j < entry.opens_to_count; j++) {
489+
this.buf.writeUint16(entry.opens_to_index[j]);
490+
}
491+
}
492+
493+
this.buf.writeUint16(attribute_info.uses_count);
494+
for (var i = 0; i < attribute_info.uses_count; i++) {
495+
this.buf.writeUint16(attribute_info.uses_index[i]);
496+
}
497+
498+
this.buf.writeUint16(attribute_info.provides_count);
499+
for (var i = 0; i < attribute_info.provides_count; i++) {
500+
const entry = attribute_info.provides[i];
501+
502+
this.buf.writeUint16(entry.provides_index);
503+
this.buf.writeUint16(entry.provides_with_count);
504+
505+
for (var j = 0; j < entry.provides_with_count; j++) {
506+
this.buf.writeUint16(entry.provides_with_index[j]);
507+
}
508+
}
509+
break;
510+
}
511+
512+
case 'ModulePackages':
513+
this.buf.writeUint16(attribute_info.package_count);
514+
for (var i = 0; i < attribute_info.package_count; i++) {
515+
this.buf.writeUint16(attribute_info.package_index[i]);
516+
}
517+
break;
518+
519+
/* Not specified in JVMS 9 */
520+
case 'ModuleTarget':
521+
this.buf.writeUint16(attribute_info.target_platform_index);
522+
break;
523+
524+
case 'ModuleHashes': {
525+
this.buf.writeUint16(attribute_info.algorithm_index);
526+
this.buf.writeUint16(attribute_info.hashes_table_length);
527+
528+
for (var i = 0; i < attribute_info.hashes_table_length; i++) {
529+
const entry = attribute_info.hashes_table[i];
530+
this.buf.writeUint16(entry.module_name_index);
531+
this.buf.writeUint16(entry.hash_length);
532+
533+
for (var j = 0; j < entry.hash_length; j++) {
534+
this.buf.writeUint8(entry.hash[j]);
535+
}
536+
}
537+
break;
538+
}
539+
/* -- */
540+
453541
default:
454542
throw Error(`Unexpected attributeName: ${attributeName}`);
455543
}
@@ -504,6 +592,8 @@ class JavaClassFileWriter {
504592
this.buf.writeUint32(entry.low_bytes);
505593
break;
506594

595+
case ConstantType.PACKAGE:
596+
case ConstantType.MODULE:
507597
case ConstantType.CLASS:
508598
this.buf.writeUint16(entry.name_index);
509599
break;
@@ -539,7 +629,7 @@ class JavaClassFileWriter {
539629
break;
540630

541631
default:
542-
throw Error(`Unexpected tag: ${cp_info.tag}`);
632+
throw Error(`Unexpected tag: ${entry.tag}`);
543633
}
544634

545635
}

0 commit comments

Comments
 (0)