[Wasm] Add Promise and support tests that return Promise
Co-authored-by: Ilya Gorbunov <Ilya.Gorbunov@jetbrains.com> Merge-request: KT-MR-7717 Merged-by: Svyatoslav Kuzmich <svyatoslav.kuzmich@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
002f6bd34a
commit
7ec6608e29
@@ -201,4 +201,36 @@ fun <T> Nothing?.unsafeCast(): Dynamic? = null
|
||||
/**
|
||||
* Reinterprets Dynamic type value as a value of the specified type [T] without any actual type checking.
|
||||
*/
|
||||
fun <T> Dynamic.unsafeCast(): T = this as T
|
||||
fun <T> Dynamic.unsafeCast(): T = this as T
|
||||
|
||||
@JsFun("e => { throw e; }")
|
||||
private external fun jsThrow(e: Dynamic)
|
||||
|
||||
@JsFun("""(f) => {
|
||||
let result = null;
|
||||
try {
|
||||
f();
|
||||
} catch (e) {
|
||||
result = e;
|
||||
}
|
||||
return result;
|
||||
}""")
|
||||
private external fun jsCatch(f: () -> Unit): Dynamic?
|
||||
|
||||
/**
|
||||
* For a Dynamic value caught in JS, returns the corresponding [Throwable]
|
||||
* if it was thrown from Kotlin, or null otherwise.
|
||||
*/
|
||||
public fun Dynamic.toThrowableOrNull(): Throwable? {
|
||||
val thisAny: Any = this
|
||||
if (thisAny is Throwable) return thisAny
|
||||
var result: Throwable? = null
|
||||
jsCatch {
|
||||
try {
|
||||
jsThrow(this)
|
||||
} catch (e: Throwable) {
|
||||
result = e
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.js
|
||||
|
||||
/**
|
||||
* Exposes the JavaScript [Promise object](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) to Kotlin.
|
||||
*/
|
||||
public external class Promise<out T>(executor: (resolve: (Dynamic?) -> Unit, reject: (Dynamic) -> Unit) -> Unit) {
|
||||
public open fun then(onFulfilled: (Dynamic?) -> Dynamic?): Promise<Dynamic?>
|
||||
public open fun then(onFulfilled: (Dynamic?) -> Dynamic?, onRejected: (Dynamic) -> Dynamic?): Promise<Dynamic?>
|
||||
public open fun catch(onRejected: (Dynamic) -> Dynamic?): Promise<Dynamic?>
|
||||
public open fun finally(onFinally: () -> Unit): Promise<Dynamic?>
|
||||
|
||||
public companion object {
|
||||
public fun reject(e: Dynamic): Promise<Dynamic?>
|
||||
public fun resolve(e: Dynamic): Promise<Dynamic?>
|
||||
public fun resolve(e: Promise<Dynamic?>): Promise<Dynamic?>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
|
||||
package kotlin.js
|
||||
|
||||
external class Promise<T>
|
||||
|
||||
@PublishedApi
|
||||
internal val undefined: Nothing? = null
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package test.js
|
||||
|
||||
import kotlin.js.*
|
||||
import kotlin.test.*
|
||||
|
||||
@JsFun("async () => 'foo'")
|
||||
internal external fun jsAsyncFoo(): Promise<Dynamic?>
|
||||
|
||||
@JsFun("() => 'foo'")
|
||||
internal external fun jsFoo(): Dynamic
|
||||
|
||||
var state: Dynamic? = null
|
||||
|
||||
class MyThrowable : Throwable()
|
||||
|
||||
class AsyncTest {
|
||||
@Test
|
||||
fun test1(): Promise<Dynamic?> {
|
||||
var thenExecuted = false
|
||||
val p = jsAsyncFoo().then { value ->
|
||||
state = value
|
||||
thenExecuted = true
|
||||
null
|
||||
}
|
||||
assertFalse(thenExecuted)
|
||||
return p
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test2(): Promise<Dynamic?> {
|
||||
assertEquals(state, jsFoo())
|
||||
var thenExecuted = false
|
||||
val p = jsAsyncFoo().then {
|
||||
assertEquals(state, jsFoo())
|
||||
thenExecuted = true
|
||||
null
|
||||
}.then {
|
||||
assertEquals(state, jsFoo())
|
||||
thenExecuted = true
|
||||
null
|
||||
}
|
||||
assertFalse(thenExecuted)
|
||||
return p
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testJsValueToThrowableOrNull1(): Promise<Dynamic?> {
|
||||
val p = jsAsyncFoo().then {
|
||||
throw MyThrowable()
|
||||
null
|
||||
}.catch { e ->
|
||||
assert(e.toThrowableOrNull() is MyThrowable)
|
||||
null
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testJsValueToThrowableOrNull2() {
|
||||
val e = MyThrowable()
|
||||
assertEquals((e as Dynamic).toThrowableOrNull(), e)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user