generated from polyvariant/library-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
154 additions
and
1 deletion.
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,23 @@ | ||
<html> | ||
<body> | ||
<h1>Classfile analyzer v0.0.0.0.0.1</h1> | ||
<h2>Pls upload classfile</h2> | ||
<input type="file" id="file_input" accept=".class" /> | ||
<div id="output"></div> | ||
</body> | ||
<script | ||
type="text/javascript" | ||
src=".js/target/scala-3.2.1/demo-fastopt.js" | ||
></script> | ||
<style> | ||
*, | ||
*:before, | ||
*:after { | ||
box-sizing: border-box; | ||
} | ||
td { | ||
border: 1px solid black; | ||
padding: 10px; | ||
} | ||
</style> | ||
</html> |
100 changes: 100 additions & 0 deletions
100
demo/src/main/scala/org/polyvariant/classfile/demo/DemoMain.scala
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,100 @@ | ||
package org.polyvariant.classfile.demo | ||
|
||
import org.scalajs.dom._ | ||
import org.scalajs.dom.HTMLInputElement | ||
import scala.scalajs.js.typedarray.Uint8Array | ||
import scalatags.JsDom.all._ | ||
import scodec.bits.ByteVector | ||
import org.polyvariant.classfile.codecs.ClassFileCodecs | ||
import org.polyvariant.classfile.examples.ExampleCode | ||
import org.polyvariant.classfile.examples.MethodModel | ||
import scalatags.JsDom.TypedTag | ||
import org.polyvariant.classfile.examples.AttributeModel | ||
|
||
object DemoMain { | ||
|
||
val fileInput = document.getElementById("file_input").asInstanceOf[HTMLInputElement] | ||
|
||
val output = document.getElementById("output") | ||
|
||
def renderMethod(method: MethodModel): TypedTag[Element] = tr( | ||
td(method.name), | ||
td(method.descriptor), | ||
td( | ||
table( | ||
tbody( | ||
method.attributes.map { attr => | ||
val extras = | ||
attr match { | ||
case c: AttributeModel.Code => | ||
List( | ||
td("max stack: " + c.maxStack), | ||
td("max locals: " + c.maxLocals), | ||
td("instruction count: " + c.code.length), | ||
td("exception table count: " + c.exceptionTable.length), | ||
td("attriute count: " + c.attributes.length), | ||
) | ||
case _ => td("attribute unknown") :: Nil | ||
} | ||
|
||
tr( | ||
td(attr.attrName), | ||
extras, | ||
) | ||
} | ||
) | ||
) | ||
), | ||
) | ||
|
||
def handleBytes( | ||
bytes: Array[Byte] | ||
): Unit = { | ||
while (output.firstChild != null) | ||
output.removeChild(output.firstChild) | ||
|
||
output.appendChild(p(s"Loaded ", b(bytes.length), " bytes").render) | ||
|
||
val bits = ByteVector(bytes).bits | ||
|
||
val decoded = | ||
ClassFileCodecs | ||
.classFile | ||
.decode(bits) | ||
.map(_.value) | ||
.map(ExampleCode.decode) | ||
.toTry | ||
.get | ||
|
||
output.appendChild( | ||
div( | ||
p("Loaded class: " + decoded.thisClass), | ||
p("Superclass: " + decoded.superClass), | ||
p("Methods:"), | ||
table( | ||
thead( | ||
tr( | ||
th("name"), | ||
th("descriptor"), | ||
th("attributes"), | ||
) | ||
), | ||
tbody( | ||
decoded.methods.map(renderMethod) | ||
), | ||
), | ||
p("toString: "), | ||
pre(code(pprint(decoded).plainText)), | ||
).render | ||
) | ||
} | ||
|
||
def main(args: Array[String]): Unit = | ||
fileInput.onchange = { _ => | ||
val file = fileInput.files.item(0) | ||
file | ||
.arrayBuffer() | ||
.`then`(bytes => handleBytes(new Uint8Array(bytes).toArray.map(_.toByte))) | ||
} | ||
|
||
} |