SourceLocation.h (1786B)
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 #ifndef mozilla_SourceLocation_h 8 #define mozilla_SourceLocation_h 9 10 #include "mozilla/Variant.h" 11 #include "nsCOMPtr.h" 12 #include "nsString.h" 13 14 struct JSContext; 15 class nsIURI; 16 17 namespace mozilla { 18 19 struct SourceLocation { 20 mozilla::Variant<nsCString, nsCOMPtr<nsIURI>> mResource{VoidCString()}; 21 uint32_t mLine = 0; 22 uint32_t mColumn = 1; 23 24 SourceLocation(); 25 explicit SourceLocation(nsCString&&, uint32_t aLine = 0, uint32_t aCol = 1); 26 explicit SourceLocation(nsCOMPtr<nsIURI>&&, uint32_t aLine = 0, 27 uint32_t aCol = 1); 28 29 ~SourceLocation(); 30 31 bool IsEmpty() const { 32 return mResource.is<nsCString>() ? mResource.as<nsCString>().IsEmpty() 33 : !mResource.as<nsCOMPtr<nsIURI>>(); 34 } 35 explicit operator bool() const { return !IsEmpty(); } 36 }; 37 38 struct JSCallingLocation : SourceLocation { 39 const nsCString& FileName() const { return mResource.as<nsCString>(); } 40 41 static JSCallingLocation Get(); 42 static JSCallingLocation Get(JSContext*); 43 44 class MOZ_STACK_CLASS AutoFallback { 45 public: 46 explicit AutoFallback(const JSCallingLocation* aFallback) 47 : mOldFallback(GetFallback()) { 48 SetFallback(aFallback); 49 } 50 ~AutoFallback() { SetFallback(mOldFallback); } 51 52 private: 53 const JSCallingLocation* mOldFallback; 54 }; 55 56 private: 57 static const JSCallingLocation* GetFallback(); 58 static void SetFallback(const JSCallingLocation*); 59 }; 60 61 } // namespace mozilla 62 63 #endif