зеркало из https://github.com/mozilla/pjs.git
Bug 332648 - Part g: Move AutoIdArray to jsapi.h; r=evilpie
This commit is contained in:
Родитель
1b76f399af
Коммит
53c85bc3c6
|
@ -501,9 +501,9 @@ nsHTMLCanvasElement::GetContext(const nsAString& aContextId,
|
||||||
contextProps = do_CreateInstance("@mozilla.org/hash-property-bag;1");
|
contextProps = do_CreateInstance("@mozilla.org/hash-property-bag;1");
|
||||||
|
|
||||||
JSObject *opts = JSVAL_TO_OBJECT(aContextOptions);
|
JSObject *opts = JSVAL_TO_OBJECT(aContextOptions);
|
||||||
JSIdArray *props = JS_Enumerate(cx, opts);
|
JS::AutoIdArray props(cx, JS_Enumerate(cx, opts));
|
||||||
for (int i = 0; props && i < JS_IdArrayLength(cx, props); ++i) {
|
for (size_t i = 0; !!props && i < props.length(); ++i) {
|
||||||
jsid propid = JS_IdArrayGet(cx, props, i);
|
jsid propid = props[i];
|
||||||
jsval propname, propval;
|
jsval propname, propval;
|
||||||
if (!JS_IdToValue(cx, propid, &propname) ||
|
if (!JS_IdToValue(cx, propid, &propname) ||
|
||||||
!JS_GetPropertyById(cx, opts, propid, &propval)) {
|
!JS_GetPropertyById(cx, opts, propid, &propval)) {
|
||||||
|
@ -513,13 +513,12 @@ nsHTMLCanvasElement::GetContext(const nsAString& aContextId,
|
||||||
JSString *propnameString = JS_ValueToString(cx, propname);
|
JSString *propnameString = JS_ValueToString(cx, propname);
|
||||||
nsDependentJSString pstr;
|
nsDependentJSString pstr;
|
||||||
if (!propnameString || !pstr.init(cx, propnameString)) {
|
if (!propnameString || !pstr.init(cx, propnameString)) {
|
||||||
JS_DestroyIdArray(cx, props);
|
|
||||||
mCurrentContext = nsnull;
|
mCurrentContext = nsnull;
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (JSVAL_IS_BOOLEAN(propval)) {
|
if (JSVAL_IS_BOOLEAN(propval)) {
|
||||||
contextProps->SetPropertyAsBool(pstr, propval == JSVAL_TRUE ? true : false);
|
contextProps->SetPropertyAsBool(pstr, JSVAL_TO_BOOLEAN(propval));
|
||||||
} else if (JSVAL_IS_INT(propval)) {
|
} else if (JSVAL_IS_INT(propval)) {
|
||||||
contextProps->SetPropertyAsInt32(pstr, JSVAL_TO_INT(propval));
|
contextProps->SetPropertyAsInt32(pstr, JSVAL_TO_INT(propval));
|
||||||
} else if (JSVAL_IS_DOUBLE(propval)) {
|
} else if (JSVAL_IS_DOUBLE(propval)) {
|
||||||
|
@ -528,7 +527,6 @@ nsHTMLCanvasElement::GetContext(const nsAString& aContextId,
|
||||||
JSString *propvalString = JS_ValueToString(cx, propval);
|
JSString *propvalString = JS_ValueToString(cx, propval);
|
||||||
nsDependentJSString vstr;
|
nsDependentJSString vstr;
|
||||||
if (!propvalString || !vstr.init(cx, propvalString)) {
|
if (!propvalString || !vstr.init(cx, propvalString)) {
|
||||||
JS_DestroyIdArray(cx, props);
|
|
||||||
mCurrentContext = nsnull;
|
mCurrentContext = nsnull;
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
@ -536,7 +534,6 @@ nsHTMLCanvasElement::GetContext(const nsAString& aContextId,
|
||||||
contextProps->SetPropertyAsAString(pstr, vstr);
|
contextProps->SetPropertyAsAString(pstr, vstr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JS_DestroyIdArray(cx, props);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -966,25 +966,21 @@ nsJSObjWrapper::NP_Enumerate(NPObject *npobj, NPIdentifier **idarray,
|
||||||
if (!ac.enter(cx, npjsobj->mJSObj))
|
if (!ac.enter(cx, npjsobj->mJSObj))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
JSIdArray *ida = ::JS_Enumerate(cx, npjsobj->mJSObj);
|
JS::AutoIdArray ida(cx, JS_Enumerate(cx, npjsobj->mJSObj));
|
||||||
if (!ida) {
|
if (!ida) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
*count = ida->length;
|
*count = ida.length();
|
||||||
*idarray = (NPIdentifier *)PR_Malloc(*count * sizeof(NPIdentifier));
|
*idarray = (NPIdentifier *)PR_Malloc(*count * sizeof(NPIdentifier));
|
||||||
if (!*idarray) {
|
if (!*idarray) {
|
||||||
ThrowJSException(cx, "Memory allocation failed for NPIdentifier!");
|
ThrowJSException(cx, "Memory allocation failed for NPIdentifier!");
|
||||||
|
|
||||||
::JS_DestroyIdArray(cx, ida);
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (PRUint32 i = 0; i < *count; i++) {
|
for (PRUint32 i = 0; i < *count; i++) {
|
||||||
jsval v;
|
jsval v;
|
||||||
if (!::JS_IdToValue(cx, ida->vector[i], &v)) {
|
if (!JS_IdToValue(cx, ida[i], &v)) {
|
||||||
::JS_DestroyIdArray(cx, ida);
|
|
||||||
PR_Free(*idarray);
|
PR_Free(*idarray);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -993,10 +989,8 @@ nsJSObjWrapper::NP_Enumerate(NPObject *npobj, NPIdentifier **idarray,
|
||||||
if (JSVAL_IS_STRING(v)) {
|
if (JSVAL_IS_STRING(v)) {
|
||||||
JSString *str = JS_InternJSString(cx, JSVAL_TO_STRING(v));
|
JSString *str = JS_InternJSString(cx, JSVAL_TO_STRING(v));
|
||||||
if (!str) {
|
if (!str) {
|
||||||
::JS_DestroyIdArray(cx, ida);
|
PR_Free(*idarray);
|
||||||
PR_Free(*idarray);
|
return false;
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
id = StringToNPIdentifier(cx, str);
|
id = StringToNPIdentifier(cx, str);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1008,8 +1002,6 @@ nsJSObjWrapper::NP_Enumerate(NPObject *npobj, NPIdentifier **idarray,
|
||||||
(*idarray)[i] = id;
|
(*idarray)[i] = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
::JS_DestroyIdArray(cx, ida);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,6 @@
|
||||||
* ***** END LICENSE BLOCK ***** */
|
* ***** END LICENSE BLOCK ***** */
|
||||||
|
|
||||||
#include "base/basictypes.h"
|
#include "base/basictypes.h"
|
||||||
#include "jscntxt.h"
|
|
||||||
|
|
||||||
#include "mozilla/jsipc/ContextWrapperChild.h"
|
#include "mozilla/jsipc/ContextWrapperChild.h"
|
||||||
#include "mozilla/jsipc/ObjectWrapperChild.h"
|
#include "mozilla/jsipc/ObjectWrapperChild.h"
|
||||||
|
|
|
@ -3471,6 +3471,57 @@ JS_IdArrayGet(JSContext *cx, JSIdArray *ida, jsint index);
|
||||||
extern JS_PUBLIC_API(void)
|
extern JS_PUBLIC_API(void)
|
||||||
JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
|
JS_DestroyIdArray(JSContext *cx, JSIdArray *ida);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
class AutoIdArray : private AutoGCRooter {
|
||||||
|
public:
|
||||||
|
AutoIdArray(JSContext *cx, JSIdArray *ida JS_GUARD_OBJECT_NOTIFIER_PARAM)
|
||||||
|
: AutoGCRooter(cx, IDARRAY), idArray(ida)
|
||||||
|
{
|
||||||
|
JS_GUARD_OBJECT_NOTIFIER_INIT;
|
||||||
|
}
|
||||||
|
~AutoIdArray() {
|
||||||
|
if (idArray)
|
||||||
|
JS_DestroyIdArray(context, idArray);
|
||||||
|
}
|
||||||
|
bool operator!() {
|
||||||
|
return !idArray;
|
||||||
|
}
|
||||||
|
jsid operator[](size_t i) const {
|
||||||
|
JS_ASSERT(idArray);
|
||||||
|
JS_ASSERT(i < length());
|
||||||
|
return JS_IdArrayGet(context, idArray, i);
|
||||||
|
}
|
||||||
|
size_t length() const {
|
||||||
|
return JS_IdArrayLength(context, idArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
friend void AutoGCRooter::trace(JSTracer *trc);
|
||||||
|
|
||||||
|
JSIdArray *steal() {
|
||||||
|
JSIdArray *copy = idArray;
|
||||||
|
idArray = NULL;
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
inline void trace(JSTracer *trc);
|
||||||
|
|
||||||
|
private:
|
||||||
|
JSIdArray *idArray;
|
||||||
|
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
|
||||||
|
|
||||||
|
/* No copy or assignment semantics. */
|
||||||
|
AutoIdArray(AutoIdArray &ida) MOZ_DELETE;
|
||||||
|
void operator=(AutoIdArray &ida) MOZ_DELETE;
|
||||||
|
};
|
||||||
|
|
||||||
|
} /* namespace JS */
|
||||||
|
|
||||||
|
#endif /* __cplusplus */
|
||||||
|
|
||||||
extern JS_PUBLIC_API(JSBool)
|
extern JS_PUBLIC_API(JSBool)
|
||||||
JS_ValueToId(JSContext *cx, jsval v, jsid *idp);
|
JS_ValueToId(JSContext *cx, jsval v, jsid *idp);
|
||||||
|
|
||||||
|
|
|
@ -1598,48 +1598,6 @@ typedef RootedVar<jsid> RootedVarId;
|
||||||
typedef RootedVar<Value> RootedVarValue;
|
typedef RootedVar<Value> RootedVarValue;
|
||||||
|
|
||||||
/* FIXME(bug 332648): Move this into a public header. */
|
/* FIXME(bug 332648): Move this into a public header. */
|
||||||
class AutoIdArray : private AutoGCRooter {
|
|
||||||
public:
|
|
||||||
AutoIdArray(JSContext *cx, JSIdArray *ida JS_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
||||||
: AutoGCRooter(cx, IDARRAY), idArray(ida)
|
|
||||||
{
|
|
||||||
JS_GUARD_OBJECT_NOTIFIER_INIT;
|
|
||||||
}
|
|
||||||
~AutoIdArray() {
|
|
||||||
if (idArray)
|
|
||||||
JS_DestroyIdArray(context, idArray);
|
|
||||||
}
|
|
||||||
bool operator!() {
|
|
||||||
return idArray == NULL;
|
|
||||||
}
|
|
||||||
jsid operator[](size_t i) const {
|
|
||||||
JS_ASSERT(idArray);
|
|
||||||
JS_ASSERT(i < size_t(idArray->length));
|
|
||||||
return idArray->vector[i];
|
|
||||||
}
|
|
||||||
size_t length() const {
|
|
||||||
return idArray->length;
|
|
||||||
}
|
|
||||||
|
|
||||||
friend void AutoGCRooter::trace(JSTracer *trc);
|
|
||||||
|
|
||||||
JSIdArray *steal() {
|
|
||||||
JSIdArray *copy = idArray;
|
|
||||||
idArray = NULL;
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
inline void trace(JSTracer *trc);
|
|
||||||
|
|
||||||
private:
|
|
||||||
JSIdArray * idArray;
|
|
||||||
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
||||||
|
|
||||||
AutoIdArray(AutoIdArray &ida) MOZ_DELETE;
|
|
||||||
void operator=(AutoIdArray &ida) MOZ_DELETE;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* The auto-root for enumeration object and its state. */
|
/* The auto-root for enumeration object and its state. */
|
||||||
class AutoEnumStateRooter : private AutoGCRooter
|
class AutoEnumStateRooter : private AutoGCRooter
|
||||||
{
|
{
|
||||||
|
|
|
@ -1963,7 +1963,7 @@ AutoGCRooter::trace(JSTracer *trc)
|
||||||
|
|
||||||
case IDARRAY: {
|
case IDARRAY: {
|
||||||
JSIdArray *ida = static_cast<AutoIdArray *>(this)->idArray;
|
JSIdArray *ida = static_cast<AutoIdArray *>(this)->idArray;
|
||||||
MarkIdRange(trc, ida->vector, ida->vector + ida->length, "js::AutoIdArray.idArray");
|
MarkIdRange(trc, ida->vector, ida->vector + ida->length, "JS::AutoIdArray.idArray");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,8 +61,6 @@
|
||||||
#include "jsgc.h"
|
#include "jsgc.h"
|
||||||
#include "jsfriendapi.h"
|
#include "jsfriendapi.h"
|
||||||
|
|
||||||
#include "jscntxt.h" // AutoIdArray
|
|
||||||
|
|
||||||
using namespace mozilla;
|
using namespace mozilla;
|
||||||
using namespace js;
|
using namespace js;
|
||||||
/***************************************************************************/
|
/***************************************************************************/
|
||||||
|
@ -3758,7 +3756,7 @@ nsXPCComponents_Utils::MakeObjectPropsNormal(const jsval &vobj, JSContext *cx)
|
||||||
if (!ac.enter(cx, obj))
|
if (!ac.enter(cx, obj))
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
||||||
js::AutoIdArray ida(cx, JS_Enumerate(cx, obj));
|
JS::AutoIdArray ida(cx, JS_Enumerate(cx, obj));
|
||||||
if (!ida)
|
if (!ida)
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@
|
||||||
#include "AccessCheck.h"
|
#include "AccessCheck.h"
|
||||||
#include "nsJSUtils.h"
|
#include "nsJSUtils.h"
|
||||||
|
|
||||||
#include "jscntxt.h" // mJSContext->errorReporter, JSIdArray, js::AutoValueVector
|
#include "jscntxt.h" // mJSContext->errorReporter, js::AutoValueVector
|
||||||
|
|
||||||
NS_IMPL_THREADSAFE_ISUPPORTS1(nsXPCWrappedJSClass, nsIXPCWrappedJSClass)
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsXPCWrappedJSClass, nsIXPCWrappedJSClass)
|
||||||
|
|
||||||
|
@ -405,60 +405,49 @@ nsXPCWrappedJSClass::BuildPropertyEnumerator(XPCCallContext& ccx,
|
||||||
nsISimpleEnumerator** aEnumerate)
|
nsISimpleEnumerator** aEnumerate)
|
||||||
{
|
{
|
||||||
JSContext* cx = ccx.GetJSContext();
|
JSContext* cx = ccx.GetJSContext();
|
||||||
nsresult retval = NS_ERROR_FAILURE;
|
|
||||||
JSIdArray* idArray = nsnull;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
// Saved state must be restored, all exits through 'out'...
|
|
||||||
AutoScriptEvaluate scriptEval(cx);
|
AutoScriptEvaluate scriptEval(cx);
|
||||||
if (!scriptEval.StartEvaluating(aJSObj))
|
if (!scriptEval.StartEvaluating(aJSObj))
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
||||||
idArray = JS_Enumerate(cx, aJSObj);
|
JS::AutoIdArray idArray(cx, JS_Enumerate(cx, aJSObj));
|
||||||
if (!idArray)
|
if (!idArray)
|
||||||
return retval;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
||||||
|
nsCOMArray<nsIProperty> propertyArray(idArray.length());
|
||||||
|
for (size_t i = 0; i < idArray.length(); i++) {
|
||||||
|
jsid idName = idArray[i];
|
||||||
|
|
||||||
nsCOMArray<nsIProperty> propertyArray(idArray->length);
|
|
||||||
for (i = 0; i < idArray->length; i++) {
|
|
||||||
nsCOMPtr<nsIVariant> value;
|
nsCOMPtr<nsIVariant> value;
|
||||||
jsid idName = idArray->vector[i];
|
|
||||||
nsresult rv;
|
nsresult rv;
|
||||||
|
|
||||||
if (!GetNamedPropertyAsVariantRaw(ccx, aJSObj, idName,
|
if (!GetNamedPropertyAsVariantRaw(ccx, aJSObj, idName,
|
||||||
getter_AddRefs(value), &rv)) {
|
getter_AddRefs(value), &rv)) {
|
||||||
if (NS_FAILED(rv))
|
if (NS_FAILED(rv))
|
||||||
retval = rv;
|
return rv;
|
||||||
goto out;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
jsval jsvalName;
|
jsval jsvalName;
|
||||||
if (!JS_IdToValue(cx, idName, &jsvalName))
|
if (!JS_IdToValue(cx, idName, &jsvalName))
|
||||||
goto out;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
||||||
JSString* name = JS_ValueToString(cx, jsvalName);
|
JSString* name = JS_ValueToString(cx, jsvalName);
|
||||||
if (!name)
|
if (!name)
|
||||||
goto out;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
||||||
size_t length;
|
size_t length;
|
||||||
const jschar *chars = JS_GetStringCharsAndLength(cx, name, &length);
|
const jschar *chars = JS_GetStringCharsAndLength(cx, name, &length);
|
||||||
if (!chars)
|
if (!chars)
|
||||||
goto out;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
||||||
nsCOMPtr<nsIProperty> property =
|
nsCOMPtr<nsIProperty> property =
|
||||||
new xpcProperty(chars, (PRUint32) length, value);
|
new xpcProperty(chars, (PRUint32) length, value);
|
||||||
if (!property)
|
|
||||||
goto out;
|
|
||||||
|
|
||||||
if (!propertyArray.AppendObject(property))
|
if (!propertyArray.AppendObject(property))
|
||||||
goto out;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = NS_NewArrayEnumerator(aEnumerate, propertyArray);
|
return NS_NewArrayEnumerator(aEnumerate, propertyArray);
|
||||||
|
|
||||||
out:
|
|
||||||
JS_DestroyIdArray(cx, idArray);
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/***************************************************************************/
|
/***************************************************************************/
|
||||||
|
|
Загрузка…
Ссылка в новой задаче