From 475af0b2e116fdb12eb3d5886d5f728e0b85902a Mon Sep 17 00:00:00 2001 From: "Evgeniy.Zhelenskiy" Date: Mon, 23 Oct 2023 19:04:42 +0200 Subject: [PATCH] Add wat generation for Kotlin/Wasm --- .../server/compiler/components/KotlinToJSTranslator.kt | 4 ++-- .../kotlin/com/compiler/server/model/ExecutionResult.kt | 1 + .../com/compiler/server/generator/TestProjectRunner.kt | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/compiler/server/compiler/components/KotlinToJSTranslator.kt b/src/main/kotlin/com/compiler/server/compiler/components/KotlinToJSTranslator.kt index 0c3c506e0..c6070e55c 100644 --- a/src/main/kotlin/com/compiler/server/compiler/components/KotlinToJSTranslator.kt +++ b/src/main/kotlin/com/compiler/server/compiler/components/KotlinToJSTranslator.kt @@ -176,11 +176,11 @@ class KotlinToJSTranslator( baseFileName = "moduleId", emitNameSection = false, allowIncompleteImplementations = true, - generateWat = false, + generateWat = true, generateSourceMaps = false ) - return TranslationWasmResult(res.jsUninstantiatedWrapper, res.jsWrapper, res.wasm) + return TranslationWasmResult(res.jsUninstantiatedWrapper, res.jsWrapper, res.wasm, res.wat) } private fun getJsCodeFromModule(compiledModule: CompilerResult): String { diff --git a/src/main/kotlin/com/compiler/server/model/ExecutionResult.kt b/src/main/kotlin/com/compiler/server/model/ExecutionResult.kt index 0a593f64e..19c5c4f87 100644 --- a/src/main/kotlin/com/compiler/server/model/ExecutionResult.kt +++ b/src/main/kotlin/com/compiler/server/model/ExecutionResult.kt @@ -37,6 +37,7 @@ data class TranslationWasmResult( override val jsCode: String? = null, val jsInstantiated: String, val wasm: ByteArray, + val wat: String?, override var exception: ExceptionDescriptor? = null, override var errors: Map> = emptyMap() ) : TranslationResultWithJsCode(jsCode, errors, exception) diff --git a/src/test/kotlin/com/compiler/server/generator/TestProjectRunner.kt b/src/test/kotlin/com/compiler/server/generator/TestProjectRunner.kt index 28b687fa0..74d904ba0 100644 --- a/src/test/kotlin/com/compiler/server/generator/TestProjectRunner.kt +++ b/src/test/kotlin/com/compiler/server/generator/TestProjectRunner.kt @@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component import java.io.IOException import kotlin.io.path.* +import kotlin.test.assertTrue @Component @@ -166,6 +167,14 @@ class TestProjectRunner { val wasmMain = tmpDir.resolve("moduleId.wasm") wasmMain.writeBytes(result.wasm) + val wat = result.wat + val maxWatLengthInMessage = 97 + val formattedWat = wat?.let { if (it.length > maxWatLengthInMessage) "${it.take(maxWatLengthInMessage)}..." else it } + assertTrue( + actual = wat != null && wat.dropWhile { it.isWhitespace() }.startsWith("(module"), + message = "wat is expected to start with \"(module\" but is $formattedWat" + ) + val textResult = startNodeJsApp( System.getenv("kotlin.wasm.node.path"), jsMain.normalize().absolutePathString()