我正在使用Duktape在我的安卓应用程序中运行JS代码。我正在尝试在JS中实现一个日志函数,它接收多个varargs参数。当运行代码时,我得到了一个异常:
方法引发了“com.squareup.duktape.DuktapeException”异常。SyntaxError:期望的标识符(第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发布于 2020-04-08 15:00:41
duktape仅完全支持ES5,外加来自ES6和ES7 (see post ES5 features)的一些功能。扩展语法是一个ES6特性。
https://stackoverflow.com/questions/61082566
复制相似问题