[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:
Svyatoslav Kuzmich
2022-11-25 20:56:47 +00:00
committed by Space Team
parent 002f6bd34a
commit 7ec6608e29
5 changed files with 215 additions and 28 deletions
+33 -1
View File
@@ -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