1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5class LiveHeapObject(object):
6  """Data structure for representing an object in the heap snapshot.
7
8  Attributes:
9    object_id: int, identifier for the object.
10    type_string: str, describes the type of the node.
11    class_name: str, describes the class of the JavaScript object
12        represented by this LiveHeapObject.
13    edges_to: [RetainingEdge], edges whose end point this LiveHeapObject is.
14    edges_from: [RetainingEdge], edges whose start point this LiveHeapObject is.
15    string: str, for string LiveHeapObjects, contains the string the
16        LiveHeapObject represents. Empty string for LiveHeapObjects which are
17        not strings.
18    name: str, how to refer to this LiveHeapObject.
19  """
20
21  def __init__(self, object_id, type_string, class_name):
22    """Initializes the LiveHeapObject object.
23
24    Args:
25      object_id: int, identifier for the LiveHeapObject.
26      type_string: str, the type of the node.
27      class_name: str, the class of the object this LiveHeapObject represents.
28    """
29    self.object_id = object_id
30    self.type_string = type_string
31    self.class_name = class_name
32    self.edges_to = []
33    self.edges_from = []
34    self.string = ''
35    self.name = ''
36
37  def AddEdgeTo(self, edge):
38    """Associates an Edge with the LiveHeapObject (the end point).
39
40    Args:
41      edge: Edge, an edge whose end point this LiveHeapObject is.
42    """
43    self.edges_to.append(edge)
44
45  def AddEdgeFrom(self, edge):
46    """Associates an Edge with the LiveHeapObject (the start point).
47
48    Args:
49      edge: Edge, an edge whose start point this LiveHeapObject is.
50    """
51    self.edges_from.append(edge)
52
53  def __str__(self):
54    prefix = 'LiveHeapObject(' + str(self.object_id) + ' '
55    if self.type_string == 'object':
56      return prefix + self.class_name + ')'
57    return prefix + self.type_string + ')'
58