[JS] Support secondary constructors in JsExport
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
// EXPECTED_REACHABLE_NODES: 1290
|
||||
|
||||
// TODO: Support JsExport on object declarations: KT-39117
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
|
||||
@JsExport
|
||||
object A {
|
||||
@JsName("js_method") fun f() = "method"
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
// EXPECTED_REACHABLE_NODES: 1291
|
||||
|
||||
// TODO: Support JsExport on object declarations: KT-39117
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
|
||||
@JsExport
|
||||
object A {
|
||||
@JsName("js_f") fun f(x: Int) = "f($x)"
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
declare namespace JS_TESTS {
|
||||
type Nullable<T> = T | null | undefined
|
||||
class ClassWithDefaultCtor {
|
||||
constructor()
|
||||
readonly x: string;
|
||||
}
|
||||
class ClassWithPrimaryCtor {
|
||||
constructor(x: string)
|
||||
readonly x: string;
|
||||
}
|
||||
class ClassWithSecondaryCtor {
|
||||
readonly x: string;
|
||||
static create(y: string): ClassWithSecondaryCtor
|
||||
}
|
||||
class ClassWithMultipleSecondaryCtors {
|
||||
readonly x: string;
|
||||
static createFromString(y: string): ClassWithMultipleSecondaryCtors
|
||||
static createFromInts(y: number, z: number): ClassWithMultipleSecondaryCtors
|
||||
}
|
||||
class OpenClassWithMixedConstructors {
|
||||
constructor(x: string)
|
||||
readonly x: string;
|
||||
static createFromStrings(y: string, z: string): OpenClassWithMixedConstructors
|
||||
static createFromInts(y: number, z: number): OpenClassWithMixedConstructors
|
||||
}
|
||||
class DerivedClassWithSecondaryCtor extends OpenClassWithMixedConstructors {
|
||||
static delegateToPrimary(y: string): DerivedClassWithSecondaryCtor
|
||||
static delegateToCreateFromInts(y: number, z: number): DerivedClassWithSecondaryCtor
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// TARGET_BACKEND: JS_IR
|
||||
// CHECK_TYPESCRIPT_DECLARATIONS
|
||||
// RUN_PLAIN_BOX_FUNCTION
|
||||
// SKIP_MINIFICATION
|
||||
// SKIP_NODE_JS
|
||||
|
||||
// TODO fix statics export in DCE-driven mode
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
@file:JsExport
|
||||
|
||||
class ClassWithDefaultCtor {
|
||||
val x = "ClassWithDefaultCtor::x"
|
||||
}
|
||||
|
||||
class ClassWithPrimaryCtor(
|
||||
val x: String
|
||||
)
|
||||
|
||||
class ClassWithSecondaryCtor {
|
||||
val x: String
|
||||
@JsName("create")
|
||||
constructor(y: String) {
|
||||
x = y
|
||||
}
|
||||
}
|
||||
|
||||
class ClassWithMultipleSecondaryCtors {
|
||||
val x: String
|
||||
|
||||
@JsName("createFromString")
|
||||
constructor(y: String) {
|
||||
x = "fromString:$y"
|
||||
}
|
||||
|
||||
@JsName("createFromInts")
|
||||
constructor(y: Int, z: Int) {
|
||||
x = "fromInts:$y:$z"
|
||||
}
|
||||
}
|
||||
|
||||
open class OpenClassWithMixedConstructors(val x: String) {
|
||||
@JsName("createFromStrings")
|
||||
constructor(y: String, z: String) : this("fromStrings:$y:$z")
|
||||
|
||||
@JsName("createFromInts")
|
||||
constructor(y: Int, z: Int) : this(y.toString(), z.toString())
|
||||
}
|
||||
|
||||
class DerivedClassWithSecondaryCtor : OpenClassWithMixedConstructors {
|
||||
@JsName("delegateToPrimary")
|
||||
constructor(y: String) : super(y)
|
||||
|
||||
@JsName("delegateToCreateFromInts")
|
||||
constructor(y: Int, z: Int) : super(y, z)
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
"use strict";
|
||||
var ClassWithDefaultCtor = JS_TESTS.ClassWithDefaultCtor, ClassWithPrimaryCtor = JS_TESTS.ClassWithPrimaryCtor, ClassWithSecondaryCtor = JS_TESTS.ClassWithSecondaryCtor, ClassWithMultipleSecondaryCtors = JS_TESTS.ClassWithMultipleSecondaryCtors, DerivedClassWithSecondaryCtor = JS_TESTS.DerivedClassWithSecondaryCtor, OpenClassWithMixedConstructors = JS_TESTS.OpenClassWithMixedConstructors;
|
||||
function box() {
|
||||
var o1 = new ClassWithDefaultCtor();
|
||||
if (o1.x !== "ClassWithDefaultCtor::x")
|
||||
return "Fail: ClassWithDefaultCtor";
|
||||
var o2 = new ClassWithPrimaryCtor("foo");
|
||||
if (o2.x !== "foo")
|
||||
return "Fail: ClassWithPrimaryCtor";
|
||||
var o3 = ClassWithSecondaryCtor.create("foo2");
|
||||
if (o3.x !== "foo2")
|
||||
return "Fail: ClassWithSecondaryCtor.create";
|
||||
var o4 = ClassWithMultipleSecondaryCtors.createFromString("foo3");
|
||||
if (o4.x !== "fromString:foo3")
|
||||
return "Fail: ClassWithMultipleSecondaryCtors.createFromString";
|
||||
var o5 = ClassWithMultipleSecondaryCtors.createFromInts(1, 2);
|
||||
if (o5.x !== "fromInts:1:2")
|
||||
return "Fail: ClassWithMultipleSecondaryCtors.createFromInts";
|
||||
var o6 = new OpenClassWithMixedConstructors("foo4");
|
||||
if (o6.x !== "foo4")
|
||||
return "Fail: OpenClassWithMixedConstructors";
|
||||
var o7 = OpenClassWithMixedConstructors.createFromStrings("foo", "bar");
|
||||
if (o7.x !== "fromStrings:foo:bar")
|
||||
return "Fail: OpenClassWithMixedConstructors.createFromStrings";
|
||||
var o8 = OpenClassWithMixedConstructors.createFromInts(10, -20);
|
||||
if (o8.x !== "fromStrings:10:-20")
|
||||
return "Fail: OpenClassWithMixedConstructors.createFromInts";
|
||||
var o9 = DerivedClassWithSecondaryCtor.delegateToPrimary("foo6");
|
||||
if (o9.x !== "foo6")
|
||||
return "Fail: DerivedClassWithSecondaryCtor.delegateToPrimary";
|
||||
var o10 = DerivedClassWithSecondaryCtor.delegateToCreateFromInts(-10, 20);
|
||||
if (o10.x !== "fromStrings:-10:20")
|
||||
return "Fail: DerivedClassWithSecondaryCtor.delegateToCreateFromInts";
|
||||
return "OK";
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
const {
|
||||
ClassWithDefaultCtor,
|
||||
ClassWithPrimaryCtor,
|
||||
ClassWithSecondaryCtor,
|
||||
ClassWithMultipleSecondaryCtors,
|
||||
DerivedClassWithSecondaryCtor,
|
||||
OpenClassWithMixedConstructors
|
||||
} = JS_TESTS;
|
||||
|
||||
function box(): string {
|
||||
const o1 = new ClassWithDefaultCtor();
|
||||
if (o1.x !== "ClassWithDefaultCtor::x") return "Fail: ClassWithDefaultCtor";
|
||||
|
||||
|
||||
const o2 = new ClassWithPrimaryCtor("foo");
|
||||
if (o2.x !== "foo") return "Fail: ClassWithPrimaryCtor";
|
||||
|
||||
|
||||
const o3 = ClassWithSecondaryCtor.create("foo2");
|
||||
if (o3.x !== "foo2") return "Fail: ClassWithSecondaryCtor.create";
|
||||
|
||||
|
||||
const o4 = ClassWithMultipleSecondaryCtors.createFromString("foo3");
|
||||
if (o4.x !== "fromString:foo3") return "Fail: ClassWithMultipleSecondaryCtors.createFromString";
|
||||
|
||||
const o5 = ClassWithMultipleSecondaryCtors.createFromInts(1, 2);
|
||||
if (o5.x !== "fromInts:1:2") return "Fail: ClassWithMultipleSecondaryCtors.createFromInts";
|
||||
|
||||
|
||||
const o6 = new OpenClassWithMixedConstructors("foo4");
|
||||
if (o6.x !== "foo4") return "Fail: OpenClassWithMixedConstructors";
|
||||
|
||||
const o7 = OpenClassWithMixedConstructors.createFromStrings("foo", "bar");
|
||||
if (o7.x !== "fromStrings:foo:bar") return "Fail: OpenClassWithMixedConstructors.createFromStrings";
|
||||
|
||||
const o8 = OpenClassWithMixedConstructors.createFromInts(10, -20);
|
||||
if (o8.x !== "fromStrings:10:-20") return "Fail: OpenClassWithMixedConstructors.createFromInts";
|
||||
|
||||
|
||||
const o9 = DerivedClassWithSecondaryCtor.delegateToPrimary("foo6");
|
||||
if (o9.x !== "foo6") return "Fail: DerivedClassWithSecondaryCtor.delegateToPrimary";
|
||||
|
||||
const o10 = DerivedClassWithSecondaryCtor.delegateToCreateFromInts(-10, 20);
|
||||
if (o10.x !== "fromStrings:-10:20") return "Fail: DerivedClassWithSecondaryCtor.delegateToCreateFromInts";
|
||||
|
||||
return "OK";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"include": [ "./*" ],
|
||||
"extends": "../common.tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user