Skip to content

Commit

Permalink
Merge pull request #694 from zhelenskiy/add_wat
Browse files Browse the repository at this point in the history
Add wat generation for Kotlin/Wasm
  • Loading branch information
nikpachoo authored Oct 24, 2023
2 parents 02c676a + 475af0b commit 4ba7e76
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<ErrorDescriptor>> = emptyMap()
) : TranslationResultWithJsCode(jsCode, errors, exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4ba7e76

Please sign in to comment.