Debian Codegraph @

js/src/vm/Id.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "js/Id.h"
8 #include "js/Printer.h" // js::GenericPrinter, js::Fprinter
9 #include "js/RootingAPI.h"
10
11 #include "vm/JSContext.h"
12 #include "vm/JSONPrinter.h" // js::JSONPrinter
13 #include "vm/SymbolType.h"
14
15 #include "vm/JSAtomUtils-inl.h" // AtomToId
16
17 using namespace js;
18
19 static const JS::PropertyKey voidKeyValue = JS::PropertyKey::Void();
20
21 const JS::HandleId JS::VoidHandlePropertyKey =
22 JS::HandleId::fromMarkedLocation(&voidKeyValue);
23
24 bool JS::PropertyKey::isPrivateName() const {
25 return isSymbol() && toSymbol()->isPrivateName();
26 }
27
28 bool JS::PropertyKey::isWellKnownSymbol(JS::SymbolCode code) const {
29 MOZ_ASSERT(uint32_t(code) < WellKnownSymbolLimit);
30 if (!isSymbol()) {
31 return false;
32 }
33 return toSymbol()->code() == code;
34 }
35
36 /* static */ JS::PropertyKey JS::PropertyKey::fromPinnedString(JSString* str) {
37 MOZ_ASSERT(AtomIsPinned(TlsContext.get(), &str->asAtom()));
38 return js::AtomToId(&str->asAtom());
39 }
40
41 /* static */ bool JS::PropertyKey::isNonIntAtom(JSAtom* atom) {
42 uint32_t index;
43 if (!atom->isIndex(&index)) {
44 return true;
45 }
46 static_assert(PropertyKey::IntMin == 0);
47 return index > PropertyKey::IntMax;
48 }
49
50 /* static */ bool JS::PropertyKey::isNonIntAtom(JSString* str) {
51 return JS::PropertyKey::isNonIntAtom(&str->asAtom());
52 }
53
54 #if defined(DEBUG) || defined(JS_JITSPEW)
55
56 void JS::PropertyKey::dump() const {
57 js::Fprinter out(stderr);
58 dump(out);
59 }
60
61 void JS::PropertyKey::dump(js::GenericPrinter& out) const {
62 js::JSONPrinter json(out);
63 dump(json);
64 out.put("\n");
65 }
66
67 void JS::PropertyKey::dump(js::JSONPrinter& json) const {
68 json.beginObject();
69 dumpFields(json);
70 json.endObject();
71 }
72
73 void JS::PropertyKey::dumpFields(js::JSONPrinter& json) const {
74 if (isAtom()) {
75 json.property("type", "atom");
76 toAtom()->dumpFields(json);
77 } else if (isInt()) {
78 json.property("type", "int");
79 json.property("value", toInt());
80 } else if (isSymbol()) {
81 json.property("type", "symbol");
82 toSymbol()->dumpFields(json);
83 } else if (isVoid()) {
84 json.property("type", "void");
85 } else {
86 json.formatProperty("type", "Unknown(%zx)", size_t(asRawBits()));
87 }
88 }
89
90 void JS::PropertyKey::dumpPropertyName(js::GenericPrinter& out) const {
91 if (isAtom()) {
92 toAtom()->dumpPropertyName(out);
93 } else if (isInt()) {
94 out.printf("%d", toInt());
95 } else if (isSymbol()) {
96 toSymbol()->dumpPropertyName(out);
97 } else if (isVoid()) {
98 out.put("(void)");
99 } else {
100 out.printf("Unknown(%zx)", size_t(asRawBits()));
101 }
102 }
103
104 void JS::PropertyKey::dumpStringContent(js::GenericPrinter& out) const {
105 if (isAtom()) {
106 toAtom()->dumpStringContent(out);
107 } else if (isInt()) {
108 out.printf("%d", toInt());
109 } else if (isSymbol()) {
110 toSymbol()->dumpStringContent(out);
111 } else if (isVoid()) {
112 out.put("(void)");
113 } else {
114 out.printf("Unknown(%zx)", size_t(asRawBits()));
115 }
116 }
117
118 #endif /* defined(DEBUG) || defined(JS_JITSPEW) */