string.js (2185B)
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 "use strict"; 5 6 const protocol = require("resource://devtools/shared/protocol.js"); 7 const { Arg, RetVal, generateActorSpec } = protocol; 8 9 const longStringSpec = generateActorSpec({ 10 typeName: "longstractor", 11 12 methods: { 13 substring: { 14 request: { 15 start: Arg(0), 16 end: Arg(1), 17 }, 18 response: { substring: RetVal() }, 19 }, 20 release: { release: true }, 21 }, 22 }); 23 24 exports.longStringSpec = longStringSpec; 25 26 /** 27 * When a caller is expecting a LongString actor but the string is already available on 28 * client, the SimpleStringFront can be used as it shares the same API as a 29 * LongStringFront but will not make unnecessary trips to the server. 30 */ 31 class SimpleStringFront { 32 constructor(str) { 33 this.str = str; 34 } 35 36 get length() { 37 return this.str.length; 38 } 39 40 get initial() { 41 return this.str; 42 } 43 44 string() { 45 return Promise.resolve(this.str); 46 } 47 48 substring(start, end) { 49 return Promise.resolve(this.str.substring(start, end)); 50 } 51 52 release() { 53 this.str = null; 54 return Promise.resolve(undefined); 55 } 56 } 57 58 exports.SimpleStringFront = SimpleStringFront; 59 60 // The long string actor needs some custom marshalling, because it is sometimes 61 // returned as a primitive rather than a complete form. 62 63 var stringActorType = protocol.types.getType("longstractor"); 64 protocol.types.addType("longstring", { 65 _actor: true, 66 write: (value, context, detail) => { 67 if (!(context instanceof protocol.Actor)) { 68 throw Error("Passing a longstring as an argument isn't supported."); 69 } 70 71 if (value.short) { 72 return value.str; 73 } 74 return stringActorType.write(value, context, detail); 75 }, 76 read: (value, context, detail) => { 77 if (context instanceof protocol.Actor) { 78 throw Error("Passing a longstring as an argument isn't supported."); 79 } 80 if (typeof value === "string") { 81 return new SimpleStringFront(value); 82 } 83 return stringActorType.read(value, context, detail); 84 }, 85 });