JS declarations: added eval, typeof, undefined.

#KT-4136 Fixed
This commit is contained in:
Zalim Bashorov
2014-12-24 16:02:13 +03:00
parent 0988d6cace
commit 9ab045e598
14 changed files with 81 additions and 60 deletions
+17 -5
View File
@@ -5,22 +5,34 @@ import java.util.*;
native
public val <T> 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 <K, V> MutableMap<K, V>.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
native
public fun js(code: String): dynamic = noImpl
@@ -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();
}
}
@@ -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 {}"
}
@@ -1,7 +1,5 @@
package foo
native val undefined: Any = noImpl
fun box(): String {
val a: Int? = null
val r = a == null
@@ -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"
}
@@ -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))
@@ -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
@@ -17,9 +17,6 @@ val EXPECTED_NEWLINE_FOR_EACH = """Hello
"""
native
fun eval(code: String): Any? = noImpl
native
var buffer: String = noImpl
@@ -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"
}
@@ -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"
}
@@ -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;
}
@@ -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
@@ -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]")
@@ -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)