michael@0: # Pretty-printers for SpiderMonkey strings. michael@0: michael@0: import gdb michael@0: import mozilla.prettyprinters michael@0: from mozilla.prettyprinters import pretty_printer, ptr_pretty_printer michael@0: michael@0: # Forget any printers from previous loads of this module. michael@0: mozilla.prettyprinters.clear_module_printers(__name__) michael@0: michael@0: # Cache information about the JSString type for this objfile. michael@0: class JSStringTypeCache(object): michael@0: def __init__(self, cache): michael@0: dummy = gdb.Value(0).cast(cache.JSString_ptr_t) michael@0: self.LENGTH_SHIFT = dummy['LENGTH_SHIFT'] michael@0: self.FLAGS_MASK = dummy['FLAGS_MASK'] michael@0: self.ROPE_FLAGS = dummy['ROPE_FLAGS'] michael@0: self.ATOM_BIT = dummy['ATOM_BIT'] michael@0: michael@0: class Common(mozilla.prettyprinters.Pointer): michael@0: def __init__(self, value, cache): michael@0: super(Common, self).__init__(value, cache) michael@0: if not cache.mod_JSString: michael@0: cache.mod_JSString = JSStringTypeCache(cache) michael@0: self.stc = cache.mod_JSString michael@0: michael@0: @ptr_pretty_printer("JSString") michael@0: class JSStringPtr(Common): michael@0: def display_hint(self): michael@0: return "string" michael@0: michael@0: def jschars(self): michael@0: d = self.value['d'] michael@0: lengthAndFlags = d['lengthAndFlags'] michael@0: length = lengthAndFlags >> self.stc.LENGTH_SHIFT michael@0: is_rope = (lengthAndFlags & self.stc.FLAGS_MASK) == self.stc.ROPE_FLAGS michael@0: if is_rope: michael@0: for c in JSStringPtr(d['u1']['left'], self.cache).jschars(): michael@0: yield c michael@0: for c in JSStringPtr(d['s']['u2']['right'], self.cache).jschars(): michael@0: yield c michael@0: else: michael@0: chars = d['u1']['chars'] michael@0: for i in xrange(length): michael@0: yield chars[i] michael@0: michael@0: def to_string(self): michael@0: s = u'' michael@0: for c in self.jschars(): michael@0: s += unichr(c) michael@0: return s michael@0: michael@0: @ptr_pretty_printer("JSAtom") michael@0: class JSAtomPtr(Common): michael@0: def to_string(self): michael@0: return self.value.cast(self.cache.JSString_ptr_t)