diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java index 6db6976d229..1e3593be7b5 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java @@ -88,4 +88,12 @@ public final class NativeInteropTest extends SingleFileTranslationTest { public void testNativePropertyWithCustomName() throws Exception { checkFooBoxIsOk(); } + + public void testPassExtLambdaToNative() throws Exception { + checkFooBoxIsOk(); + } + + public void testPassExtLambdaFromNative() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/testData/closure/cases/closureReceiverInLocalExtFunByLocalExtFun.kt b/js/js.translator/testData/closure/cases/closureReceiverInLocalExtFunByLocalExtFun.kt index 9102fc4cff8..c80c40ce8d4 100644 --- a/js/js.translator/testData/closure/cases/closureReceiverInLocalExtFunByLocalExtFun.kt +++ b/js/js.translator/testData/closure/cases/closureReceiverInLocalExtFunByLocalExtFun.kt @@ -1,15 +1,30 @@ package foo +fun assertEquals(expected: T, actual: T, message: String) { + if (expected != actual) throw Exception("Failed when $message, expected = $expected, actual = $actual") +} + +// workaround for Rhino +var n = 0 +class A { + val i = ++n +} + fun box(): String { - fun Int.foo(): Boolean { - fun Int.bar() = this == 2 && this@foo == 1 - val b = { this == 1 } + fun A.foo() { + fun A.bar() { + assertEquals(2, this.i, "check this.i in A.bar()") + assertEquals(1, this@foo.i, "check this@foo.i in A.bar()") + } + val b = { assertEquals(1, this.i, "check this.i in b") } - return this == 1 && 2.bar() && b() - } + assertEquals(1, this.i, "check this.i in A.foo()") + A().bar() + b() + } - if (!1.foo()) return "Failed" + A().foo() - return "OK" + return "OK" } diff --git a/js/js.translator/testData/closure/cases/closureThisInConstructor.kt b/js/js.translator/testData/closure/cases/closureThisInConstructor.kt index 07fe5d09c66..72f58a10154 100644 --- a/js/js.translator/testData/closure/cases/closureThisInConstructor.kt +++ b/js/js.translator/testData/closure/cases/closureThisInConstructor.kt @@ -1,17 +1,20 @@ // KT-2388 package foo -var done = 0 +// workaround for Rhino +class Done(val i: Int) + +var done = Done(0) object foo { var result = "FAIL" val lambda = { result = "foo.lambda OK" - done = 3 + done = Done(3) } - val extLambda: Int.() -> Unit = { + val extLambda: Done.() -> Unit = { result = "foo.extLambda OK" done = this } @@ -22,10 +25,10 @@ class Foo { val lambda = { result = "Foo::lambda OK" - done = -7 + done = Done(-7) } - val extLambda: Int.() -> Unit = { + val extLambda: Done.() -> Unit = { result = "Foo::extLambda OK" done = this } @@ -41,20 +44,20 @@ fun box(): String { a() if (foo.result != "foo.lambda OK") return "foo.result = \"${foo.result}\", but expected \"foo.lambda OK\"" - if (done != 3) return "done = $done, but expected 3" + if (done.i != 3) return "done.i = ${done.i}, but expected 3" - 23.b() + Done(23).b() if (foo.result != "foo.extLambda OK") return "foo.result = \"${foo.result}\", but expected \"foo.extLambda OK\"" - if (done != 23) return "done = $done, but expected 23" + if (done.i != 23) return "done.i = ${done.i}, but expected 23" c() if (f.result != "Foo::lambda OK") return "a.result = \"${f.result}\", but expected \"Foo::lambda OK\"" - if (done != -7) return "done = $done, but expected -7" + if (done.i != -7) return "done.i = ${done.i}, but expected -7" - 71.d() + Done(71).d() if (f.result != "Foo::extLambda OK") return "a.result = \"${f.result}\", but expected \"Foo::extLambda OK\"" - if (done != 71) return "done = $done, but expected 71" + if (done.i != 71) return "done.i = ${done.i}, but expected 71" return "OK" } diff --git a/js/js.translator/testData/native/cases/passExtLambdaFromNative.kt b/js/js.translator/testData/native/cases/passExtLambdaFromNative.kt new file mode 100644 index 00000000000..51653a260fa --- /dev/null +++ b/js/js.translator/testData/native/cases/passExtLambdaFromNative.kt @@ -0,0 +1,18 @@ +package foo + +native +class A(val v: String) + +class B { + fun bar(a: A, extLambda: A.(Int, String) -> String): String = a.extLambda(7, "_rr_") +} + +native +fun nativeBox(b: B): String = noImpl + +fun box(): String { + val r = nativeBox(B()) + if (r != "foo_rr_7") return r + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/native/cases/passExtLambdaToNative.kt b/js/js.translator/testData/native/cases/passExtLambdaToNative.kt new file mode 100644 index 00000000000..1085a9a6551 --- /dev/null +++ b/js/js.translator/testData/native/cases/passExtLambdaToNative.kt @@ -0,0 +1,16 @@ +package foo + +native +class A(val v: String) + +native +fun bar(a: A, extLambda: A.(Int, String) -> String): String = noImpl + +fun box(): String { + val a = A("test") + + val r = bar(a) { i, s -> "${this.v} $i $s"} + if (r != "test 4 boo") return r + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/native/native/passExtLambdaFromNative.js b/js/js.translator/testData/native/native/passExtLambdaFromNative.js new file mode 100644 index 00000000000..17901e7e753 --- /dev/null +++ b/js/js.translator/testData/native/native/passExtLambdaFromNative.js @@ -0,0 +1,7 @@ +function A(v) { + this.v = v; +} + +function nativeBox(b) { + return b.bar(new A("foo"), function(i, s) { return "" + this.v + s + i }) +} diff --git a/js/js.translator/testData/native/native/passExtLambdaToNative.js b/js/js.translator/testData/native/native/passExtLambdaToNative.js new file mode 100644 index 00000000000..23727858fec --- /dev/null +++ b/js/js.translator/testData/native/native/passExtLambdaToNative.js @@ -0,0 +1,7 @@ +function A(v) { + this.v = v; +} + +function bar(a, extLambda) { + return extLambda.call(a, 4, "boo") +}