Skip to content

Instantly share code, notes, and snippets.

@mnaamani
Created May 8, 2013 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnaamani/5540054 to your computer and use it in GitHub Desktop.
Save mnaamani/5540054 to your computer and use it in GitHub Desktop.
module.exports = {
makeArrayBuffer:function(){
var arr = new ArrayBuffer(2);
arr[0]=1;arr[1]=2;
return arr;
},
makeUint8Array:function(n){
var arr = new ArrayBuffer(2);
arr[0]=3;arr[1]=4;
return new Uint8Array(arr);
}
}
<!doctype html>
<html>
<head>
<script src="./main.js"></script>
</head>
<body>
</body>
</html>
var A = require("./arr.js");
var arr = A.makeArrayBuffer();
console.log(arr instanceof ArrayBuffer);/* prints: false ,should be true*/
//in node-webkit arr is not recognised as an ArrayBuffer so the Array view is not created properly.
console.log(arr.byteLength);/*correctly prints: 2*/
var u8 = new Uint8Array(arr);
console.log("u8 length:",u8.length);/* should be 2, prints 0 */
console.log(u8[0],u8[1]);/* prints 'undefined undefined' it should print: 1 2 */
var view = A.makeUint8Array();
console.log(view instanceof Uint8Array);/*prints false, should be true*/
console.log(view[0],view[1]); /*correctly prints: 3 4 */
{
"name":"typed-arrays-test",
"main":"index.html"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment