##// END OF EJS Templates
Make it possible for msg.buffers in javascript to be either ArrayBuffers or views
Jason Grout -
Show More
@@ -75,15 +75,17 b' define(['
75 msg = _.clone(msg);
75 msg = _.clone(msg);
76 var offsets = [];
76 var offsets = [];
77 var buffers = [];
77 var buffers = [];
78 msg.buffers.map(function (buf) {
78 var i;
79 buffers.push(buf);
79 for (i = 0; i < msg.buffers.length; i++) {
80 });
80 // msg.buffers elements could be either views or ArrayBuffers
81 // buffers elements are ArrayBuffers
82 buffers.push(msg.buffers[i].buffer || msg.buffers[i])
83 }
81 delete msg.buffers;
84 delete msg.buffers;
82 var json_utf8 = (new TextEncoder('utf8')).encode(JSON.stringify(msg));
85 var json_utf8 = (new TextEncoder('utf8')).encode(JSON.stringify(msg));
83 buffers.unshift(json_utf8);
86 buffers.unshift(json_utf8);
84 var nbufs = buffers.length;
87 var nbufs = buffers.length;
85 offsets.push(4 * (nbufs + 1));
88 offsets.push(4 * (nbufs + 1));
86 var i;
87 for (i = 0; i + 1 < buffers.length; i++) {
89 for (i = 0; i + 1 < buffers.length; i++) {
88 offsets.push(offsets[offsets.length-1] + buffers[i].byteLength);
90 offsets.push(offsets[offsets.length-1] + buffers[i].byteLength);
89 }
91 }
@@ -100,7 +102,7 b' define(['
100 }
102 }
101 // write all the buffers at their respective offsets
103 // write all the buffers at their respective offsets
102 for (i = 0; i < buffers.length; i++) {
104 for (i = 0; i < buffers.length; i++) {
103 msg_buf.set(new Uint8Array(buffers[i].buffer), offsets[i]);
105 msg_buf.set(new Uint8Array(buffers[i]), offsets[i]);
104 }
106 }
105
107
106 // return raw ArrayBuffer
108 // return raw ArrayBuffer
General Comments 0
You need to be logged in to leave comments. Login now