pnizzle
pnizzle

Reputation: 6431

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)
}

Upvotes: 1

Views: 442

Answers (2)

pnizzle
pnizzle

Reputation: 6431

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.

Upvotes: 1

Eugene
Eugene

Reputation: 1533

Try this :

var buffer = V8ArrayBuffer(bytes, bytes.size)
var v8Array = V8TypedArray(v8, bytes, V8Value.BYTE, 0, 2)
V8Array(v8).add("v8Array", v8Array)

or

...
V8Array(v8).push( v8Array)

Upvotes: -1

Related Questions