tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PinnedStringId.h (1313B)


      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/. */
      6 
      7 #ifndef DOM_BINDINGS_PINNEDSTRINGID_H_
      8 #define DOM_BINDINGS_PINNEDSTRINGID_H_
      9 
     10 #include "js/GCAnnotations.h"
     11 #include "js/Id.h"
     12 #include "js/RootingAPI.h"
     13 #include "js/String.h"
     14 #include "jsapi.h"
     15 
     16 class JSString;
     17 struct JSContext;
     18 
     19 namespace mozilla::dom {
     20 /*
     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.
     23 */
     24 class PinnedStringId {
     25  jsid id;
     26 
     27 public:
     28  constexpr PinnedStringId() : id(JS::PropertyKey::Void()) {}
     29 
     30  bool init(JSContext* cx, const char* string) {
     31    JSString* str = JS_AtomizeAndPinString(cx, string);
     32    if (!str) {
     33      return false;
     34    }
     35    id = JS::PropertyKey::fromPinnedString(str);
     36    return true;
     37  }
     38 
     39  operator const jsid&() const { return id; }
     40 
     41  operator JS::Handle<jsid>() const {
     42    /* This is safe because we have pinned the string. */
     43    return JS::Handle<jsid>::fromMarkedLocation(&id);
     44  }
     45 } JS_HAZ_ROOTED;
     46 }  // namespace mozilla::dom
     47 
     48 #endif