2

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 ?

5
  • Are you sure your version of Rhino supports typed arrays? edit I think they're only supported in Rhino 1.8 or later. Commented Oct 24, 2016 at 13:04
  • I'm using version 1.7.7.1 which in the change logs says typedArray supported. Do I need java 8 maybe? I'll try now Commented Oct 24, 2016 at 13:21
  • Well Java 8 ships with Nashorn, the new JavaScript runtime for the JVM. I just did a quick search and found a bug on the topic. Commented Oct 24, 2016 at 13:22
  • 1
    I'm doing this call from Android so I cannot use nashorn. Either way I just tried my original code running with java 1.8 I still get the same error (To be clear I'm testing this on a normal Java (not from android) so that I'm 100% eveything is working before using it on Android) Commented Oct 24, 2016 at 13:28
  • How do you call JS code? Do you use rhino native classes or jdk API. Could you post your java code. Commented Oct 27, 2016 at 9:35

2 Answers 2

2

Have you set setLanguageVersion(Context.VERSION_ES6)? Typed Array is new Javascript feature. Rhino does not allow Typed array call in old js versions.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.