Skip to content

Commit 075abc3

Browse files
authored
Merge pull request #40 from emilycares/module
Add Module attribute parser
2 parents df04353 + de4f819 commit 075abc3

12 files changed

Lines changed: 248 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,4 @@ fn main() {
111111
- [x] LocalVariableTable
112112
- [x] LocalVariableTypeTable
113113
- [x] Deprecated
114+
- [x] Module

java-assets/compile.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ javac -d java-assets/compiled-classes/ java-assets/src/UnicodeStrings.java
2727
javac -d java-assets/compiled-classes/ java-assets/src/DeprecatedAnnotation.java
2828
javac -d java-assets/compiled-classes/ java-assets/src/InnerClasses.java
2929
javac -d java-assets/compiled-classes/ java-assets/src/Annotations.java
30+
javac -d java-assets/compiled-classes/ java-assets/src/module-info.java java-assets/src/com/some/Thing.java
3031

3132
javac -g -d java-assets/compiled-classes/ java-assets/src/LocalVariableTable.java
3233
javac -d java-assets/compiled-classes/ java-assets/src/HelloWorld.java
193 Bytes
Binary file not shown.
177 Bytes
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.some;
2+
3+
public class Thing {}

java-assets/src/module-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module my.module {
2+
exports com.some;
3+
}

src/attribute_info/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use self::parser::exceptions_attribute_parser;
1313
pub use self::parser::inner_classes_attribute_parser;
1414
pub use self::parser::line_number_table_attribute_parser;
1515
pub use self::parser::method_parameters_attribute_parser;
16+
pub use self::parser::module_attribute_parser;
1617
pub use self::parser::runtime_invisible_annotations_attribute_parser;
1718
pub use self::parser::runtime_invisible_parameter_annotations_attribute_parser;
1819
pub use self::parser::runtime_invisible_type_annotations_attribute_parser;

src/attribute_info/parser.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,110 @@ pub fn sourcefile_attribute_parser(
697697
let (input, sourcefile_index) = be_u16(input)?;
698698
Ok((input, SourceFileAttribute { sourcefile_index }))
699699
}
700+
701+
pub fn module_attribute_parser(input: &[u8]) -> Result<(&[u8], ModuleAttribute), Err<&[u8]>> {
702+
let (input, module_name_index) = be_u16(input)?;
703+
let (input, module_flags) = be_u16(input)?;
704+
let (input, module_version_index) = be_u16(input)?;
705+
706+
let (input, requires_count) = be_u16(input)?;
707+
let (input, requires) =
708+
count(module_requires_attribute_parser, requires_count as usize)(input)?;
709+
710+
let (input, exports_count) = be_u16(input)?;
711+
let (input, exports) = count(module_exports_attribute_parser, exports_count as usize)(input)?;
712+
713+
let (input, opens_count) = be_u16(input)?;
714+
let (input, opens) = count(module_opens_attribute_parser, opens_count as usize)(input)?;
715+
716+
let (input, uses_count) = be_u16(input)?;
717+
let (input, uses) = count(be_u16, uses_count as usize)(input)?;
718+
719+
let (input, provides_count) = be_u16(input)?;
720+
let (input, provides) =
721+
count(module_provides_attribute_parser, provides_count as usize)(input)?;
722+
723+
Ok((
724+
input,
725+
ModuleAttribute {
726+
module_name_index,
727+
module_flags,
728+
module_version_index,
729+
requires,
730+
exports,
731+
opens,
732+
uses,
733+
provides,
734+
},
735+
))
736+
}
737+
738+
pub fn module_requires_attribute_parser(
739+
input: &[u8],
740+
) -> Result<(&[u8], ModuleRequiresAttribute), Err<&[u8]>> {
741+
let (input, requires_index) = be_u16(input)?;
742+
let (input, requires_flags) = be_u16(input)?;
743+
let (input, requires_version_index) = be_u16(input)?;
744+
745+
Ok((
746+
input,
747+
ModuleRequiresAttribute {
748+
requires_index,
749+
requires_flags,
750+
requires_version_index,
751+
},
752+
))
753+
}
754+
755+
pub fn module_exports_attribute_parser(
756+
input: &[u8],
757+
) -> Result<(&[u8], ModuleExportsAttribute), Err<&[u8]>> {
758+
let (input, exports_index) = be_u16(input)?;
759+
let (input, exports_flags) = be_u16(input)?;
760+
let (input, exports_to_count) = be_u16(input)?;
761+
762+
let (input, exports_to_index) = count(be_u16, exports_to_count as usize)(input)?;
763+
764+
Ok((
765+
input,
766+
ModuleExportsAttribute {
767+
exports_index,
768+
exports_flags,
769+
exports_to_index,
770+
},
771+
))
772+
}
773+
774+
pub fn module_opens_attribute_parser(
775+
input: &[u8],
776+
) -> Result<(&[u8], ModuleOpensAttribute), Err<&[u8]>> {
777+
let (input, opens_index) = be_u16(input)?;
778+
let (input, opens_flags) = be_u16(input)?;
779+
let (input, opens_to_count) = be_u16(input)?;
780+
let (input, opens_to_index) = count(be_u16, opens_to_count as usize)(input)?;
781+
782+
Ok((
783+
input,
784+
ModuleOpensAttribute {
785+
opens_index,
786+
opens_flags,
787+
opens_to_index,
788+
},
789+
))
790+
}
791+
792+
pub fn module_provides_attribute_parser(
793+
input: &[u8],
794+
) -> Result<(&[u8], ModuleProvidesAttribute), Err<&[u8]>> {
795+
let (input, provides_index) = be_u16(input)?;
796+
let (input, provides_with_count) = be_u16(input)?;
797+
let (input, provides_with_index) = count(be_u16, provides_with_count as usize)(input)?;
798+
799+
Ok((
800+
input,
801+
ModuleProvidesAttribute {
802+
provides_index,
803+
provides_with_index,
804+
},
805+
))
806+
}

src/attribute_info/types.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,39 @@ pub struct SourceFileAttribute {
343343
/// The constant_pool entry at that index must be a CONSTANT_Utf8_info structure representing a string.
344344
pub sourcefile_index: u16,
345345
}
346+
347+
#[derive(Clone, Debug, Eq, PartialEq)]
348+
pub struct ModuleAttribute {
349+
pub module_name_index: u16,
350+
pub module_flags: u16,
351+
pub module_version_index: u16,
352+
pub requires: Vec<ModuleRequiresAttribute>,
353+
pub exports: Vec<ModuleExportsAttribute>,
354+
pub opens: Vec<ModuleOpensAttribute>,
355+
pub uses: Vec<u16>,
356+
pub provides: Vec<ModuleProvidesAttribute>,
357+
}
358+
359+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
360+
pub struct ModuleRequiresAttribute {
361+
pub requires_index: u16,
362+
pub requires_flags: u16,
363+
pub requires_version_index: u16,
364+
}
365+
#[derive(Clone, Debug, Eq, PartialEq)]
366+
pub struct ModuleExportsAttribute {
367+
pub exports_index: u16,
368+
pub exports_flags: u16,
369+
pub exports_to_index: Vec<u16>,
370+
}
371+
#[derive(Clone, Debug, Eq, PartialEq)]
372+
pub struct ModuleOpensAttribute {
373+
pub opens_index: u16,
374+
pub opens_flags: u16,
375+
pub opens_to_index: Vec<u16>,
376+
}
377+
#[derive(Clone, Debug, Eq, PartialEq)]
378+
pub struct ModuleProvidesAttribute {
379+
pub provides_index: u16,
380+
pub provides_with_index: Vec<u16>,
381+
}

src/constant_info/parser.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ fn const_invoke_dynamic(input: &[u8]) -> ConstantInfoResult<'_> {
131131
))
132132
}
133133

134+
fn const_module(input: &[u8]) -> ConstantInfoResult<'_> {
135+
let (input, name_index) = be_u16(input)?;
136+
Ok((input, ConstantInfo::Module(ModuleConstant { name_index })))
137+
}
138+
139+
fn const_package(input: &[u8]) -> ConstantInfoResult<'_> {
140+
let (input, name_index) = be_u16(input)?;
141+
Ok((input, ConstantInfo::Package(PackageConstant { name_index })))
142+
}
143+
134144
type ConstantInfoResult<'a> = Result<(&'a [u8], ConstantInfo), Err<Error<&'a [u8]>>>;
135145
type ConstantInfoVecResult<'a> = Result<(&'a [u8], Vec<ConstantInfo>), Err<Error<&'a [u8]>>>;
136146

@@ -150,6 +160,8 @@ fn const_block_parser(input: &[u8], const_type: u8) -> ConstantInfoResult<'_> {
150160
15 => const_method_handle(input),
151161
16 => const_method_type(input),
152162
18 => const_invoke_dynamic(input),
163+
19 => const_module(input),
164+
20 => const_package(input),
153165
_ => Result::Err(Err::Error(error_position!(input, ErrorKind::Alt))),
154166
}
155167
}

0 commit comments

Comments
 (0)