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 file,
3 # You can obtain one at http://mozilla.org/MPL/2.0/.
5 # Pretty-printers for SpiderMonkey strings.
9 import mozilla
.prettyprinters
10 from mozilla
.CellHeader
import get_header_length_and_flags
11 from mozilla
.prettyprinters
import ptr_pretty_printer
14 chr(10000) # UPPER RIGHT PENCIL
15 except ValueError: # yuck, we are in Python 2.x, so chr() is 8-bit
16 chr = unichr # replace with teh unicodes
18 # Forget any printers from previous loads of this module.
19 mozilla
.prettyprinters
.clear_module_printers(__name__
)
22 class JSStringTypeCache(object):
23 # Cache information about the JSString type for this objfile.
24 def __init__(self
, cache
):
25 dummy
= gdb
.Value(0).cast(cache
.JSString_ptr_t
)
26 self
.ATOM_BIT
= dummy
["ATOM_BIT"]
27 self
.LINEAR_BIT
= dummy
["LINEAR_BIT"]
28 self
.INLINE_CHARS_BIT
= dummy
["INLINE_CHARS_BIT"]
29 self
.TYPE_FLAGS_MASK
= dummy
["TYPE_FLAGS_MASK"]
30 self
.LATIN1_CHARS_BIT
= dummy
["LATIN1_CHARS_BIT"]
33 class Common(mozilla
.prettyprinters
.Pointer
):
34 def __init__(self
, value
, cache
):
35 super(Common
, self
).__init
__(value
, cache
)
36 if not cache
.mod_JSString
:
37 cache
.mod_JSString
= JSStringTypeCache(cache
)
38 self
.stc
= cache
.mod_JSString
41 @ptr_pretty_printer("JSString")
42 class JSStringPtr(Common
):
43 def display_hint(self
):
48 length
, flags
= get_header_length_and_flags(self
.value
, self
.cache
)
51 0x2F2F2F2F: "JS_FRESH_NURSERY_PATTERN",
52 0x2B2B2B2B: "JS_SWEPT_NURSERY_PATTERN",
53 0xE5E5E5E5: "jemalloc freed memory",
54 }.get(flags
& 0xFFFFFFFF)
56 for ch
in "<CORRUPT:%s>" % corrupt
:
59 is_rope
= (flags
& self
.stc
.LINEAR_BIT
) == 0
61 for c
in JSStringPtr(d
["s"]["u2"]["left"], self
.cache
).chars():
63 for c
in JSStringPtr(d
["s"]["u3"]["right"], self
.cache
).chars():
66 is_inline
= (flags
& self
.stc
.INLINE_CHARS_BIT
) != 0
67 is_latin1
= (flags
& self
.stc
.LATIN1_CHARS_BIT
) != 0
70 chars
= d
["inlineStorageLatin1"]
72 chars
= d
["inlineStorageTwoByte"]
75 chars
= d
["s"]["u2"]["nonInlineCharsLatin1"]
77 chars
= d
["s"]["u2"]["nonInlineCharsTwoByte"]
78 for i
in range(int(length
)):
81 def to_string(self
, maxlen
=200):
83 invalid_chars_allowed
= 2
84 for c
in self
.chars():
90 # Convert from gdb.Value to string.
93 if invalid_chars_allowed
== 0:
94 s
+= "<TOO_MANY_INVALID_CHARS>"
97 invalid_chars_allowed
-= 1
98 s
+= "\\x%04x" % (c
& 0xFFFF)
102 @ptr_pretty_printer("JSAtom")
103 class JSAtomPtr(Common
):
105 return self
.value
.cast(self
.cache
.JSString_ptr_t
)