Search code examples
javascriptkotlinj2v8

How to pass ByteArray as parameter to function using J2V8


I am attempting to use the J2V8 library on Android to call a javascript function and pass a parameter to it. The below code is what I have, but can't figure out how to pass the bytes ByteArray to the called function.

private fun decodePbfBytes(bytes: ByteArray?){
    val params = arrayOf(bytes)
    val v8 = V8.createV8Runtime()
    var v8Array = V8Array(v8) //how do I add bytes to this?
    val fileContents = MyApp.sharedInstance.assets.open("pbfIndex.js").bufferedReader().use { it.readText() }
    v8.executeScript(fileContents)
    val result = v8.executeStringFunction("", v8Array)
}

Solution

  • As a temporary solution, though very naive, I converted the byte array to a string of comma separated numbers then separating them in js into a byte array. Of coarse the size of the data passed around increases multi-fold. But for now that works and I can move forward with everything else.