1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/gdb/mozilla/JSString.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,55 @@ 1.4 +# Pretty-printers for SpiderMonkey strings. 1.5 + 1.6 +import gdb 1.7 +import mozilla.prettyprinters 1.8 +from mozilla.prettyprinters import pretty_printer, ptr_pretty_printer 1.9 + 1.10 +# Forget any printers from previous loads of this module. 1.11 +mozilla.prettyprinters.clear_module_printers(__name__) 1.12 + 1.13 +# Cache information about the JSString type for this objfile. 1.14 +class JSStringTypeCache(object): 1.15 + def __init__(self, cache): 1.16 + dummy = gdb.Value(0).cast(cache.JSString_ptr_t) 1.17 + self.LENGTH_SHIFT = dummy['LENGTH_SHIFT'] 1.18 + self.FLAGS_MASK = dummy['FLAGS_MASK'] 1.19 + self.ROPE_FLAGS = dummy['ROPE_FLAGS'] 1.20 + self.ATOM_BIT = dummy['ATOM_BIT'] 1.21 + 1.22 +class Common(mozilla.prettyprinters.Pointer): 1.23 + def __init__(self, value, cache): 1.24 + super(Common, self).__init__(value, cache) 1.25 + if not cache.mod_JSString: 1.26 + cache.mod_JSString = JSStringTypeCache(cache) 1.27 + self.stc = cache.mod_JSString 1.28 + 1.29 +@ptr_pretty_printer("JSString") 1.30 +class JSStringPtr(Common): 1.31 + def display_hint(self): 1.32 + return "string" 1.33 + 1.34 + def jschars(self): 1.35 + d = self.value['d'] 1.36 + lengthAndFlags = d['lengthAndFlags'] 1.37 + length = lengthAndFlags >> self.stc.LENGTH_SHIFT 1.38 + is_rope = (lengthAndFlags & self.stc.FLAGS_MASK) == self.stc.ROPE_FLAGS 1.39 + if is_rope: 1.40 + for c in JSStringPtr(d['u1']['left'], self.cache).jschars(): 1.41 + yield c 1.42 + for c in JSStringPtr(d['s']['u2']['right'], self.cache).jschars(): 1.43 + yield c 1.44 + else: 1.45 + chars = d['u1']['chars'] 1.46 + for i in xrange(length): 1.47 + yield chars[i] 1.48 + 1.49 + def to_string(self): 1.50 + s = u'' 1.51 + for c in self.jschars(): 1.52 + s += unichr(c) 1.53 + return s 1.54 + 1.55 +@ptr_pretty_printer("JSAtom") 1.56 +class JSAtomPtr(Common): 1.57 + def to_string(self): 1.58 + return self.value.cast(self.cache.JSString_ptr_t)