Skip to content

Commit

Permalink
Final WASM retake (with disabled libraries depending on coroutines/at…
Browse files Browse the repository at this point in the history
…omicfu) (#1677)
  • Loading branch information
soywiz committed Jun 12, 2023
1 parent 5239b87 commit 31e2313
Show file tree
Hide file tree
Showing 29 changed files with 210 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ open class KorgeExtension(
}
}

/**
* Configures WASM target
*/
fun targetWasm(binaryen: Boolean = false) {
target("wasm") {
project.configureWasm(projectType, binaryen)
}
}

/**
* Configures Desktop targets depending on the host:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun Project.configureAndroidDirect(projectType: ProjectType, isKorge: Boolean) {
//val generated = AndroidGenerated(korge)

dependencies {
if (SemVer(BuildVersions.KOTLIN) >= SemVer("1.9.20")) {
if (SemVer(BuildVersions.KOTLIN) >= SemVer("1.9.0")) {
add("androidUnitTestImplementation", "org.jetbrains.kotlin:kotlin-test")
}
add("androidTestImplementation", "org.jetbrains.kotlin:kotlin-test")
Expand Down
50 changes: 50 additions & 0 deletions buildSrc/src/main/kotlin/korlibs/korge/gradle/targets/js/Wasm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package korlibs.korge.gradle.targets.js

import korlibs.korge.gradle.gkotlin
import korlibs.korge.gradle.kotlin
import korlibs.korge.gradle.targets.*
import korlibs.korge.gradle.targets.js.configureWasm
import korlibs.korge.gradle.targets.wasm.*
import korlibs.korge.gradle.util.*
import korlibs.root.*
import org.gradle.api.*
import org.jetbrains.kotlin.gradle.targets.js.npm.*
import java.io.*

fun Project.configureWasm(projectType: ProjectType, binaryen: Boolean = false) {
if (gkotlin.targets.findByName("wasm") != null) return

configureWasm(executable = true, binaryen = binaryen)

if (projectType.isExecutable) {

project.tasks.createThis<Task>("wasmCreateIndex") {
doFirst {
wasmCreateIndex(project)
}
}
project.tasks.findByName("wasmBrowserDevelopmentRun")?.apply {
dependsOn("wasmCreateIndex")
doFirst { wasmCreateIndex(project) }
}
val task = project.tasks.createThis<Task>("runWasm") {
dependsOn("wasmRun")
}
}
}

fun wasmCreateIndex(project: Project) {
val compilation = project.kotlin.wasm().compilations["main"]!!
val npmDir = compilation.npmProject.dir
File(npmDir, "kotlin/index.html").also { it.parentFile.mkdirs() }.writeText(
"""
<html>
<script type = 'module'>
import module from "./${npmDir.name}.mjs"
console.log(module)
//instantiate();
</script>
</html>
""".trimIndent()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package korlibs.korge.gradle.targets.wasm

import korlibs.*
import org.gradle.api.*

fun Project.configureWasm(executable: Boolean, binaryen: Boolean = false) {
kotlin {
wasm {
if (binaryen) applyBinaryen()

if (executable) {
binaries.executable()
}
//applyBinaryen()
browser {
commonWebpackConfig { experiments = mutableSetOf("topLevelAwait") }
if (executable) {
this.distribution {
}
}
//testTask {
// it.useKarma {
// //useChromeHeadless()
// this.webpackConfig.configDirectory = File(rootProject.rootDir, "karma.config.d")
// }
//}
}
}
}
}
48 changes: 4 additions & 44 deletions buildSrc/src/main/kotlin/korlibs/root/RootKorlibsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import korlibs.korge.gradle.targets.all.*
import korlibs.korge.gradle.targets.android.*
import korlibs.korge.gradle.targets.ios.*
import korlibs.korge.gradle.targets.js.*
import korlibs.korge.gradle.targets.js.configureWasm
import korlibs.korge.gradle.targets.jvm.*
import korlibs.korge.gradle.targets.native.*
import korlibs.korge.gradle.targets.wasm.*
import korlibs.korge.gradle.util.*
import korlibs.korge.gradle.util.create
import korlibs.kotlin
Expand Down Expand Up @@ -355,21 +357,7 @@ object RootKorlibsPlugin {
AddFreeCompilerArgs.addFreeCompilerArgs(project, this)
}
if (project.findProperty("enable.wasm") == "true") {
wasm {
//this.
//this.applyBinaryen()
//nodejs { commonWebpackConfig { experiments = mutableSetOf("topLevelAwait") } }
//browser { commonWebpackConfig { experiments = mutableSetOf("topLevelAwait") } }
browser {
commonWebpackConfig { experiments = mutableSetOf("topLevelAwait") }
//testTask {
// it.useKarma {
// //useChromeHeadless()
// this.webpackConfig.configDirectory = File(rootProject.rootDir, "karma.config.d")
// }
//}
}
}
configureWasm(executable = false)
val wasmBrowserTest = tasks.getByName("wasmBrowserTest") as KotlinJsTest
// ~/projects/korge/build/js/packages/korge-root-klock-wasm-test
wasmBrowserTest.doFirst {
Expand Down Expand Up @@ -627,35 +615,7 @@ object RootKorlibsPlugin {
fun Project.initSamples() {
rootProject.samples {
if (project.findProperty("enable.wasm") == "true") {
kotlin {
wasm {
binaries.executable()
browser {
this.distribution {
}
//testTask {
// it.useKarma {
// //useChromeHeadless()
// this.webpackConfig.configDirectory = File(rootProject.rootDir, "karma.config.d")
// }
//}
}
}
}
fun wasmCreateIndex(project: Project) {
val compilation = project.kotlin.wasm().compilations["main"]!!
val npmDir = compilation.npmProject.dir
File(npmDir, "kotlin/index.html").also { it.parentFile.mkdirs() }.writeText(
"""
<html>
<script type = 'module'>
import { instantiate } from "./${npmDir.name}.uninstantiated.mjs"
instantiate();
</script>
</html>
""".trimIndent()
)
}
configureWasm(executable = true)
project.tasks.createThis<Task>("wasmCreateIndex") {
doFirst {
wasmCreateIndex(project)
Expand Down
5 changes: 4 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ proguard-gradle = "7.2.2"
kotlin = "1.8.21"
kotlinx-coroutines = "1.7.1"
kotlinx-atomicfu = "0.20.2"
kotlinx-serialization = "1.5.1"

#maven { url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") }

# https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev/org/jetbrains/kotlin/kotlin-stdlib/
#kotlin = "1.9.0-dev-6976"
Expand All @@ -18,8 +21,8 @@ kotlinx-atomicfu = "0.20.2"
#kotlinx-atomicfu = "0.20.2-wasm0"
# https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental/org/jetbrains/kotlinx/kotlinx-coroutines-core/
#kotlinx-coroutines = "1.7.0-RC-wasm0"
#kotlinx-serialization = "1.5.1-wasm0"

kotlinx-serialization = "1.5.1"
kotlinx-benchmark = "0.4.7"
dokka = "1.8.10"
kover = "0.6.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ package korlibs.datastructure.internal
// //return -1
//}

private val anyIdentityHashCodeOnce by lazy {
//console.error("!!!!! anyIdentityHashCode not implemented in WASM!")
println("!!!!! anyIdentityHashCode not implemented in WASM!")
Unit
}

internal actual fun anyIdentityHashCode(obj: Any?): Int {
if (obj == null) return 0
println("anyIdentityHashCode not implemented in WASM!")
anyIdentityHashCodeOnce
return -1
}
2 changes: 1 addition & 1 deletion korau/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#enable.wasm=true
enable.wasm=false
2 changes: 1 addition & 1 deletion korcoutines/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#enable.wasm=true
enable.wasm=false
2 changes: 1 addition & 1 deletion korge-sandbox/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#enable.wasm=true
enable.wasm=false
4 changes: 2 additions & 2 deletions korge-sandbox/src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ suspend fun main() = Korge(
Demo(::MainRotatedAtlas),
Demo(::MainConnect4),
Demo(::MainMutableAtlasTest),
Demo(::MainTerminalEmulator),
//Demo(::MainTerminalEmulator),
Demo(::MainBezierSample),
Demo(::MainEditor),
Demo(::MainAnimations),
Demo(::MainBunnymark),
//Demo(::MainBunnymark),
Demo(::MainFiltersSample),
Demo(::MainTextMetrics),
Demo(::MainRenderText),
Expand Down
4 changes: 2 additions & 2 deletions korge-sandbox/src/commonMain/kotlin/samples/MainBVH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import korlibs.math.random.*
import korlibs.time.*
import kotlin.random.*

class MainBVH : Scene() {
var SolidRect.movingDirection by extraProperty { -1 }
private var SolidRect.movingDirection by extraProperty { -1 }

class MainBVH : Scene() {
override suspend fun SContainer.sceneMain() {
val bvh = BVH2D<View>()
val rand = Random(0)
Expand Down
2 changes: 2 additions & 0 deletions korge-sandbox/src/commonMain/kotlin/samples/MainBunnymark.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import korlibs.time.*
import kotlin.collections.random
import kotlin.random.*

/*
class MainBunnymark : Scene() {
// @TODO: We could autogenerate this via gradle
val ResourcesContainer.korge_png by resourceBitmap("korge.png")
Expand Down Expand Up @@ -181,3 +182,4 @@ suspend fun main() {
*/
}
*/
7 changes: 3 additions & 4 deletions korge-sandbox/src/commonMain/kotlin/samples/MainHelloWorld.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import korlibs.image.bitmap.*
import korlibs.math.geom.degrees
import korlibs.math.interpolation.Easing

class MainHelloWorld : ScaledScene(512, 512) {
// @TODO: We could autogenerate this via gradle
val korlibs.io.resources.ResourcesContainer.korge_png by resourceBitmap("korge.png")
// @TODO: We could autogenerate this via gradle
private val korlibs.io.resources.ResourcesContainer.korge_png by resourceBitmap("korge.png")

class MainHelloWorld : ScaledScene(512, 512) {
override suspend fun SContainer.sceneMain() {

gameWindow.icon = korge_png.get().bmp.toBMP32().scaled(32, 32)

val minDegrees = (-16).degrees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import korlibs.image.format.*
import korlibs.io.file.std.*
import korlibs.math.geom.slice.*

/*
class MainTerminalEmulator : Scene() {
override suspend fun SContainer.sceneMain() {
container {
Expand Down Expand Up @@ -99,3 +100,4 @@ class MainTerminalEmulator : Scene() {
}
}
*/
2 changes: 1 addition & 1 deletion korge/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#enable.wasm=true
enable.wasm=false
2 changes: 1 addition & 1 deletion korgw/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#enable.wasm=true
enable.wasm=false
4 changes: 2 additions & 2 deletions korgw/src/iosMacosMain/kotlin/korlibs/metal/AGMetal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class AGMetal(
val uniform = block.block.uniforms.map { it.uniform }.first()
val bufferLocation = currentProgram.indexOfUniformOnBuffer(uniform)
logger.trace { "$uniform will be bind at location $bufferLocation" }
setVertexBuffer(buffer.toMetal.buffer, 0, bufferLocation)
setVertexBuffer(buffer.toMetal.buffer, 0.convert(), bufferLocation)
}

if (indices != null) {
Expand All @@ -101,7 +101,7 @@ class AGMetal(
vertexCount.toULong(),
indexType.toMetal(),
indices.toMetal.buffer,
0
0.convert()
)
//drawIndexedPrimitives(drawType.toMetal(), vertexCount.toULong(), indexType.toMetal(), indices.toMetal.buffer, 0.convert())
} else {
Expand Down
2 changes: 1 addition & 1 deletion korgw/src/wasmMain/kotlin/korlibs/graphics/gl/GlExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fun AGDefaultCanvas(): HTMLCanvasElement {
}

fun AGWebgl(config: AGConfig, canvas: HTMLCanvasElement = AGDefaultCanvas()): AGOpengl {
val kmlGl: KmlGl = KmlGlJsCanvas(
val kmlGl: KmlGl = KmlGlWasmCanvas(
canvas, jsObject(
"premultipliedAlpha" to false, // To be like the other targets
"alpha" to false,
Expand Down
4 changes: 2 additions & 2 deletions korgw/src/wasmMain/kotlin/korlibs/kgl/KmlGlContextJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class WebGLContext(window: Any?, parent: KmlGlContext?) : KmlGlContext(window, c
}

companion object {
fun createJsCanvas(): KmlGlJsCanvas {
fun createJsCanvas(): KmlGlWasmCanvas {
if (!Platform.isJsBrowser) error("Can't run WebGL outside a browser")
return KmlGlJsCanvas(document.createElement("canvas").unsafeCast<HTMLCanvasElement>(), jsObject())
return KmlGlWasmCanvas(document.createElement("canvas").unsafeCast<HTMLCanvasElement>(), jsObject())
}
}
}
Loading

0 comments on commit 31e2313

Please sign in to comment.