Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following in a js file:

function serialize() {
return new Uint8Array(1024);
}

I invoke that function with the java scripting api like so:

byte[] bytes = (byte[]) ((Invocable)engine).invokeFunction(SERIALIZE_FUNC);

Which immediately throws the exception:

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "Uint8Array" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "Uint8Array" is not defined. (<Unknown source>#1)

I dont know much about javascript, but I thought this is how you define an array in JavaScript?

share|improve this question
    
You define an array like this: return [];. –  tieTYT 19 hours ago
    
@tieTYT in javascript? then what is all this about: developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays –  Mark W 19 hours ago
    
Hm, I have a feeling your JavaScript engine simply doesn't support it. Which engine are you using? –  tieTYT 19 hours ago
    
@tieTYT The default engine, RhinoScriptEngine –  Mark W 19 hours ago
    
What version of Java are you running? It may be available in JDK 8. –  Pointy 19 hours ago

1 Answer 1

Your run time doesn't support typed arrays by the looks of it. Try this instead:

function serialize() {
    return [];
}

or if it needs to be 1024 in length create an empty array of said length like so instead

function serialize() {
    return new Array(1024);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.