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"
12 #include "js/RootingAPI.h"
13 #include "js/String.h"
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
{
28 constexpr PinnedStringId() : id(JS::PropertyKey::Void()) {}
30 bool init(JSContext
* cx
, const char* string
) {
31 JSString
* str
= JS_AtomizeAndPinString(cx
, string
);
35 id
= JS::PropertyKey::fromPinnedString(str
);
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
);
46 } // namespace mozilla::dom