Expose Map and Set size

RESOLVED FIXED in mozilla13

Status

()

Core
JavaScript Engine
RESOLVED FIXED
5 years ago
5 years ago

People

(Reporter: jorendorff, Assigned: jorendorff)

Tracking

(Blocks: 1 bug, {dev-doc-complete})

Other Branch
mozilla13
dev-doc-complete
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Assignee)

Description

5 years ago
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().
(Assignee)

Comment 1

5 years ago
Created attachment 596323 [details] [diff] [review]
v1
Assignee: general → jorendorff
Attachment #596323 - Flags: review?(luke)

Comment 2

5 years ago
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+
(Assignee)

Comment 3

5 years ago
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.

Comment 4

5 years ago
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
Last Resolved: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla13
https://hg.mozilla.org/mozilla-central/rev/2d534a06e561
Keywords: dev-doc-needed
Added the method description and added it to the example code.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Map an Set
Keywords: dev-doc-needed → dev-doc-complete
You need to log in before you can comment on or make changes to this bug.