2001-10-10 03:42:22 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04: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/. */
|
2001-10-10 03:42:22 +04:00
|
|
|
|
|
|
|
/* nsIVariant implementation for xpconnect. */
|
|
|
|
|
|
|
|
#include "xpcprivate.h"
|
2013-06-05 08:11:19 +04:00
|
|
|
#include "nsCxPusher.h"
|
2001-10-10 03:42:22 +04:00
|
|
|
|
2012-01-15 12:13:11 +04:00
|
|
|
#include "jsfriendapi.h"
|
2013-08-18 02:50:18 +04:00
|
|
|
#include "jsprf.h"
|
2013-09-10 01:14:10 +04:00
|
|
|
#include "jswrapper.h"
|
2012-01-11 12:23:08 +04:00
|
|
|
|
2013-04-17 19:38:44 +04:00
|
|
|
using namespace JS;
|
2013-06-05 08:11:19 +04:00
|
|
|
using namespace mozilla;
|
2013-04-17 19:38:44 +04:00
|
|
|
|
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
|
|
|
|
2012-09-20 17:03:28 +04:00
|
|
|
XPCVariant::XPCVariant(JSContext* cx, jsval aJSVal)
|
2012-01-26 17:52:25 +04:00
|
|
|
: mJSVal(aJSVal), mCCGeneration(0)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
nsVariant::Initialize(&mData);
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JSVAL_IS_PRIMITIVE(mJSVal)) {
|
2013-03-21 19:20:44 +04:00
|
|
|
// XXXbholley - The innerization here was from bug 638026. Blake says
|
|
|
|
// the basic problem was that we were storing the C++ inner but the JS
|
|
|
|
// outer, which meant that, after navigation, the JS inner could be
|
|
|
|
// collected, which would cause us to try to recreate the JS inner at
|
|
|
|
// some later point after teardown, which would crash. This is shouldn't
|
|
|
|
// be a problem anymore because SetParentToWindow will do the right
|
|
|
|
// thing, but I'm saving the cleanup here for another day. Blake thinks
|
|
|
|
// that we should just not store the WN if we're creating a variant for
|
|
|
|
// an outer window.
|
2012-09-20 17:03:28 +04:00
|
|
|
JSObject *obj = JS_ObjectToInnerObject(cx, JSVAL_TO_OBJECT(mJSVal));
|
2011-03-09 02:04:59 +03:00
|
|
|
mJSVal = OBJECT_TO_JSVAL(obj);
|
|
|
|
|
2013-04-11 22:50:18 +04:00
|
|
|
JSObject *unwrapped = js::CheckedUnwrap(obj, /* stopAtOuter = */ false);
|
2013-04-19 23:58:30 +04:00
|
|
|
mReturnRawObject = !(unwrapped && IS_WN_REFLECTOR(unwrapped));
|
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
|
|
|
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(JSVAL_IS_GCTHING(val), "Must be traceable or unlinked");
|
2011-02-16 23:47:08 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2013-03-29 00:37:22 +04:00
|
|
|
MOZ_ASSERT(JSVAL_IS_TRACEABLE(mJSVal));
|
2012-01-14 05:19:19 +04:00
|
|
|
JS_SET_TRACING_DETAILS(trc, GetTraceName, this, 0);
|
2013-07-23 13:58:27 +04:00
|
|
|
JS_CallHeapValueTracer(trc, &mJSVal, "XPCTraceableVariant::mJSVal");
|
2007-06-02 19:11:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2012-01-14 05:19:19 +04:00
|
|
|
XPCTraceableVariant::GetTraceName(JSTracer* trc, char *buf, size_t bufsize)
|
2007-06-02 19:11:52 +04:00
|
|
|
{
|
|
|
|
JS_snprintf(buf, bufsize, "XPCVariant[0x%p].mJSVal", trc->debugPrintArg);
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(XPCVariant)
|
|
|
|
|
2007-02-18 23:05:32 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(XPCVariant)
|
2012-05-11 19:46:26 +04:00
|
|
|
JS::Value val = tmp->GetJSValPreserveColor();
|
|
|
|
if (val.isObjectOrNull()) {
|
2012-01-24 03:25:53 +04:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mJSVal");
|
2012-05-03 23:28:10 +04:00
|
|
|
cb.NoteJSChild(JSVAL_TO_OBJECT(val));
|
2012-01-24 03:25:53 +04:00
|
|
|
}
|
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)
|
2012-05-11 19:46:26 +04:00
|
|
|
JS::Value val = tmp->GetJSValPreserveColor();
|
2011-02-16 23:47:08 +03:00
|
|
|
|
|
|
|
// We're sharing val's buffer, clear the pointer to it so Cleanup() won't
|
|
|
|
// try to delete it
|
2012-05-11 19:46:26 +04:00
|
|
|
if (val.isString())
|
2012-07-30 18:20:58 +04:00
|
|
|
tmp->mData.u.wstr.mWStringValue = nullptr;
|
2008-01-13 21:21:08 +03:00
|
|
|
nsVariant::Cleanup(&tmp->mData);
|
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (val.isMarkable()) {
|
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
|
|
|
}
|
2012-05-11 19:46:26 +04:00
|
|
|
tmp->mJSVal = JS::NullValue();
|
2007-11-02 01:51:57 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2007-02-18 23:05:32 +03:00
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
// static
|
2012-09-20 17:03:28 +04:00
|
|
|
XPCVariant* XPCVariant::newVariant(JSContext* cx, jsval aJSVal)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
2007-06-02 19:11:52 +04:00
|
|
|
XPCVariant* variant;
|
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!JSVAL_IS_TRACEABLE(aJSVal))
|
2012-09-20 17:03:28 +04:00
|
|
|
variant = new XPCVariant(cx, aJSVal);
|
2007-06-02 19:11:52 +04:00
|
|
|
else
|
2012-09-20 17:03:28 +04:00
|
|
|
variant = new XPCTraceableVariant(cx, aJSVal);
|
2007-06-02 19:11:52 +04:00
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!variant)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2001-10-10 03:42:22 +04:00
|
|
|
NS_ADDREF(variant);
|
|
|
|
|
2012-09-20 17:03:28 +04:00
|
|
|
if (!variant->InitializeData(cx))
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_RELEASE(variant); // Also sets variant to nullptr.
|
2001-10-10 03:42:22 +04:00
|
|
|
|
|
|
|
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:
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool GetTypeForArray(JSContext* cx, HandleObject array,
|
|
|
|
uint32_t length,
|
|
|
|
nsXPTType* resultType, nsID* resultID);
|
2002-02-05 09:45:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-04-17 19:38:44 +04:00
|
|
|
XPCArrayHomogenizer::GetTypeForArray(JSContext* cx, HandleObject array,
|
2012-03-07 03:52:55 +04:00
|
|
|
uint32_t length,
|
2011-10-14 21:52:47 +04:00
|
|
|
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
|
|
|
|
2013-04-17 19:38:44 +04:00
|
|
|
RootedValue val(cx);
|
|
|
|
RootedObject jsobj(cx);
|
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++) {
|
2013-08-05 17:02:47 +04:00
|
|
|
if (!JS_GetElement(cx, array, i, &val))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (val.isInt32()) {
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tInt;
|
2012-05-11 19:46:26 +04:00
|
|
|
} else if (val.isDouble()) {
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tDbl;
|
2012-05-11 19:46:26 +04:00
|
|
|
} else if (val.isBoolean()) {
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tBool;
|
2013-04-17 19:38:44 +04:00
|
|
|
} else if (val.isUndefined()) {
|
2002-02-05 09:45:08 +03:00
|
|
|
state = tVar;
|
|
|
|
break;
|
2012-05-11 19:46:26 +04:00
|
|
|
} else if (val.isNull()) {
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tNull;
|
2012-05-11 19:46:26 +04:00
|
|
|
} else if (val.isString()) {
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tStr;
|
2012-05-11 19:46:26 +04:00
|
|
|
} else {
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(val.isObject(), "invalid type of jsval!");
|
2013-04-17 19:38:44 +04:00
|
|
|
jsobj = &val.toObject();
|
2012-09-20 17:03:28 +04:00
|
|
|
if (JS_IsArrayObject(cx, jsobj))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tArr;
|
2012-09-20 17:03:28 +04:00
|
|
|
else if (xpc_JSObjectIsID(cx, jsobj))
|
2002-02-05 09:45:08 +03:00
|
|
|
type = tID;
|
|
|
|
else
|
|
|
|
type = tISup;
|
|
|
|
}
|
|
|
|
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(state != tErr, "bad state table!");
|
|
|
|
MOZ_ASSERT(type != tErr, "bad type!");
|
|
|
|
MOZ_ASSERT(type != tVar, "bad type!");
|
|
|
|
MOZ_ASSERT(type != tUnk, "bad type!");
|
2002-02-05 09:45:08 +03:00
|
|
|
|
|
|
|
state = StateTable[state][type];
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(state != tErr, "bad state table!");
|
|
|
|
MOZ_ASSERT(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 :
|
2012-01-02 22:05:19 +04:00
|
|
|
*resultType = nsXPTType((uint8_t)TD_INT32);
|
2002-02-05 09:45:08 +03:00
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tDbl :
|
2012-01-02 22:05:19 +04:00
|
|
|
*resultType = nsXPTType((uint8_t)TD_DOUBLE);
|
2002-02-05 09:45:08 +03:00
|
|
|
break;
|
|
|
|
case tBool:
|
2012-01-02 22:05:19 +04:00
|
|
|
*resultType = nsXPTType((uint8_t)TD_BOOL);
|
2002-02-05 09:45:08 +03:00
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tStr :
|
2012-01-02 22:05:19 +04:00
|
|
|
*resultType = nsXPTType((uint8_t)TD_PWSTRING);
|
2002-02-05 09:45:08 +03:00
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tID :
|
2012-01-02 22:05:19 +04:00
|
|
|
*resultType = nsXPTType((uint8_t)TD_PNSIID);
|
2002-02-05 09:45:08 +03:00
|
|
|
break;
|
2011-10-14 21:52:47 +04:00
|
|
|
case tISup:
|
2012-01-02 22:05:19 +04:00
|
|
|
*resultType = nsXPTType((uint8_t)TD_INTERFACE_IS_TYPE);
|
2002-02-05 09:45:08 +03:00
|
|
|
*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 :
|
2012-01-02 22:05:19 +04:00
|
|
|
*resultType = nsXPTType((uint8_t)TD_INTERFACE_IS_TYPE);
|
2002-02-05 09:45:08 +03:00
|
|
|
*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
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool XPCVariant::InitializeData(JSContext* cx)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
2012-09-20 17:03:28 +04:00
|
|
|
JS_CHECK_RECURSION(cx, return false);
|
2010-08-24 17:50:15 +04:00
|
|
|
|
2013-04-17 19:38:44 +04:00
|
|
|
RootedValue val(cx, GetJSVal());
|
2012-05-11 19:46:26 +04:00
|
|
|
|
|
|
|
if (val.isInt32())
|
|
|
|
return NS_SUCCEEDED(nsVariant::SetFromInt32(&mData, val.toInt32()));
|
|
|
|
if (val.isDouble())
|
|
|
|
return NS_SUCCEEDED(nsVariant::SetFromDouble(&mData, val.toDouble()));
|
|
|
|
if (val.isBoolean())
|
|
|
|
return NS_SUCCEEDED(nsVariant::SetFromBool(&mData, val.toBoolean()));
|
|
|
|
if (val.isUndefined())
|
2006-06-14 20:41:29 +04:00
|
|
|
return NS_SUCCEEDED(nsVariant::SetToVoid(&mData));
|
2012-05-11 19:46:26 +04:00
|
|
|
if (val.isNull())
|
2001-10-10 03:42:22 +04:00
|
|
|
return NS_SUCCEEDED(nsVariant::SetToEmpty(&mData));
|
2012-05-11 19:46:26 +04:00
|
|
|
if (val.isString()) {
|
|
|
|
JSString* str = val.toString();
|
2012-08-25 02:00:38 +04:00
|
|
|
if (!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.
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(mData.mType == nsIDataType::VTYPE_EMPTY,
|
|
|
|
"Why do we already have data?");
|
2006-09-24 20:44:51 +04:00
|
|
|
|
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;
|
2012-09-20 17:03:28 +04:00
|
|
|
const jschar *chars = JS_GetStringCharsZAndLength(cx, str, &length);
|
2010-12-03 11:24:17 +03:00
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
// uint32_t is not valid on some platforms.
|
|
|
|
mData.u.wstr.mWStringLength = (uint32_t)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...
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(val.isObject(), "invalid type of jsval!");
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2013-04-17 19:38:44 +04:00
|
|
|
RootedObject jsobj(cx, &val.toObject());
|
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
|
|
|
|
2012-09-20 17:03:28 +04:00
|
|
|
const nsID* id = xpc_JSObjectToID(cx, 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
|
|
|
|
2012-09-20 17:03:28 +04:00
|
|
|
if (JS_IsArrayObject(cx, jsobj) && JS_GetArrayLength(cx, jsobj, &len)) {
|
2011-10-14 21:52:48 +04:00
|
|
|
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;
|
|
|
|
|
2012-09-20 17:03:28 +04:00
|
|
|
if (!XPCArrayHomogenizer::GetTypeForArray(cx, jsobj, len, &type, &id))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2002-02-05 09:45:08 +03:00
|
|
|
|
2013-06-05 08:11:19 +04:00
|
|
|
if (!XPCConvert::JSArray2Native(&mData.u.array.mArrayValue,
|
2012-07-30 18:20:58 +04:00
|
|
|
val, len, type, &id, nullptr))
|
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.
|
|
|
|
|
2013-05-17 09:49:41 +04:00
|
|
|
nsXPConnect* xpc = nsXPConnect::XPConnect();
|
2001-10-10 03:42:22 +04:00
|
|
|
nsCOMPtr<nsISupports> wrapper;
|
|
|
|
const nsIID& iid = NS_GET_IID(nsISupports);
|
|
|
|
|
2013-05-17 09:49:41 +04:00
|
|
|
return NS_SUCCEEDED(xpc->WrapJS(cx, 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
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-06-05 08:11:19 +04:00
|
|
|
XPCVariant::VariantDataToJS(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.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t 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
|
|
|
|
2013-06-05 08:11:19 +04:00
|
|
|
AutoJSContext cx;
|
2013-04-17 19:38:44 +04:00
|
|
|
RootedValue realVal(cx);
|
|
|
|
nsresult rv = variant->GetAsJSVal(realVal.address());
|
2009-09-01 20:45:05 +04:00
|
|
|
|
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)) {
|
2013-04-17 19:38:44 +04:00
|
|
|
if (!JS_WrapValue(cx, realVal.address()))
|
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) {
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(type == nsIDataType::VTYPE_INTERFACE ||
|
|
|
|
type == nsIDataType::VTYPE_INTERFACE_IS,
|
|
|
|
"Weird variant");
|
2009-09-01 20:45:05 +04:00
|
|
|
|
2013-04-17 19:38:44 +04:00
|
|
|
if (!JS_WrapValue(cx, realVal.address()))
|
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...
|
|
|
|
|
|
|
|
nsID iid;
|
|
|
|
|
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
|
|
|
{
|
2013-05-09 13:39:21 +04:00
|
|
|
double d;
|
|
|
|
if (NS_FAILED(variant->GetAsDouble(&d)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-05-09 13:39:21 +04:00
|
|
|
*pJSVal = JS_NumberValue(d);
|
2012-08-02 04:59:47 +04:00
|
|
|
return true;
|
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
|
|
|
{
|
2013-05-09 13:39:21 +04:00
|
|
|
bool b;
|
|
|
|
if (NS_FAILED(variant->GetAsBool(&b)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-05-09 13:39:21 +04:00
|
|
|
*pJSVal = BOOLEAN_TO_JSVAL(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:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
if (NS_FAILED(variant->GetAsChar(&c)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-06-05 08:11:19 +04:00
|
|
|
return XPCConvert::NativeData2JS(pJSVal, (const void*)&c, TD_CHAR, &iid, pErr);
|
2013-05-09 13:39:21 +04:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_WCHAR:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
PRUnichar wc;
|
|
|
|
if (NS_FAILED(variant->GetAsWChar(&wc)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-06-05 08:11:19 +04:00
|
|
|
return XPCConvert::NativeData2JS(pJSVal, (const void*)&wc, TD_WCHAR, &iid, pErr);
|
2013-05-09 13:39:21 +04:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_ID:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
2011-10-14 21:52:48 +04:00
|
|
|
if (NS_FAILED(variant->GetAsID(&iid)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-05-09 13:39:21 +04:00
|
|
|
nsID *v = &iid;
|
2013-06-05 08:11:19 +04:00
|
|
|
return XPCConvert::NativeData2JS(pJSVal, (const void*)&v, TD_PNSIID, &iid, pErr);
|
2013-05-09 13:39:21 +04:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_ASTRING:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
nsAutoString 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;
|
2013-05-09 13:39:21 +04:00
|
|
|
nsAutoString *v = &astring;
|
2013-06-05 08:11:19 +04:00
|
|
|
return XPCConvert::NativeData2JS(pJSVal, (const void*)&v, TD_ASTRING, &iid, pErr);
|
2013-05-09 13:39:21 +04:00
|
|
|
}
|
2002-03-05 05:22:02 +03:00
|
|
|
case nsIDataType::VTYPE_DOMSTRING:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
nsAutoString 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;
|
2013-05-09 13:39:21 +04:00
|
|
|
nsAutoString *v = &astring;
|
2013-06-05 08:11:19 +04:00
|
|
|
return XPCConvert::NativeData2JS(pJSVal, (const void*)&v,
|
2013-05-09 13:39:21 +04:00
|
|
|
TD_DOMSTRING, &iid, pErr);
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_CSTRING:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
nsAutoCString 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;
|
2013-05-09 13:39:21 +04:00
|
|
|
nsAutoCString *v = &cString;
|
2013-06-05 08:11:19 +04:00
|
|
|
return XPCConvert::NativeData2JS(pJSVal, (const void*)&v,
|
2013-05-09 13:39:21 +04:00
|
|
|
TD_CSTRING, &iid, pErr);
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_UTF8STRING:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
nsUTF8String 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;
|
2013-05-09 13:39:21 +04:00
|
|
|
nsUTF8String *v = &utf8String;
|
2013-06-05 08:11:19 +04:00
|
|
|
return XPCConvert::NativeData2JS(pJSVal, (const void*)&v,
|
2013-05-09 13:39:21 +04:00
|
|
|
TD_UTF8STRING, &iid, pErr);
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_CHAR_STR:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
char *pc;
|
|
|
|
if (NS_FAILED(variant->GetAsString(&pc)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-06-05 08:11:19 +04:00
|
|
|
bool success = XPCConvert::NativeData2JS(pJSVal, (const void*)&pc,
|
2013-05-09 13:39:21 +04:00
|
|
|
TD_PSTRING, &iid, pErr);
|
|
|
|
nsMemory::Free(pc);
|
|
|
|
return success;
|
|
|
|
}
|
2001-11-01 04:15:53 +03:00
|
|
|
case nsIDataType::VTYPE_STRING_SIZE_IS:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
char *pc;
|
|
|
|
uint32_t size;
|
|
|
|
if (NS_FAILED(variant->GetAsStringWithSize(&size, &pc)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-06-05 08:11:19 +04:00
|
|
|
bool success = XPCConvert::NativeStringWithSize2JS(pJSVal, (const void*)&pc,
|
2013-05-09 13:39:21 +04:00
|
|
|
TD_PSTRING_SIZE_IS, size, pErr);
|
|
|
|
nsMemory::Free(pc);
|
|
|
|
return success;
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_WCHAR_STR:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
PRUnichar *pwc;
|
|
|
|
if (NS_FAILED(variant->GetAsWString(&pwc)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-06-05 08:11:19 +04:00
|
|
|
bool success = XPCConvert::NativeData2JS(pJSVal, (const void*)&pwc,
|
2013-05-09 13:39:21 +04:00
|
|
|
TD_PSTRING, &iid, pErr);
|
|
|
|
nsMemory::Free(pwc);
|
|
|
|
return success;
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS:
|
2013-05-09 13:39:21 +04:00
|
|
|
{
|
|
|
|
PRUnichar *pwc;
|
|
|
|
uint32_t size;
|
|
|
|
if (NS_FAILED(variant->GetAsWStringWithSize(&size, &pwc)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2013-06-05 08:11:19 +04:00
|
|
|
bool success = XPCConvert::NativeStringWithSize2JS(pJSVal, (const void*)&pwc,
|
2013-05-09 13:39:21 +04:00
|
|
|
TD_PWSTRING_SIZE_IS, size, pErr);
|
|
|
|
nsMemory::Free(pwc);
|
|
|
|
return success;
|
|
|
|
}
|
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
|
|
|
{
|
2013-05-09 13:39:21 +04:00
|
|
|
nsISupports *pi;
|
2001-10-10 03:42:22 +04:00
|
|
|
nsID* piid;
|
2013-05-09 13:39:21 +04:00
|
|
|
if (NS_FAILED(variant->GetAsInterface(&piid, (void **)&pi)))
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2001-10-10 03:42:22 +04:00
|
|
|
|
|
|
|
iid = *piid;
|
|
|
|
nsMemory::Free((char*)piid);
|
|
|
|
|
2013-06-05 08:11:19 +04:00
|
|
|
bool success = XPCConvert::NativeData2JS(pJSVal, (const void*)&pi,
|
2013-05-09 13:39:21 +04:00
|
|
|
TD_INTERFACE_IS_TYPE, &iid, pErr);
|
|
|
|
if (pi)
|
|
|
|
pi->Release();
|
|
|
|
return success;
|
2001-10-10 03:42:22 +04:00
|
|
|
}
|
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;
|
2013-05-09 13:39:21 +04:00
|
|
|
bool success = false;
|
2002-02-05 09:45:08 +03:00
|
|
|
|
|
|
|
nsXPTType conversionType;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t elementType = du.u.array.mArrayType;
|
2012-07-30 18:20:58 +04:00
|
|
|
const nsID* pid = nullptr;
|
2002-02-05 09:45:08 +03:00
|
|
|
|
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:
|
2012-01-02 22:05:19 +04:00
|
|
|
conversionType = nsXPTType((uint8_t)elementType);
|
2002-02-05 09:45:08 +03:00
|
|
|
break;
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
case nsIDataType::VTYPE_ID:
|
|
|
|
case nsIDataType::VTYPE_CHAR_STR:
|
|
|
|
case nsIDataType::VTYPE_WCHAR_STR:
|
2012-01-02 22:05:19 +04:00
|
|
|
conversionType = nsXPTType((uint8_t)elementType);
|
2002-02-05 09:45:08 +03:00
|
|
|
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);
|
2012-01-02 22:05:19 +04:00
|
|
|
conversionType = nsXPTType((uint8_t)elementType);
|
2002-02-05 09:45:08 +03:00
|
|
|
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;
|
2012-01-02 22:05:19 +04:00
|
|
|
conversionType = nsXPTType((uint8_t)elementType);
|
2002-02-05 09:45:08 +03:00
|
|
|
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 =
|
2013-06-05 08:11:19 +04:00
|
|
|
XPCConvert::NativeArray2JS(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
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
JSObject* array = JS_NewArrayObject(cx, 0, nullptr);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
// XXX These default implementations need to be improved to allow for
|
|
|
|
// some more interesting conversions.
|
|
|
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* readonly attribute uint16_t dataType; */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetDataType(uint16_t *aDataType)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
*aDataType = mData.mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* uint8_t getAsInt8 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInt8(uint8_t *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInt8(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* int16_t getAsInt16 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInt16(int16_t *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInt16(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* int32_t getAsInt32 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInt32(int32_t *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInt32(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* int64_t getAsInt64 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsInt64(int64_t *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToInt64(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* uint8_t getAsUint8 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsUint8(uint8_t *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToUint8(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* uint16_t getAsUint16 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsUint16(uint16_t *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToUint16(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* uint32_t getAsUint32 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsUint32(uint32_t *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToUint32(mData, _retval);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* uint64_t getAsUint64 (); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsUint64(uint64_t *_retval)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* [notxpcom] nsresult getAsArray (out uint16_t type, out nsIID iid, out uint32_t count, out voidPtr ptr); */
|
|
|
|
NS_IMETHODIMP_(nsresult) XPCVariant::GetAsArray(uint16_t *type, nsIID *iid, uint32_t *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
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* void getAsStringWithSize (out uint32_t size, [size_is (size), retval] out string str); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsStringWithSize(uint32_t *size, char **str)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToStringWithSize(mData, size, str);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* void getAsWStringWithSize (out uint32_t size, [size_is (size), retval] out wstring str); */
|
|
|
|
NS_IMETHODIMP XPCVariant::GetAsWStringWithSize(uint32_t *size, PRUnichar **str)
|
2001-10-10 03:42:22 +04:00
|
|
|
{
|
|
|
|
return nsVariant::ConvertToWStringWithSize(mData, size, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|