2014-04-03 04:58:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-09-08 10:48:14 -07:00
|
|
|
|
2008-11-05 22:42:51 -08:00
|
|
|
#ifndef __NSAUTOJSVALHOLDER_H__
|
|
|
|
#define __NSAUTOJSVALHOLDER_H__
|
2008-09-08 10:48:14 -07:00
|
|
|
|
2009-08-28 16:16:19 -07:00
|
|
|
#include "nsDebug.h"
|
2013-08-19 23:45:26 -07:00
|
|
|
#include "jsapi.h"
|
2009-08-28 16:16:19 -07:00
|
|
|
|
2008-09-08 10:48:14 -07:00
|
|
|
/**
|
2013-08-19 23:45:26 -07:00
|
|
|
* Simple class that looks and acts like a JS::Value except that it unroots
|
2008-09-08 10:48:14 -07:00
|
|
|
* itself automatically if Root() is ever called. Designed to be rooted on the
|
2008-11-05 22:42:51 -08:00
|
|
|
* context or runtime (but not both!).
|
2008-09-08 10:48:14 -07:00
|
|
|
*/
|
2008-11-05 22:42:51 -08:00
|
|
|
class nsAutoJSValHolder
|
2008-09-08 10:48:14 -07:00
|
|
|
{
|
|
|
|
public:
|
2012-12-10 16:12:49 -08:00
|
|
|
nsAutoJSValHolder()
|
|
|
|
: mVal(JSVAL_NULL), mRt(nullptr)
|
2008-11-05 22:42:51 -08:00
|
|
|
{
|
|
|
|
// nothing to do
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always release on destruction.
|
|
|
|
*/
|
2008-11-05 22:42:51 -08:00
|
|
|
virtual ~nsAutoJSValHolder() {
|
2008-09-08 10:48:14 -07:00
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2012-12-10 16:12:49 -08:00
|
|
|
nsAutoJSValHolder(const nsAutoJSValHolder& aOther)
|
|
|
|
: mVal(JSVAL_NULL), mRt(nullptr)
|
|
|
|
{
|
2011-11-03 08:57:30 -07:00
|
|
|
*this = aOther;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoJSValHolder& operator=(const nsAutoJSValHolder& aOther) {
|
|
|
|
if (this != &aOther) {
|
|
|
|
if (aOther.IsHeld()) {
|
|
|
|
// XXX No error handling here...
|
|
|
|
this->Hold(aOther.mRt);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->Release();
|
|
|
|
}
|
2013-08-19 23:45:26 -07:00
|
|
|
*this = static_cast<JS::Value>(aOther);
|
2011-11-03 08:57:30 -07:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-09-08 10:48:14 -07:00
|
|
|
/**
|
|
|
|
* Hold by rooting on the context's runtime.
|
|
|
|
*/
|
2011-11-03 08:57:30 -07:00
|
|
|
bool Hold(JSContext* aCx) {
|
2008-09-08 10:48:14 -07:00
|
|
|
return Hold(JS_GetRuntime(aCx));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hold by rooting on the runtime.
|
2011-01-20 15:30:03 -08:00
|
|
|
* Note that mVal may be JSVAL_NULL, which is not a problem.
|
2008-09-08 10:48:14 -07:00
|
|
|
*/
|
2011-11-03 08:57:30 -07:00
|
|
|
bool Hold(JSRuntime* aRt) {
|
2014-04-16 01:47:57 -07:00
|
|
|
MOZ_ASSERT_IF(mRt, mRt == aRt);
|
2011-11-03 08:57:30 -07:00
|
|
|
|
2014-04-16 01:47:57 -07:00
|
|
|
if (!mRt && JS::AddNamedValueRootRT(aRt, &mVal, "nsAutoJSValHolder")) {
|
2011-11-03 08:57:30 -07:00
|
|
|
mRt = aRt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!mRt;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-20 15:30:03 -08:00
|
|
|
* Manually release, nullifying mVal, and mRt, but returning
|
2013-08-19 23:45:26 -07:00
|
|
|
* the original JS::Value.
|
2008-09-08 10:48:14 -07:00
|
|
|
*/
|
2013-08-19 23:45:26 -07:00
|
|
|
JS::Value Release() {
|
|
|
|
JS::Value oldval = mVal;
|
2008-11-05 22:42:51 -08:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
if (mRt) {
|
2014-04-16 01:47:57 -07:00
|
|
|
JS::RemoveValueRootRT(mRt, &mVal); // infallible
|
2012-07-30 07:20:58 -07:00
|
|
|
mRt = nullptr;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
2008-11-05 22:42:51 -08:00
|
|
|
|
|
|
|
mVal = JSVAL_NULL;
|
|
|
|
|
|
|
|
return oldval;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if Hold has been called.
|
|
|
|
*/
|
2011-11-03 08:57:30 -07:00
|
|
|
bool IsHeld() const {
|
|
|
|
return !!mRt;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-18 17:16:15 -08:00
|
|
|
* Explicit JSObject* conversion.
|
2008-09-08 10:48:14 -07:00
|
|
|
*/
|
2008-11-18 17:16:15 -08:00
|
|
|
JSObject* ToJSObject() const {
|
2012-05-11 08:46:26 -07:00
|
|
|
return mVal.isObject()
|
|
|
|
? &mVal.toObject()
|
2012-07-30 07:20:58 -07:00
|
|
|
: nullptr;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-19 23:45:26 -07:00
|
|
|
* Pretend to be a JS::Value.
|
2008-09-08 10:48:14 -07:00
|
|
|
*/
|
2013-08-19 23:45:26 -07:00
|
|
|
operator JS::Value() const { return mVal; }
|
2014-04-16 01:47:57 -07:00
|
|
|
JS::Value get() const { return mVal; }
|
2008-11-05 22:42:51 -08:00
|
|
|
|
|
|
|
nsAutoJSValHolder &operator=(JSObject* aOther) {
|
|
|
|
return *this = OBJECT_TO_JSVAL(aOther);
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
2013-08-19 23:45:26 -07:00
|
|
|
nsAutoJSValHolder &operator=(JS::Value aOther) {
|
2008-11-04 13:49:04 -08:00
|
|
|
#ifdef DEBUG
|
2014-04-28 16:01:30 -07:00
|
|
|
if (aOther.isGCThing() && !aOther.isNull()) {
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(IsHeld(), "Not rooted!");
|
2008-11-04 13:49:04 -08:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-05 22:42:51 -08:00
|
|
|
mVal = aOther;
|
|
|
|
return *this;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-04-16 01:47:57 -07:00
|
|
|
JS::Heap<JS::Value> mVal;
|
2011-11-03 08:57:30 -07:00
|
|
|
JSRuntime* mRt;
|
2008-09-08 10:48:14 -07:00
|
|
|
};
|
|
|
|
|
2008-11-05 22:42:51 -08:00
|
|
|
#endif /* __NSAUTOJSVALHOLDER_H__ */
|