Skip to content

Commit 67b168f

Browse files
authored
Merge pull request #836 from simple-robot/more-wasm-support
更多模块支持wasmJs平台: 所有与Ktor无关的多平台模块均支持wasmJs平台目标
2 parents 5b81696 + bc8814a commit 67b168f

File tree

16 files changed

+397
-166
lines changed

16 files changed

+397
-166
lines changed

buildSrc/src/main/kotlin/JsConfig.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ fun Project.configJsTestTasks() {
7171
}
7272
}
7373

74-
@Suppress("UNUSED_PARAMETER")
7574
inline fun KotlinWasmJsTargetDsl.configWasmJs(
7675
nodeJs: Boolean = true,
7776
browser: Boolean = true,
7877
block: () -> Unit = {}
7978
) {
79+
if (nodeJs) {
80+
nodejs()
81+
}
8082
// if (nodeJs && isLinux) {
81-
// win in candy node `21.0.0-v8-canary202309143a48826a08` is not supported
82-
// nodejs()
83+
// // win in candy node `21.0.0-v8-canary202309143a48826a08` is not supported
84+
// nodejs()
8385
// }
8486

8587
if (browser) {

simbot-api/build.gradle.kts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import love.forte.gradle.common.kotlin.multiplatform.applyTier1
2626
import love.forte.gradle.common.kotlin.multiplatform.applyTier2
2727
import love.forte.gradle.common.kotlin.multiplatform.applyTier3
2828
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
29+
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
2930

3031
plugins {
3132
kotlin("multiplatform")
@@ -58,9 +59,11 @@ kotlin {
5859
applyTier3()
5960

6061
// wasm?
61-
// @Suppress("OPT_IN_USAGE")
62-
// wasmJs()
63-
// @Suppress("OPT_IN_USAGE")
62+
@OptIn(ExperimentalWasmDsl::class)
63+
wasmJs {
64+
configWasmJs()
65+
}
66+
6467
// wasmWasi()
6568

6669
compilerOptions {
@@ -69,20 +72,13 @@ kotlin {
6972
)
7073
}
7174

72-
// withKotlinTargets { target ->
73-
// targets.findByName(target.name)?.compilations?.all {
74-
// // 'expect'/'actual' classes (including interfaces, objects, annotations, enums, and 'actual' typealiases) are in Beta. You can use -Xexpect-actual-classes flag to suppress this warning. Also see: https://youtrack.jetbrains.com/issue/KT-61573
75-
// kotlinOptions.freeCompilerArgs += "-Xexpect-actual-classes"
76-
// }
77-
// }
78-
7975
sourceSets {
8076
commonMain {
8177
dependencies {
8278
// jvm compile only
83-
compileOnly(libs.jetbrains.annotations)
84-
compileOnly(project(":simbot-commons:simbot-common-annotations"))
85-
compileOnly(libs.kotlinx.serialization.json)
79+
api(libs.jetbrains.annotations)
80+
api(project(":simbot-commons:simbot-common-annotations"))
81+
api(libs.kotlinx.serialization.json)
8682
api(project(":simbot-commons:simbot-common-suspend-runner"))
8783
api(project(":simbot-commons:simbot-common-core"))
8884
api(project(":simbot-commons:simbot-common-collection"))
@@ -105,6 +101,10 @@ kotlin {
105101

106102
jvmMain {
107103
dependencies {
104+
compileOnly(libs.jetbrains.annotations)
105+
compileOnly(libs.kotlinx.serialization.json)
106+
compileOnly(project(":simbot-commons:simbot-common-annotations"))
107+
108108
compileOnly(libs.kotlinx.coroutines.reactive)
109109
compileOnly(libs.kotlinx.coroutines.reactor)
110110
compileOnly(libs.kotlinx.coroutines.rx2)
@@ -126,20 +126,6 @@ kotlin {
126126
}
127127
}
128128

129-
nativeMain.dependencies {
130-
api(libs.kotlinx.serialization.json)
131-
api(libs.jetbrains.annotations)
132-
api(project(":simbot-commons:simbot-common-annotations"))
133-
134-
}
135-
136-
jsMain.dependencies {
137-
api(libs.kotlinx.serialization.json)
138-
api(project(":simbot-commons:simbot-common-annotations"))
139-
api(libs.jetbrains.annotations)
140-
141-
}
142-
143129
jsTest.dependencies {
144130
implementation(libs.ktor.client.js)
145131
implementation(libs.ktor.client.core)
@@ -165,7 +151,6 @@ kotlin {
165151
// implementation(libs.ktor.client.winhttp)
166152
}
167153

168-
169154
}
170155
}
171156

simbot-api/src/jsTest/kotlin/MessagesTests.js.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Project https://github.com/simple-robot/simpler-robot
55
66
*
7-
* This file is part of the Simple Robot Library.
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
88
*
99
* This program is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU Lesser General Public License as published by
@@ -24,4 +24,5 @@
2424
import love.forte.simbot.message.MessagesBuilder
2525

2626
internal actual fun MessagesBuilder.addIntoMessages() {
27+
// nothing.
2728
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.bot.configuration
25+
26+
import kotlinx.coroutines.CoroutineDispatcher
27+
28+
/**
29+
* 获取 `IO` 调度器,始终返回 `null`。
30+
*/
31+
internal actual fun ioDispatcher(): CoroutineDispatcher? = null
32+
33+
/**
34+
* 获取自定义调度器。不支持或无法构建时返回 `null`。
35+
*/
36+
internal actual fun customDispatcher(
37+
coreThreads: Int?,
38+
maxThreads: Int?,
39+
keepAliveMillis: Long?,
40+
name: String?
41+
): CoroutineDispatcher? = null
42+
43+
/**
44+
* 当平台为 Java21+ 的 JVM平台时得到虚拟线程调度器,否则得到 `null`。
45+
*/
46+
internal actual fun virtualDispatcher(): CoroutineDispatcher? = null
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.component
25+
26+
import love.forte.simbot.common.services.Services
27+
28+
/**
29+
* 获取通过 [addComponentFactoryProvider] 添加的内容的副本序列。
30+
*/
31+
public actual fun loadComponentProviders(): Sequence<ComponentFactoryProvider<*>> =
32+
Services.loadProviders<ComponentFactoryProvider<*>>().map { it() }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.event
25+
26+
import kotlinx.coroutines.Deferred
27+
import kotlinx.coroutines.await
28+
import kotlinx.coroutines.flow.Flow
29+
import kotlinx.coroutines.flow.toList
30+
import kotlin.js.Promise
31+
32+
/**
33+
* 收集 [StandardEventResult.CollectableReactivelyResult.content] 的结果并返回。
34+
* 如果结果不可收集或不支持收集,则得到原值。
35+
*
36+
* native 平台下支持 Kotlin Coroutines 本身的可挂起类型 [Deferred] 和 [Flow] 和 [Promise]。
37+
* 可收集类型参考 [StandardEventResult.CollectableReactivelyResult.content] 说明。
38+
*
39+
* @see StandardEventResult.CollectableReactivelyResult.content
40+
* @return The collected result.
41+
*/
42+
public actual suspend fun StandardEventResult.CollectableReactivelyResult.collectCollectableReactively(): Any? {
43+
return when (val c = content) {
44+
null -> null
45+
is Deferred<*> -> c.await()
46+
is Flow<*> -> c.toList()
47+
is Promise<*> -> c.await()
48+
else -> content
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.message
25+
26+
import kotlinx.serialization.modules.PolymorphicModuleBuilder
27+
28+
/**
29+
* 整合平台特别实现的序列化信息。
30+
*/
31+
internal actual fun PolymorphicModuleBuilder<Message.Element>.resolvePlatformStandardSerializers() {
32+
// nothing.
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.message
25+
26+
import love.forte.simbot.resource.Resource
27+
28+
/**
29+
* 将 [Resource] 转化为 [OfflineResourceImage]。
30+
*
31+
*/
32+
public actual fun Resource.toOfflineResourceImage(): OfflineResourceImage =
33+
SimpleOfflineResourceImage(this)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.plugin
25+
26+
import love.forte.simbot.common.services.Services
27+
import love.forte.simbot.component.addComponentFactoryProvider
28+
29+
/**
30+
* 加载所有通过 [addComponentFactoryProvider] 添加的函数构建出来的 [PluginFactoryProvider] 实例。
31+
*/
32+
public actual fun loadPluginProviders(): Sequence<PluginFactoryProvider<*>> =
33+
Services.loadProviders<PluginFactoryProvider<*>>().map { it() }

0 commit comments

Comments
 (0)