Added support spread operator in JS-Backend.
This commit is contained in:
@@ -13,5 +13,57 @@ fun testSum(expectedSum : Int, vararg i : Int) : Boolean {
|
||||
return (expectedSum == sum)
|
||||
}
|
||||
|
||||
fun box() = testSize(0) && testSum(0) && testSize(3, 1, 1, 1) && testSum(3, 1, 1, 1) && testSize(6, 1, 1, 1, 2, 3, 4) &&
|
||||
testSum(30, 10, 20, 0)
|
||||
fun testSpreadOperator(vararg args : Int): Boolean {
|
||||
var sum = 0;
|
||||
for (a in args) sum += a
|
||||
|
||||
return testSize(args.size, *args) && testSum(sum, *args)
|
||||
}
|
||||
|
||||
class Bar(val size: Int, val sum: Int) {
|
||||
fun test(vararg args : Int) = testSize(size, *args) && testSum(sum, *args)
|
||||
}
|
||||
|
||||
object obj {
|
||||
fun test(size: Int, sum: Int, vararg args: Int) = testSize(size, *args) && testSum(sum, *args)
|
||||
}
|
||||
|
||||
fun spreadInMethodCall(size: Int, sum: Int, vararg args: Int) = Bar(size, sum).test(*args)
|
||||
|
||||
fun spreadInObjectMethodCall(size: Int, sum: Int, vararg args: Int) = obj.test(size, sum, *args)
|
||||
|
||||
fun testVarargWithFunLit(vararg args: Int, f: (a: IntArray) -> Boolean): Boolean = f(args)
|
||||
|
||||
fun box(): String {
|
||||
if (!testSize(0))
|
||||
return "wrong vararg size when call function without args"
|
||||
|
||||
if (!testSum(0))
|
||||
return "wrong vararg sum (arguments) when call function without args"
|
||||
|
||||
if (!testSize(6, 1, 1, 1, 2, 3, 4))
|
||||
return "wrong vararg size when call function with some args (1)"
|
||||
|
||||
if (!testSum(30, 10, 20, 0))
|
||||
return "wrong vararg sum (arguments) when call function with some args (1)"
|
||||
|
||||
if (!testSpreadOperator(30, 10, 20, 0))
|
||||
return "failed when call function using spread operator"
|
||||
|
||||
if (!Bar(3, 30).test(10, 20, 0))
|
||||
return "failed when call method"
|
||||
|
||||
if (!spreadInMethodCall(2, 3, 1, 2))
|
||||
return "failed when call method using spread operator"
|
||||
|
||||
if (!obj.test(5, 15, 1, 2, 3, 4, 5))
|
||||
return "failed when call method of object"
|
||||
|
||||
if (!spreadInObjectMethodCall(2, 3, 1, 2))
|
||||
return "failed when call method of object using spread operator"
|
||||
|
||||
if (!testVarargWithFunLit(1, 2, 3) { args -> args.size == 3 })
|
||||
return "failed when call function with vararg and fun literal"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -5,20 +5,85 @@ import js.*
|
||||
native
|
||||
fun paramCount(vararg a : Int) : Int = js.noImpl
|
||||
|
||||
fun count(vararg a : Int) = a.size
|
||||
// test spread operator
|
||||
fun count(vararg a : Int) = paramCount(*a)
|
||||
|
||||
fun box() : Boolean {
|
||||
if (paramCount(1, 2 ,3) != 3) {
|
||||
return false;
|
||||
}
|
||||
if (paramCount() != 0) {
|
||||
return false;
|
||||
}
|
||||
if (count() != 0) {
|
||||
return false;
|
||||
}
|
||||
if (count(1, 1, 1, 1) != 4) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
native
|
||||
class Bar(val size: Int, order: Int = 0) {
|
||||
fun test(order: Int, dummy: Int, vararg args: Int): Boolean = js.noImpl
|
||||
class object {
|
||||
fun startNewTest(): Boolean = js.noImpl
|
||||
var hasOrderProblem: Boolean = false
|
||||
}
|
||||
}
|
||||
|
||||
native
|
||||
object obj {
|
||||
fun test(size: Int, vararg args: Int): Boolean = js.noImpl
|
||||
}
|
||||
|
||||
fun spreadInMethodCall(size: Int, vararg args: Int) = Bar(size).test(0, 1, *args)
|
||||
|
||||
fun spreadInObjectMethodCall(size: Int, vararg args: Int) = obj.test(size, *args)
|
||||
|
||||
native
|
||||
fun testNativeVarargWithFunLit(vararg args: Int, f: (a: IntArray) -> Boolean): Boolean = js.noImpl
|
||||
|
||||
fun testSpreadOperatorWithSafeCall(a: Bar?, expected: Boolean?, vararg args: Int): Boolean {
|
||||
return a?.test(0, 1, *args) == expected
|
||||
}
|
||||
|
||||
fun testSpreadOperatorWithSureCall(a: Bar?, vararg args: Int): Boolean {
|
||||
return a!!.test(0, 1, *args)
|
||||
}
|
||||
|
||||
fun testCallOrder(vararg args: Int) =
|
||||
Bar.startNewTest() &&
|
||||
Bar(args.size, 0).test(1, 1, *args) && Bar(args.size, 2).test(3, 1, *args) &&
|
||||
!Bar.hasOrderProblem
|
||||
|
||||
fun box(): String {
|
||||
if (paramCount() != 0)
|
||||
return "failed when call native function without args"
|
||||
|
||||
if (paramCount(1, 2 ,3) != 3)
|
||||
return "failed when call native function with some args"
|
||||
|
||||
if (count() != 0)
|
||||
return "failed when call native function without args using spread operator"
|
||||
|
||||
if (count(1, 1, 1, 1) != 4)
|
||||
return "failed when call native function with some args using spread operator"
|
||||
|
||||
if (!Bar(5).test(0, 1, 1, 2, 3, 4, 5))
|
||||
return "failed when call method with some args"
|
||||
|
||||
if (!spreadInMethodCall(2, 1, 2))
|
||||
return "failed when call method using spread operator"
|
||||
|
||||
if (!(obj.test(5, 1, 2, 3, 4, 5)))
|
||||
return "failed when call method of object"
|
||||
|
||||
if (!(spreadInObjectMethodCall(2, 1, 2)))
|
||||
return "failed when call method of object using spread operator"
|
||||
|
||||
if (!(testNativeVarargWithFunLit(1, 2, 3) { args -> args.size == 3 }))
|
||||
return "failed when call native function with vararg and fun literal"
|
||||
|
||||
if (!(testSpreadOperatorWithSafeCall(null, null)))
|
||||
return "failed when test spread operator with SafeCall (?.) using null receiver"
|
||||
|
||||
if (!(testSpreadOperatorWithSafeCall(Bar(3), true, 1, 2, 3)))
|
||||
return "failed when test spread operator with SafeCall (?.)"
|
||||
|
||||
if (!(testSpreadOperatorWithSureCall(Bar(3), 1, 2, 3)))
|
||||
return "failed when test spread operator with SureCall (!!)"
|
||||
|
||||
if (!(testCallOrder()))
|
||||
return "failed when test calling order when using spread operator without args"
|
||||
|
||||
if (!(testCallOrder(1, 2, 3, 4)))
|
||||
return "failed when test calling order when using spread operator without args"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -16,4 +16,40 @@
|
||||
|
||||
function paramCount() {
|
||||
return arguments.length
|
||||
}
|
||||
|
||||
function Bar(size, order) {
|
||||
this.size = size;
|
||||
Bar.checkOrder(order);
|
||||
}
|
||||
|
||||
Bar.order = 0;
|
||||
Bar.hasOrderProblem = false;
|
||||
Bar.checkOrder = function (expectedOrder) {
|
||||
var curOrder = Bar.order++;
|
||||
Bar.hasOrderProblem = Bar.hasOrderProblem || curOrder !== expectedOrder;
|
||||
};
|
||||
|
||||
Bar.startNewTest = function () {
|
||||
Bar.hasOrderProblem = false;
|
||||
Bar.order = 0;
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
Bar.prototype.test = function (order, dummy /*, args */) {
|
||||
Bar.checkOrder(order);
|
||||
return dummy === 1 && (arguments.length - 2) === this.size;
|
||||
};
|
||||
|
||||
var obj = {
|
||||
test : function (size /*, args */) {
|
||||
return (arguments.length - 1) === size;
|
||||
}
|
||||
};
|
||||
|
||||
function testNativeVarargWithFunLit(/* args, f */) {
|
||||
var args = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
|
||||
var f = arguments[arguments.length - 1];
|
||||
return typeof f === "function" && f(args);
|
||||
}
|
||||
Reference in New Issue
Block a user