1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
/*
* Copyright (C) 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
WI.ObjectTreeView = class ObjectTreeView extends WI.Object
{
constructor(object, mode, propertyPath, forceExpanding)
{
super();
console.assert(object instanceof WI.RemoteObject);
console.assert(!propertyPath || propertyPath instanceof WI.PropertyPath);
var providedPropertyPath = propertyPath instanceof WI.PropertyPath;
this._object = object;
this._mode = mode || WI.ObjectTreeView.defaultModeForObject(object);
this._propertyPath = propertyPath || new WI.PropertyPath(this._object, "this");
this._expanded = false;
this._hasLosslessPreview = false;
this._includeProtoProperty = true;
// If ObjectTree is used outside of the console, we do not know when to release
// WeakMap entries. Currently collapse would work. For the console, we can just
// listen for console clear events. Currently all ObjectTrees are in the console.
this._inConsole = true;
// Always force expanding for classes.
if (this._object.isClass())
forceExpanding = true;
this._element = document.createElement("div");
this._element.className = "object-tree";
if (this._object.preview) {
this._previewView = new WI.ObjectPreviewView(this._object.preview);
this._previewView.setOriginatingObjectInfo(this._object, providedPropertyPath ? propertyPath : null);
this._previewView.element.addEventListener("click", this._handlePreviewOrTitleElementClick.bind(this));
this._element.appendChild(this._previewView.element);
if (this._previewView.lossless && !this._propertyPath.parent && !forceExpanding) {
this._hasLosslessPreview = true;
this.element.classList.add("lossless-preview");
}
} else {
this._titleElement = document.createElement("span");
this._titleElement.className = "title";
this._titleElement.appendChild(WI.FormattedValue.createElementForRemoteObject(this._object));
this._titleElement.addEventListener("click", this._handlePreviewOrTitleElementClick.bind(this));
this._element.appendChild(this._titleElement);
}
this._outline = new WI.TreeOutline;
this._outline.compact = true;
this._outline.customIndent = true;
this._outline.element.classList.add("object");
this._element.appendChild(this._outline.element);
// FIXME: Support editable ObjectTrees.
}
// Static
static defaultModeForObject(object)
{
if (object.subtype === "class")
return WI.ObjectTreeView.Mode.ClassAPI;
return WI.ObjectTreeView.Mode.Properties;
}
static createEmptyMessageElement(message)
{
var emptyMessageElement = document.createElement("div");
emptyMessageElement.className = "empty-message";
emptyMessageElement.textContent = message;
return emptyMessageElement;
}
static comparePropertyDescriptors(propertyA, propertyB)
{
var a = propertyA.name;
var b = propertyB.name;
// Put __proto__ at the bottom.
if (a === "__proto__")
return 1;
if (b === "__proto__")
return -1;
// Put Internal properties at the top.
if (propertyA.isInternalProperty && !propertyB.isInternalProperty)
return -1;
if (propertyB.isInternalProperty && !propertyA.isInternalProperty)
return 1;
// Put Symbol properties at the bottom.
if (propertyA.symbol && !propertyB.symbol)
return 1;
if (propertyB.symbol && !propertyA.symbol)
return -1;
// Symbol properties may have the same description string but be different objects.
if (a === b)
return 0;
var diff = 0;
var chunk = /^\d+|^\D+/;
var chunka, chunkb, anum, bnum;
while (diff === 0) {
if (!a && b)
return -1;
if (!b && a)
return 1;
chunka = a.match(chunk)[0];
chunkb = b.match(chunk)[0];
anum = !isNaN(chunka);
bnum = !isNaN(chunkb);
if (anum && !bnum)
return -1;
if (bnum && !anum)
return 1;
if (anum && bnum) {
diff = chunka - chunkb;
if (diff === 0 && chunka.length !== chunkb.length) {
if (!+chunka && !+chunkb) // chunks are strings of all 0s (special case)
return chunka.length - chunkb.length;
else
return chunkb.length - chunka.length;
}
} else if (chunka !== chunkb)
return (chunka < chunkb) ? -1 : 1;
a = a.substring(chunka.length);
b = b.substring(chunkb.length);
}
return diff;
}
// Public
get object()
{
return this._object;
}
get element()
{
return this._element;
}
get treeOutline()
{
return this._outline;
}
get expanded()
{
return this._expanded;
}
expand()
{
if (this._expanded)
return;
if (this._hasLosslessPreview)
return;
this._expanded = true;
this._element.classList.add("expanded");
if (this._previewView)
this._previewView.showTitle();
this._trackWeakEntries();
this.update();
}
collapse()
{
if (!this._expanded)
return;
this._expanded = false;
this._element.classList.remove("expanded");
if (this._previewView)
this._previewView.showPreview();
this._untrackWeakEntries();
}
showOnlyProperties()
{
this._inConsole = false;
this._element.classList.add("properties-only");
this._includeProtoProperty = false;
}
appendTitleSuffix(suffixElement)
{
if (this._previewView)
this._previewView.element.appendChild(suffixElement);
else
this._titleElement.appendChild(suffixElement);
}
appendExtraPropertyDescriptor(propertyDescriptor)
{
if (!this._extraProperties)
this._extraProperties = [];
this._extraProperties.push(propertyDescriptor);
}
setPrototypeNameOverride(override)
{
this._prototypeNameOverride = override;
}
// Protected
update()
{
if (this._object.isCollectionType() && this._mode === WI.ObjectTreeView.Mode.Properties)
this._object.getCollectionEntries(0, 100, this._updateChildren.bind(this, this._updateEntries));
else if (this._object.isClass())
this._object.classPrototype.getDisplayablePropertyDescriptors(this._updateChildren.bind(this, this._updateProperties));
else if (this._mode === WI.ObjectTreeView.Mode.PureAPI)
this._object.getOwnPropertyDescriptors(this._updateChildren.bind(this, this._updateProperties));
else
this._object.getDisplayablePropertyDescriptors(this._updateChildren.bind(this, this._updateProperties));
}
// Private
_updateChildren(handler, list)
{
this._outline.removeChildren();
if (!list) {
var errorMessageElement = WI.ObjectTreeView.createEmptyMessageElement(WI.UIString("Could not fetch properties. Object may no longer exist."));
this._outline.appendChild(new WI.TreeElement(errorMessageElement, null, false));
return;
}
handler.call(this, list, this._propertyPath);
this.dispatchEventToListeners(WI.ObjectTreeView.Event.Updated);
}
_updateEntries(entries, propertyPath)
{
for (var entry of entries) {
if (entry.key) {
this._outline.appendChild(new WI.ObjectTreeMapKeyTreeElement(entry.key, propertyPath));
this._outline.appendChild(new WI.ObjectTreeMapValueTreeElement(entry.value, propertyPath, entry.key));
} else
this._outline.appendChild(new WI.ObjectTreeSetIndexTreeElement(entry.value, propertyPath));
}
if (!this._outline.children.length) {
var emptyMessageElement = WI.ObjectTreeView.createEmptyMessageElement(WI.UIString("No Entries"));
this._outline.appendChild(new WI.TreeElement(emptyMessageElement, null, false));
}
// Show the prototype so users can see the API.
this._object.getOwnPropertyDescriptor("__proto__", (propertyDescriptor) => {
if (propertyDescriptor)
this._outline.appendChild(new WI.ObjectTreePropertyTreeElement(propertyDescriptor, propertyPath, this._mode));
});
}
_updateProperties(properties, propertyPath)
{
if (this._extraProperties)
properties = properties.concat(this._extraProperties);
properties.sort(WI.ObjectTreeView.comparePropertyDescriptors);
var isArray = this._object.isArray();
var isPropertyMode = this._mode === WI.ObjectTreeView.Mode.Properties;
var hadProto = false;
for (var propertyDescriptor of properties) {
if (propertyDescriptor.name === "__proto__") {
// COMPATIBILITY (iOS 8): Sometimes __proto__ is not a value, but a get/set property.
// In those cases it is actually not useful to show.
if (!propertyDescriptor.hasValue())
continue;
if (!this._includeProtoProperty)
continue;
hadProto = true;
}
if (isArray && isPropertyMode) {
if (propertyDescriptor.isIndexProperty())
this._outline.appendChild(new WI.ObjectTreeArrayIndexTreeElement(propertyDescriptor, propertyPath));
else if (propertyDescriptor.name === "__proto__")
this._outline.appendChild(new WI.ObjectTreePropertyTreeElement(propertyDescriptor, propertyPath, this._mode, this._prototypeNameOverride));
} else
this._outline.appendChild(new WI.ObjectTreePropertyTreeElement(propertyDescriptor, propertyPath, this._mode, this._prototypeNameOverride));
}
if (!this._outline.children.length || (hadProto && this._outline.children.length === 1)) {
var emptyMessageElement = WI.ObjectTreeView.createEmptyMessageElement(WI.UIString("No Properties"));
this._outline.insertChild(new WI.TreeElement(emptyMessageElement, null, false), 0);
}
}
_handlePreviewOrTitleElementClick(event)
{
if (this._hasLosslessPreview)
return;
if (!this._expanded)
this.expand();
else
this.collapse();
event.stopPropagation();
}
_trackWeakEntries()
{
if (this._trackingEntries)
return;
if (!this._object.isWeakCollection())
return;
this._trackingEntries = true;
if (this._inConsole) {
WI.logManager.addEventListener(WI.LogManager.Event.Cleared, this._untrackWeakEntries, this);
WI.logManager.addEventListener(WI.LogManager.Event.SessionStarted, this._untrackWeakEntries, this);
}
}
_untrackWeakEntries()
{
if (!this._trackingEntries)
return;
if (!this._object.isWeakCollection())
return;
this._trackingEntries = false;
this._object.releaseWeakCollectionEntries();
if (this._inConsole) {
WI.logManager.removeEventListener(WI.LogManager.Event.Cleared, this._untrackWeakEntries, this);
WI.logManager.removeEventListener(WI.LogManager.Event.SessionStarted, this._untrackWeakEntries, this);
}
// FIXME: This only tries to release weak entries if this object was a WeakMap.
// If there was a WeakMap expanded in a sub-object, we will never release those values.
// Should we attempt walking the entire tree and release weak collections?
}
};
WI.ObjectTreeView.Mode = {
Properties: Symbol("object-tree-properties"), // Properties
PrototypeAPI: Symbol("object-tree-prototype-api"), // API view on a live object instance, so getters can be invoked.
ClassAPI: Symbol("object-tree-class-api"), // API view without an object instance, can not invoke getters.
PureAPI: Symbol("object-tree-pure-api"), // API view without special displayable property handling, just own properties. Getters can be invoked if the property path has a non-prototype object.
};
WI.ObjectTreeView.Event = {
Updated: "object-tree-updated",
};
|