Closed Bug 726298 Opened 12 years ago Closed 12 years ago

Expose Map and Set size

Categories

(Core :: JavaScript Engine, defect)

Other Branch
defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla13

People

(Reporter: jorendorff, Assigned: jorendorff)

References

(Blocks 1 open bug)

Details

(Keywords: dev-doc-complete)

Attachments

(1 file)

There is currently no way to ask how many entries are in a Map or Set.

TC39 apparently did not reach a consensus at the last meeting as to what this method or accessor should be called. I don't have an opinion. I'm going with size().
Attached patch v1Splinter Review
Assignee: general → jorendorff
Attachment #596323 - Flags: review?(luke)
Comment on attachment 596323 [details] [diff] [review]
v1

Review of attachment 596323 [details] [diff] [review]:
-----------------------------------------------------------------

Sorry for the delay.

::: js/src/builtin/MapObject.cpp
@@ +246,5 @@
>  JSBool
> +MapObject::size(JSContext *cx, uintN argc, Value *vp)
> +{
> +    THIS_MAP(get, cx, argc, vp, args, map);
> +    args.rval().setNumber(double(map.count()));

How about a

  JS_STATIC_ASSERT(sizeof map.count() <= sizeof uint32_t);

here and in SetObject::size?
Attachment #596323 - Flags: review?(luke) → review+
Hmm. Currently we have:

   uint32_t js::detail::HashTable<>::count() const;
   size_t js::HashMap<>::count() const { return impl.count(); }
   size_t js::HashSet<>::count() const { return impl.count(); }

So, do you mean I should change the latter two to return uint32_t? If so, then I can add the static assertions you mention and remove the 'double()' part.
Oops, didn't even realize that; that's a bug; HashTable::count returns a uint32_t.  Yes, changing them sounds good.
https://hg.mozilla.org/mozilla-central/rev/3f51e7052b7d
Status: NEW → RESOLVED
Closed: 12 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla13
Added the method description and added it to the example code.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Map an Set
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: