2008-09-08 10:48:14 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is worker threads.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Ben Turner <bent.mozilla@gmail.com> (Original Author)
|
2008-11-05 22:42:51 -08:00
|
|
|
* Ben Newman <b{enjamn,newman}@mozilla.com>
|
2008-09-08 10:48:14 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2008-11-05 22:42:51 -08:00
|
|
|
#ifndef __NSAUTOJSVALHOLDER_H__
|
|
|
|
#define __NSAUTOJSVALHOLDER_H__
|
2008-09-08 10:48:14 -07:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
2009-08-28 16:16:19 -07:00
|
|
|
#include "nsDebug.h"
|
|
|
|
|
2008-09-08 10:48:14 -07:00
|
|
|
/**
|
2008-11-05 22:42:51 -08:00
|
|
|
* Simple class that looks and acts like a jsval 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:
|
|
|
|
|
2008-11-05 22:42:51 -08:00
|
|
|
nsAutoJSValHolder()
|
|
|
|
: mRt(NULL)
|
|
|
|
, mVal(JSVAL_NULL)
|
|
|
|
, mHeld(JS_FALSE)
|
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hold by rooting on the context's runtime.
|
|
|
|
*/
|
|
|
|
JSBool Hold(JSContext* aCx) {
|
|
|
|
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
|
|
|
*/
|
|
|
|
JSBool Hold(JSRuntime* aRt) {
|
|
|
|
if (!mHeld) {
|
2011-01-20 15:30:03 -08:00
|
|
|
if (js_AddRootRT(aRt, &mVal, "nsAutoJSValHolder")) {
|
2008-09-08 10:48:14 -07:00
|
|
|
mRt = aRt;
|
2008-11-05 22:42:51 -08:00
|
|
|
mHeld = JS_TRUE;
|
|
|
|
} else {
|
|
|
|
Release(); // out of memory
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mHeld;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-20 15:30:03 -08:00
|
|
|
* Manually release, nullifying mVal, and mRt, but returning
|
2008-11-05 22:42:51 -08:00
|
|
|
* the original jsval.
|
2008-09-08 10:48:14 -07:00
|
|
|
*/
|
2008-11-05 22:42:51 -08:00
|
|
|
jsval Release() {
|
2008-09-08 10:48:14 -07:00
|
|
|
NS_ASSERTION(!mHeld || mRt, "Bad!");
|
2008-11-05 22:42:51 -08:00
|
|
|
|
|
|
|
jsval oldval = mVal;
|
|
|
|
|
2008-09-08 10:48:14 -07:00
|
|
|
if (mHeld) {
|
2011-01-20 15:30:03 -08:00
|
|
|
js_RemoveRoot(mRt, &mVal); // infallible
|
2008-11-05 22:42:51 -08:00
|
|
|
mHeld = JS_FALSE;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
2008-11-05 22:42:51 -08:00
|
|
|
|
|
|
|
mVal = JSVAL_NULL;
|
|
|
|
mRt = NULL;
|
|
|
|
|
|
|
|
return oldval;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if Hold has been called.
|
|
|
|
*/
|
|
|
|
JSBool IsHeld() {
|
|
|
|
return mHeld;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 {
|
2008-11-05 22:42:51 -08:00
|
|
|
return JSVAL_IS_OBJECT(mVal)
|
|
|
|
? JSVAL_TO_OBJECT(mVal)
|
2008-11-18 17:16:15 -08:00
|
|
|
: NULL;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
2008-12-07 16:15:49 -08:00
|
|
|
jsval* ToJSValPtr() {
|
|
|
|
return &mVal;
|
|
|
|
}
|
|
|
|
|
2008-09-08 10:48:14 -07:00
|
|
|
/**
|
2008-11-05 22:42:51 -08:00
|
|
|
* Pretend to be a jsval.
|
2008-09-08 10:48:14 -07:00
|
|
|
*/
|
2008-11-05 22:42:51 -08:00
|
|
|
operator jsval() const { return mVal; }
|
|
|
|
|
|
|
|
nsAutoJSValHolder &operator=(JSObject* aOther) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (aOther) {
|
|
|
|
NS_ASSERTION(mHeld, "Not rooted!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return *this = OBJECT_TO_JSVAL(aOther);
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
2008-11-05 22:42:51 -08:00
|
|
|
nsAutoJSValHolder &operator=(jsval aOther) {
|
2008-11-04 13:49:04 -08:00
|
|
|
#ifdef DEBUG
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSVAL_IS_OBJECT(aOther) && JSVAL_TO_OBJECT(aOther)) {
|
2008-11-04 13:49:04 -08:00
|
|
|
NS_ASSERTION(mHeld, "Not rooted!");
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-05 22:42:51 -08:00
|
|
|
mVal = aOther;
|
|
|
|
return *this;
|
2008-09-08 10:48:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSRuntime* mRt;
|
2008-11-05 22:42:51 -08:00
|
|
|
jsval mVal;
|
2008-09-08 10:48:14 -07:00
|
|
|
JSBool mHeld;
|
|
|
|
};
|
|
|
|
|
2008-11-05 22:42:51 -08:00
|
|
|
#endif /* __NSAUTOJSVALHOLDER_H__ */
|