property-descriptor.js (2729B)
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 DevToolsUtils = require("resource://devtools/shared/DevToolsUtils.js"); 8 9 const { 10 isStorage, 11 } = require("resource://devtools/server/actors/object/utils.js"); 12 13 /** 14 * A helper method that creates a property descriptor for the provided object, 15 * properly formatted for sending in a protocol response. 16 * 17 * @param ObjectActor objectActor 18 * The object actor of the object we are current listing properties. 19 * @param string name 20 * The property that the descriptor is generated for. 21 * @param {number} depth 22 * Current depth in the generated preview object sent to the client. 23 * @param boolean [onlyEnumerable] 24 * Optional: true if you want a descriptor only for an enumerable 25 * property, false otherwise. 26 * @return object|undefined 27 * The property descriptor, or undefined if objectActor is not an enumerable 28 * property and onlyEnumerable=true. 29 */ 30 function propertyDescriptor(objectActor, name, depth, onlyEnumerable) { 31 if (!DevToolsUtils.isSafeDebuggerObject(objectActor.obj)) { 32 return undefined; 33 } 34 35 let desc; 36 try { 37 desc = objectActor.obj.getOwnPropertyDescriptor(name); 38 } catch (e) { 39 // Calling getOwnPropertyDescriptor on wrapped native prototypes is not 40 // allowed (bug 560072). Inform the user with a bogus, but hopefully 41 // explanatory, descriptor. 42 return { 43 configurable: false, 44 writable: false, 45 enumerable: false, 46 value: e.name, 47 }; 48 } 49 50 if (isStorage(objectActor.obj)) { 51 if (name === "length") { 52 return undefined; 53 } 54 return desc; 55 } 56 57 if (!desc || (onlyEnumerable && !desc.enumerable)) { 58 return undefined; 59 } 60 61 const retval = { 62 configurable: desc.configurable, 63 enumerable: desc.enumerable, 64 }; 65 const { rawObj } = objectActor; 66 67 if ("value" in desc) { 68 retval.writable = desc.writable; 69 retval.value = objectActor.createValueGrip(desc.value, depth); 70 } else if (objectActor.threadActor.getWatchpoint(rawObj, name.toString())) { 71 const watchpoint = objectActor.threadActor.getWatchpoint(rawObj, name.toString()); 72 retval.value = objectActor.createValueGrip(watchpoint.desc.value, depth); 73 retval.watchpoint = watchpoint.watchpointType; 74 } else { 75 if ("get" in desc) { 76 retval.get = objectActor.createValueGrip(desc.get, depth); 77 } 78 79 if ("set" in desc) { 80 retval.set = objectActor.createValueGrip(desc.set, depth); 81 } 82 } 83 return retval; 84 } 85 86 exports.propertyDescriptor = propertyDescriptor;