1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/public/nsAutoJSValHolder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef __NSAUTOJSVALHOLDER_H__ 1.11 +#define __NSAUTOJSVALHOLDER_H__ 1.12 + 1.13 +#include "nsDebug.h" 1.14 +#include "jsapi.h" 1.15 + 1.16 +/** 1.17 + * Simple class that looks and acts like a JS::Value except that it unroots 1.18 + * itself automatically if Root() is ever called. Designed to be rooted on the 1.19 + * context or runtime (but not both!). 1.20 + */ 1.21 +class nsAutoJSValHolder 1.22 +{ 1.23 +public: 1.24 + nsAutoJSValHolder() 1.25 + : mVal(JSVAL_NULL), mRt(nullptr) 1.26 + { 1.27 + // nothing to do 1.28 + } 1.29 + 1.30 + /** 1.31 + * Always release on destruction. 1.32 + */ 1.33 + virtual ~nsAutoJSValHolder() { 1.34 + Release(); 1.35 + } 1.36 + 1.37 + nsAutoJSValHolder(const nsAutoJSValHolder& aOther) 1.38 + : mVal(JSVAL_NULL), mRt(nullptr) 1.39 + { 1.40 + *this = aOther; 1.41 + } 1.42 + 1.43 + nsAutoJSValHolder& operator=(const nsAutoJSValHolder& aOther) { 1.44 + if (this != &aOther) { 1.45 + if (aOther.IsHeld()) { 1.46 + // XXX No error handling here... 1.47 + this->Hold(aOther.mRt); 1.48 + } 1.49 + else { 1.50 + this->Release(); 1.51 + } 1.52 + *this = static_cast<JS::Value>(aOther); 1.53 + } 1.54 + return *this; 1.55 + } 1.56 + 1.57 + /** 1.58 + * Hold by rooting on the context's runtime. 1.59 + */ 1.60 + bool Hold(JSContext* aCx) { 1.61 + return Hold(JS_GetRuntime(aCx)); 1.62 + } 1.63 + 1.64 + /** 1.65 + * Hold by rooting on the runtime. 1.66 + * Note that mVal may be JSVAL_NULL, which is not a problem. 1.67 + */ 1.68 + bool Hold(JSRuntime* aRt) { 1.69 + MOZ_ASSERT_IF(mRt, mRt == aRt); 1.70 + 1.71 + if (!mRt && JS::AddNamedValueRootRT(aRt, &mVal, "nsAutoJSValHolder")) { 1.72 + mRt = aRt; 1.73 + } 1.74 + 1.75 + return !!mRt; 1.76 + } 1.77 + 1.78 + /** 1.79 + * Manually release, nullifying mVal, and mRt, but returning 1.80 + * the original JS::Value. 1.81 + */ 1.82 + JS::Value Release() { 1.83 + JS::Value oldval = mVal; 1.84 + 1.85 + if (mRt) { 1.86 + JS::RemoveValueRootRT(mRt, &mVal); // infallible 1.87 + mRt = nullptr; 1.88 + } 1.89 + 1.90 + mVal = JSVAL_NULL; 1.91 + 1.92 + return oldval; 1.93 + } 1.94 + 1.95 + /** 1.96 + * Determine if Hold has been called. 1.97 + */ 1.98 + bool IsHeld() const { 1.99 + return !!mRt; 1.100 + } 1.101 + 1.102 + /** 1.103 + * Explicit JSObject* conversion. 1.104 + */ 1.105 + JSObject* ToJSObject() const { 1.106 + return mVal.isObject() 1.107 + ? &mVal.toObject() 1.108 + : nullptr; 1.109 + } 1.110 + 1.111 + /** 1.112 + * Pretend to be a JS::Value. 1.113 + */ 1.114 + operator JS::Value() const { return mVal; } 1.115 + JS::Value get() const { return mVal; } 1.116 + 1.117 + nsAutoJSValHolder &operator=(JSObject* aOther) { 1.118 + return *this = OBJECT_TO_JSVAL(aOther); 1.119 + } 1.120 + 1.121 + nsAutoJSValHolder &operator=(JS::Value aOther) { 1.122 +#ifdef DEBUG 1.123 + if (JSVAL_IS_GCTHING(aOther) && !JSVAL_IS_NULL(aOther)) { 1.124 + MOZ_ASSERT(IsHeld(), "Not rooted!"); 1.125 + } 1.126 +#endif 1.127 + mVal = aOther; 1.128 + return *this; 1.129 + } 1.130 + 1.131 +private: 1.132 + JS::Heap<JS::Value> mVal; 1.133 + JSRuntime* mRt; 1.134 +}; 1.135 + 1.136 +#endif /* __NSAUTOJSVALHOLDER_H__ */