-
Notifications
You must be signed in to change notification settings - Fork 54
/
SymbolType.cpp
143 lines (122 loc) · 3.65 KB
/
SymbolType.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "vm/SymbolType.h"
#include "builtin/Symbol.h"
#include "gc/Allocator.h"
#include "gc/HashUtil.h"
#include "gc/Rooting.h"
#include "util/StringBuffer.h"
#include "vm/JSContext.h"
#include "vm/Realm.h"
#include "vm/Realm-inl.h"
using JS::Symbol;
using namespace js;
Symbol* Symbol::newInternal(JSContext* cx, JS::SymbolCode code, uint32_t hash,
HandleAtom description) {
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
AutoAllocInAtomsZone az(cx);
Symbol* p = Allocate<JS::Symbol>(cx);
if (!p) {
return nullptr;
}
return new (p) Symbol(code, hash, description);
}
Symbol* Symbol::new_(JSContext* cx, JS::SymbolCode code,
HandleString description) {
RootedAtom atom(cx);
if (description) {
atom = AtomizeString(cx, description);
if (!atom) {
return nullptr;
}
}
Symbol* sym = newInternal(cx, code, cx->runtime()->randomHashCode(), atom);
if (sym) {
cx->markAtom(sym);
}
return sym;
}
Symbol* Symbol::for_(JSContext* cx, HandleString description) {
RootedAtom atom(cx, AtomizeString(cx, description));
if (!atom) {
return nullptr;
}
SymbolRegistry& registry = cx->symbolRegistry();
DependentAddPtr<SymbolRegistry> p(cx, registry, atom);
if (p) {
cx->markAtom(*p);
return *p;
}
// Rehash the hash of the atom to give the corresponding symbol a hash
// that is different than the hash of the corresponding atom.
HashNumber hash = mozilla::HashGeneric(atom->hash());
Symbol* sym = newInternal(cx, SymbolCode::InSymbolRegistry, hash, atom);
if (!sym) {
return nullptr;
}
if (!p.add(cx, registry, atom, sym)) {
return nullptr;
}
cx->markAtom(sym);
return sym;
}
#if defined(DEBUG) || defined(JS_JITSPEW)
void Symbol::dump() {
js::Fprinter out(stderr);
dump(out);
}
void Symbol::dump(js::GenericPrinter& out) {
if (isWellKnownSymbol()) {
// All the well-known symbol names are ASCII.
description_->dumpCharsNoNewline(out);
} else if (code_ == SymbolCode::InSymbolRegistry ||
code_ == SymbolCode::UniqueSymbol) {
out.printf(code_ == SymbolCode::InSymbolRegistry ? "Symbol.for("
: "Symbol(");
if (description_) {
description_->dumpCharsNoNewline(out);
} else {
out.printf("undefined");
}
out.putChar(')');
if (code_ == SymbolCode::UniqueSymbol) {
out.printf("@%p", (void*)this);
}
} else {
out.printf("<Invalid Symbol code=%u>", unsigned(code_));
}
}
#endif // defined(DEBUG) || defined(JS_JITSPEW)
bool js::SymbolDescriptiveString(JSContext* cx, Symbol* sym,
MutableHandleValue result) {
// steps 2-5
JSStringBuilder sb(cx);
if (!sb.append("Symbol(")) {
return false;
}
if (JSAtom* desc = sym->description()) {
if (!sb.append(desc)) {
return false;
}
}
if (!sb.append(')')) {
return false;
}
// step 6
JSString* str = sb.finishString();
if (!str) {
return false;
}
result.setString(str);
return true;
}
JS::ubi::Node::Size JS::ubi::Concrete<JS::Symbol>::size(
mozilla::MallocSizeOf mallocSizeOf) const {
// If we start allocating symbols in the nursery, we will need to update
// this method.
MOZ_ASSERT(get().isTenured());
return js::gc::Arena::thingSize(get().asTenured().getAllocKind());
}