Skip to content
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

Get WASI set up enough to do a directory list #1297

Merged
merged 11 commits into from
Jul 21, 2023
12 changes: 12 additions & 0 deletions okio-wasifilesystem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
WASI FileSystem
===============

⚠️ This is a work in progress ⚠️

This module implements Okio's FileSystem API using the [WebAssembly System Interface (WASI)][wasi].

It currently uses the WASI [preview1] APIs and is tested on NodeJS with the
`--experimental-wasi-unstable-preview1` option.

[wasi]: https://wasi.dev/
[preview1]: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md
89 changes: 89 additions & 0 deletions okio-wasifilesystem/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import com.vanniktech.maven.publish.JavadocJar.Dokka
import com.vanniktech.maven.publish.KotlinMultiplatform
import com.vanniktech.maven.publish.MavenPublishBaseExtension
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest

plugins {
kotlin("multiplatform")
id("org.jetbrains.dokka")
id("com.vanniktech.maven.publish.base")
id("build-support")
id("binary-compatibility-validator")
}

kotlin {
configureOrCreateWasmPlatform()
sourceSets {
val wasmMain by getting {
dependencies {
implementation(projects.okio)
}
}
val wasmTest by getting {
dependencies {
implementation(libs.kotlin.test)
}
}
}
}

configure<MavenPublishBaseExtension> {
configure(
KotlinMultiplatform(javadocJar = Dokka("dokkaGfm")),
)
}

/**
* Inspired by runner.mjs in kowasm, this rewrites the JavaScript bootstrap script to set up WASI.
*
* See also:
* * https://github.com/kowasm/kowasm
* * https://github.com/nodejs/node/blob/main/doc/api/wasi.md
*
* This task overwrites the output of `compileTestKotlinWasm` and must run after that task. It
* must also run before the WASM test execution tasks that read this script.
*
* Note that this includes which file paths are exposed to the WASI sandbox.
*/
val injectWasiInit by tasks.creating {
dependsOn("compileTestKotlinWasm")
val moduleName = "${rootProject.name}-${project.name}-wasm-test"

val entryPointMjs = File(
buildDir,
"compileSync/wasm/test/testDevelopmentExecutable/kotlin/$moduleName.mjs"
)

outputs.file(entryPointMjs)

doLast {
entryPointMjs.writeText(
"""
import {instantiate} from './$moduleName.uninstantiated.mjs';
import {WASI} from "wasi";

export const wasi = new WASI({
version: 'preview1',
preopens: {
'/okio': '$rootDir'
}
});

const {instance, exports} = await instantiate({wasi_snapshot_preview1: wasi.wasiImport});

wasi.initialize(instance);

export default exports;
""".trimIndent()
)
}
}
tasks.withType<KotlinJsTest>().configureEach {
nodeJsArgs += "--experimental-wasi-unstable-preview1"
}
tasks.named("wasmTestTestDevelopmentExecutableCompileSync").configure {
dependsOn(injectWasiInit)
}
tasks.named("wasmTestTestProductionExecutableCompileSync").configure {
dependsOn(injectWasiInit)
}
Loading
Loading