-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instruction verifying for writing class files
* introduced `InstructionMajorVersionVerifier` for verifying instructions before writing in `ClassWriter#verify` refs #8
- Loading branch information
Showing
4 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as _ from 'lodash'; | ||
import { OPCODE_VERSIONS } from '../instructions/Opcodes'; | ||
const WritingErrors = require('../../../errors').Writing; | ||
|
||
class InstructionMajorVersionVerifier { | ||
|
||
constructor(classInfo) { | ||
this.classInfo = classInfo; | ||
this.unusableOpcodes = this.findUnusableOpcodes(); | ||
} | ||
|
||
findUnusableOpcodes() { | ||
return _.flatten(_.filter(OPCODE_VERSIONS, (opcodes, version) => { | ||
return version > this.classInfo.major; | ||
})); | ||
} | ||
|
||
findInvalidMethodOpcodes(method) { | ||
let opcodes = _.pluck(method.instructions, 'opcode'); | ||
return _.intersection(opcodes, this.unusableOpcodes); | ||
} | ||
|
||
verify() { | ||
return _.every(this.classInfo.methods, (method) => { | ||
let invalid = this.findInvalidMethodOpcodes(method); | ||
if (invalid.length > 0) { | ||
throw WritingErrors.UnsupportedOpcode(unusable[0], this.classInfo, method); | ||
} | ||
return true; | ||
}); | ||
} | ||
} | ||
|
||
export default { | ||
InstructionMajorVersionVerifier | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import { OPCODE_TO_NAME } from '../core/jvm/instructions/Opcodes'; | ||
|
||
export default { | ||
Parsing: { | ||
InvalidClassFileFormat: new Error('Invalid class file format!') | ||
}, | ||
Writing: { | ||
UnsupportedOpcode: (opcode, classInfo, method) => { | ||
return new Error(OPCODE_TO_NAME[opcode] + ' is not supported on major ' + classInfo.major + | ||
' in ' + classInfo.name + '#' + method.name + method.desc); | ||
} | ||
}, | ||
NotImplementedError: new Error('This has not yet been implemented.') | ||
} |