2008-09-08 21:48:14 +04: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-06 09:42:51 +03:00
|
|
|
* Ben Newman <b{enjamn,newman}@mozilla.com>
|
2008-09-08 21:48:14 +04: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-06 09:42:51 +03:00
|
|
|
#ifndef __NSAUTOJSVALHOLDER_H__
|
|
|
|
#define __NSAUTOJSVALHOLDER_H__
|
2008-09-08 21:48:14 +04:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
2009-08-29 03:16:19 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
|
2008-09-08 21:48:14 +04:00
|
|
|
/**
|
2008-11-06 09:42:51 +03:00
|
|
|
* Simple class that looks and acts like a jsval except that it unroots
|
2008-09-08 21:48:14 +04:00
|
|
|
* itself automatically if Root() is ever called. Designed to be rooted on the
|
2008-11-06 09:42:51 +03:00
|
|
|
* context or runtime (but not both!).
|
2008-09-08 21:48:14 +04:00
|
|
|
*/
|
2008-11-06 09:42:51 +03:00
|
|
|
class nsAutoJSValHolder
|
2008-09-08 21:48:14 +04:00
|
|
|
{
|
|
|
|
public:
|
2011-11-03 05:03:15 +04:00
|
|
|
nsAutoJSValHolder() : mVal(JSVAL_NULL), mRt(nsnull)
|
2008-11-06 09:42:51 +03:00
|
|
|
{
|
|
|
|
// nothing to do
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always release on destruction.
|
|
|
|
*/
|
2008-11-06 09:42:51 +03:00
|
|
|
virtual ~nsAutoJSValHolder() {
|
2008-09-08 21:48:14 +04:00
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2011-11-03 05:03:15 +04:00
|
|
|
nsAutoJSValHolder(const nsAutoJSValHolder& aOther) {
|
|
|
|
*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();
|
|
|
|
}
|
|
|
|
*this = static_cast<jsval>(aOther);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-09-08 21:48:14 +04:00
|
|
|
/**
|
|
|
|
* Hold by rooting on the context's runtime.
|
|
|
|
*/
|
2011-11-03 05:03:15 +04:00
|
|
|
bool Hold(JSContext* aCx) {
|
2008-09-08 21:48:14 +04:00
|
|
|
return Hold(JS_GetRuntime(aCx));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hold by rooting on the runtime.
|
2011-01-21 02:30:03 +03:00
|
|
|
* Note that mVal may be JSVAL_NULL, which is not a problem.
|
2008-09-08 21:48:14 +04:00
|
|
|
*/
|
2011-11-03 05:03:15 +04:00
|
|
|
bool Hold(JSRuntime* aRt) {
|
|
|
|
// Do we really care about different runtimes?
|
|
|
|
if (mRt && aRt != mRt) {
|
|
|
|
js_RemoveRoot(mRt, &mVal);
|
|
|
|
mRt = nsnull;
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
2011-11-03 05:03:15 +04:00
|
|
|
|
|
|
|
if (!mRt && js_AddRootRT(aRt, &mVal, "nsAutoJSValHolder")) {
|
|
|
|
mRt = aRt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!mRt;
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-21 02:30:03 +03:00
|
|
|
* Manually release, nullifying mVal, and mRt, but returning
|
2008-11-06 09:42:51 +03:00
|
|
|
* the original jsval.
|
2008-09-08 21:48:14 +04:00
|
|
|
*/
|
2008-11-06 09:42:51 +03:00
|
|
|
jsval Release() {
|
|
|
|
jsval oldval = mVal;
|
|
|
|
|
2011-11-03 05:03:15 +04:00
|
|
|
if (mRt) {
|
2011-01-21 02:30:03 +03:00
|
|
|
js_RemoveRoot(mRt, &mVal); // infallible
|
2011-11-03 05:03:15 +04:00
|
|
|
mRt = nsnull;
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
2008-11-06 09:42:51 +03:00
|
|
|
|
|
|
|
mVal = JSVAL_NULL;
|
|
|
|
|
|
|
|
return oldval;
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if Hold has been called.
|
|
|
|
*/
|
2011-11-03 05:03:15 +04:00
|
|
|
bool IsHeld() const {
|
|
|
|
return !!mRt;
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-19 04:16:15 +03:00
|
|
|
* Explicit JSObject* conversion.
|
2008-09-08 21:48:14 +04:00
|
|
|
*/
|
2008-11-19 04:16:15 +03:00
|
|
|
JSObject* ToJSObject() const {
|
2008-11-06 09:42:51 +03:00
|
|
|
return JSVAL_IS_OBJECT(mVal)
|
|
|
|
? JSVAL_TO_OBJECT(mVal)
|
2011-11-03 05:03:15 +04:00
|
|
|
: nsnull;
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
|
|
|
|
2008-12-08 03:15:49 +03:00
|
|
|
jsval* ToJSValPtr() {
|
|
|
|
return &mVal;
|
|
|
|
}
|
|
|
|
|
2008-09-08 21:48:14 +04:00
|
|
|
/**
|
2008-11-06 09:42:51 +03:00
|
|
|
* Pretend to be a jsval.
|
2008-09-08 21:48:14 +04:00
|
|
|
*/
|
2008-11-06 09:42:51 +03:00
|
|
|
operator jsval() const { return mVal; }
|
|
|
|
|
|
|
|
nsAutoJSValHolder &operator=(JSObject* aOther) {
|
|
|
|
return *this = OBJECT_TO_JSVAL(aOther);
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
|
|
|
|
2008-11-06 09:42:51 +03:00
|
|
|
nsAutoJSValHolder &operator=(jsval aOther) {
|
2008-11-05 00:49:04 +03:00
|
|
|
#ifdef DEBUG
|
2011-11-03 05:03:15 +04:00
|
|
|
if (JSVAL_IS_GCTHING(aOther) && !JSVAL_IS_NULL(aOther)) {
|
|
|
|
NS_ASSERTION(IsHeld(), "Not rooted!");
|
2008-11-05 00:49:04 +03:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-06 09:42:51 +03:00
|
|
|
mVal = aOther;
|
|
|
|
return *this;
|
2008-09-08 21:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2008-11-06 09:42:51 +03:00
|
|
|
jsval mVal;
|
2011-11-03 05:03:15 +04:00
|
|
|
JSRuntime* mRt;
|
2008-09-08 21:48:14 +04:00
|
|
|
};
|
|
|
|
|
2008-11-06 09:42:51 +03:00
|
|
|
#endif /* __NSAUTOJSVALHOLDER_H__ */
|