JS 使用 Kotlin 和 Duktape 在 Android 中传播语法
JS spread syntax in Android using Kotlin and Duktape
我在 Android 应用程序中使用 Duktape 到 运行 JS 代码。我正在尝试在 JS 中实现一个日志函数,它接收多个可变参数。
当 运行 处理代码时出现异常:
方法抛出 'com.squareup.duktape.DuktapeException' 异常。
语法错误:预期标识符(第 3 行)
Duktape 是否支持 Spread syntax?一般情况下是否支持 Kotlin 选项?
//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
duktape 仅完全支持 ES5,以及 ES6 和 ES7 的一些功能 (see post ES5 features)。扩展语法是 ES6 的一个特性。
我在 Android 应用程序中使用 Duktape 到 运行 JS 代码。我正在尝试在 JS 中实现一个日志函数,它接收多个可变参数。 当 运行 处理代码时出现异常:
方法抛出 'com.squareup.duktape.DuktapeException' 异常。 语法错误:预期标识符(第 3 行)
Duktape 是否支持 Spread syntax?一般情况下是否支持 Kotlin 选项?
//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
duktape 仅完全支持 ES5,以及 ES6 和 ES7 的一些功能 (see post ES5 features)。扩展语法是 ES6 的一个特性。