From b1696a9abf3410a6c150f418bc417d04677ae6b3 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 26 Jul 2013 11:39:33 -0700 Subject: [PATCH] Bug 898371 - Enumerate binary structs. r=nsm --- js/src/builtin/BinaryData.cpp | 91 ++++++++++++++------ js/src/builtin/BinaryData.h | 5 ++ js/src/tests/ecma_6/BinaryData/structtype.js | 5 ++ 3 files changed, 75 insertions(+), 26 deletions(-) diff --git a/js/src/builtin/BinaryData.cpp b/js/src/builtin/BinaryData.cpp index 96b72f387d9..f2547ed6734 100644 --- a/js/src/builtin/BinaryData.cpp +++ b/js/src/builtin/BinaryData.cpp @@ -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(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(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, diff --git a/js/src/builtin/BinaryData.h b/js/src/builtin/BinaryData.h index cf18c3b4615..ef8954a428e 100644 --- a/js/src/builtin/BinaryData.h +++ b/js/src/builtin/BinaryData.h @@ -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); diff --git a/js/src/tests/ecma_6/BinaryData/structtype.js b/js/src/tests/ecma_6/BinaryData/structtype.js index 70e744efa13..aa00f249add 100644 --- a/js/src/tests/ecma_6/BinaryData/structtype.js +++ b/js/src/tests/ecma_6/BinaryData/structtype.js @@ -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);