Skip to content

Commit 3f32f2d

Browse files
Abduqodiri Qurbonzodaqurbonzoda
authored andcommitted
Support nodejs() environment for wasmJs
1 parent 29b873e commit 3f32f2d

File tree

7 files changed

+58
-13
lines changed

7 files changed

+58
-13
lines changed

examples/kotlin-multiplatform/build.gradle

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ kotlin {
2020
compilations.create("defaultExecutor") { associateWith(compilations.main) }
2121
compilations.create("builtInExecutor") { associateWith(compilations.main) }
2222
}
23-
wasm('wasmJs') { d8() }
23+
wasm('wasmJs') { nodejs() }
2424

2525
// Native targets
2626
macosX64()
@@ -134,4 +134,15 @@ benchmark {
134134
register("linuxX64")
135135
register("mingwX64")
136136
}
137-
}
137+
}
138+
139+
// Node.js with canary v8 that supports recent Wasm GC changes
140+
rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
141+
nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
142+
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
143+
}
144+
145+
// Drop this when node js version become stable
146+
rootProject.tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask.class).configureEach {
147+
args.add("--ignore-engines")
148+
}

integration/src/test/kotlin/kotlinx/benchmark/integration/InvalidTargetingTest.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@ class InvalidTargetingTest : GradleTest() {
77
@Test
88
fun testWasmNodeJs() {
99
val runner = project("invalid-target/wasm-nodejs", true)
10-
runner.runAndFail("wasmJsBenchmark") {
11-
assertOutputContains("kotlinx-benchmark supports only d8() environment for Kotlin/Wasm.")
12-
}
10+
runner.run("wasmJsBenchmark") // Successful
1311
}
1412

1513
@Test
1614
fun testWasmBrowser() {
1715
val runner = project("invalid-target/wasm-browser", true)
1816
runner.runAndFail("wasmJsBenchmark") {
19-
assertOutputContains("kotlinx-benchmark supports only d8() environment for Kotlin/Wasm.")
17+
assertOutputContains("kotlinx-benchmark only supports d8() and nodejs() environments for Kotlin/Wasm.")
2018
}
2119
}
2220

2321
@Test
2422
fun testJsD8() {
2523
val runner = project("invalid-target/js-d8", true)
2624
runner.runAndFail("jsBenchmark") {
27-
assertOutputContains("kotlinx-benchmark supports only nodejs() environment for Kotlin/JS.")
25+
assertOutputContains("kotlinx-benchmark only supports nodejs() environment for Kotlin/JS.")
2826
}
2927
}
3028

@@ -40,7 +38,7 @@ class InvalidTargetingTest : GradleTest() {
4038
fun testJsBrowser() {
4139
val runner = project("invalid-target/js-browser", true)
4240
runner.runAndFail("jsBenchmark") {
43-
assertOutputContains("kotlinx-benchmark supports only nodejs() environment for Kotlin/JS.")
41+
assertOutputContains("kotlinx-benchmark only supports nodejs() environment for Kotlin/JS.")
4442
}
4543
}
4644
}

integration/src/test/resources/templates/invalid-target/wasm-nodejs/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,29 @@ kotlin {
22
wasm("wasmJs") {
33
nodejs()
44
}
5+
6+
sourceSets {
7+
commonMain {
8+
dependencies {
9+
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.5.0-SNAPSHOT")
10+
}
11+
}
12+
}
513
}
614

715
benchmark {
816
targets {
917
register("wasmJs")
1018
}
1119
}
20+
21+
// Node.js with canary v8 that supports recent Wasm GC changes
22+
rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
23+
nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
24+
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
25+
}
26+
27+
// Drop this when node js version become stable
28+
tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask.class).configureEach {
29+
args.add("--ignore-engines")
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test
2+
3+
import kotlinx.benchmark.*
4+
import kotlin.math.*
5+
6+
@State(Scope.Benchmark)
7+
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
8+
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
9+
@BenchmarkMode(Mode.Throughput)
10+
open class CommonBenchmark {
11+
@Benchmark
12+
open fun mathBenchmark(): Double {
13+
return log(sqrt(3.0) * cos(3.0), 2.0)
14+
}
15+
}

plugin/main/src/kotlinx/benchmark/gradle/JsEngineExecTasks.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ fun Project.createJsEngineBenchmarkExecTask(
2626
if (compilationTarget.isD8Configured) {
2727
val execTask = createD8Exec(config, target, compilation, taskName)
2828
tasks.getByName(config.prefixName(RUN_BENCHMARKS_TASKNAME)).dependsOn(execTask)
29+
} else if (compilationTarget.isNodejsConfigured) {
30+
val execTask = createNodeJsExec(config, target, compilation, taskName)
31+
tasks.getByName(config.prefixName(RUN_BENCHMARKS_TASKNAME)).dependsOn(execTask)
2932
} else {
30-
throw GradleException("kotlinx-benchmark supports only d8() environment for Kotlin/Wasm.")
33+
throw GradleException("kotlinx-benchmark only supports d8() and nodejs() environments for Kotlin/Wasm.")
3134
}
3235
}
3336
KotlinPlatformType.js -> {
3437
if (compilationTarget.isNodejsConfigured) {
3538
val execTask = createNodeJsExec(config, target, compilation, taskName)
3639
tasks.getByName(config.prefixName(RUN_BENCHMARKS_TASKNAME)).dependsOn(execTask)
3740
} else {
38-
throw GradleException("kotlinx-benchmark supports only nodejs() environment for Kotlin/JS.")
41+
throw GradleException("kotlinx-benchmark only supports nodejs() environment for Kotlin/JS.")
3942
}
4043
}
4144
else -> {
@@ -62,7 +65,6 @@ private val KotlinJsIrCompilation.isWasmCompilation: Boolean get() =
6265

6366
private fun MutableList<String>.addWasmArguments() {
6467
add("--experimental-wasm-gc")
65-
add("--experimental-wasm-eh")
6668
}
6769

6870
private fun MutableList<String>.addJsArguments() {

runtime/jsMain/src/kotlinx/benchmark/js/JsBenchmarkExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class JsBenchmarkExecutor(name: String, @Suppress("UNUSED_PARAMETER") dummy_args
77
SuiteExecutor(name, jsEngineSupport.arguments()[0]) {
88

99
init {
10-
check(!isD8) { "${JsBenchmarkExecutor::class.simpleName} does not supports d8 engine" }
10+
check(!isD8) { "${JsBenchmarkExecutor::class.simpleName} does not support d8 engine" }
1111
}
1212

1313
private val benchmarkJs: dynamic = require("benchmark")

runtime/jsMain/src/kotlinx/benchmark/js/JsBuiltInExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class JsBuiltInExecutor(
1818
complete: () -> Unit
1919
) {
2020
if (benchmarks.any { it.isAsync }) {
21-
error("${JsBuiltInExecutor::class.simpleName} does not supports async functions")
21+
error("${JsBuiltInExecutor::class.simpleName} does not support async functions")
2222
}
2323
super.run(runnerConfiguration, benchmarks, start, complete)
2424
}

0 commit comments

Comments
 (0)