Bug 1664775 [wpt PR 25515] - Fieldset NG: Fix hit-testing for the rendered legend...
[gecko.git] / dom / bindings / PinnedStringId.h
blob43feeb60be573637f0db2b807365286388ab262d
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 "jsapi.h"
15 class JSString;
16 struct JSContext;
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 {
24 jsid id;
26 public:
27 constexpr PinnedStringId() : id(JSID_VOID) {}
29 bool init(JSContext* cx, const char* string) {
30 JSString* str = JS_AtomizeAndPinString(cx, string);
31 if (!str) {
32 return false;
34 id = JS::PropertyKey::fromPinnedString(str);
35 return true;
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);
44 } JS_HAZ_ROOTED;
45 } // namespace mozilla::dom
47 #endif