问题
I'm trying to use the Rhino lib to call some javascript from java code. But it seems that it is choking on a typed array. Here is my simple js file
function decrypt(version, iv, encryptedBuffer) {
var output8;
output8 = new Uint8Array(encryptedBuffer);
var outputBuffer = output8.buffer;
var output32 = new Int32Array(outputBuffer);
... more funny code
}
But when calling
jsFunction.call(rhino, scope, scope, params);
I get this
sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "Uint8Array" is not defined. (JavaScript#5680)
Is there something extra to configure to have my little js snippet run ?
回答1:
Have you set setLanguageVersion(Context.VERSION_ES6)? Typed Array is new Javascript feature. Rhino does not allow Typed array call in old js versions.
回答2:
Try to use classes from package org.mozilla.javascript.typedarrays:
importPackage(org.mozilla.javascript.typedarrays);
var decrypt = function(version, iv, encryptedBuffer, off, len) {
var output8 = new NativeUint8Array(encryptedBuffer,off,len);
var outputBuffer = output8.buffer;
var output32 = new NativeInt32Array(outputBuffer,off,len);
//... more funny code
}
var encryptedBuffer = new NativeArrayBuffer(1024);
decrypt(null,null,encryptedBuffer,0,1024);
来源:https://stackoverflow.com/questions/40219226/java-calling-js-with-rhino-uint8array-is-not-defined