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"
18 namespace mozilla::dom
{
20 * Holds a jsid that is initialized to a pinned string, with automatic
21 * conversion to Handle<jsid>, as it is held live forever by pinning.
23 class PinnedStringId
{
27 constexpr PinnedStringId() : id(JSID_VOID
) {}
29 bool init(JSContext
* cx
, const char* string
) {
30 JSString
* str
= JS_AtomizeAndPinString(cx
, string
);
34 id
= JS::PropertyKey::fromPinnedString(str
);
38 operator const jsid
&() const { return id
; }
40 operator JS::Handle
<jsid
>() const {
41 /* This is safe because we have pinned the string. */
42 return JS::Handle
<jsid
>::fromMarkedLocation(&id
);
45 } // namespace mozilla::dom