Skip to content

wasmtime downloading support #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ To build and run the application:
`./gradlew wasmWasiWasmEdgeTest`
<br>&nbsp;<br>

* **Run the program with Wasmtime:**

`./gradlew wasmWasiWasmtimeRun`

> **Note:**
> The Exception Handling proposal [is not yet implemented](https://github.com/bytecodealliance/wasmtime/issues/3427)

* **Run tests with Wasmtime:**

`./gradlew wasmWasiWasmtimeTest`

* **Run the program with NodeJs:**

`./gradlew wasmWasiNodeRun`
Expand Down
141 changes: 141 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import org.gradle.internal.os.OperatingSystem
import de.undercouch.gradle.tasks.download.Download
import org.gradle.api.internal.file.archive.compression.*
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import org.jetbrains.kotlin.gradle.testing.internal.KotlinTestReport
import java.io.*
import java.net.*
import java.nio.file.Files
import java.util.Locale

Expand All @@ -14,6 +17,13 @@ plugins {
alias(libs.plugins.undercouchDownload) apply false
}

buildscript {
dependencies {
// to extract `tar.xz`
classpath("org.tukaani:xz:1.9")
}
}

repositories {
mavenCentral()
}
Expand Down Expand Up @@ -352,3 +362,134 @@ tasks.withType<NodeJsExec>().all {
)
}
}

// Wasmtime tasks
val wasmtimeVersion = "dev" // only `dev` supports GC

val wasmtimeSuffix = when (currentOsType) {
OsType(OsName.LINUX, OsArch.X86_64) -> "x86_64-linux"
OsType(OsName.LINUX, OsArch.ARM64) -> "aarch64-linux"
OsType(OsName.MAC, OsArch.X86_64) -> "x86_64-macos"
OsType(OsName.MAC, OsArch.ARM64) -> "aarch64-macos"
OsType(OsName.WINDOWS, OsArch.X86_32),
OsType(OsName.WINDOWS, OsArch.X86_64) -> "x86_64-windows"

else -> error("unsupported os type $currentOsType")
}

val wasmtimeArtifactName = "wasmtime-$wasmtimeVersion-$wasmtimeSuffix"

val unzipWasmtime = run {
val wasmtimeDirectory = "https://github.com/bytecodealliance/wasmtime/releases/download/$wasmtimeVersion"
val archiveType = if (currentOsType.name == OsName.WINDOWS) "zip" else "tar.xz"
val wasmtimeArchiveName = "$wasmtimeArtifactName.$archiveType"
val wasmtimeLocation = "$wasmtimeDirectory/$wasmtimeArchiveName"

val downloadedTools = File(layout.buildDirectory.asFile.get(), "tools")

val downloadWasmtime = tasks.register("wasmtimeDownload", Download::class) {
src(wasmtimeLocation)
dest(File(downloadedTools, wasmtimeArchiveName))
overwrite(false)
}

tasks.register("wasmtimeUnzip", Copy::class) {
dependsOn(downloadWasmtime)

val archive = downloadWasmtime.get().dest

from(if (archive.extension == "zip") zipTree(archive) else tarTree(XzArchiver(archive)))

into(downloadedTools)
}
}

private class XzArchiver(private val file: File) : CompressedReadableResource {
override fun read(): InputStream = org.tukaani.xz.XZInputStream(file.inputStream().buffered())
override fun getURI(): URI = URIBuilder(file.toURI()).schemePrefix("xz:").build()
override fun getBackingFile(): File = file
override fun getBaseName(): String = file.name
override fun getDisplayName(): String = file.path
}

fun Project.createWasmtimeExec(
nodeMjsFile: RegularFileProperty,
taskName: String,
taskGroup: String?,
startFunction: String
): TaskProvider<Exec> {
val outputDirectory = nodeMjsFile.map { it.asFile.parentFile }
val wasmFileName = nodeMjsFile.map { "${it.asFile.nameWithoutExtension}.wasm" }

return tasks.register(taskName, Exec::class) {
dependsOn(unzipWasmtime)
inputs.property("wasmFileName", wasmFileName)

taskGroup?.let { group = it }

description = "Executes tests with Wasmtime"

val wasmtimeDirectory = unzipWasmtime.get().destinationDir.resolve(wasmtimeArtifactName)

val executableName = when (currentOsType.name) {
OsName.WINDOWS -> "wasmtime.exe"
else -> "wasmtime"
}
executable = wasmtimeDirectory.resolve(executableName).absolutePath

doFirst {
val newArgs = mutableListOf<String>()

newArgs.add("-W")
newArgs.add("function-references,gc")

newArgs.add("-D")
newArgs.add("logging=y")

newArgs.add("--invoke")
newArgs.add(startFunction)

newArgs.add(wasmFileName.get())

args(newArgs)
workingDir(outputDirectory)

// to show stacktraces
environment("RUST_BACKTRACE", "full")
}
}
}

tasks.withType<KotlinJsTest>().all {
val wasmtimeRunTask = createWasmtimeExec(
inputFileProperty,
name.replace("Node", "Wasmtime"),
group,
"startUnitTests"
)

wasmtimeRunTask.configure {
dependsOn(
project.provider { [email protected] }
)
}

tasks.withType<KotlinTestReport> {
dependsOn(wasmtimeRunTask)
}
}

tasks.withType<NodeJsExec>().all {
val wasmtimeRunTask = createWasmtimeExec(
inputFileProperty,
name.replace("Node", "Wasmtime"),
group,
"dummy"
)

wasmtimeRunTask.configure {
dependsOn(
project.provider { [email protected] }
)
}
}