tor-browser

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

SourceLocation.cpp (2028B)


      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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/SourceLocation.h"
      8 
      9 #include "jsapi.h"
     10 #include "mozilla/ThreadLocal.h"
     11 #include "nsContentUtils.h"
     12 
     13 namespace mozilla {
     14 
     15 SourceLocation::SourceLocation() = default;
     16 SourceLocation::~SourceLocation() = default;
     17 
     18 SourceLocation::SourceLocation(nsCString&& aResource, uint32_t aLine,
     19                               uint32_t aCol)
     20    : mResource(std::move(aResource)), mLine(aLine), mColumn(aCol) {}
     21 
     22 SourceLocation::SourceLocation(nsCOMPtr<nsIURI>&& aResource, uint32_t aLine,
     23                               uint32_t aCol)
     24    : mResource(std::move(aResource)), mLine(aLine), mColumn(aCol) {}
     25 
     26 static MOZ_THREAD_LOCAL(const JSCallingLocation*) tlsFallback;
     27 const JSCallingLocation* JSCallingLocation::GetFallback() {
     28  if (!tlsFallback.initialized()) {
     29    return nullptr;
     30  }
     31  return tlsFallback.get();
     32 }
     33 
     34 void JSCallingLocation::SetFallback(const JSCallingLocation* aFallback) {
     35  if (!tlsFallback.init()) {
     36    return;
     37  }
     38  tlsFallback.set(aFallback);
     39 }
     40 
     41 JSCallingLocation JSCallingLocation::Get() {
     42  return Get(nsContentUtils::GetCurrentJSContext());
     43 }
     44 
     45 JSCallingLocation JSCallingLocation::Get(JSContext* aCx) {
     46  JSCallingLocation result;
     47  if (const JSCallingLocation* loc = GetFallback()) {
     48    result = *loc;
     49  }
     50  if (!aCx) {
     51    return result;
     52  }
     53  JS::AutoFilename filename;
     54  uint32_t line;
     55  JS::ColumnNumberOneOrigin column;
     56  if (!JS::DescribeScriptedCaller(&filename, aCx, &line, &column)) {
     57    return result;
     58  }
     59  nsCString file;
     60  if (!file.Assign(filename.get(), fallible)) {
     61    return result;
     62  }
     63  result.mResource = AsVariant(std::move(file));
     64  result.mLine = line;
     65  result.mColumn = column.oneOriginValue();
     66  return result;
     67 }
     68 
     69 }  // namespace mozilla