[K/Wasm] Add simple TypeScript definitions generating ^KT-65009 Fixed

This commit is contained in:
Artem Kobzar
2024-01-16 11:10:27 +00:00
committed by Space Team
parent baa0748375
commit a55c65e3e2
35 changed files with 1176 additions and 32 deletions
@@ -0,0 +1,29 @@
type Nullable<T> = T | null | undefined
export declare function getResult(): not.exported.org.second.Result<string>;
declare namespace not.exported.org.second {
class Result<T extends NonNullable<unknown>> extends not.exported.org.second.BaseResult<T> {
constructor();
}
}
declare namespace not.exported.org.second {
abstract class BaseResult<T extends NonNullable<unknown>> {
constructor(foo: typeof not.exported.org.second.Foo);
}
}
declare namespace not.exported.org.second {
const Foo: {
get bar(): number;
get baz(): string;
} & not.exported.Baz<string>;
}
declare namespace not.exported {
interface Baz<T> extends not.exported.Bar {
readonly baz?: T;
readonly bar: number;
}
}
declare namespace not.exported {
interface Bar {
readonly bar: number;
}
}
@@ -0,0 +1,38 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// TARGET_BACKEND: WASM
// MODULE: main
// FILE: first.kt
external interface Bar {
val bar: Int
}
external interface Baz<T: JsAny?> : Bar {
val baz: T
}
// FILE: second.kt
package org.second
import Bar
import Baz
external object Foo : Baz<JsString> {
override val bar: Int
override val baz: JsString
}
external abstract class BaseResult<T: JsAny>(foo: Foo)
external class Result<T: JsAny> : BaseResult<T>
fun getResultInternal(): Result<JsString> = js("({})")
@JsExport
fun getResult(): Result<JsString> = getResultInternal()
// FILE: entry.mjs
import main from "./index.mjs";
if (JSON.stringify(main.getResult()) != "{}") throw new Error("Unexpected result")
@@ -0,0 +1,20 @@
type Nullable<T> = T | null | undefined
export declare function simple<T>(x: T): T;
export declare function second<A, B>(a: A, b: B): B;
export declare function simpleWithConstraint<T extends number>(x: T): T;
export declare function complexConstraint<A extends not.exported.Foo<bigint> & not.exported.Bar, B extends typeof not.exported.Baz>(x: A): B;
declare namespace not.exported {
interface Foo<T extends NonNullable<unknown>> {
readonly foo: T;
}
}
declare namespace not.exported {
interface Bar {
readonly bar: string;
}
}
declare namespace not.exported {
const Baz: {
get baz(): boolean;
};
}
@@ -0,0 +1,39 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// TARGET_BACKEND: WASM
// MODULE: main
// FILE: generics.kt
@JsExport
fun <T: JsAny?> simple(x: T): T = x
@JsExport
fun <A: JsAny?, B: JsAny?> second(a: A, b: B): B = b
@JsExport
fun <T: JsNumber> simpleWithConstraint(x: T): T = x
external interface Foo<T: JsAny> : JsAny {
val foo: T
}
external interface Bar : JsAny {
val bar: JsString
}
external object Baz : JsAny {
val baz: JsBoolean
}
fun getBaz(x: Foo<JsBigInt>): JsAny = js("({ baz: x.foo > 0n })")
@JsExport
fun <A, B> complexConstraint(x: A): B where A: Foo<JsBigInt>, A: Bar, B: Baz = getBaz(x).unsafeCast<B>()
// FILE: entry.mjs
import main from "./index.mjs";
if (main.simple("OK") != "OK") throw new Error("Unexpected result from `simple` function")
if (main.second(1, "OK") != "OK") throw new Error("Unexpected result from `second` function")
if (main.simpleWithConstraint(42) != 42) throw new Error("Unexpected result from `simpleConstraint` function")
if (JSON.stringify(main.complexConstraint({ foo: 1n, bar: "bar" })) != "{\"baz\":true}") throw new Error("Unexpected result from `complexConstraint` function")
@@ -0,0 +1,11 @@
type Nullable<T> = T | null | undefined
export declare function produceBoolean(): boolean;
export declare function produceNumber(): number;
export declare function produceBigInt(): bigint;
export declare function produceString(): string;
export declare function produceAny(): NonNullable<unknown>;
export declare function consumeBoolean(x: boolean): string;
export declare function consumeNumber(x: number): string;
export declare function consumeBigInt(x: bigint): string;
export declare function consumeString(x: string): string;
export declare function consumeAny(x: NonNullable<unknown>): string;
@@ -0,0 +1,51 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// TARGET_BACKEND: WASM
// MODULE: main
// FILE: jsPrimitives.kt
@JsExport
fun produceBoolean(): JsBoolean = true.toJsBoolean()
@JsExport
fun produceNumber(): JsNumber = Int.MAX_VALUE.toJsNumber()
@JsExport
fun produceBigInt(): JsBigInt = Long.MAX_VALUE.toJsBigInt()
@JsExport
fun produceString(): JsString = "OK".toJsString()
@JsExport
fun produceAny(): JsAny = 42.toJsNumber()
@JsExport
fun consumeBoolean(x: JsBoolean): String = x.toBoolean().toString()
@JsExport
fun consumeNumber(x: JsNumber): String = x.toInt().toString()
@JsExport
fun consumeBigInt(x: JsBigInt): String = x.toLong().toString()
@JsExport
fun consumeString(x: JsString): String = x.toString()
@JsExport
fun consumeAny(x: JsAny): String = x.toString()
// FILE: entry.mjs
import main from "./index.mjs"
// PRODUCING
if (!main.produceBoolean()) throw new Error("Unexpected value was returned from the `produceBoolean` function")
if (main.produceNumber() != 2147483647) throw new Error("Unexpected value was returned from the `produceNumber` function")
if (main.produceBigInt() != 9223372036854775807n) throw new Error("Unexpected value was returned from the `produceBigInt` function")
if (main.produceString() != "OK") throw new Error("Unexpected value was returned from the `produceString` function")
if (main.produceAny() != 42) throw new Error("Unexpected value was returned from the `produceAny` function")
// CONSUMPTION
if (main.consumeBoolean(false) != "false") throw new Error("Unexpected value was returned from the `consumeBoolean` function")
if (main.consumeNumber(-2147483648) != "-2147483648") throw new Error("Unexpected value was returned from the `consumeNumber` function")
if (main.consumeBigInt(-9223372036854775808n) != "-9223372036854775808") throw new Error("Unexpected value was returned from the `consumeBigInt` function")
if (main.consumeAny(24) != 24) throw new Error("Unexpected value was returned from the `consumeAny` function")
@@ -0,0 +1,11 @@
type Nullable<T> = T | null | undefined
export declare function produceBoolean(): Nullable<boolean>;
export declare function produceNumber(): Nullable<number>;
export declare function produceBigInt(): Nullable<bigint>;
export declare function produceString(): Nullable<string>;
export declare function produceAny(): unknown;
export declare function consumeBoolean(x: Nullable<boolean>): Nullable<string>;
export declare function consumeNumber(x: Nullable<number>): Nullable<string>;
export declare function consumeBigInt(x: Nullable<bigint>): Nullable<string>;
export declare function consumeString(x: Nullable<string>): Nullable<string>;
export declare function consumeAny(x: unknown): Nullable<string>;
@@ -0,0 +1,51 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// TARGET_BACKEND: WASM
// MODULE: main
// FILE: nullableJsPrimitives.kt
@JsExport
fun produceBoolean(): JsBoolean? = true.toJsBoolean()
@JsExport
fun produceNumber(): JsNumber? = Int.MAX_VALUE.toJsNumber()
@JsExport
fun produceBigInt(): JsBigInt? = Long.MAX_VALUE.toJsBigInt()
@JsExport
fun produceString(): JsString? = "OK".toJsString()
@JsExport
fun produceAny(): JsAny? = 42.toJsNumber()
@JsExport
fun consumeBoolean(x: JsBoolean?): String? = x?.toBoolean()?.toString()
@JsExport
fun consumeNumber(x: JsNumber?): String? = x?.toInt()?.toString()
@JsExport
fun consumeBigInt(x: JsBigInt?): String? = x?.toLong()?.toString()
@JsExport
fun consumeString(x: JsString?): String? = x?.toString()
@JsExport
fun consumeAny(x: JsAny?): String? = x?.toString()
// FILE: entry.mjs
import main from "./index.mjs"
// PRODUCING
if (!main.produceBoolean()) throw new Error("Unexpected value was returned from the `produceBoolean` function")
if (main.produceNumber() != 2147483647) throw new Error("Unexpected value was returned from the `produceNumber` function")
if (main.produceBigInt() != 9223372036854775807n) throw new Error("Unexpected value was returned from the `produceBigInt` function")
if (main.produceString() != "OK") throw new Error("Unexpected value was returned from the `produceString` function")
if (main.produceAny() != 42) throw new Error("Unexpected value was returned from the `produceAny` function")
// CONSUMPTION
if (main.consumeBoolean(false) != "false") throw new Error("Unexpected value was returned from the `consumeBoolean` function")
if (main.consumeNumber(-2147483648) != "-2147483648") throw new Error("Unexpected value was returned from the `consumeNumber` function")
if (main.consumeBigInt(-9223372036854775808n) != "-9223372036854775808") throw new Error("Unexpected value was returned from the `consumeBigInt` function")
if (main.consumeAny(24) != 24) throw new Error("Unexpected value was returned from the `consumeAny` function")
@@ -0,0 +1,17 @@
type Nullable<T> = T | null | undefined
export declare function produceBoolean(): Nullable<boolean>;
export declare function produceByte(): Nullable<number>;
export declare function produceShort(): Nullable<number>;
export declare function produceInt(): Nullable<number>;
export declare function produceLong(): Nullable<bigint>;
export declare function produceChar(): Nullable<number>;
export declare function produceString(): Nullable<string>;
export declare function produceFunction(): Nullable<() => number>;
export declare function consumeBoolean(x: Nullable<boolean>): Nullable<string>;
export declare function consumeByte(x: Nullable<number>): Nullable<string>;
export declare function consumeShort(x: Nullable<number>): Nullable<string>;
export declare function consumeInt(x: Nullable<number>): Nullable<string>;
export declare function consumeLong(x: Nullable<bigint>): Nullable<string>;
export declare function consumeChar(x: Nullable<number>): Nullable<string>;
export declare function consumeString(x: Nullable<string>): Nullable<string>;
export declare function consumeFunction(fn: Nullable<(p0: string) => number>): Nullable<number>;
@@ -0,0 +1,76 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// TARGET_BACKEND: WASM
// MODULE: main
// FILE: nullablePrimitives.kt
@JsExport
fun produceBoolean(): Boolean? = true
@JsExport
fun produceByte(): Byte? = Byte.MAX_VALUE
@JsExport
fun produceShort(): Short? = Short.MAX_VALUE
@JsExport
fun produceInt(): Int? = Int.MAX_VALUE
@JsExport
fun produceLong(): Long? = Long.MAX_VALUE
@JsExport
fun produceChar(): Char? = 'a'
@JsExport
fun produceString(): String? = "OK"
@JsExport
fun produceFunction(): (() -> Int)? = { 42 }
@JsExport
fun consumeBoolean(x: Boolean?): String? = x?.toString()
@JsExport
fun consumeByte(x: Byte?): String? = x?.toString()
@JsExport
fun consumeShort(x: Short?): String? = x?.toString()
@JsExport
fun consumeInt(x: Int?): String? = x?.toString()
@JsExport
fun consumeLong(x: Long?): String? = x?.toString()
@JsExport
fun consumeChar(x: Char?): String? = x?.toString()
@JsExport
fun consumeString(x: String?): String? = x
@JsExport
fun consumeFunction(fn: ((String) -> Int)?): Int? = fn?.invoke("42")
// FILE: entry.mjs
import main from "./index.mjs"
// PRODUCING
if (!main.produceBoolean()) throw new Error("Unexpected value was returned from the `produceBoolean` function")
if (main.produceByte() != 127) throw new Error("Unexpected value was returned from the `produceByte` function")
if (main.produceShort() != 32767) throw new Error("Unexpected value was returned from the `produceShort` function")
if (main.produceInt() != 2147483647) throw new Error("Unexpected value was returned from the `produceInt` function")
if (main.produceLong() != 9223372036854775807n) throw new Error("Unexpected value was returned from the `produceLong` function")
if (String.fromCharCode(main.produceChar()) != "a") throw new Error("Unexpected value was returned from the `produceChar` function")
if (main.produceString() != "OK") throw new Error("Unexpected value was returned from the `produceString` function")
if (main.produceFunction()() != 42) throw new Error("Unexpected value was returned from the `produceFunction` function")
// CONSUMPTION
if (main.consumeBoolean(false) != "false") throw new Error("Unexpected value was returned from the `consumeBoolean` function")
if (main.consumeByte(-128) != "-128") throw new Error("Unexpected value was returned from the `consumeByte` function")
if (main.consumeShort(-32768) != "-32768") throw new Error("Unexpected value was returned from the `consumeShort` function")
if (main.consumeInt(-2147483648) != "-2147483648") throw new Error("Unexpected value was returned from the `consumeInt` function")
if (main.consumeLong(-9223372036854775808n) != "-9223372036854775808") throw new Error("Unexpected value was returned from the `consumeLong` function")
if (main.consumeChar("b".charCodeAt()) != "b") throw new Error("Unexpected value was returned from the `consumeChar` function")
if (main.consumeString("🙂") != "🙂") throw new Error("Unexpected value was returned from the `consumeString` function")
if (main.consumeFunction(parseInt) != 42) throw new Error("Unexpected value was returned from the `consumeFunction` function")
@@ -0,0 +1,11 @@
type Nullable<T> = T | null | undefined
export declare function produceUByte(): Nullable<number>;
export declare function produceUShort(): Nullable<number>;
export declare function produceUInt(): Nullable<number>;
export declare function produceULong(): Nullable<bigint>;
export declare function produceFunction(): () => Nullable<number>;
export declare function consumeUByte(x: Nullable<number>): Nullable<string>;
export declare function consumeUShort(x: Nullable<number>): Nullable<string>;
export declare function consumeUInt(x: Nullable<number>): Nullable<string>;
export declare function consumeULong(x: Nullable<bigint>): Nullable<string>;
export declare function consumeFunction(fn: (p0: string) => Nullable<number>): Nullable<number>;
@@ -0,0 +1,52 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// TARGET_BACKEND: WASM
// MODULE: main
// FILE: nullableUnsigned.kt
@JsExport
fun produceUByte(): UByte? = UByte.MAX_VALUE
@JsExport
fun produceUShort(): UShort? = UShort.MAX_VALUE
@JsExport
fun produceUInt(): UInt? = UInt.MAX_VALUE
@JsExport
fun produceULong(): ULong? = ULong.MAX_VALUE
@JsExport
fun produceFunction(): () -> UInt? = ::produceUInt
@JsExport
fun consumeUByte(x: UByte?): String? = x?.toString()
@JsExport
fun consumeUShort(x: UShort?): String? = x?.toString()
@JsExport
fun consumeUInt(x: UInt?): String? = x?.toString()
@JsExport
fun consumeULong(x: ULong?): String? = x?.toString()
@JsExport
fun consumeFunction(fn: (String) -> UInt?): UInt? = fn("42")
// FILE: entry.mjs
import main from "./index.mjs"
// PRODUCING
if (main.produceUByte() != 255) throw new Error("Unexpected value was returned from the `produceUByte` function")
if (main.produceUShort() != 65535) throw new Error("Unexpected value was returned from the `produceUShort` function")
if (main.produceUInt() != 4294967295) throw new Error("Unexpected value was returned from the `produceUInt` function")
if (main.produceULong() != 18446744073709551615n) throw new Error("Unexpected value was returned from the `produceULong` function")
if (main.produceFunction()() != 4294967295) throw new Error("Unexpected value was returned from the `produceFunction` function")
// CONSUMPTION
if (main.consumeUByte(-128) != "128") throw new Error("Unexpected value was returned from the `consumeUByte` function")
if (main.consumeUShort(-32768) != "32768") throw new Error("Unexpected value was returned from the `consumeUShort` function")
if (main.consumeUInt(-2147483648) != "2147483648") throw new Error("Unexpected value was returned from the `consumeUInt` function")
if (main.consumeULong(-9223372036854775808n) != "9223372036854775808") throw new Error("Unexpected value was returned from the `consumeULong` function")
if (main.consumeFunction(parseInt) != 42) throw new Error("Unexpected value was returned from the `consumeFunction` function")
@@ -0,0 +1,19 @@
type Nullable<T> = T | null | undefined
export declare function produceBoolean(): boolean;
export declare function produceByte(): number;
export declare function produceShort(): number;
export declare function produceInt(): number;
export declare function produceLong(): bigint;
export declare function produceChar(): number;
export declare function produceString(): string;
export declare function getState(): string;
export declare function mutateState(): void;
export declare function produceFunction(): () => number;
export declare function consumeBoolean(x: boolean): string;
export declare function consumeByte(x: number): string;
export declare function consumeShort(x: number): string;
export declare function consumeInt(x: number): string;
export declare function consumeLong(x: bigint): string;
export declare function consumeChar(x: number): string;
export declare function consumeString(x: string): string;
export declare function consumeFunction(fn: (p0: string) => number): number;
@@ -0,0 +1,89 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// TARGET_BACKEND: WASM
// MODULE: main
// FILE: primitives.kt
@JsExport
fun produceBoolean(): Boolean = true
@JsExport
fun produceByte(): Byte = Byte.MAX_VALUE
@JsExport
fun produceShort(): Short = Short.MAX_VALUE
@JsExport
fun produceInt(): Int = Int.MAX_VALUE
@JsExport
fun produceLong(): Long = Long.MAX_VALUE
@JsExport
fun produceChar(): Char = 'a'
@JsExport
fun produceString(): String = "OK"
private var state = "INITIAL"
@JsExport
fun getState(): String = state
@JsExport
fun mutateState() {
state = "MUTATED"
}
@JsExport
fun produceFunction(): () -> Int = ::produceInt
@JsExport
fun consumeBoolean(x: Boolean): String = x.toString()
@JsExport
fun consumeByte(x: Byte): String = x.toString()
@JsExport
fun consumeShort(x: Short): String = x.toString()
@JsExport
fun consumeInt(x: Int): String = x.toString()
@JsExport
fun consumeLong(x: Long): String = x.toString()
@JsExport
fun consumeChar(x: Char): String = x.toString()
@JsExport
fun consumeString(x: String): String = x
@JsExport
fun consumeFunction(fn: (String) -> Int): Int = fn("42")
// FILE: entry.mjs
import main from "./index.mjs"
// PRODUCING
if (!main.produceBoolean()) throw new Error("Unexpected value was returned from the `produceBoolean` function")
if (main.produceByte() != 127) throw new Error("Unexpected value was returned from the `produceByte` function")
if (main.produceShort() != 32767) throw new Error("Unexpected value was returned from the `produceShort` function")
if (main.produceInt() != 2147483647) throw new Error("Unexpected value was returned from the `produceInt` function")
if (main.produceLong() != 9223372036854775807n) throw new Error("Unexpected value was returned from the `produceLong` function")
if (String.fromCharCode(main.produceChar()) != "a") throw new Error("Unexpected value was returned from the `produceChar` function")
if (main.produceString() != "OK") throw new Error("Unexpected value was returned from the `produceString` function")
if (main.getState() != "INITIAL") throw new Error("Unexpected value was returned from the `getState` function before the mutation")
main.mutateState()
if (main.getState() != "MUTATED") throw new Error("Unexpected value was returned from the `getState` function after the mutation")
if (main.produceFunction()() != 2147483647) throw new Error("Unexpected value was returned from the `produceFunction` function")
// CONSUMPTION
if (main.consumeBoolean(false) != "false") throw new Error("Unexpected value was returned from the `consumeBoolean` function")
if (main.consumeByte(-128) != "-128") throw new Error("Unexpected value was returned from the `consumeByte` function")
if (main.consumeShort(-32768) != "-32768") throw new Error("Unexpected value was returned from the `consumeShort` function")
if (main.consumeInt(-2147483648) != "-2147483648") throw new Error("Unexpected value was returned from the `consumeInt` function")
if (main.consumeLong(-9223372036854775808n) != "-9223372036854775808") throw new Error("Unexpected value was returned from the `consumeLong` function")
if (main.consumeChar("b".charCodeAt()) != "b") throw new Error("Unexpected value was returned from the `consumeChar` function")
if (main.consumeString("🙂") != "🙂") throw new Error("Unexpected value was returned from the `consumeString` function")
if (main.consumeFunction(parseInt) != 42) throw new Error("Unexpected value was returned from the `consumeFunction` function")
@@ -0,0 +1,11 @@
type Nullable<T> = T | null | undefined
export declare function produceUByte(): number;
export declare function produceUShort(): number;
export declare function produceUInt(): number;
export declare function produceULong(): bigint;
export declare function produceFunction(): () => number;
export declare function consumeUByte(x: number): string;
export declare function consumeUShort(x: number): string;
export declare function consumeUInt(x: number): string;
export declare function consumeULong(x: bigint): string;
export declare function consumeFunction(fn: (p0: string) => number): number;
@@ -0,0 +1,52 @@
// CHECK_TYPESCRIPT_DECLARATIONS
// TARGET_BACKEND: WASM
// MODULE: main
// FILE: unsigned.kt
@JsExport
fun produceUByte(): UByte = UByte.MAX_VALUE
@JsExport
fun produceUShort(): UShort = UShort.MAX_VALUE
@JsExport
fun produceUInt(): UInt = UInt.MAX_VALUE
@JsExport
fun produceULong(): ULong = ULong.MAX_VALUE
@JsExport
fun produceFunction(): () -> UInt = ::produceUInt
@JsExport
fun consumeUByte(x: UByte): String = x.toString()
@JsExport
fun consumeUShort(x: UShort): String = x.toString()
@JsExport
fun consumeUInt(x: UInt): String = x.toString()
@JsExport
fun consumeULong(x: ULong): String = x.toString()
@JsExport
fun consumeFunction(fn: (String) -> UInt): UInt = fn("42")
// FILE: entry.mjs
import main from "./index.mjs"
// PRODUCING
if (main.produceUByte() != 255) throw new Error("Unexpected value was returned from the `produceUByte` function")
if (main.produceUShort() != 65535) throw new Error("Unexpected value was returned from the `produceUShort` function")
if (main.produceUInt() != 4294967295) throw new Error("Unexpected value was returned from the `produceUInt` function")
if (main.produceULong() != 18446744073709551615n) throw new Error("Unexpected value was returned from the `produceULong` function")
if (main.produceFunction()() != 4294967295) throw new Error("Unexpected value was returned from the `produceFunction` function")
// CONSUMPTION
if (main.consumeUByte(-128) != "128") throw new Error("Unexpected value was returned from the `consumeUByte` function")
if (main.consumeUShort(-32768) != "32768") throw new Error("Unexpected value was returned from the `consumeUShort` function")
if (main.consumeUInt(-2147483648) != "2147483648") throw new Error("Unexpected value was returned from the `consumeUInt` function")
if (main.consumeULong(-9223372036854775808n) != "9223372036854775808") throw new Error("Unexpected value was returned from the `consumeULong` function")
if (main.consumeFunction(parseInt) != 42) throw new Error("Unexpected value was returned from the `consumeFunction` function")