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