Skip to content

Commit

Permalink
Get WASI set up enough to do a directory list
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Jul 20, 2023
1 parent 84789c4 commit da1675b
Show file tree
Hide file tree
Showing 19 changed files with 3,436 additions and 0 deletions.
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

0 comments on commit da1675b

Please sign in to comment.