Bug 1635839 - Part 1: Add PrivateName Symbol code. r=jorendorff

Differential Revision: https://phabricator.services.mozilla.com/D74102
This commit is contained in:
André Bargull 2020-06-02 12:29:27 +00:00
parent 904cf26dfd
commit 2e3e2805a9
4 changed files with 30 additions and 13 deletions

View File

@ -75,6 +75,7 @@ enum class SymbolCode : uint32_t {
WellKnownAPILimit =
0x80000000, // matches JS::shadow::Symbol::WellKnownAPILimit for inline
// use
PrivateNameSymbol = 0xfffffffd, // created by the #PrivateName syntax.
InSymbolRegistry =
0xfffffffe, // created by Symbol.for() or JS::GetSymbolFor()
UniqueSymbol = 0xffffffff // created by Symbol() or JS::NewSymbol()

View File

@ -12,6 +12,7 @@ from mozilla.CellHeader import get_header_ptr
mozilla.prettyprinters.clear_module_printers(__name__)
# JS::SymbolCode enumerators
PrivateNameSymbol = 0xfffffffd
InSymbolRegistry = 0xfffffffe
UniqueSymbol = 0xffffffff
@ -30,6 +31,8 @@ class JSSymbolPtr(mozilla.prettyprinters.Pointer):
return "Symbol.for({})".format(desc)
elif code == UniqueSymbol:
return "Symbol({})".format(desc)
elif code == PrivateNameSymbol:
return "#{}".format(desc)
else:
# Well-known symbol. Strip off the quotes added by the JSString *
# pretty-printer.

View File

@ -103,6 +103,11 @@ void Symbol::dump(js::GenericPrinter& out) {
if (code_ == SymbolCode::UniqueSymbol) {
out.printf("@%p", (void*)this);
}
} else if (code_ == SymbolCode::PrivateNameSymbol) {
MOZ_ASSERT(description());
out.putChar('#');
description()->dumpCharsNoNewline(out);
out.printf("@%p", (void*)this);
} else {
out.printf("<Invalid Symbol code=%u>", unsigned(code_));
}

View File

@ -59,26 +59,34 @@ static JSString* StringToSource(JSContext* cx, JSString* str) {
static JSString* SymbolToSource(JSContext* cx, Symbol* symbol) {
RootedString desc(cx, symbol->description());
SymbolCode code = symbol->code();
if (code != SymbolCode::InSymbolRegistry &&
code != SymbolCode::UniqueSymbol) {
if (symbol->isWellKnownSymbol()) {
// Well-known symbol.
MOZ_ASSERT(uint32_t(code) < JS::WellKnownSymbolLimit);
return desc;
}
JSStringBuilder buf(cx);
if (code == SymbolCode::InSymbolRegistry ? !buf.append("Symbol.for(")
: !buf.append("Symbol(")) {
return nullptr;
}
if (desc) {
UniqueChars quoted = QuoteString(cx, desc, '"');
if (!quoted || !buf.append(quoted.get(), strlen(quoted.get()))) {
if (code == SymbolCode::PrivateNameSymbol) {
MOZ_ASSERT(desc);
if (!buf.append('#') || !buf.append(desc)) {
return nullptr;
}
} else {
MOZ_ASSERT(code == SymbolCode::InSymbolRegistry ||
code == SymbolCode::UniqueSymbol);
if (code == SymbolCode::InSymbolRegistry ? !buf.append("Symbol.for(")
: !buf.append("Symbol(")) {
return nullptr;
}
if (desc) {
UniqueChars quoted = QuoteString(cx, desc, '"');
if (!quoted || !buf.append(quoted.get(), strlen(quoted.get()))) {
return nullptr;
}
}
if (!buf.append(')')) {
return nullptr;
}
}
if (!buf.append(')')) {
return nullptr;
}
return buf.finishString();
}