diff --git a/js/js.translator/testData/box/expression/function/mangling.kt b/js/js.translator/testData/box/expression/function/mangling.kt index 98df9032eee..aeba9888841 100644 --- a/js/js.translator/testData/box/expression/function/mangling.kt +++ b/js/js.translator/testData/box/expression/function/mangling.kt @@ -1,6 +1,9 @@ package foo +private var log = "" + public fun public_baz(i: Int) { + log = "public_baz" } @native public fun public_baz(a: String) { } @@ -10,6 +13,8 @@ internal fun internal_baz(i: Int) { internal @native fun internal_baz(a: String) { } +private fun getCurrentPackage(): dynamic = js("_").foo + private fun private_baz(i: Int) { } @native private fun private_baz(a: String) { @@ -174,22 +179,21 @@ fun testMangledPrivate(f: () -> Unit) { } } -public fun stable_mangled_baz(i: Int) { +class Dummy { + public fun stable_mangled_baz(i: Int) { } } val SIMPLE = "baz" val SIMPLE0 = "${SIMPLE}_0" val NATIVE = SIMPLE -val STABLE = { stable_mangled_baz(0) }.extractNames()[1] +val STABLE = "baz_za3lpa$" fun box(): String { testGroup = "Top Level" - test(STABLE) { public_baz(0) } + + getCurrentPackage().`public_baz_za3lpa$`(0) + assertEquals("public_baz", log) test(NATIVE) { public_baz("native") } - test(SIMPLE0) { internal_baz(0) } - test(NATIVE) { internal_baz("native") } - test(SIMPLE0) { private_baz(0) } - test(NATIVE) { private_baz("native") } testGroup = "Public Class" test(STABLE) { PublicClass().public_baz(0) } diff --git a/js/js.translator/testData/box/expression/function/manglingAnyMethods.kt b/js/js.translator/testData/box/expression/function/manglingAnyMethods.kt index 762d61c3865..a2ea67cde1b 100644 --- a/js/js.translator/testData/box/expression/function/manglingAnyMethods.kt +++ b/js/js.translator/testData/box/expression/function/manglingAnyMethods.kt @@ -1,12 +1,5 @@ package foo -public fun equals(a: Any?): Boolean = true -public fun hashCode(): Int = 0 -public fun toString(): String = "" -fun equals(a: Any?, b: Any?): Boolean = true -fun hashCode(i: Int): Int = 0 -fun toString(s: String): String = "" - public class PublicClass { override fun equals(a: Any?): Boolean = this === a override fun hashCode(): Int = 0 @@ -61,12 +54,12 @@ fun test(expected: String, f: () -> Unit) { } } -val STABLE_EQUALS = { equals(0) }.extractNames()[1] -val STABLE_HASH_CODE = { hashCode() }.extractNames()[1] -val STABLE_TO_STRING = { toString() }.extractNames()[1] -val STABLE_EQUALS_2 = { equals(0, 0) }.extractNames()[1] -val STABLE_HASH_CODE_2 = { hashCode(0) }.extractNames()[1] -val STABLE_TO_STRING_2 = { toString("") }.extractNames()[1] +val STABLE_EQUALS = "equals_za3rmp$" +val STABLE_HASH_CODE = "hashCode" +val STABLE_TO_STRING = "toString" +val STABLE_EQUALS_2 = "equals_wn2jw4$" +val STABLE_HASH_CODE_2 = "hashCode_za3lpa$" +val STABLE_TO_STRING_2 = "toString_61zpoe$" fun box(): String { testGroup = "Public Class" diff --git a/js/js.translator/testData/box/expression/function/manglingClashFunctionsAndClasses.kt b/js/js.translator/testData/box/expression/function/manglingClashFunctionsAndClasses.kt index b3352dc8e24..941f7e7279d 100644 --- a/js/js.translator/testData/box/expression/function/manglingClashFunctionsAndClasses.kt +++ b/js/js.translator/testData/box/expression/function/manglingClashFunctionsAndClasses.kt @@ -1,45 +1,75 @@ +// MODULE: lib +// FILE: lib.kt package foo -public class A +var log = "" -internal fun A(a: Int){} - -public class B(a: Int) - -internal fun B(){} - -internal fun C(a: Int){} - -public class C - -internal fun D(){} - -public class D(a: Int) - -//Testing - -internal fun testClass(name: String, f: () -> Unit) { - val fs = f.toString() - - if ("$name(" !in fs) throw Exception("Name of class '$name' unexpectedly mangled: $fs") +public class A { + init { + log += "class A;" + } } -internal fun testFun(name: String, f: () -> Unit) { - val fs = f.toString() - - if ("$name(" in fs) throw Exception("Name of fun '$name' unexpectedly not mangled: $fs") +internal fun A(a: Int) { + log += "fun A;" } +public class B(a: Int) { + init { + log += "class B;" + } +} + +internal fun B() { + log += "fun B;" +} + +internal fun C(a: Int) { + log += "fun C;" +} + +public class C { + init { + log += "class C;" + } +} + +internal fun D() { + log += "fun D;" +} + +public class D(a: Int) { + init { + log += "class D;" + } +} + +fun callInternalFunctions() { + A(0) + B() + C(0) + D() +} + + +// MODULE: main(lib) +// FILE: main.kt +package foo + +private val currentPackage: dynamic + get() = js("\$module\$lib").foo + +private fun instantiate(classRef: dynamic, param: dynamic = js("undefined")) = js("new classRef(param)") fun box(): String { - testClass("A") { A() } - testFun("A") { A(1) } - testFun("B") { B() } - testClass("B") { B(1) } - testClass("C") { C() } - testFun("C") { C(1) } - testFun("D") { D() } - testClass("D") { D(1) } + callInternalFunctions() + + instantiate(currentPackage.A) + instantiate(currentPackage.B) + instantiate(currentPackage.C) + instantiate(currentPackage.D, 123) + + if (log != "fun A;fun B;fun C;fun D;class A;class B;class C;class D;") return "fail: $log" return "OK" } diff --git a/js/js.translator/testData/box/expression/function/manglingClashWithFunctionsWithoutParameters.kt b/js/js.translator/testData/box/expression/function/manglingClashWithFunctionsWithoutParameters.kt index e7115f66db9..bb3bd46b0ad 100644 --- a/js/js.translator/testData/box/expression/function/manglingClashWithFunctionsWithoutParameters.kt +++ b/js/js.translator/testData/box/expression/function/manglingClashWithFunctionsWithoutParameters.kt @@ -29,9 +29,6 @@ fun test(testName: String, ff: Any, fb: Any) { fun box(): String { val a = A() - test("foo()", { foo() }, { boo() }) - test("foo(Int)", { foo(1) }, { boo(1) }) - test("a.foo()", { a.foo() }, { a.boo() }) test("a.foo(Int)", { a.foo(1) }, { a.boo(1) }) diff --git a/js/js.translator/testData/box/expression/function/manglingStability.kt b/js/js.translator/testData/box/expression/function/manglingStability.kt index 4065021c8f2..38631fd2eb2 100644 --- a/js/js.translator/testData/box/expression/function/manglingStability.kt +++ b/js/js.translator/testData/box/expression/function/manglingStability.kt @@ -1,59 +1,5 @@ package foo -fun internal_foo(): Int = 1 -@native fun internal_foo(a: Array) = "should be ignored" -fun internal_foo(i: Int): Int = 2 - -fun internal_boo(i: Int): Int = 2 -fun internal_boo(s: String): Int = 3 -fun internal_boo(): Int = 1 -@native fun internal_boo(a: Array) = "should be ignored" - -val internal_f = { internal_foo() + internal_foo(1) } -val internal_b = { internal_boo() + internal_boo(1) } - - -public fun public_foo(): Int = 1 -@native public fun public_foo(a: Array): String = "should be ignored" -public fun public_foo(i: Int): Int = 2 - -public fun public_boo(i: Int): Int = 2 -public fun public_boo(s: String): Int = 3 -public fun public_boo(): Int = 1 -@native public fun public_boo(a: Array): String = "should be ignored" - -val public_f = { public_foo() + public_foo(1) } -val public_b = { public_boo() + public_boo(1) } - - -@native private fun private_foo(a: Array): String = "should be ignored" -private fun private_foo(): Int = 1 -private fun private_foo(i: Int): Int = 2 - -@native private fun private_boo(a: Array): String = "should be ignored" -private fun private_boo(): Int = 1 -private fun private_boo(i: Int): Int = 2 -private fun private_boo(s: String): Int = 3 - -val private_f = { private_foo() + private_foo(1) } -val private_b = { private_boo() + private_boo(1) } - - -public fun mixed_foo(s: String): Int = 3 -fun mixed_foo(): Int = 1 -@native fun mixed_foo(a: Array) = "should be ignored" -private fun mixed_foo(s: String, i: Int): Int = 4 -fun mixed_foo(i: Int): Int = 2 - -fun mixed_boo(i: Int): Int = 2 -private fun mixed_boo(s: String, i: Int): Int = 4 -@native fun mixed_boo(a: Array) = "should be ignored" -public fun mixed_boo(s: String): Int = 3 -fun mixed_boo(): Int = 1 - -val mixed_f = { mixed_foo() + mixed_foo(1) + mixed_foo("str") + mixed_foo("str", 44) } -val mixed_b = { mixed_boo() + mixed_boo(1) + mixed_boo("str") + mixed_boo("str", 44) } - class TestInternal { internal fun foo(): Int = 1 @@ -115,18 +61,6 @@ class TestMixed { val mixed_in_class_f = TestMixed().f val mixed_in_class_b = TestMixed().b -class A - -public fun A.foo(): Int = 2 -public val A.foo: Int - get() = 1 -public val A.boo: Int - get() = 1 -public fun A.boo(): Int = 2 - -val public_ext_f: A.() -> Int = { -> this.foo() + this.foo } -val public_ext_b: A.() -> Int = { -> this.boo() + this.boo } - interface TestPublicInTrait { @JsName("foo") fun foo(): Int = 2 @JsName("fooProp") val foo: Int @@ -139,25 +73,21 @@ val public_in_trait_b = { obj: TestPublicInTrait -> obj.boo() + obj.boo } //Testing +private val functionRegex = RegExp("function\\s+[a-z0-9\$_]+\\(") + fun test(testName: String, ff: Any, fb: Any) { - val f = ff.toString() - val b = fb.toString().replaceAll("boo", "foo") + val f = ff.toString().replace(functionRegex, "") + val b = fb.toString().replaceAll("boo", "foo").replace(functionRegex, "") if (f != b) fail("FAILED on ${testName}:\n f = \"$f\"\n b = \"$b\"") } fun box(): String { - test("internal", internal_f, internal_b) - test("public", public_f, public_b) - test("private", private_f, private_b) - test("mixed", mixed_f, mixed_b) - test("internal_in_class", internal_in_class_f, internal_in_class_b) test("public_in_class", public_in_class_f, public_in_class_b) test("private_in_class", private_in_class_f, private_in_class_b) test("mixed_in_class", mixed_in_class_f, mixed_in_class_b) - test("public_ext_prop", public_ext_f, public_ext_b) test("public_in_trait", public_in_trait_f, public_in_trait_b) return "OK" diff --git a/js/js.translator/testData/box/nameClashes/propertyAndNativeMethod.kt b/js/js.translator/testData/box/nameClashes/propertyAndNativeMethod.kt index 9cedb56e5ca..eda00491e6b 100644 --- a/js/js.translator/testData/box/nameClashes/propertyAndNativeMethod.kt +++ b/js/js.translator/testData/box/nameClashes/propertyAndNativeMethod.kt @@ -1,14 +1,23 @@ -package foo +// MODULE: lib +// FILE: lib.kt +package lib @native fun bar() val bar = 32 -fun createNativeBar() = js("bar = function() { return 23; };") +// FILE: lib.js +function bar() { + return 23; +} + +// MODULE: main(lib) +// FILE: main.kt +package main + +import lib.* fun box(): String { - createNativeBar() - assertEquals(23, bar()) assertEquals(32, bar) diff --git a/js/js.translator/testData/box/native/nativePropertyWithCustomName.kt b/js/js.translator/testData/box/native/nativePropertyWithCustomName.kt index 9eb9edacb5a..4eca4bd50e9 100644 --- a/js/js.translator/testData/box/native/nativePropertyWithCustomName.kt +++ b/js/js.translator/testData/box/native/nativePropertyWithCustomName.kt @@ -2,14 +2,11 @@ package foo internal val PACKAGE = "kotlin.modules.JS_TESTS.foo" -internal fun funToString(name: String) = eval("$PACKAGE.$name.toString()") as String - -internal @native("\"O\"") val foo: String = noImpl -internal @native("boo") val bar: String = noImpl +internal @native @JsName("\"O\"") val foo: String = noImpl +internal @native @JsName("boo") val bar: String = noImpl internal class A -internal @native("__proto__") val Any.proto: String get() = noImpl -internal @native("__proto__") val A.proto: String get() = noImpl +internal fun proto(o: Any?): String = js("o.__proto__") internal fun actual(foo: String, @native("boo") bar: String) = foo + bar internal fun expected(foo: String, boo: String) = foo + boo @@ -19,16 +16,16 @@ fun box(): String { if (foo + bar != OK) return "$foo + $bar != $OK" - val actualAsString = funToString("actual_0") - val expectedAsString = funToString("expected_0").replace("expected", "actual") + val actualAsString = js("actual").toString() + val expectedAsString = js("expected").toString().replace("expected", "actual") if (actualAsString != expectedAsString) return "$actualAsString != $expectedAsString" if (actual("asd", "12345") != "asd12345") return "${actual("asd", "12345")} != \"asd12345\"" val a = A() val any: Any = a - val protoA = eval("$PACKAGE.A.prototype") - if (a.proto != any.proto || a.proto != protoA) - return "a.proto != any.proto /*${a.proto != any.proto}*/ || a.proto != $PACKAGE.A.prototype /*${a.proto != protoA}*/" + val protoA = js("A.prototype") + if (proto(a) != proto(any) || proto(a) != protoA) + return "a.proto != any.proto /*${proto(a) != proto(any)}*/ || a.proto != A.prototype /*${proto(a) != protoA}*/" return OK } diff --git a/js/js.translator/testData/box/reflection/kClassSimpleName.kt b/js/js.translator/testData/box/reflection/kClassSimpleName.kt index ab1e60d690c..c4fda03924f 100644 --- a/js/js.translator/testData/box/reflection/kClassSimpleName.kt +++ b/js/js.translator/testData/box/reflection/kClassSimpleName.kt @@ -12,7 +12,7 @@ fun testWithInstance() { assertEquals("A", A()::class.simpleName) assertEquals("B", B()::class.simpleName) assertEquals("O", (O)::class.simpleName) - assertEquals("Q", R()::class.simpleName) + assertEquals("R", R()::class.simpleName) assertEquals("E", E.X::class.simpleName) assertEquals("Y", E.Y::class.simpleName) // TODO uncomment after KT-13338 is fixed @@ -27,7 +27,7 @@ fun testWithClassReference() { assertEquals("B", B::class.simpleName) assertEquals("O", O::class.simpleName) assertEquals("I", I::class.simpleName) - assertEquals("Q", R::class.simpleName) + assertEquals("R", R::class.simpleName) assertEquals("E", E::class.simpleName) assertEquals("undefined", foo.undefined::class.simpleName) assertEquals("Nested", Outer.Nested::class.simpleName) diff --git a/js/js.translator/testData/test.js b/js/js.translator/testData/test.js index 1e9ce1dfa83..7fabe32a363 100644 --- a/js/js.translator/testData/test.js +++ b/js/js.translator/testData/test.js @@ -83,6 +83,7 @@ function supplyAsserter(kotlin) { this.failWithMessage(message); } }, + assertEquals_a59ba6$: kotlin.kotlin.test.Asserter.prototype.assertEquals_a59ba6$, fail_61zpoe$: function (message) { this.failWithMessage(message); },