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
@@ -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;
}