[K/Wasm] Unmute most of the stepping tests for Wasm in K1

This commit is contained in:
Artem Kobzar
2023-12-28 16:32:10 +00:00
committed by Space Team
parent 3fd9e22f37
commit 327085e026
120 changed files with 3370 additions and 174 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.wasm.test.handlers
import org.jetbrains.kotlin.backend.wasm.WasmCompilerResult
import org.jetbrains.kotlin.backend.wasm.writeCompilationResult
import org.jetbrains.kotlin.js.parser.sourcemaps.*
import org.jetbrains.kotlin.test.DebugMode
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.configuration.WasmEnvironmentConfigurator
@@ -15,6 +16,7 @@ import org.jetbrains.kotlin.test.services.moduleStructure
import org.jetbrains.kotlin.test.utils.SteppingTestLoggedData
import org.jetbrains.kotlin.test.utils.checkSteppingTestResult
import org.jetbrains.kotlin.test.utils.formatAsSteppingTestExpectation
import org.jetbrains.kotlin.utils.addToStdlib.butIf
import org.jetbrains.kotlin.wasm.test.tools.WasmVM
import java.io.File
@@ -31,18 +33,13 @@ class WasmDebugRunner(testServices: TestServices) : AbstractWasmArtifactsCollect
val originalFile = testServices.moduleStructure.originalTestDataFiles.first()
val mainModule = WasmEnvironmentConfigurator.getMainModule(testServices)
val collectedJsArtifacts = collectJsArtifacts(originalFile)
val debugMode = DebugMode.fromSystemProperty("kotlin.wasm.debugMode")
val testFileContent = """
let messageId = 0;
const locations = [];
function addLocation(frame) {
locations.push({
functionName: frame.functionName,
functionLine: frame.functionLocation?.lineNumber,
functionColumn: frame.functionLocation?.columnNumber,
line: frame.location.lineNumber,
column: frame.location.columnNumber
})
locations.push({ functionName: frame.functionName, line: frame.location.lineNumber, column: frame.location.columnNumber })
}
function sendMessage(message) { send(JSON.stringify(Object.assign(message, { id: messageId++ }))) }
function enableDebugger() { sendMessage({ method: 'Debugger.enable' }) }
@@ -76,7 +73,9 @@ class WasmDebugRunner(testServices: TestServices) : AbstractWasmArtifactsCollect
enableDebugger();
setBreakpoint(box);
box();
try {
box();
} catch(e) { console.error(e) }
disableDebugger();
print(JSON.stringify(locations))
""".trimIndent()
@@ -92,6 +91,37 @@ class WasmDebugRunner(testServices: TestServices) : AbstractWasmArtifactsCollect
val (jsFilePaths) = collectedJsArtifacts.saveJsArtifacts(dir)
if (debugMode >= DebugMode.DEBUG) {
File(dir, "index.html").writeText(
"""
<!DOCTYPE html>
<html lang="en">
<body>
<span id="test">UNKNOWN</span>
<script type="module">
let test = document.getElementById("test")
try {
const jsModule = await import('./index.mjs');
try { jsModule.default.box(); } catch(e) { alert(e) }
test.style.backgroundColor = "#0f0";
test.textContent = "OK"
} catch(e) {
test.style.backgroundColor = "#f00";
test.textContent = "NOT OK"
throw e;
}
</script>
</body>
</html>
""".trimIndent()
)
// To have access to the content of original files from a browser's DevTools
testServices.moduleStructure.modules.last().files.forEach {
if (it.originalFile === originalFile) File(dir, it.name).writeText(it.originalContent)
}
}
val exception = try {
val result = WasmVM.V8.run(
entryMjs = "./${collectedJsArtifacts.entryPath}",
@@ -99,38 +129,42 @@ class WasmDebugRunner(testServices: TestServices) : AbstractWasmArtifactsCollect
workingDirectory = dir,
toolArgs = listOf("--enable-inspector", "--allow-natives-syntax")
)
val parsedLocations = FrameParser(result).parse().mapNotNull { frame ->
val originalFunctionName = frame.currentFunctionLocation.let {
sourceMap.segmentForGeneratedLocation(it.line, it.column)?.name ?: frame.functionName
}
val pausedLocation = sourceMap.segmentForGeneratedLocation(
frame.pausedLocation.line,
frame.pausedLocation.column,
)
val debuggerSteps = FrameParser(result).parse().mapNotNull { frame ->
val pausedLocation = sourceMap.segmentForGeneratedLocation(frame.pausedLocation.line, frame.pausedLocation.column)
?.takeIf { it.sourceLineNumber >= 0 }
val testFileName = pausedLocation?.sourceFileName
if (testFileName == null || pausedLocation.sourceLineNumber < 0) {
null
} else {
SteppingTestLoggedData(
pausedLocation.sourceLineNumber + 1,
false,
formatAsSteppingTestExpectation(
testFileName,
pausedLocation.sourceLineNumber + 1,
originalFunctionName,
false
)
pausedLocation?.sourceFileName?.let { sourceFileName ->
ProcessedStep(
sourceFileName,
frame.functionName,
Location(pausedLocation.sourceLineNumber, pausedLocation.sourceColumnNumber)
)
}
}
val groupedByLinesSteppingTestLoggedData = debuggerSteps
.groupBy { Triple(it.fileName, it.functionName, it.location.line) }
.map { (key, debuggerSteps) ->
val (fileName, functionName, lineNumber) = key
val aggregatedColumns = debuggerSteps
.takeIf { it.size > 1 }
.orEmpty()
.map { it.location.column }
.joinToString(", ")
.let { if (it.isNotEmpty()) " ($it)" else it }
SteppingTestLoggedData(
lineNumber + 1,
false,
formatAsSteppingTestExpectation(fileName, lineNumber + 1, functionName, false) + aggregatedColumns
)
}
checkSteppingTestResult(
frontendKind = mainModule.frontendKind,
mainModule.targetBackend ?: TargetBackend.WASM,
originalFile,
parsedLocations
groupedByLinesSteppingTestLoggedData
)
null
@@ -150,7 +184,8 @@ class WasmDebugRunner(testServices: TestServices) : AbstractWasmArtifactsCollect
}
private class Location(val line: Int, val column: Int)
private class Frame(val functionName: String, val currentFunctionLocation: Location, val pausedLocation: Location)
private class Frame(val functionName: String, val pausedLocation: Location)
private class ProcessedStep(val fileName: String, val functionName: String, val location: Location)
private class FrameParser(private val input: String) {
fun parse(): List<Frame> =
(parseJson(input) as JsonArray).elements
@@ -158,7 +193,6 @@ class WasmDebugRunner(testServices: TestServices) : AbstractWasmArtifactsCollect
val frameObject = it as JsonObject
Frame(
frameObject.properties["functionName"].asString(),
Location(frameObject.properties["functionLine"].asInt(), frameObject.properties["functionColumn"].asInt()),
Location(frameObject.properties["line"].asInt(), frameObject.properties["column"].asInt())
)
}
@@ -81,7 +81,8 @@ internal sealed class WasmVM(val shortName: String) {
internal class ExternalTool(val path: String) {
fun run(vararg arguments: String, workingDirectory: File? = null): String {
val command = arrayOf(path, *arguments)
val processBuilder = ProcessBuilder(*command).redirectErrorStream(true)
val processBuilder = ProcessBuilder(*command)
.redirectErrorStream(true)
if (workingDirectory != null) {
processBuilder.directory(workingDirectory)