Skip to content

Commit

Permalink
feat: add overload to get instruction as type
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed May 5, 2023
1 parent d83e937 commit 49c173d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/main/kotlin/app/revanced/patcher/extensions/Extensions.kt
Expand Up @@ -101,12 +101,20 @@ fun MutableMethod.removeInstruction(index: Int) = this.implementation!!.removeIn
fun MutableMethod.label(index: Int) = this.implementation!!.newLabelForIndex(index)

/**
* Get the instruction at the given index in the method's implementation.
* Get an instruction at the given index in the method's implementation.
* @param index The index to get the instruction at.
* @return The instruction.
*/
fun MutableMethod.instruction(index: Int): BuilderInstruction = this.implementation!!.instructions[index]

/**
* Get an instruction at the given index in the method's implementation.
* @param index The index to get the instruction at.
* @param T The type of instruction to return.
* @return The instruction.
*/
fun <T> MutableMethod.instruction(index: Int): T = instruction(index) as T

/**
* Add smali instructions to the method.
* @param index The index to insert the instructions at.
Expand Down
Expand Up @@ -44,7 +44,7 @@ internal class InlineSmaliCompilerTest {
"""
)

val insn = method.instruction(insnIndex) as BuilderInstruction21t
val insn = method.instruction<BuilderInstruction21t>(insnIndex)
assertEquals(targetIndex, insn.target.location.index)
}

Expand Down Expand Up @@ -73,7 +73,7 @@ internal class InlineSmaliCompilerTest {
)
)

val insn = method.instruction(insnIndex) as BuilderInstruction21t
val insn = method.instruction<BuilderInstruction21t>(insnIndex)
assertTrue(insn.target.isPlaced, "Label was not placed")
assertEquals(labelIndex, insn.target.location.index)
}
Expand Down

0 comments on commit 49c173d

Please sign in to comment.