diff --git a/js/js.libraries/src/core/core.kt b/js/js.libraries/src/core/core.kt index 1d8689e3833..fa20b75ed5c 100644 --- a/js/js.libraries/src/core/core.kt +++ b/js/js.libraries/src/core/core.kt @@ -5,22 +5,34 @@ import java.util.*; native public val noImpl: T = throw Exception() +native +public fun eval(expr: String): dynamic = noImpl + +native +public fun typeof(a: Any?): String = noImpl + +native +public val undefined: Nothing? = noImpl + // Drop this after KT-2093 will be fixed and restore MutableMap.set in Maps.kt from MapsJVM.kt /** Provides [] access to maps */ [suppress("BASE_WITH_NULLABLE_UPPER_BOUND")] native public fun MutableMap.set(key: K, value: V): V? = noImpl -library("println") +library public fun println() {} -library("println") +library public fun println(s : Any?) {} -library("print") +library public fun print(s : Any?) {} + //TODO: consistent parseInt -native public fun parseInt(s: String, radix: Int = 10): Int = noImpl +native +public fun parseInt(s: String, radix: Int = 10): Int = noImpl library public fun safeParseInt(s : String) : Int? = noImpl library public fun safeParseDouble(s : String) : Double? = noImpl -native public fun js(code: String): dynamic = noImpl \ No newline at end of file +native +public fun js(code: String): dynamic = noImpl 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 eec2c6896f3..6e8c7443f1d 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 @@ -68,10 +68,6 @@ public final class NativeInteropTest extends SingleFileTranslationTest { checkFooBoxIsOk(); } - public void testUndefined() throws Exception { - fooBoxTest(); - } - public void testKt1519() throws Exception { fooBoxTest(); } @@ -143,4 +139,16 @@ public final class NativeInteropTest extends SingleFileTranslationTest { public void testNativeGetterAndNativeSetter() throws Exception { checkFooBoxIsOk(); } + + public void testEval() throws Exception { + checkFooBoxIsOk(); + } + + public void testUndefined() throws Exception { + checkFooBoxIsOk(); + } + + public void testTypeof() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/testData/dynamic/_commonFiles/common.kt b/js/js.translator/testData/dynamic/_commonFiles/common.kt index 859cad59f65..585bc11680e 100644 --- a/js/js.translator/testData/dynamic/_commonFiles/common.kt +++ b/js/js.translator/testData/dynamic/_commonFiles/common.kt @@ -9,9 +9,6 @@ var arr: dynamic = noImpl native var baz: dynamic = noImpl -native -val undefined: Nothing? = noImpl - object t { override fun toString() = "object t {}" } diff --git a/js/js.translator/testData/expression/equals/cases/equalsNullOrUndefined.kt b/js/js.translator/testData/expression/equals/cases/equalsNullOrUndefined.kt index 7a4d7c7aab1..e0317d36a88 100644 --- a/js/js.translator/testData/expression/equals/cases/equalsNullOrUndefined.kt +++ b/js/js.translator/testData/expression/equals/cases/equalsNullOrUndefined.kt @@ -1,7 +1,5 @@ package foo -native val undefined: Any = noImpl - fun box(): String { val a: Int? = null val r = a == null diff --git a/js/js.translator/testData/native/cases/eval.kt b/js/js.translator/testData/native/cases/eval.kt new file mode 100644 index 00000000000..2cf122fe9d6 --- /dev/null +++ b/js/js.translator/testData/native/cases/eval.kt @@ -0,0 +1,16 @@ +package foo + +val top = "TOP LEVEL" + +fun box(): String { + // Does't work in Rhino, but should. + // val v = 1 + // assertEquals(3, eval("v + 2")) + + assertEquals(5, eval("3 + 2")) + + val PACKAGE = "Kotlin.modules.JS_TESTS.foo" + assertEquals(top, eval("$PACKAGE.top")) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/native/cases/nativeGetterAndNativeSetter.kt b/js/js.translator/testData/native/cases/nativeGetterAndNativeSetter.kt index cada6c3467f..7bfd9f2df45 100644 --- a/js/js.translator/testData/native/cases/nativeGetterAndNativeSetter.kt +++ b/js/js.translator/testData/native/cases/nativeGetterAndNativeSetter.kt @@ -30,13 +30,10 @@ fun JsObject.put(a: String, v: Any?): Unit = noImpl object t{} -native -val undefined: Any = noImpl - native fun getTestObject(): JsObject = noImpl -fun test(obj: JsObject, key: String, oldValue: Any, newValue: Any) { +fun test(obj: JsObject, key: String, oldValue: Any?, newValue: Any) { assertEquals(oldValue, obj[key]) obj[key] = newValue assertEquals(newValue, obj[key]) @@ -44,7 +41,7 @@ fun test(obj: JsObject, key: String, oldValue: Any, newValue: Any) { assertEquals(null, obj[key]) } -fun test(obj: JsObject, key: Int, oldValue: Any, newValue: Any) { +fun test(obj: JsObject, key: Int, oldValue: Any?, newValue: Any) { assertEquals(oldValue, obj.take(key)) obj.put(key, newValue) assertEquals(newValue, obj.take(key)) @@ -52,7 +49,7 @@ fun test(obj: JsObject, key: Int, oldValue: Any, newValue: Any) { assertEquals(null, obj.take(key)) } -fun testExtensions(obj: JsObject, key: Int, oldValue: Any, newValue: Any) { +fun testExtensions(obj: JsObject, key: Int, oldValue: Any?, newValue: Any) { assertEquals(oldValue, obj[key]) obj[key] = newValue assertEquals(newValue, obj[key]) @@ -60,7 +57,7 @@ fun testExtensions(obj: JsObject, key: Int, oldValue: Any, newValue: Any) { assertEquals(null, obj[key]) } -fun testExtensions(obj: JsObject, key: String, oldValue: Any, newValue: Any) { +fun testExtensions(obj: JsObject, key: String, oldValue: Any?, newValue: Any) { assertEquals(oldValue, obj.take(key)) obj.put(key, newValue) assertEquals(newValue, obj.take(key)) diff --git a/js/js.translator/testData/native/cases/nativePropertyWithCustomName.kt b/js/js.translator/testData/native/cases/nativePropertyWithCustomName.kt index 868ec4fdfd7..00991c3660c 100644 --- a/js/js.translator/testData/native/cases/nativePropertyWithCustomName.kt +++ b/js/js.translator/testData/native/cases/nativePropertyWithCustomName.kt @@ -2,7 +2,6 @@ package foo val PACKAGE = "Kotlin.modules.JS_TESTS.foo" -native fun eval(e: String): Any? = noImpl fun funToString(name: String) = eval("$PACKAGE.$name.toString()") as String native("\"O\"") val foo: String = noImpl diff --git a/js/js.translator/testData/native/cases/print.kt b/js/js.translator/testData/native/cases/print.kt index c90c7ddf8a8..94bc44edc20 100644 --- a/js/js.translator/testData/native/cases/print.kt +++ b/js/js.translator/testData/native/cases/print.kt @@ -17,9 +17,6 @@ val EXPECTED_NEWLINE_FOR_EACH = """Hello """ -native -fun eval(code: String): Any? = noImpl - native var buffer: String = noImpl diff --git a/js/js.translator/testData/native/cases/typeof.kt b/js/js.translator/testData/native/cases/typeof.kt new file mode 100644 index 00000000000..98cc9190495 --- /dev/null +++ b/js/js.translator/testData/native/cases/typeof.kt @@ -0,0 +1,16 @@ +package foo + +class A + +fun box(): String { + assertEquals("number", typeof(1)) + assertEquals("number", typeof(1.2)) + assertEquals("boolean", typeof(true)) + assertEquals("string", typeof("sss")) + assertEquals("object", typeof(null)) + assertEquals("undefined", typeof(undefined)) + assertEquals("object", typeof(object {})) + assertEquals("object", typeof(A())) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/native/cases/undefined.kt b/js/js.translator/testData/native/cases/undefined.kt index 1402c6d586a..c742e9fb3d4 100644 --- a/js/js.translator/testData/native/cases/undefined.kt +++ b/js/js.translator/testData/native/cases/undefined.kt @@ -1,22 +1,20 @@ package foo -/*function g should return 0 if a parameter is undefined 1 if parameter is 1 and 3 if parameter is 3*/ -native -fun f(g: (Int?) -> Int): Boolean = noImpl +fun box(): String { + assertEquals(eval("undefined"), undefined) + assertEquals(js("undefined"), undefined) + assertNotEquals(1, undefined) + assertNotEquals("sss", undefined) + assertNotEquals(object {}, undefined) -fun box(): Boolean { - val b = { - (a: Int?) -> - if (a == null) { - 0 - } - else if (a == 1) { - 1 - } - else { - 3 - } - } - return f(b) -} + val a: dynamic = 1 + assertEquals(a.foo, undefined) + assertNotEquals(a.toString, undefined) + + val b: dynamic = object {val bar = ""} + assertEquals(b.foo, undefined) + assertNotEquals(b.bar, undefined) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/native/native/undefined.js b/js/js.translator/testData/native/native/undefined.js deleted file mode 100644 index a4615ccd145..00000000000 --- a/js/js.translator/testData/native/native/undefined.js +++ /dev/null @@ -1,12 +0,0 @@ -function f(g) { - if (g() != 0) { - return false; - } - if (g(1) != 1) { - return false; - } - if (g(2) != 3) { - return false; - } - return true; -} \ No newline at end of file diff --git a/js/js.translator/testData/propertyAccess/cases/nativePropertiesNameClashes.kt b/js/js.translator/testData/propertyAccess/cases/nativePropertiesNameClashes.kt index e0ffbab5762..93445b4c01f 100644 --- a/js/js.translator/testData/propertyAccess/cases/nativePropertiesNameClashes.kt +++ b/js/js.translator/testData/propertyAccess/cases/nativePropertiesNameClashes.kt @@ -2,8 +2,6 @@ package foo val PACKAGE = "Kotlin.modules.JS_TESTS.foo" -native fun eval(e: String): Any? = noImpl - class A native val Any.__proto__: String get() = noImpl native val A.__proto__: String get() = noImpl diff --git a/js/js.translator/testData/standardClasses/cases/arrayFactoryMethods.kt b/js/js.translator/testData/standardClasses/cases/arrayFactoryMethods.kt index 80f9655a4a9..9c83fdde016 100644 --- a/js/js.translator/testData/standardClasses/cases/arrayFactoryMethods.kt +++ b/js/js.translator/testData/standardClasses/cases/arrayFactoryMethods.kt @@ -1,7 +1,6 @@ package foo class Fail(val message: String) : Exception(message) -native fun eval(arg: String): Any? = noImpl fun test(testName: String, actual: Any, expectedAsString: String) { val expected = eval("[$expectedAsString]") diff --git a/js/js.translator/testData/trait/cases/checkImplementationCharacteristics.kt b/js/js.translator/testData/trait/cases/checkImplementationCharacteristics.kt index f51d558807e..58478429ecc 100644 --- a/js/js.translator/testData/trait/cases/checkImplementationCharacteristics.kt +++ b/js/js.translator/testData/trait/cases/checkImplementationCharacteristics.kt @@ -9,8 +9,6 @@ public trait B : A { } } -native fun eval(code: String): Any = noImpl -native val undefined: Any = noImpl native class Function(vararg args: String) val hasProp = Function("obj, prop", "return obj[prop] !== undefined") as ((Any, String) -> Boolean)