Skip to content

Commit

Permalink
Bug 332648 - Part g: Move AutoIdArray to jsapi.h; r=evilpie
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Jan 11, 2012
1 parent 293542c commit 0397f60
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 92 deletions.
11 changes: 4 additions & 7 deletions content/html/content/src/nsHTMLCanvasElement.cpp
Expand Up @@ -501,9 +501,9 @@ nsHTMLCanvasElement::GetContext(const nsAString& aContextId,
contextProps = do_CreateInstance("@mozilla.org/hash-property-bag;1");

JSObject *opts = JSVAL_TO_OBJECT(aContextOptions);
JSIdArray *props = JS_Enumerate(cx, opts);
for (int i = 0; props && i < JS_IdArrayLength(cx, props); ++i) {
jsid propid = JS_IdArrayGet(cx, props, i);
JS::AutoIdArray props(cx, JS_Enumerate(cx, opts));
for (size_t i = 0; !!props && i < props.length(); ++i) {
jsid propid = props[i];
jsval propname, propval;
if (!JS_IdToValue(cx, propid, &propname) ||
!JS_GetPropertyById(cx, opts, propid, &propval)) {
Expand All @@ -513,13 +513,12 @@ nsHTMLCanvasElement::GetContext(const nsAString& aContextId,
JSString *propnameString = JS_ValueToString(cx, propname);
nsDependentJSString pstr;
if (!propnameString || !pstr.init(cx, propnameString)) {
JS_DestroyIdArray(cx, props);
mCurrentContext = nsnull;
return NS_ERROR_FAILURE;
}

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)) {
contextProps->SetPropertyAsInt32(pstr, JSVAL_TO_INT(propval));
} else if (JSVAL_IS_DOUBLE(propval)) {
Expand All @@ -528,15 +527,13 @@ nsHTMLCanvasElement::GetContext(const nsAString& aContextId,
JSString *propvalString = JS_ValueToString(cx, propval);
nsDependentJSString vstr;
if (!propvalString || !vstr.init(cx, propvalString)) {
JS_DestroyIdArray(cx, props);
mCurrentContext = nsnull;
return NS_ERROR_FAILURE;
}

contextProps->SetPropertyAsAString(pstr, vstr);
}
}
JS_DestroyIdArray(cx, props);
}
}

Expand Down
18 changes: 5 additions & 13 deletions dom/plugins/base/nsJSNPRuntime.cpp
Expand Up @@ -966,25 +966,21 @@ nsJSObjWrapper::NP_Enumerate(NPObject *npobj, NPIdentifier **idarray,
if (!ac.enter(cx, npjsobj->mJSObj))
return false;

JSIdArray *ida = ::JS_Enumerate(cx, npjsobj->mJSObj);
JS::AutoIdArray ida(cx, JS_Enumerate(cx, npjsobj->mJSObj));
if (!ida) {
return false;
}

*count = ida->length;
*count = ida.length();
*idarray = (NPIdentifier *)PR_Malloc(*count * sizeof(NPIdentifier));
if (!*idarray) {
ThrowJSException(cx, "Memory allocation failed for NPIdentifier!");

::JS_DestroyIdArray(cx, ida);

return false;
}

for (PRUint32 i = 0; i < *count; i++) {
jsval v;
if (!::JS_IdToValue(cx, ida->vector[i], &v)) {
::JS_DestroyIdArray(cx, ida);
if (!JS_IdToValue(cx, ida[i], &v)) {
PR_Free(*idarray);
return false;
}
Expand All @@ -993,10 +989,8 @@ nsJSObjWrapper::NP_Enumerate(NPObject *npobj, NPIdentifier **idarray,
if (JSVAL_IS_STRING(v)) {
JSString *str = JS_InternJSString(cx, JSVAL_TO_STRING(v));
if (!str) {
::JS_DestroyIdArray(cx, ida);
PR_Free(*idarray);

return false;
PR_Free(*idarray);
return false;
}
id = StringToNPIdentifier(cx, str);
} else {
Expand All @@ -1008,8 +1002,6 @@ nsJSObjWrapper::NP_Enumerate(NPObject *npobj, NPIdentifier **idarray,
(*idarray)[i] = id;
}

::JS_DestroyIdArray(cx, ida);

return true;
}

Expand Down
1 change: 0 additions & 1 deletion js/ipc/ObjectWrapperChild.cpp
Expand Up @@ -39,7 +39,6 @@
* ***** END LICENSE BLOCK ***** */

#include "base/basictypes.h"
#include "jscntxt.h"

#include "mozilla/jsipc/ContextWrapperChild.h"
#include "mozilla/jsipc/ObjectWrapperChild.h"
Expand Down
51 changes: 51 additions & 0 deletions js/src/jsapi.h
Expand Up @@ -3471,6 +3471,57 @@ JS_IdArrayGet(JSContext *cx, JSIdArray *ida, jsint index);
extern JS_PUBLIC_API(void)
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)
JS_ValueToId(JSContext *cx, jsval v, jsid *idp);

Expand Down
42 changes: 0 additions & 42 deletions js/src/jscntxt.h
Expand Up @@ -1598,48 +1598,6 @@ typedef RootedVar<jsid> RootedVarId;
typedef RootedVar<Value> RootedVarValue;

/* 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. */
class AutoEnumStateRooter : private AutoGCRooter
{
Expand Down
2 changes: 1 addition & 1 deletion js/src/jsgc.cpp
Expand Up @@ -1963,7 +1963,7 @@ AutoGCRooter::trace(JSTracer *trc)

case 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;
}

Expand Down
4 changes: 1 addition & 3 deletions js/xpconnect/src/XPCComponents.cpp
Expand Up @@ -61,8 +61,6 @@
#include "jsgc.h"
#include "jsfriendapi.h"

#include "jscntxt.h" // AutoIdArray

using namespace mozilla;
using namespace js;
/***************************************************************************/
Expand Down Expand Up @@ -3758,7 +3756,7 @@ nsXPCComponents_Utils::MakeObjectPropsNormal(const jsval &vobj, JSContext *cx)
if (!ac.enter(cx, obj))
return NS_ERROR_FAILURE;

js::AutoIdArray ida(cx, JS_Enumerate(cx, obj));
JS::AutoIdArray ida(cx, JS_Enumerate(cx, obj));
if (!ida)
return NS_ERROR_FAILURE;

Expand Down
39 changes: 14 additions & 25 deletions js/xpconnect/src/XPCWrappedJSClass.cpp
Expand Up @@ -49,7 +49,7 @@
#include "AccessCheck.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)

Expand Down Expand Up @@ -405,60 +405,49 @@ nsXPCWrappedJSClass::BuildPropertyEnumerator(XPCCallContext& ccx,
nsISimpleEnumerator** aEnumerate)
{
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);
if (!scriptEval.StartEvaluating(aJSObj))
return NS_ERROR_FAILURE;

idArray = JS_Enumerate(cx, aJSObj);
JS::AutoIdArray idArray(cx, JS_Enumerate(cx, aJSObj));
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;
jsid idName = idArray->vector[i];
nsresult rv;

if (!GetNamedPropertyAsVariantRaw(ccx, aJSObj, idName,
getter_AddRefs(value), &rv)) {
if (NS_FAILED(rv))
retval = rv;
goto out;
return rv;
return NS_ERROR_FAILURE;
}

jsval jsvalName;
if (!JS_IdToValue(cx, idName, &jsvalName))
goto out;
return NS_ERROR_FAILURE;

JSString* name = JS_ValueToString(cx, jsvalName);
if (!name)
goto out;
return NS_ERROR_FAILURE;

size_t length;
const jschar *chars = JS_GetStringCharsAndLength(cx, name, &length);
if (!chars)
goto out;
return NS_ERROR_FAILURE;

nsCOMPtr<nsIProperty> property =
new xpcProperty(chars, (PRUint32) length, value);
if (!property)
goto out;

if (!propertyArray.AppendObject(property))
goto out;
return NS_ERROR_FAILURE;
}

retval = NS_NewArrayEnumerator(aEnumerate, propertyArray);

out:
JS_DestroyIdArray(cx, idArray);

return retval;
return NS_NewArrayEnumerator(aEnumerate, propertyArray);
}

/***************************************************************************/
Expand Down

0 comments on commit 0397f60

Please sign in to comment.