Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
1 # Pretty-printers and utilities for SpiderMonkey rooting templates:
2 # Rooted, Handle, MutableHandle, etc.
4 import mozilla.prettyprinters
5 from mozilla.prettyprinters import pretty_printer, template_pretty_printer
7 # Forget any printers from previous loads of this module.
8 mozilla.prettyprinters.clear_module_printers(__name__)
10 # Common base class for all the rooting template pretty-printers. All these
11 # templates have one member holding the referent (or a pointer to it), so
12 # there's not much to it.
13 class Common(object):
14 # The name of the template member holding the referent.
15 member = 'ptr'
17 # If True, this is a handle type, and should be dereferenced. If False,
18 # the template member holds the referent directly.
19 handle = False
21 # Initialize a pretty-printer for |value|, using |cache|.
22 #
23 # If given, |content_printer| is a pretty-printer constructor to use for
24 # this handle/root/etc.'s referent. Usually, we can just omit this argument
25 # and let GDB choose a pretty-printer for the referent given its type, but
26 # when the referent is a typedef of an integral type (say, |jsid| in a
27 # non-|DEBUG| build), the GNU toolchain (at least) loses the typedef name,
28 # and all we know about the referent is its fundamental integer type ---
29 # |JS::Rooted<jsid>|, for example, appears in GDB as |JS::Rooted<long>| ---
30 # and we are left with no way to choose a meaningful pretty-printer based on
31 # the type of the referent alone. However, because we know that the only
32 # integer type for which |JS::Rooted| is likely to be instantiated is
33 # |jsid|, we *can* register a pretty-printer constructor for the full
34 # instantiation |JS::Rooted<long>|. That constructor creates a |JS::Rooted|
35 # pretty-printer, and explicitly specifies the constructor for the referent,
36 # using this initializer's |content_printer| argument.
37 def __init__(self, value, cache, content_printer=None):
38 self.value = value
39 self.cache = cache
40 self.content_printer = content_printer
41 def to_string(self):
42 ptr = self.value[self.member]
43 if self.handle:
44 ptr = ptr.dereference()
45 if self.content_printer:
46 return self.content_printer(ptr, self.cache).to_string()
47 else:
48 # As of 2012-11, GDB suppresses printing pointers in replacement
49 # values; see http://sourceware.org/ml/gdb/2012-11/msg00055.html
50 # That means that simply returning the 'ptr' member won't work.
51 # Instead, just invoke GDB's formatter ourselves.
52 return str(ptr)
54 @template_pretty_printer("JS::Rooted")
55 class Rooted(Common):
56 pass
58 @template_pretty_printer("JS::Handle")
59 class Handle(Common):
60 handle = True
62 @template_pretty_printer("JS::MutableHandle")
63 class MutableHandle(Common):
64 handle = True
66 @template_pretty_printer("js::EncapsulatedPtr")
67 class EncapsulatedPtr(Common):
68 member = 'value'
70 @pretty_printer("js::EncapsulatedValue")
71 class EncapsulatedValue(Common):
72 member = 'value'
74 @pretty_printer("js::BarrieredValue")
75 class BarrieredValue(Common):
76 member = 'value'
78 # Return the referent of a HeapPtr, Rooted, or Handle.
79 def deref(root):
80 tag = root.type.strip_typedefs().tag
81 if not tag:
82 raise TypeError("Can't dereference type with no structure tag: %s" % (root.type,))
83 elif tag.startswith('js::HeapPtr<'):
84 return root['value']
85 elif tag.startswith('JS::Rooted<'):
86 return root['ptr']
87 elif tag.startswith('JS::Handle<'):
88 return root['ptr']
89 else:
90 raise NotImplementedError