Skip to content

Commit 5bd1082

Browse files
committed
update README.md
1 parent e8c6ec7 commit 5bd1082

File tree

2 files changed

+134
-8
lines changed

2 files changed

+134
-8
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,69 @@ class Foo {
117117
}
118118
```
119119

120+
### MarkName
121+
122+
> since v0.13.0, [#96](https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin/pull/96)
123+
124+
You can use `markName` to add a name mark annotation (e.g. `@JvmName`, `@JsName`) to the generated synthetic function.
125+
126+
For example the JVM:
127+
128+
```kotlin
129+
class Foo {
130+
@JvmBlocking(markName = "namedWaitAndGet")
131+
suspend fun waitAndGet(): String {
132+
delay(5)
133+
return "Hello"
134+
}
135+
}
136+
```
137+
138+
compiled 👇
139+
140+
```kotlin
141+
class Foo {
142+
// Hide from Java
143+
@JvmSynthetic
144+
suspend fun waitAndGet(): String {
145+
delay(5)
146+
return "Hello"
147+
}
148+
@Api4J // RequiresOptIn annotation, provide warnings to Kotlin
149+
@JvmName("namedWaitAndGet") // From the `markName`'s value
150+
fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() } // 'runInBlocking' from the runtime provided by the plugin
151+
}
152+
```
153+
154+
Note: `@JvmName` has limitations on non-final functions, and even the compiler may prevent compilation.
155+
156+
For example the JS:
157+
158+
```kotlin
159+
class Foo {
160+
@JsPromise(markName = "namedWaitAndGet")
161+
suspend fun waitAndGet(): String {
162+
delay(5)
163+
return "Hello"
164+
}
165+
}
166+
```
167+
168+
compiled 👇
169+
170+
```kotlin
171+
class Foo {
172+
suspend fun waitAndGet(): String {
173+
delay(5)
174+
return "Hello"
175+
}
176+
177+
@Api4Js // RequiresOptIn annotation, provide warnings to Kotlin
178+
@JsName("namedWaitAndGet") // From the `markName`'s value
179+
fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin
180+
}
181+
```
182+
120183
## Usage
121184

122185
### The version
@@ -246,7 +309,7 @@ You can also disable them and add dependencies manually.
246309
```Kotlin
247310
plugin {
248311
kotlin("jvm") version "..." // Take the Kotlin/JVM as an example
249-
id("love.forte.plugin.suspend-transform") version "2.1.20-0.12.0"
312+
id("love.forte.plugin.suspend-transform") version "<VERSION>"
250313
}
251314

252315
dependencies {

README_CN.md

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,69 @@ class Foo {
117117
}
118118
```
119119

120+
### MarkName
121+
122+
> 自 v0.13.0 起, [#96](https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin/pull/96)
123+
124+
你可以使用 `markName` 为生成的合成函数添加名称标记注解(例如 `@JvmName``@JsName`)。
125+
126+
例如 JVM:
127+
128+
```kotlin
129+
class Foo {
130+
@JvmBlocking(markName = "namedWaitAndGet")
131+
suspend fun waitAndGet(): String {
132+
delay(5)
133+
return "Hello"
134+
}
135+
}
136+
```
137+
138+
编译后 👇
139+
140+
```kotlin
141+
class Foo {
142+
// 对 Java 隐藏
143+
@JvmSynthetic
144+
suspend fun waitAndGet(): String {
145+
delay(5)
146+
return "Hello"
147+
}
148+
@Api4J // 需要显式启用的注解,向 Kotlin 提供警告
149+
@JvmName("namedWaitAndGet") // 来自 `markName` 的值
150+
fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() } // 'runInBlocking' 来自插件提供的运行时
151+
}
152+
```
153+
154+
注意:`@JvmName` 在非 final 函数上有限制,甚至编译器可能会阻止编译。
155+
156+
例如 JS:
157+
158+
```kotlin
159+
class Foo {
160+
@JsPromise(markName = "namedWaitAndGet")
161+
suspend fun waitAndGet(): String {
162+
delay(5)
163+
return "Hello"
164+
}
165+
}
166+
```
167+
168+
编译后 👇
169+
170+
```kotlin
171+
class Foo {
172+
suspend fun waitAndGet(): String {
173+
delay(5)
174+
return "Hello"
175+
}
176+
177+
@Api4Js // 需要显式启用的注解,向 Kotlin 提供警告
178+
@JsName("namedWaitAndGet") // 来自 `markName` 的值
179+
fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() } // 'runInAsync' 来自插件提供的运行时
180+
}
181+
```
182+
120183
## 使用方式
121184

122185
### 版本说明
@@ -222,7 +285,7 @@ suspendTransformPlugin {
222285
// 默认与插件版本相同
223286
version = "<ANNOTATION_VERSION>"
224287
}
225-
288+
226289
// 包含运行时
227290
// 默认为 `true`
228291
includeRuntime = true
@@ -315,7 +378,7 @@ class Cat {
315378
suspend fun meow() {
316379
// ...
317380
}
318-
381+
319382
// 生成:
320383
fun meowBlocking() {
321384
`$runInBlocking$` { meow() }
@@ -345,7 +408,7 @@ suspendTransformPlugin {
345408
class Cat {
346409
@JvmBlocking
347410
suspend fun meow(): String = "Meow!"
348-
411+
349412
// 生成:
350413
fun meowAsync(): CompletableFuture<out String> {
351414
`$runInAsync$`(block = { meow() }, scope = this as? CoroutineScope)
@@ -382,7 +445,7 @@ suspendTransformPlugin {
382445
class Cat {
383446
@JsPromise
384447
suspend fun meow(): String = "Meow!"
385-
448+
386449
// 生成:
387450
fun meowAsync(): Promise<String> {
388451
`$runInAsync$`(block = { meow() }, scope = this as? CoroutineScope)
@@ -437,7 +500,7 @@ suspendTransformPlugin {
437500
// 自定义时可能无需默认注解和运行时
438501
includeAnnotation = false
439502
includeRuntime = false
440-
503+
441504
transformer {
442505
// 具体配置见下文
443506
}
@@ -596,12 +659,12 @@ suspendTransformPlugin {
596659
defaultSuffix = "Blocking"
597660
defaultAsProperty = false
598661
}
599-
662+
600663
transformFunctionInfo {
601664
packageName = "com.example"
602665
functionName = "inBlock"
603666
}
604-
667+
605668
copyAnnotationsToSyntheticFunction = true
606669
copyAnnotationsToSyntheticProperty = true
607670

0 commit comments

Comments
 (0)