Merge pull request #412 from mipt-npm/commandertvis/asm-debug

Add debug property for dumping classes generated with ASM
This commit is contained in:
Alexander Nozik 2021-08-21 18:07:27 +03:00 committed by GitHub
commit b0bbdc122e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,9 +14,11 @@ import space.kscience.kmath.expressions.Expression
import space.kscience.kmath.expressions.MST import space.kscience.kmath.expressions.MST
import java.lang.invoke.MethodHandles import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType import java.lang.invoke.MethodType
import java.nio.file.Paths
import java.util.stream.Collectors.toMap import java.util.stream.Collectors.toMap
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
import kotlin.io.path.writeBytes
/** /**
* ASM Builder is a structure that abstracts building a class designated to unwrap [MST] to plain Java expression. * ASM Builder is a structure that abstracts building a class designated to unwrap [MST] to plain Java expression.
@ -194,15 +196,18 @@ internal class AsmBuilder<T>(
visitEnd() visitEnd()
} }
val cls = classLoader.defineClass(className, classWriter.toByteArray()) val binary = classWriter.toByteArray()
// java.io.File("dump.class").writeBytes(classWriter.toByteArray()) val cls = classLoader.defineClass(className, binary)
if (System.getProperty("space.kscience.communicator.prettyapi.dump.generated.classes") == "1")
Paths.get("$className.class").writeBytes(binary)
val l = MethodHandles.publicLookup() val l = MethodHandles.publicLookup()
if (hasConstants) (if (hasConstants)
l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array<Any>::class.java)) l.findConstructor(cls, MethodType.methodType(Void.TYPE, Array<Any>::class.java))(constants.toTypedArray())
.invoke(constants.toTypedArray()) as Expression<T>
else else
l.findConstructor(cls, MethodType.methodType(Void.TYPE)).invoke() as Expression<T> l.findConstructor(cls, MethodType.methodType(Void.TYPE))()) as Expression<T>
} }
/** /**