property-iterator.js (1898B)
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 { 8 FrontClassWithSpec, 9 registerFront, 10 } = require("resource://devtools/shared/protocol.js"); 11 const { 12 propertyIteratorSpec, 13 } = require("resource://devtools/shared/specs/property-iterator.js"); 14 const { 15 getAdHocFrontOrPrimitiveGrip, 16 } = require("resource://devtools/client/fronts/object.js"); 17 18 /** 19 * A PropertyIteratorFront provides a way to access to property names and 20 * values of an object efficiently, slice by slice. 21 * Note that the properties can be sorted in the backend, 22 * this is controled while creating the PropertyIteratorFront 23 * from ObjectFront.enumProperties. 24 */ 25 class PropertyIteratorFront extends FrontClassWithSpec(propertyIteratorSpec) { 26 form(data) { 27 this.actorID = data.actor; 28 this.count = data.count; 29 } 30 31 async slice(start, count) { 32 const result = await super.slice({ start, count }); 33 return this._onResult(result); 34 } 35 36 async all() { 37 const result = await super.all(); 38 return this._onResult(result); 39 } 40 41 _onResult(result) { 42 if (!result.ownProperties) { 43 return result; 44 } 45 46 // The result packet can have multiple properties that hold grips which we may need 47 // to turn into fronts. 48 const gripKeys = ["value", "getterValue", "get", "set"]; 49 50 Object.entries(result.ownProperties).forEach(([key, descriptor]) => { 51 if (descriptor) { 52 for (const gripKey of gripKeys) { 53 if (descriptor.hasOwnProperty(gripKey)) { 54 result.ownProperties[key][gripKey] = getAdHocFrontOrPrimitiveGrip( 55 descriptor[gripKey], 56 this 57 ); 58 } 59 } 60 } 61 }); 62 return result; 63 } 64 } 65 66 registerFront(PropertyIteratorFront);