Bug 1885489 - Part 5: Add SnapshotIterator::readInt32(). r=iain
[gecko.git] / dom / bindings / PinnedStringId.h
blob3d18c1b07cfd976b59d8251e1b4fd283042a6a84
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_BINDINGS_PINNEDSTRINGID_H_
8 #define DOM_BINDINGS_PINNEDSTRINGID_H_
10 #include "js/GCAnnotations.h"
11 #include "js/Id.h"
12 #include "js/RootingAPI.h"
13 #include "js/String.h"
14 #include "jsapi.h"
16 class JSString;
17 struct JSContext;
19 namespace mozilla::dom {
21 * Holds a jsid that is initialized to a pinned string, with automatic
22 * conversion to Handle<jsid>, as it is held live forever by pinning.
24 class PinnedStringId {
25 jsid id;
27 public:
28 constexpr PinnedStringId() : id(JS::PropertyKey::Void()) {}
30 bool init(JSContext* cx, const char* string) {
31 JSString* str = JS_AtomizeAndPinString(cx, string);
32 if (!str) {
33 return false;
35 id = JS::PropertyKey::fromPinnedString(str);
36 return true;
39 operator const jsid&() const { return id; }
41 operator JS::Handle<jsid>() const {
42 /* This is safe because we have pinned the string. */
43 return JS::Handle<jsid>::fromMarkedLocation(&id);
45 } JS_HAZ_ROOTED;
46 } // namespace mozilla::dom
48 #endif