Last Comment Bug 726298 - Expose Map and Set size
: Expose Map and Set size
: dev-doc-complete
Product: Core
Classification: Components
Component: JavaScript Engine (show other bugs)
: Other Branch
: All All
: -- normal (vote)
: mozilla13
Assigned To: Jason Orendorff [:jorendorff]
: Jason Orendorff [:jorendorff]
Depends on:
Blocks: es6
  Show dependency treegraph
Reported: 2012-02-11 06:41 PST by Jason Orendorff [:jorendorff]
Modified: 2012-04-28 10:38 PDT (History)
5 users (show)
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---

v1 (9.00 KB, patch)
2012-02-11 07:13 PST, Jason Orendorff [:jorendorff]
luke: review+
Details | Diff | Splinter Review

Description Jason Orendorff [:jorendorff] 2012-02-11 06:41:23 PST
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().
Comment 1 Jason Orendorff [:jorendorff] 2012-02-11 07:13:39 PST
Created attachment 596323 [details] [diff] [review]
Comment 2 Luke Wagner [:luke] 2012-02-20 12:04:57 PST
Comment on attachment 596323 [details] [diff] [review]

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?
Comment 3 Jason Orendorff [:jorendorff] 2012-02-25 13:25:51 PST
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 Luke Wagner [:luke] 2012-02-25 13:35:55 PST
Oops, didn't even realize that; that's a bug; HashTable::count returns a uint32_t.  Yes, changing them sounds good.
Comment 5 Marco Bonardo [::mak] 2012-03-02 06:20:34 PST
Comment 6 Marco Bonardo [::mak] 2012-03-02 06:28:04 PST
Comment 7 Tom Schuster [:evilpie] 2012-04-28 10:38:58 PDT
Added the method description and added it to the example code. an Set

Note You need to log in before you can comment on or make changes to this bug.