array-buffer.js (1823B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { Actor } = require("resource://devtools/shared/protocol.js"); 8 const { 9 arrayBufferSpec, 10 } = require("resource://devtools/shared/specs/array-buffer.js"); 11 12 /** 13 * Creates an actor for the specified ArrayBuffer. 14 * 15 * @param {DevToolsServerConnection} conn 16 * The server connection. 17 * @param buffer ArrayBuffer 18 * The buffer. 19 */ 20 class ArrayBufferActor extends Actor { 21 constructor(conn, buffer) { 22 super(conn, arrayBufferSpec); 23 this.buffer = buffer; 24 this.bufferLength = buffer.byteLength; 25 26 // Align with ObjectActor interface 27 // (this is used from eval-with-debugger) 28 this.rawObj = this.buffer; 29 } 30 31 form() { 32 return { 33 actor: this.actorID, 34 length: this.bufferLength, 35 // The `typeName` is read in the source spec when reading "sourcedata" 36 // which can either be an ArrayBuffer actor or a LongString actor. 37 typeName: this.typeName, 38 }; 39 } 40 41 slice(start, count) { 42 const slice = new Uint8Array(this.buffer, start, count); 43 const parts = []; 44 let offset = 0; 45 const PortionSize = 0x6000; // keep it divisible by 3 for btoa() and join() 46 while (offset + PortionSize < count) { 47 parts.push( 48 btoa( 49 String.fromCharCode.apply( 50 null, 51 slice.subarray(offset, offset + PortionSize) 52 ) 53 ) 54 ); 55 offset += PortionSize; 56 } 57 parts.push( 58 btoa(String.fromCharCode.apply(null, slice.subarray(offset, count))) 59 ); 60 return { 61 from: this.actorID, 62 encoded: parts.join(""), 63 }; 64 } 65 } 66 67 module.exports = { 68 ArrayBufferActor, 69 };