【发布时间】:2020-02-26 11:20:53
【问题描述】:
我正在使用 Rhino 评估一些 javascript,我只是将 ByteArray 从 kotlin 传递给 Javascript 中的函数。我知道这样说不好,但我在 Swift 和 .Net Core 中使用相同的 Js 文件,在这种情况下失败的行没有问题。
如下,我将bytes,一个ByteArray,传递给JS函数decodeByteArray()。失败的那一行是我用//invalid argument注释标记的那一行
我已经检查了 ByteArray 的内容,它们与预期的一样。
我做错了什么或遗漏了什么?
Javascript
function decodeByteArray(buf) {
var pbf = new Pbf(buf);
return JSON.stringify(decode(pbf));
}
function Pbf(buf) {
this.buf = ArrayBuffer.isView && ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
this.pos = 0;
this.type = 0;
this.length = this.buf.length;
setUp(this);
}
科特林
private fun decodePbfBytes(bytes: ByteArray?): Any? {
var jsResult: Any? = null;
var params = arrayOf(bytes)
val rhino = org.mozilla.javascript.Context.enter()
rhino.optimizationLevel = -1
rhino.languageVersion = org.mozilla.javascript.Context.VERSION_ES6
try{
val scope = rhino.initStandardObjects()
val assetManager = MyApp.sharedInstance.assets
val input = assetManager.open("pbfIndex.js") //the js file containing js code
val targetReader = InputStreamReader(input)
rhino.evaluateReader(scope, targetReader, "JavaScript", 1,null)
val obj = scope.get("decodeByteArray", scope)
if (obj is org.mozilla.javascript.Function){
jsResult = obj.call(rhino, scope, scope, params)
jsResult = org.mozilla.javascript.Context.toString(jsResult)
}
}catch (ex: Exception){
Log.e("Error", ex.localizedMessage)
}finally {
org.mozilla.javascript.Context.exit()
}
return jsResult
}
【问题讨论】:
-
我在这个 Rhino 上花了这么多时间我打算去其他库寻找改变:medium.com/tech-quizlet/…