Digital Da
Digital Da

Reputation: 911

JS spread syntax in Android using Kotlin and Duktape

I am using Duktape to run JS code in my Android application. I am trying to implement a log function in JS which receives multiple varargs arguments. When running the code I am getting an exception:

Method threw 'com.squareup.duktape.DuktapeException' exception. SyntaxError: expected identifier (line 3)

Does Duktape support Spread syntax? Are Kotlin optionals supported in general?

//JS code
"""
    var Console = {
        log : function(...args) {
            __consoleImpl.log(args);
                    }
            };
"""

//Kotlin interface
interface Console {

    fun log(arg1:String? = null, arg2:String? = null, arg3:String? = null, arg4:String? = null,
            arg5:String? = null, arg6:String? = null, arg7:String? = null, arg8:String? = null,
            arg9:String? = null, arg10:String? = null)
}


//Interface impl
class ConsoleImpl() : Console {

    override fun log(arg1: String?, arg2: String?, arg3: String?, arg4: String?, arg5: String?, arg6: String?, arg7: String?, arg8: String?, arg9: String?, arg10: String?) {
    val values = listOfNotNull(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10).map {it.toString()}.filter { it != "undefined" }
    Log.d("ConsoleImpl", values.joinToString())
    }
}

//in setup
duktape.set("__consoleImpl", Console::class.java, ConsoleImpl())
duktape.evaluate("Console.log("message")) //exception thrown here

Upvotes: 0

Views: 256

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53437

duktape only supports ES5 fully, plus a few features from ES6 and ES7 (see post ES5 features). The spread syntax is an ES6 feature.

Upvotes: 1

Related Questions