Debian Codegraph @

js/src/vm/JSAtomUtils-inl.h

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 #ifndef vm_JSAtomUtils_inl_h
8 #define vm_JSAtomUtils_inl_h
9
10 #include "vm/JSAtomUtils.h"
11
12 #include "mozilla/RangedPtr.h"
13
14 #include "jsnum.h"
15
16 #include "gc/MaybeRooted.h"
17 #include "vm/JSAtomState.h"
18 #include "vm/JSContext.h"
19 #include "vm/StringType.h"
20
21 namespace js {
22
23 MOZ_ALWAYS_INLINE jsid AtomToId(JSAtom* atom) {
24 static_assert(JS::PropertyKey::IntMin == 0);
25
26 uint32_t index;
27 if (atom->isIndex(&index) && index <= JS::PropertyKey::IntMax) {
28 return JS::PropertyKey::Int(int32_t(index));
29 }
30
31 return JS::PropertyKey::NonIntAtom(atom);
32 }
33
34 // Use the NameToId method instead!
35 inline jsid AtomToId(PropertyName* name) = delete;
36
37 template <AllowGC allowGC>
38 extern bool PrimitiveValueToIdSlow(
39 JSContext* cx, typename MaybeRooted<JS::Value, allowGC>::HandleType v,
40 typename MaybeRooted<jsid, allowGC>::MutableHandleType idp);
41
42 template <AllowGC allowGC>
43 inline bool PrimitiveValueToId(
44 JSContext* cx, typename MaybeRooted<Value, allowGC>::HandleType v,
45 typename MaybeRooted<jsid, allowGC>::MutableHandleType idp) {
46 // Non-primitive values should call ToPropertyKey.
47 MOZ_ASSERT(v.isPrimitive());
48
49 if (v.isString()) {
50 JSAtom* atom = AtomizeString(cx, v.toString());
51 if (!atom) {
52 if constexpr (!allowGC) {
53 cx->recoverFromOutOfMemory();
54 }
55 return false;
56 }
57 idp.set(AtomToId(atom));
58 return true;
59 }
60
61 if (v.isInt32()) {
62 if (PropertyKey::fitsInInt(v.toInt32())) {
63 idp.set(PropertyKey::Int(v.toInt32()));
64 return true;
65 }
66 } else if (v.isSymbol()) {
67 idp.set(PropertyKey::Symbol(v.toSymbol()));
68 return true;
69 }
70
71 return PrimitiveValueToIdSlow<allowGC>(cx, v, idp);
72 }
73
74 bool IndexToIdSlow(JSContext* cx, uint32_t index, MutableHandleId idp);
75
76 inline bool IndexToId(JSContext* cx, uint32_t index, MutableHandleId idp) {
77 if (index <= PropertyKey::IntMax) {
78 idp.set(PropertyKey::Int(index));
79 return true;
80 }
81
82 return IndexToIdSlow(cx, index, idp);
83 }
84
85 static MOZ_ALWAYS_INLINE JSLinearString* IdToString(
86 JSContext* cx, jsid id, gc::Heap heap = gc::Heap::Default) {
87 if (id.isString()) {
88 return id.toAtom();
89 }
90
91 if (MOZ_LIKELY(id.isInt())) {
92 return Int32ToStringWithHeap<CanGC>(cx, id.toInt(), heap);
93 }
94
95 RootedValue idv(cx, IdToValue(id));
96 JSString* str = ToStringSlow<CanGC>(cx, idv);
97 if (!str) {
98 return nullptr;
99 }
100
101 return str->ensureLinear(cx);
102 }
103
104 inline Handle<PropertyName*> TypeName(JSType type, const JSAtomState& names) {
105 MOZ_ASSERT(type < JSTYPE_LIMIT);
106 static_assert(offsetof(JSAtomState, undefined) +
107 JSTYPE_LIMIT * sizeof(ImmutableTenuredPtr<PropertyName*>) <=
108 sizeof(JSAtomState));
109 static_assert(JSTYPE_UNDEFINED == 0);
110 return (&names.undefined)[type];
111 }
112
113 inline Handle<PropertyName*> ClassName(JSProtoKey key, JSAtomState& atomState) {
114 MOZ_ASSERT(key < JSProto_LIMIT);
115 static_assert(offsetof(JSAtomState, Null) +
116 JSProto_LIMIT *
117 sizeof(ImmutableTenuredPtr<PropertyName*>) <=
118 sizeof(JSAtomState));
119 static_assert(JSProto_Null == 0);
120 return (&atomState.Null)[key];
121 }
122
123 } // namespace js
124
125 #endif /* vm_JSAtomUtils_inl_h */