JSSymbol.py (1390B)
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/. 4 5 # Pretty-printer for SpiderMonkey symbols. 6 7 import mozilla.prettyprinters 8 from mozilla.CellHeader import get_header_ptr 9 from mozilla.prettyprinters import ptr_pretty_printer 10 11 # Forget any printers from previous loads of this module. 12 mozilla.prettyprinters.clear_module_printers(__name__) 13 14 # JS::SymbolCode enumerators 15 PrivateNameSymbol = 0xFFFFFFFD 16 InSymbolRegistry = 0xFFFFFFFE 17 UniqueSymbol = 0xFFFFFFFF 18 19 20 @ptr_pretty_printer("JS::Symbol") 21 class JSSymbolPtr(mozilla.prettyprinters.Pointer): 22 def __init__(self, value, cache): 23 super().__init__(value, cache) 24 self.value = value 25 26 def to_string(self): 27 code = int(self.value["code_"]) & 0xFFFFFFFF 28 desc = str(get_header_ptr(self.value, self.cache.JSString_ptr_t)) 29 if code == InSymbolRegistry: 30 return f"Symbol.for({desc})" 31 elif code == UniqueSymbol: 32 return f"Symbol({desc})" 33 elif code == PrivateNameSymbol: 34 return f"#{desc}" 35 else: 36 # Well-known symbol. Strip off the quotes added by the JSString * 37 # pretty-printer. 38 assert desc[0] == '"' 39 assert desc[-1] == '"' 40 return desc[1:-1]