Bug 898371 - Enumerate binary structs. r=nsm
authorNiko Matsakis <niko@alum.mit.edu>
Fri, 26 Jul 2013 11:39:33 -0700
changeset 140202 97ad9f8fd485505e77d0413a987ab7207d924373
parent 140201 9bdc850e67f6743cb63d56aa9c775a91dc84235c
child 140203 a04093b3aaa4bcffa8efae07d9f854bc6c74c103
push id1945
push userryanvm@gmail.com
push dateSat, 27 Jul 2013 02:27:26 +0000
treeherderfx-team@4874fa438b1c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnsm
bugs898371
milestone25.0a1
Bug 898371 - Enumerate binary structs. r=nsm
js/src/builtin/BinaryData.cpp
js/src/builtin/BinaryData.h
js/src/tests/ecma_6/BinaryData/structtype.js
--- a/js/src/builtin/BinaryData.cpp
+++ b/js/src/builtin/BinaryData.cpp
@@ -1341,17 +1341,17 @@ BinaryArray::obj_getGenericAttributes(JS
         return true;
     }
 
     if (JSID_IS_ATOM(id, cx->names().length)) {
         *attrsp = JSPROP_READONLY | JSPROP_PERMANENT;
         return true;
     }
 
-	return false;
+    return false;
 }
 
 JSBool
 BinaryArray::obj_getPropertyAttributes(JSContext *cx, HandleObject obj,
                                         HandlePropertyName name,
                                         unsigned *attrsp)
 {
     RootedId id(cx, NameToId(name));
@@ -1450,46 +1450,46 @@ Class BinaryStruct::class_ = {
     BinaryStruct::finalize,
     NULL,           /* checkAccess */
     NULL,           /* call        */
     NULL,           /* construct   */
     NULL,           /* hasInstance */
     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 */
     }
 };
 
 /*
  * NOTE: layout() does not check for duplicates in fields since the arguments
  * to StructType are currently passed as an object literal. Fix this if it
  * changes to taking an array of arrays.
  */
@@ -1813,16 +1813,55 @@ BinaryStruct::obj_trace(JSTracer *tracer
         MarkObject(tracer, &owner, "binarystruct.blockRefOwner");
     }
 
     HeapPtrObject type(obj->getFixedSlot(SLOT_DATATYPE).toObjectOrNull());
     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,
                              MutableHandleValue vp)
 {
     if (!IsBinaryStruct(obj)) {
         char *valueStr = JS_EncodeString(cx, JS_ValueToString(cx, ObjectValue(*obj)));
         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
                 JSMSG_BINARYDATA_NOT_BINARYSTRUCT, valueStr);
--- a/js/src/builtin/BinaryData.h
+++ b/js/src/builtin/BinaryData.h
@@ -290,16 +290,21 @@ class BinaryStruct : public JSObject
 
     static JSObject *create(JSContext *cx, HandleObject type,
                             HandleObject owner, size_t offset);
     static JSBool construct(JSContext *cx, unsigned int argc, jsval *vp);
 
     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);
 
     static JSBool obj_getProperty(JSContext *cx, HandleObject obj,
                                   HandleObject receiver,
                                   HandlePropertyName name,
                                   MutableHandleValue vp);
--- a/js/src/tests/ecma_6/BinaryData/structtype.js
+++ b/js/src/tests/ecma_6/BinaryData/structtype.js
@@ -46,16 +46,21 @@ function runTests() {
     civic.color = white;
     civic.weight = 1000;
 
     assertEq(civic.weight, 1000);
     assertEq(civic.color.r, 255);
     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);
     assertEq(civic.color.b, 0);
 
     assertThrows(function() civic.color = 5);
     assertThrows(function() civic.color = []);
     assertThrows(function() civic.color = {});