Bug 898371 - Enumerate binary structs. r=nsm

This commit is contained in:
Niko Matsakis 2013-07-26 11:39:33 -07:00
parent 6753f28dcc
commit b1696a9abf
3 changed files with 75 additions and 26 deletions

View File

@ -1346,7 +1346,7 @@ BinaryArray::obj_getGenericAttributes(JSContext *cx, HandleObject obj,
return true;
}
return false;
return false;
}
JSBool
@ -1455,36 +1455,36 @@ Class BinaryStruct::class_ = {
BinaryStruct::obj_trace,
JS_NULL_CLASS_EXT,
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL, /* lookupGeneric */
NULL, /* lookupProperty */
NULL, /* lookupElement */
NULL, /* lookupSpecial */
NULL, /* defineGeneric */
NULL, /* defineProperty */
NULL, /* defineElement */
NULL, /* defineSpecial */
BinaryStruct::obj_getGeneric,
BinaryStruct::obj_getProperty,
NULL,
NULL,
NULL, /* getElement */
NULL, /* getElementIfPresent */
BinaryStruct::obj_getSpecial,
BinaryStruct::obj_setGeneric,
BinaryStruct::obj_setProperty,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL, /* setElement */
NULL, /* setSpecial */
NULL, /* getGenericAttributes */
NULL, /* getPropertyAttributes */
NULL, /* getElementAttributes */
NULL, /* getSpecialAttributes */
NULL, /* setGenericAttributes */
NULL, /* setPropertyAttributes */
NULL, /* setElementAttributes */
NULL, /* setSpecialAttributes */
NULL, /* deleteProperty */
NULL, /* deleteElement */
NULL, /* deleteSpecial */
BinaryStruct::obj_enumerate,
NULL, /* thisObject */
}
};
@ -1817,6 +1817,45 @@ BinaryStruct::obj_trace(JSTracer *tracer, JSObject *obj)
MarkObject(tracer, &type, "binarystruct.type");
}
JSBool
BinaryStruct::obj_enumerate(JSContext *cx, HandleObject obj, JSIterateOp enum_op,
MutableHandleValue statep, MutableHandleId idp)
{
JS_ASSERT(IsBinaryStruct(obj));
RootedObject type(cx, GetType(obj));
FieldList *fieldList = static_cast<FieldList *>(type->getPrivate());
JS_ASSERT(fieldList);
uint32_t index;
switch (enum_op) {
case JSENUMERATE_INIT_ALL:
case JSENUMERATE_INIT:
statep.setInt32(0);
idp.set(INT_TO_JSID(fieldList->size()));
break;
case JSENUMERATE_NEXT:
index = static_cast<uint32_t>(statep.toInt32());
if (index < fieldList->size()) {
idp.set(fieldList->at(index).name);
statep.setInt32(index + 1);
} else {
statep.setNull();
}
break;
case JSENUMERATE_DESTROY:
statep.setNull();
break;
}
return true;
}
JSBool
BinaryStruct::obj_getGeneric(JSContext *cx, HandleObject obj,
HandleObject receiver, HandleId id,

View File

@ -295,6 +295,11 @@ class BinaryStruct : public JSObject
static void finalize(js::FreeOp *op, JSObject *obj);
static void obj_trace(JSTracer *tracer, JSObject *obj);
static JSBool obj_enumerate(JSContext *cx, HandleObject obj,
JSIterateOp enum_op,
MutableHandleValue statep,
MutableHandleId idp);
static JSBool obj_getGeneric(JSContext *cx, HandleObject obj,
HandleObject receiver, HandleId id,
MutableHandleValue vp);

View File

@ -51,6 +51,11 @@ function runTests() {
assertEq(civic.color.g, 255);
assertEq(civic.color.b, 255);
var keys = Object.keys(civic).sort();
assertEq(keys.length, 2);
assertEq(keys.indexOf("color"), 0);
assertEq(keys.indexOf("weight"), 1);
civic.color = {r: 255, g: 0, b: 0};
assertEq(civic.color.r, 255);
assertEq(civic.color.g, 0);