Merge pull request #8120 from jasongrout/arraybuffers

Make it possible for msg.buffers in javascript to be either ArrayBuffers or views
This commit is contained in:
Min RK 2015-03-23 14:14:09 -07:00
commit 4b0553ae45

View File

@ -75,15 +75,18 @@ define([
msg = _.clone(msg);
var offsets = [];
var buffers = [];
msg.buffers.map(function (buf) {
buffers.push(buf);
});
var i;
for (i = 0; i < msg.buffers.length; i++) {
// msg.buffers elements could be either views or ArrayBuffers
// buffers elements are ArrayBuffers
var b = msg.buffers[i];
buffers.push(b.buffer instanceof ArrayBuffer ? b.buffer : b);
}
delete msg.buffers;
var json_utf8 = (new TextEncoder('utf8')).encode(JSON.stringify(msg));
buffers.unshift(json_utf8);
var nbufs = buffers.length;
offsets.push(4 * (nbufs + 1));
var i;
for (i = 0; i + 1 < buffers.length; i++) {
offsets.push(offsets[offsets.length-1] + buffers[i].byteLength);
}
@ -100,7 +103,7 @@ define([
}
// write all the buffers at their respective offsets
for (i = 0; i < buffers.length; i++) {
msg_buf.set(new Uint8Array(buffers[i].buffer), offsets[i]);
msg_buf.set(new Uint8Array(buffers[i]), offsets[i]);
}
// return raw ArrayBuffer