2001-10-10 03:42:22 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-10-10 03:42:22 +04:00
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* 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.
|
2001-10-10 03:42:22 +04:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-10-10 03:42:22 +04:00
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* John Bandhauer <jband@netscape.com> (original author)
|
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of 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 ***** */
|
2001-10-10 03:42:22 +04:00
|
|
|
|
|
|
|
/* nsIVariant implementation for xpconnect. */
|
|
|
|
|
|
|
|
#include "xpcprivate.h"
|
2010-06-24 02:09:00 +04:00
|
|
|
#include "XPCWrapper.h"
|
2001-10-10 03:42:22 +04:00
|
|
|
|
2007-02-18 23:05:32 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(XPCVariant)
|
|
|
|
|
2010-06-22 20:59:57 +04:00
|
|
|
NS_IMPL_CLASSINFO(XPCVariant, NULL, 0, XPCVARIANT_CID)
|
2007-04-25 20:35:27 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(XPCVariant)
|
2007-02-18 23:05:32 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(XPCVariant)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIVariant)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_IMPL_QUERY_CLASSINFO(XPCVariant)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER2(XPCVariant, XPCVariant, nsIVariant)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(XPCVariant)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(XPCVariant)
|
2001-10-10 03:42:22 +04:00
|
|
|
|
2009-08-05 02:04:53 +04:00
|
|
|
XPCVariant::XPCVariant(XPCCallContext& ccx, jsval aJSVal)
|
2007-06-02 19:11:52 +04:00
|
|
|
: mJSVal(aJSVal)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
nsVariant::Initialize(&mData);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JSVAL_IS_PRIMITIVE(mJSVal)) {
|
2011-10-04 18:06:54 +04:00
|
|
|
JSObject *obj = JS_ObjectToInnerObject(ccx, JSVAL_TO_OBJECT(mJSVal));
|
2011-03-09 02:04:59 +03:00
|
|
|
|
|
|
|
mJSVal = OBJECT_TO_JSVAL(obj);
|
|
|
|
|
2009-08-05 02:04:53 +04:00
|
|
|
// If the incoming object is an XPCWrappedNative, then it could be a
|
|
|
|
// double-wrapped object, and we should return the double-wrapped
|
|
|
|
// object back out to script.
|
|
|
|
|
|
|
|
JSObject* proto;
|
|
|
|
XPCWrappedNative* wn =
|
|
|
|
XPCWrappedNative::GetWrappedNativeOfJSObject(ccx,
|
|
|
|
JSVAL_TO_OBJECT(mJSVal),
|
|
|
|
nsnull,
|
|
|
|
&proto);
|
|
|
|
mReturnRawObject = !wn && !proto;
|
2011-10-14 21:52:48 +04:00
|
|
|
} else
|
2011-11-26 14:05:59 +04:00
|
|
|
mReturnRawObject = false;
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
|
|
|
|
2007-06-02 19:11:52 +04:00
|
|
|
XPCTraceableVariant::~XPCTraceableVariant()
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
2011-02-16 23:47:08 +03:00
|
|
|
jsval val = GetJSValPreserveColor();
|
2007-06-02 19:11:52 +04:00
|
|
|
|
2011-02-16 23:47:08 +03:00
|
|
|
NS_ASSERTION(JSVAL_IS_GCTHING(val), "Must be traceable or unlinked");
|
|
|
|
|
|
|
|
// If val is JSVAL_STRING, we don't need to clean anything up; simply
|
|
|
|
// removing the string from the root set is good.
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JSVAL_IS_STRING(val))
|
2006-09-24 20:44:51 +04:00
|
|
|
nsVariant::Cleanup(&mData);
|
2007-06-02 19:11:52 +04:00
|
|
|
|
2011-02-16 23:47:08 +03:00
|
|
|
if (!JSVAL_IS_NULL(val))
|
2010-11-12 01:52:30 +03:00
|
|
|
RemoveFromRootSet(nsXPConnect::GetRuntimeInstance()->GetMapLock());
|
2007-06-02 19:11:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void XPCTraceableVariant::TraceJS(JSTracer* trc)
|
|
|
|
{
|
2011-02-16 23:47:08 +03:00
|
|
|
jsval val = GetJSValPreserveColor();
|
|
|
|
|
|
|
|
NS_ASSERTION(JSVAL_IS_TRACEABLE(val), "Must be traceable");
|
2007-06-02 19:11:52 +04:00
|
|
|
JS_SET_TRACING_DETAILS(trc, PrintTraceName, this, 0);
|
2011-02-16 23:47:08 +03:00
|
|
|
JS_CallTracer(trc, JSVAL_TO_TRACEABLE(val), JSVAL_TRACE_KIND(val));
|
2007-06-02 19:11:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
XPCTraceableVariant::PrintTraceName(JSTracer* trc, char *buf, size_t bufsize)
|
|
|
|
{
|
|
|
|
JS_snprintf(buf, bufsize, "XPCVariant[0x%p].mJSVal", trc->debugPrintArg);
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
2007-06-02 19:11:52 +04:00
|
|
|
#endif
|
2001-10-10 03:42:22 +04:00
|
|
|
|
2007-02-18 23:05:32 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(XPCVariant)
|
2011-02-16 23:47:08 +03:00
|
|
|
jsval val = tmp->GetJSValPreserveColor();
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_OBJECT(val))
|
2007-02-18 23:05:32 +03:00
|
|
|
cb.NoteScriptChild(nsIProgrammingLanguage::JAVASCRIPT,
|
2011-02-16 23:47:08 +03:00
|
|
|
JSVAL_TO_OBJECT(val));
|
2007-02-18 23:05:32 +03:00
|
|
|
|
|
|
|
nsVariant::Traverse(tmp->mData, cb);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2007-11-02 01:51:57 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(XPCVariant)
|
2011-02-16 23:47:08 +03:00
|
|
|
jsval val = tmp->GetJSValPreserveColor();
|
|
|
|
|
|
|
|
// We're sharing val's buffer, clear the pointer to it so Cleanup() won't
|
|
|
|
// try to delete it
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_STRING(val))
|
2008-01-13 21:21:08 +03:00
|
|
|
tmp->mData.u.wstr.mWStringValue = nsnull;
|
|
|
|
nsVariant::Cleanup(&tmp->mData);
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_TRACEABLE(val)) {
|
2007-11-02 01:51:57 +03:00
|
|
|
XPCTraceableVariant *v = static_cast<XPCTraceableVariant*>(tmp);
|
2010-11-12 01:52:30 +03:00
|
|
|
v->RemoveFromRootSet(nsXPConnect::GetRuntimeInstance()->GetMapLock());
|
2007-11-02 01:51:57 +03:00
|
|
|
}
|
|
|
|
tmp->mJSVal = JSVAL_NULL;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2007-02-18 23:05:32 +03:00
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
// static
|
2001-10-10 03:42:22 +04:00
|
|
|
XPCVariant* XPCVariant::newVariant(XPCCallContext& ccx, jsval aJSVal)
|
|
|
|
{
|
2007-06-02 19:11:52 +04:00
|
|
|
XPCVariant* variant;
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JSVAL_IS_TRACEABLE(aJSVal))
|
2009-08-05 02:04:53 +04:00
|
|
|
variant = new XPCVariant(ccx, aJSVal);
|
2007-06-02 19:11:52 +04:00
|
|
|
else
|
2009-08-05 02:04:53 +04:00
|
|
|
variant = new XPCTraceableVariant(ccx, aJSVal);
|
2007-06-02 19:11:52 +04:00
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!variant)
|
2001-10-10 03:42:22 +04:00
|
|
|
return nsnull;
|
|
|
|
NS_ADDREF(variant);
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!variant->InitializeData(ccx))
|
2001-10-10 03:42:22 +04:00
|
|
|
NS_RELEASE(variant); // Also sets variant to nsnull.
|
|
|
|
|
|
|
|
return variant;
|
|
|
|
}
|
|
|
|
|
2002-02-05 09:45:08 +03:00
|
|
|
// Helper class to give us a namespace for the table based code below.
|
|
|
|
class XPCArrayHomogenizer
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
tNull = 0 , // null value
|
|
|
|
tInt , // Integer
|
|
|
|
tDbl , // Double
|
|
|
|
tBool , // Boolean
|
|
|
|
tStr , // String
|
|
|
|
tID , // ID
|
|
|
|
tArr , // Array
|
|
|
|
tISup , // nsISupports (really just a plain JSObject)
|
|
|
|
tUnk , // Unknown. Used only for initial state.
|
|
|
|
|
|
|
|
tTypeCount , // Just a count for table dimensioning.
|
|
|
|
|
|
|
|
tVar , // nsVariant - last ditch if no other common type found.
|
2011-10-14 21:52:47 +04:00
|
|
|
tErr // No valid state or type has this value.
|
2002-02-05 09:45:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Table has tUnk as a state (column) but not as a type (row).
|
2009-05-16 03:14:17 +04:00
|
|
|
static const Type StateTable[tTypeCount][tTypeCount-1];
|
2002-02-05 09:45:08 +03:00
|
|
|
|
|
|
|
public:
|
2011-10-14 21:52:47 +04:00
|
|
|
static JSBool GetTypeForArray(XPCCallContext& ccx, JSObject* array,
|
|
|
|
jsuint length,
|
2002-02-05 09:45:08 +03:00
|
|
|
nsXPTType* resultType, nsID* resultID);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
// Current state is the column down the side.
|
|
|
|
// Current type is the row along the top.
|
2002-02-05 09:45:08 +03:00
|
|
|
// New state is in the box at the intersection.
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
const XPCArrayHomogenizer::Type
|
2002-02-05 09:45:08 +03:00
|
|
|
XPCArrayHomogenizer::StateTable[tTypeCount][tTypeCount-1] = {
|
|
|
|
/* tNull,tInt ,tDbl ,tBool,tStr ,tID ,tArr ,tISup */
|
|
|
|
/* tNull */{tNull,tVar ,tVar ,tVar ,tStr ,tID ,tVar ,tISup },
|
|
|
|
/* tInt */{tVar ,tInt ,tDbl ,tVar ,tVar ,tVar ,tVar ,tVar },
|
|
|
|
/* tDbl */{tVar ,tDbl ,tDbl ,tVar ,tVar ,tVar ,tVar ,tVar },
|
|
|
|
/* tBool */{tVar ,tVar ,tVar ,tBool,tVar ,tVar ,tVar ,tVar },
|
|
|
|
/* tStr */{tStr ,tVar ,tVar ,tVar ,tStr ,tVar ,tVar ,tVar },
|
|
|
|
/* tID */{tID ,tVar ,tVar ,tVar ,tVar ,tID ,tVar ,tVar },
|
|
|
|
/* tArr */{tErr ,tErr ,tErr ,tErr ,tErr ,tErr ,tErr ,tErr },
|
|
|
|
/* tISup */{tISup,tVar ,tVar ,tVar ,tVar ,tVar ,tVar ,tISup },
|
|
|
|
/* tUnk */{tNull,tInt ,tDbl ,tBool,tStr ,tID ,tVar ,tISup }};
|
|
|
|
|
|
|
|
// static
|
|
|
|
JSBool
|
|
|
|
XPCArrayHomogenizer::GetTypeForArray(XPCCallContext& ccx, JSObject* array,
|
2011-10-14 21:52:47 +04:00
|
|
|
jsuint length,
|
|
|
|
nsXPTType* resultType, nsID* resultID)
|
2002-02-05 09:45:08 +03:00
|
|
|
{
|
|
|
|
Type state = tUnk;
|
|
|
|
Type type;
|
2011-10-14 21:52:47 +04:00
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
2002-02-05 09:45:08 +03:00
|
|
|
jsval val;
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JS_GetElement(ccx, array, i, &val))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_INT(val))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tInt;
|
2011-10-14 21:52:48 +04:00
|
|
|
else if (JSVAL_IS_DOUBLE(val))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tDbl;
|
2011-10-14 21:52:48 +04:00
|
|
|
else if (JSVAL_IS_BOOLEAN(val))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tBool;
|
2011-10-14 21:52:48 +04:00
|
|
|
else if (JSVAL_IS_VOID(val)) {
|
2002-02-05 09:45:08 +03:00
|
|
|
state = tVar;
|
|
|
|
break;
|
2011-10-14 21:52:48 +04:00
|
|
|
} else if (JSVAL_IS_NULL(val))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tNull;
|
2011-10-14 21:52:48 +04:00
|
|
|
else if (JSVAL_IS_STRING(val))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tStr;
|
2011-10-14 21:52:48 +04:00
|
|
|
else {
|
2002-02-05 09:45:08 +03:00
|
|
|
NS_ASSERTION(JSVAL_IS_OBJECT(val), "invalid type of jsval!");
|
|
|
|
JSObject* jsobj = JSVAL_TO_OBJECT(val);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JS_IsArrayObject(ccx, jsobj))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tArr;
|
2011-10-14 21:52:48 +04:00
|
|
|
else if (xpc_JSObjectIsID(ccx, jsobj))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tID;
|
|
|
|
else
|
|
|
|
type = tISup;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(state != tErr, "bad state table!");
|
|
|
|
NS_ASSERTION(type != tErr, "bad type!");
|
|
|
|
NS_ASSERTION(type != tVar, "bad type!");
|
|
|
|
NS_ASSERTION(type != tUnk, "bad type!");
|
|
|
|
|
|
|
|
state = StateTable[state][type];
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2002-02-05 09:45:08 +03:00
|
|
|
NS_ASSERTION(state != tErr, "bad state table!");
|
|
|
|
NS_ASSERTION(state != tUnk, "bad state table!");
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (state == tVar)
|
2002-02-05 09:45:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
switch (state) {
|
2011-10-14 21:52:47 +04:00
|
|
|
case tInt :
|
2002-02-05 09:45:08 +03:00
|
|
|
*resultType = nsXPTType((uint8)TD_INT32);
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tDbl :
|
2002-02-05 09:45:08 +03:00
|
|
|
*resultType = nsXPTType((uint8)TD_DOUBLE);
|
|
|
|
break;
|
|
|
|
case tBool:
|
|
|
|
*resultType = nsXPTType((uint8)TD_BOOL);
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tStr :
|
2002-02-05 09:45:08 +03:00
|
|
|
*resultType = nsXPTType((uint8)(TD_PWSTRING | XPT_TDP_POINTER));
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tID :
|
2002-02-05 09:45:08 +03:00
|
|
|
*resultType = nsXPTType((uint8)(TD_PNSIID | XPT_TDP_POINTER));
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tISup:
|
2002-02-05 09:45:08 +03:00
|
|
|
*resultType = nsXPTType((uint8)(TD_INTERFACE_IS_TYPE | XPT_TDP_POINTER));
|
|
|
|
*resultID = NS_GET_IID(nsISupports);
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tNull:
|
2002-02-05 09:45:08 +03:00
|
|
|
// FALL THROUGH
|
|
|
|
case tVar :
|
|
|
|
*resultType = nsXPTType((uint8)(TD_INTERFACE_IS_TYPE | XPT_TDP_POINTER));
|
|
|
|
*resultID = NS_GET_IID(nsIVariant);
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tArr :
|
2002-02-05 09:45:08 +03:00
|
|
|
// FALL THROUGH
|
2011-10-14 21:52:47 +04:00
|
|
|
case tUnk :
|
2002-02-05 09:45:08 +03:00
|
|
|
// FALL THROUGH
|
2011-10-14 21:52:47 +04:00
|
|
|
case tErr :
|
2002-02-05 09:45:08 +03:00
|
|
|
// FALL THROUGH
|
|
|
|
default:
|
|
|
|
NS_ERROR("bad state");
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2002-02-05 09:45:08 +03:00
|
|
|
}
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2002-02-05 09:45:08 +03:00
|
|
|
}
|
|
|
|
|
2001-10-10 03:42:22 +04:00
|
|
|
JSBool XPCVariant::InitializeData(XPCCallContext& ccx)
|
|
|
|
{
|
2011-11-26 14:05:59 +04:00
|
|
|
JS_CHECK_RECURSION(ccx.GetJSContext(), return false);
|
2010-08-24 17:50:15 +04:00
|
|
|
|
2011-02-16 23:47:08 +03:00
|
|
|
jsval val = GetJSVal();
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_INT(val))
|
2011-02-16 23:47:08 +03:00
|
|
|
return NS_SUCCEEDED(nsVariant::SetFromInt32(&mData, JSVAL_TO_INT(val)));
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_DOUBLE(val))
|
2011-10-14 21:52:47 +04:00
|
|
|
return NS_SUCCEEDED(nsVariant::SetFromDouble(&mData,
|
2011-02-16 23:47:08 +03:00
|
|
|
JSVAL_TO_DOUBLE(val)));
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_BOOLEAN(val))
|
2011-10-14 21:52:47 +04:00
|
|
|
return NS_SUCCEEDED(nsVariant::SetFromBool(&mData,
|
2011-02-16 23:47:08 +03:00
|
|
|
JSVAL_TO_BOOLEAN(val)));
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_VOID(val))
|
2006-06-14 20:41:29 +04:00
|
|
|
return NS_SUCCEEDED(nsVariant::SetToVoid(&mData));
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_NULL(val))
|
2001-10-10 03:42:22 +04:00
|
|
|
return NS_SUCCEEDED(nsVariant::SetToEmpty(&mData));
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JSVAL_IS_STRING(val)) {
|
2006-09-24 20:44:51 +04:00
|
|
|
// Make our string immutable. This will also ensure null-termination,
|
|
|
|
// which nsVariant assumes for its PRUnichar* stuff.
|
2011-02-16 23:47:08 +03:00
|
|
|
JSString* str = JSVAL_TO_STRING(val);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JS_MakeStringImmutable(ccx, str))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2006-09-24 20:44:51 +04:00
|
|
|
|
|
|
|
// Don't use nsVariant::SetFromWStringWithSize, because that will copy
|
|
|
|
// the data. Just handle this ourselves. Note that it's ok to not
|
|
|
|
// copy because we added mJSVal as a GC root.
|
|
|
|
NS_ASSERTION(mData.mType == nsIDataType::VTYPE_EMPTY,
|
|
|
|
"Why do we already have data?");
|
|
|
|
|
2010-12-03 11:24:17 +03:00
|
|
|
// Despite the fact that the variant holds the length, there are
|
|
|
|
// implicit assumptions that mWStringValue[mWStringLength] == 0
|
|
|
|
size_t length;
|
|
|
|
const jschar *chars = JS_GetStringCharsZAndLength(ccx, str, &length);
|
|
|
|
if (!chars)
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2010-12-03 11:24:17 +03:00
|
|
|
|
|
|
|
mData.u.wstr.mWStringValue = const_cast<jschar *>(chars);
|
2006-09-24 21:14:17 +04:00
|
|
|
// Use C-style cast, because reinterpret cast from size_t to
|
|
|
|
// PRUint32 is not valid on some platforms.
|
2010-12-03 11:24:17 +03:00
|
|
|
mData.u.wstr.mWStringLength = (PRUint32)length;
|
2006-09-24 20:44:51 +04:00
|
|
|
mData.mType = nsIDataType::VTYPE_WSTRING_SIZE_IS;
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// leaving only JSObject...
|
2011-02-16 23:47:08 +03:00
|
|
|
NS_ASSERTION(JSVAL_IS_OBJECT(val), "invalid type of jsval!");
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2011-02-16 23:47:08 +03:00
|
|
|
JSObject* jsobj = JSVAL_TO_OBJECT(val);
|
2001-10-10 03:42:22 +04:00
|
|
|
|
2002-02-05 09:45:08 +03:00
|
|
|
// Let's see if it is a xpcJSID.
|
2001-10-10 03:42:22 +04:00
|
|
|
|
2008-01-12 07:30:42 +03:00
|
|
|
const nsID* id = xpc_JSObjectToID(ccx, jsobj);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (id)
|
2008-01-12 07:30:42 +03:00
|
|
|
return NS_SUCCEEDED(nsVariant::SetFromID(&mData, *id));
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2002-02-05 09:45:08 +03:00
|
|
|
// Let's see if it is a js array object.
|
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
uint32_t len;
|
2002-02-05 09:45:08 +03:00
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (JS_IsArrayObject(ccx, jsobj) && JS_GetArrayLength(ccx, jsobj, &len)) {
|
|
|
|
if (!len) {
|
2002-03-22 02:21:54 +03:00
|
|
|
// Zero length array
|
|
|
|
nsVariant::SetToEmptyArray(&mData);
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2002-03-22 02:21:54 +03:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2002-02-05 09:45:08 +03:00
|
|
|
nsXPTType type;
|
|
|
|
nsID id;
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!XPCArrayHomogenizer::GetTypeForArray(ccx, jsobj, len, &type, &id))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2002-02-05 09:45:08 +03:00
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!XPCConvert::JSArray2Native(ccx, &mData.u.array.mArrayValue,
|
2011-11-06 16:32:25 +04:00
|
|
|
val, len, type, &id, nsnull))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2002-02-05 09:45:08 +03:00
|
|
|
|
|
|
|
mData.mType = nsIDataType::VTYPE_ARRAY;
|
2011-10-14 21:52:48 +04:00
|
|
|
if (type.IsInterfacePointer())
|
2002-02-05 09:45:08 +03:00
|
|
|
mData.u.array.mArrayInterfaceID = id;
|
|
|
|
mData.u.array.mArrayCount = len;
|
|
|
|
mData.u.array.mArrayType = type.TagPart();
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2011-10-14 21:52:47 +04:00
|
|
|
}
|
2002-02-05 09:45:08 +03:00
|
|
|
|
2001-10-10 03:42:22 +04:00
|
|
|
// XXX This could be smarter and pick some more interesting iface.
|
|
|
|
|
|
|
|
nsXPConnect* xpc;
|
|
|
|
nsCOMPtr<nsISupports> wrapper;
|
|
|
|
const nsIID& iid = NS_GET_IID(nsISupports);
|
|
|
|
|
|
|
|
return nsnull != (xpc = nsXPConnect::GetXPConnect()) &&
|
2002-02-05 09:45:08 +03:00
|
|
|
NS_SUCCEEDED(xpc->WrapJS(ccx, jsobj,
|
2011-10-14 21:52:48 +04:00
|
|
|
iid, getter_AddRefs(wrapper))) &&
|
2001-10-10 03:42:22 +04:00
|
|
|
NS_SUCCEEDED(nsVariant::SetFromInterface(&mData, iid, wrapper));
|
|
|
|
}
|
|
|
|
|
2009-09-01 20:45:05 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
XPCVariant::GetAsJSVal(jsval* result)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(result, "null result arg.");
|
|
|
|
*result = GetJSVal();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
// static
|
|
|
|
JSBool
|
|
|
|
XPCVariant::VariantDataToJS(XPCLazyCallContext& lccx,
|
2001-10-10 03:42:22 +04:00
|
|
|
nsIVariant* variant,
|
2011-02-05 15:56:05 +03:00
|
|
|
nsresult* pErr, jsval* pJSVal)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
// Get the type early because we might need to spoof it below.
|
|
|
|
PRUint16 type;
|
2009-09-01 20:45:05 +04:00
|
|
|
if (NS_FAILED(variant->GetDataType(&type)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
|
2009-09-01 20:45:05 +04:00
|
|
|
jsval realVal;
|
|
|
|
nsresult rv = variant->GetAsJSVal(&realVal);
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_SUCCEEDED(rv) &&
|
|
|
|
(JSVAL_IS_PRIMITIVE(realVal) ||
|
|
|
|
type == nsIDataType::VTYPE_ARRAY ||
|
|
|
|
type == nsIDataType::VTYPE_EMPTY_ARRAY ||
|
2011-10-14 21:52:48 +04:00
|
|
|
type == nsIDataType::VTYPE_ID)) {
|
2010-10-11 02:42:12 +04:00
|
|
|
JSContext *cx = lccx.GetJSContext();
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JS_WrapValue(cx, &realVal))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2009-09-01 20:45:05 +04:00
|
|
|
*pJSVal = realVal;
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2009-09-01 20:45:05 +04:00
|
|
|
}
|
2010-01-30 05:50:46 +03:00
|
|
|
|
2009-09-01 20:45:05 +04:00
|
|
|
nsCOMPtr<XPCVariant> xpcvariant = do_QueryInterface(variant);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (xpcvariant && xpcvariant->mReturnRawObject) {
|
2009-09-01 20:45:05 +04:00
|
|
|
NS_ASSERTION(type == nsIDataType::VTYPE_INTERFACE ||
|
|
|
|
type == nsIDataType::VTYPE_INTERFACE_IS,
|
|
|
|
"Weird variant");
|
|
|
|
|
2010-10-11 02:42:12 +04:00
|
|
|
JSContext *cx = lccx.GetJSContext();
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JS_WrapValue(cx, &realVal))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2010-10-11 02:39:02 +04:00
|
|
|
*pJSVal = realVal;
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
// else, it's an object and we really need to double wrap it if we've
|
2010-06-24 02:09:00 +04:00
|
|
|
// already decided that its 'natural' type is as some sort of interface.
|
|
|
|
|
|
|
|
// We just fall through to the code below and let it do what it does.
|
|
|
|
|
2001-10-10 03:42:22 +04:00
|
|
|
// The nsIVariant is not a XPCVariant (or we act like it isn't).
|
|
|
|
// So we extract the data and do the Right Thing.
|
2009-09-01 20:45:05 +04:00
|
|
|
|
2001-10-10 03:42:22 +04:00
|
|
|
// We ASSUME that the variant implementation can do these conversions...
|
|
|
|
|
|
|
|
nsXPTCVariant xpctvar;
|
|
|
|
nsID iid;
|
|
|
|
nsAutoString astring;
|
2002-03-05 05:22:02 +03:00
|
|
|
nsCAutoString cString;
|
|
|
|
nsUTF8String utf8String;
|
2001-10-10 03:42:22 +04:00
|
|
|
PRUint32 size;
|
|
|
|
xpctvar.flags = 0;
|
2002-02-05 09:45:08 +03:00
|
|
|
JSBool success;
|
2001-10-10 03:42:22 +04:00
|
|
|
|
2009-08-10 14:12:59 +04:00
|
|
|
JSContext* cx = lccx.GetJSContext();
|
2011-10-04 18:06:54 +04:00
|
|
|
NS_ABORT_IF_FALSE(js::GetObjectCompartment(lccx.GetScopeForNewJSObjects()) == cx->compartment,
|
2011-02-05 15:56:05 +03:00
|
|
|
"bad scope for new JSObjects");
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
switch (type) {
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_INT8:
|
|
|
|
case nsIDataType::VTYPE_INT16:
|
|
|
|
case nsIDataType::VTYPE_INT32:
|
|
|
|
case nsIDataType::VTYPE_INT64:
|
|
|
|
case nsIDataType::VTYPE_UINT8:
|
|
|
|
case nsIDataType::VTYPE_UINT16:
|
|
|
|
case nsIDataType::VTYPE_UINT32:
|
|
|
|
case nsIDataType::VTYPE_UINT64:
|
|
|
|
case nsIDataType::VTYPE_FLOAT:
|
|
|
|
case nsIDataType::VTYPE_DOUBLE:
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
// Easy. Handle inline.
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsDouble(&xpctvar.val.d)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2009-08-10 14:12:59 +04:00
|
|
|
return JS_NewNumberValue(cx, xpctvar.val.d, pJSVal);
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_BOOL:
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
// Easy. Handle inline.
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsBool(&xpctvar.val.b)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
*pJSVal = BOOLEAN_TO_JSVAL(xpctvar.val.b);
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_CHAR:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsChar(&xpctvar.val.c)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
xpctvar.type = (uint8)TD_CHAR;
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_WCHAR:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsWChar(&xpctvar.val.wc)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
xpctvar.type = (uint8)TD_WCHAR;
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_ID:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsID(&iid)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
xpctvar.type = (uint8)(TD_PNSIID | XPT_TDP_POINTER);
|
|
|
|
xpctvar.val.p = &iid;
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_ASTRING:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsAString(astring)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2002-03-05 05:22:02 +03:00
|
|
|
xpctvar.type = (uint8)(TD_ASTRING | XPT_TDP_POINTER);
|
|
|
|
xpctvar.val.p = &astring;
|
|
|
|
break;
|
|
|
|
case nsIDataType::VTYPE_DOMSTRING:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsAString(astring)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
xpctvar.type = (uint8)(TD_DOMSTRING | XPT_TDP_POINTER);
|
|
|
|
xpctvar.val.p = &astring;
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_CSTRING:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsACString(cString)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2002-03-05 05:22:02 +03:00
|
|
|
xpctvar.type = (uint8)(TD_CSTRING | XPT_TDP_POINTER);
|
|
|
|
xpctvar.val.p = &cString;
|
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_UTF8STRING:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsAUTF8String(utf8String)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2002-03-05 05:22:02 +03:00
|
|
|
xpctvar.type = (uint8)(TD_UTF8STRING | XPT_TDP_POINTER);
|
|
|
|
xpctvar.val.p = &utf8String;
|
2011-10-14 21:52:47 +04:00
|
|
|
break;
|
|
|
|
case nsIDataType::VTYPE_CHAR_STR:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsString((char**)&xpctvar.val.p)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
xpctvar.type = (uint8)(TD_PSTRING | XPT_TDP_POINTER);
|
2011-09-25 18:38:01 +04:00
|
|
|
xpctvar.SetValNeedsCleanup();
|
2001-10-10 03:42:22 +04:00
|
|
|
break;
|
2001-11-01 04:15:53 +03:00
|
|
|
case nsIDataType::VTYPE_STRING_SIZE_IS:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsStringWithSize(&size,
|
|
|
|
(char**)&xpctvar.val.p)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
xpctvar.type = (uint8)(TD_PSTRING_SIZE_IS | XPT_TDP_POINTER);
|
2011-09-25 18:38:01 +04:00
|
|
|
xpctvar.SetValNeedsCleanup();
|
2001-10-10 03:42:22 +04:00
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_WCHAR_STR:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsWString((PRUnichar**)&xpctvar.val.p)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
xpctvar.type = (uint8)(TD_PWSTRING | XPT_TDP_POINTER);
|
2011-09-25 18:38:01 +04:00
|
|
|
xpctvar.SetValNeedsCleanup();
|
2001-10-10 03:42:22 +04:00
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS:
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsWStringWithSize(&size,
|
|
|
|
(PRUnichar**)&xpctvar.val.p)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
xpctvar.type = (uint8)(TD_PWSTRING_SIZE_IS | XPT_TDP_POINTER);
|
2011-09-25 18:38:01 +04:00
|
|
|
xpctvar.SetValNeedsCleanup();
|
2001-10-10 03:42:22 +04:00
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_INTERFACE:
|
|
|
|
case nsIDataType::VTYPE_INTERFACE_IS:
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
nsID* piid;
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsInterface(&piid, &xpctvar.val.p)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
|
|
|
|
iid = *piid;
|
|
|
|
nsMemory::Free((char*)piid);
|
|
|
|
|
|
|
|
xpctvar.type = (uint8)(TD_INTERFACE_IS_TYPE | XPT_TDP_POINTER);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (xpctvar.val.p)
|
2011-09-25 18:38:01 +04:00
|
|
|
xpctvar.SetValNeedsCleanup();
|
2001-10-10 03:42:22 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-02-05 09:45:08 +03:00
|
|
|
case nsIDataType::VTYPE_ARRAY:
|
|
|
|
{
|
|
|
|
nsDiscriminatedUnion du;
|
|
|
|
nsVariant::Initialize(&du);
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = variant->GetAsArray(&du.u.array.mArrayType,
|
|
|
|
&du.u.array.mArrayInterfaceID,
|
|
|
|
&du.u.array.mArrayCount,
|
|
|
|
&du.u.array.mArrayValue);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2002-02-05 09:45:08 +03:00
|
|
|
// must exit via VARIANT_DONE from here on...
|
|
|
|
du.mType = nsIDataType::VTYPE_ARRAY;
|
2011-11-26 14:05:59 +04:00
|
|
|
success = false;
|
2002-02-05 09:45:08 +03:00
|
|
|
|
|
|
|
nsXPTType conversionType;
|
|
|
|
PRUint16 elementType = du.u.array.mArrayType;
|
|
|
|
const nsID* pid = nsnull;
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
switch (elementType) {
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_INT8:
|
|
|
|
case nsIDataType::VTYPE_INT16:
|
|
|
|
case nsIDataType::VTYPE_INT32:
|
|
|
|
case nsIDataType::VTYPE_INT64:
|
|
|
|
case nsIDataType::VTYPE_UINT8:
|
|
|
|
case nsIDataType::VTYPE_UINT16:
|
|
|
|
case nsIDataType::VTYPE_UINT32:
|
|
|
|
case nsIDataType::VTYPE_UINT64:
|
|
|
|
case nsIDataType::VTYPE_FLOAT:
|
|
|
|
case nsIDataType::VTYPE_DOUBLE:
|
|
|
|
case nsIDataType::VTYPE_BOOL:
|
|
|
|
case nsIDataType::VTYPE_CHAR:
|
|
|
|
case nsIDataType::VTYPE_WCHAR:
|
2002-02-05 09:45:08 +03:00
|
|
|
conversionType = nsXPTType((uint8)elementType);
|
|
|
|
break;
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_ID:
|
|
|
|
case nsIDataType::VTYPE_CHAR_STR:
|
|
|
|
case nsIDataType::VTYPE_WCHAR_STR:
|
2002-02-05 09:45:08 +03:00
|
|
|
conversionType = nsXPTType((uint8)elementType | XPT_TDP_POINTER);
|
|
|
|
break;
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_INTERFACE:
|
2002-02-05 09:45:08 +03:00
|
|
|
pid = &NS_GET_IID(nsISupports);
|
|
|
|
conversionType = nsXPTType((uint8)elementType | XPT_TDP_POINTER);
|
|
|
|
break;
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_INTERFACE_IS:
|
2002-02-05 09:45:08 +03:00
|
|
|
pid = &du.u.array.mArrayInterfaceID;
|
|
|
|
conversionType = nsXPTType((uint8)elementType | XPT_TDP_POINTER);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// The rest are illegal.
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_VOID:
|
|
|
|
case nsIDataType::VTYPE_ASTRING:
|
|
|
|
case nsIDataType::VTYPE_DOMSTRING:
|
|
|
|
case nsIDataType::VTYPE_CSTRING:
|
|
|
|
case nsIDataType::VTYPE_UTF8STRING:
|
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS:
|
|
|
|
case nsIDataType::VTYPE_STRING_SIZE_IS:
|
2002-02-05 09:45:08 +03:00
|
|
|
case nsIDataType::VTYPE_ARRAY:
|
2002-03-22 02:21:54 +03:00
|
|
|
case nsIDataType::VTYPE_EMPTY_ARRAY:
|
2002-02-05 09:45:08 +03:00
|
|
|
case nsIDataType::VTYPE_EMPTY:
|
|
|
|
default:
|
|
|
|
NS_ERROR("bad type in array!");
|
|
|
|
goto VARIANT_DONE;
|
|
|
|
}
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
success =
|
|
|
|
XPCConvert::NativeArray2JS(lccx, pJSVal,
|
2002-02-05 09:45:08 +03:00
|
|
|
(const void**)&du.u.array.mArrayValue,
|
|
|
|
conversionType, pid,
|
2011-02-05 15:56:05 +03:00
|
|
|
du.u.array.mArrayCount, pErr);
|
2002-02-05 09:45:08 +03:00
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
VARIANT_DONE:
|
2002-02-05 09:45:08 +03:00
|
|
|
nsVariant::Cleanup(&du);
|
|
|
|
return success;
|
2011-10-14 21:52:47 +04:00
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_EMPTY_ARRAY:
|
2002-03-22 02:21:54 +03:00
|
|
|
{
|
2009-08-10 14:12:59 +04:00
|
|
|
JSObject* array = JS_NewArrayObject(cx, 0, nsnull);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!array)
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2002-03-22 02:21:54 +03:00
|
|
|
*pJSVal = OBJECT_TO_JSVAL(array);
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2002-03-22 02:21:54 +03:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_VOID:
|
2001-10-10 03:42:22 +04:00
|
|
|
*pJSVal = JSVAL_VOID;
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2006-06-14 20:41:29 +04:00
|
|
|
case nsIDataType::VTYPE_EMPTY:
|
|
|
|
*pJSVal = JSVAL_NULL;
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2001-10-10 03:42:22 +04:00
|
|
|
default:
|
|
|
|
NS_ERROR("bad type in variant!");
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are here then we need to convert the data in the xpctvar.
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (xpctvar.type.TagPart() == TD_PSTRING_SIZE_IS ||
|
2011-10-14 21:52:48 +04:00
|
|
|
xpctvar.type.TagPart() == TD_PWSTRING_SIZE_IS) {
|
2009-08-10 14:12:59 +04:00
|
|
|
success = XPCConvert::NativeStringWithSize2JS(cx, pJSVal,
|
2001-10-10 03:42:22 +04:00
|
|
|
(const void*)&xpctvar.val,
|
|
|
|
xpctvar.type,
|
|
|
|
size, pErr);
|
2011-10-14 21:52:48 +04:00
|
|
|
} else {
|
2009-08-10 14:12:59 +04:00
|
|
|
success = XPCConvert::NativeData2JS(lccx, pJSVal,
|
2009-08-05 02:04:53 +04:00
|
|
|
(const void*)&xpctvar.val,
|
|
|
|
xpctvar.type,
|
2011-02-05 15:56:05 +03:00
|
|
|
&iid, pErr);
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
|
|
|
|
2011-09-25 18:38:01 +04:00
|
|
|
// We may have done something in the above code that requires cleanup.
|
2011-10-14 21:52:48 +04:00
|
|
|
if (xpctvar.DoesValNeedCleanup()) {
|
2011-09-25 18:38:01 +04:00
|
|
|
if (type == nsIDataType::VTYPE_INTERFACE ||
|
|
|
|
type == nsIDataType::VTYPE_INTERFACE_IS)
|
|
|
|
((nsISupports*)xpctvar.val.p)->Release();
|
|
|
|
else
|
|
|
|
nsMemory::Free((char*)xpctvar.val.p);
|
|
|
|
}
|
2001-10-10 03:42:22 +04:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
// XXX These default implementations need to be improved to allow for
|
|
|
|
// some more interesting conversions.
|
|
|
|
|
|
|
|
|
|
|
|
/* readonly attribute PRUint16 dataType; */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetDataType(PRUint16 *aDataType)
|
|
|
|
{
|
|
|
|
*aDataType = mData.mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRUint8 getAsInt8 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInt8(PRUint8 *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInt8(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRInt16 getAsInt16 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInt16(PRInt16 *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInt16(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRInt32 getAsInt32 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInt32(PRInt32 *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInt32(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRInt64 getAsInt64 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInt64(PRInt64 *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInt64(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRUint8 getAsUint8 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsUint8(PRUint8 *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToUint8(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRUint16 getAsUint16 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsUint16(PRUint16 *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToUint16(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRUint32 getAsUint32 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsUint32(PRUint32 *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToUint32(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRUint64 getAsUint64 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsUint64(PRUint64 *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToUint64(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* float getAsFloat (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsFloat(float *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToFloat(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* double getAsDouble (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsDouble(double *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToDouble(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* bool getAsBool (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsBool(bool *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToBool(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* char getAsChar (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsChar(char *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToChar(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wchar getAsWChar (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsWChar(PRUnichar *_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToWChar(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [notxpcom] nsresult getAsID (out nsID retval); */
|
|
|
|
NS_IMETHODIMP_(nsresult) XPCVariant::GetAsID(nsID *retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToID(mData, retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AString getAsAString (); */
|
2002-03-24 03:16:18 +03:00
|
|
|
NS_IMETHODIMP XPCVariant::GetAsAString(nsAString & _retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToAString(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2002-03-05 05:22:02 +03:00
|
|
|
/* DOMString getAsDOMString (); */
|
2002-03-24 03:16:18 +03:00
|
|
|
NS_IMETHODIMP XPCVariant::GetAsDOMString(nsAString & _retval)
|
2002-03-05 05:22:02 +03:00
|
|
|
{
|
|
|
|
// A DOMString maps to an AString internally, so we can re-use
|
|
|
|
// ConvertToAString here.
|
|
|
|
return nsVariant::ConvertToAString(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ACString getAsACString (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsACString(nsACString & _retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToACString(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AUTF8String getAsAUTF8String (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsAUTF8String(nsAUTF8String & _retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToAUTF8String(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2001-10-10 03:42:22 +04:00
|
|
|
/* string getAsString (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsString(char **_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToString(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wstring getAsWString (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsWString(PRUnichar **_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToWString(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsISupports getAsISupports (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsISupports(nsISupports **_retval)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToISupports(mData, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void getAsInterface (out nsIIDPtr iid, [iid_is (iid), retval] out nsQIResult iface); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInterface(nsIID * *iid, void * *iface)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInterface(mData, iid, iface);
|
|
|
|
}
|
|
|
|
|
2001-10-13 00:29:19 +04:00
|
|
|
|
|
|
|
/* [notxpcom] nsresult getAsArray (out PRUint16 type, out nsIID iid, out PRUint32 count, out voidPtr ptr); */
|
|
|
|
NS_IMETHODIMP_(nsresult) XPCVariant::GetAsArray(PRUint16 *type, nsIID *iid, PRUint32 *count, void * *ptr)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
2001-10-13 00:29:19 +04:00
|
|
|
return nsVariant::ConvertToArray(mData, type, iid, count, ptr);
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void getAsStringWithSize (out PRUint32 size, [size_is (size), retval] out string str); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsStringWithSize(PRUint32 *size, char **str)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToStringWithSize(mData, size, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void getAsWStringWithSize (out PRUint32 size, [size_is (size), retval] out wstring str); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsWStringWithSize(PRUint32 *size, PRUnichar **str)
|
|
|
|
{
|
|
|
|
return nsVariant::ConvertToWStringWithSize(mData, size, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|