Java calling JS with Rhino (Uint8Array is not defined)

喜你入骨 提交于 2019-12-24 06:41:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!