Reviewers: Michael Starzinger, rossberg,

https://codereview.chromium.org/11409002/diff/1/src/collection.js
File src/collection.js (right):

https://codereview.chromium.org/11409002/diff/1/src/collection.js#newcode106
src/collection.js:106: %SetInitialize(this);
I first added a %SetClear before I realized that it would do exactly the
same as %SetInitialize. It might be less confusing to have SetClear?
WDYT?

Description:
ES6: Add support for Set and Map clear method

http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts, section
15.14.5.3 and 15.14.5.2

BUG=2400


Please review this at https://codereview.chromium.org/11409002/

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/collection.js
  M test/mjsunit/harmony/collections.js


Index: src/collection.js
diff --git a/src/collection.js b/src/collection.js
index d5a8fe21a456c3d99bd6572853a9dddc82a04fcd..b3c2db72d792732be2ae8f8caf9554df8c34ffaa 100644
--- a/src/collection.js
+++ b/src/collection.js
@@ -97,6 +97,16 @@ function SetGetSize() {
 }


+function SetClear() {
+  if (!IS_SET(this)) {
+    throw MakeTypeError('incompatible_method_receiver',
+                        ['Set.prototype.clear', this]);
+  }
+  // Replace the internal table with a new empty table.
+  %SetInitialize(this);
+}
+
+
 function MapConstructor() {
   if (%_IsConstructCall()) {
     %MapInitialize(this);
@@ -163,6 +173,16 @@ function MapGetSize() {
 }


+function MapClear() {
+  if (!IS_MAP(this)) {
+    throw MakeTypeError('incompatible_method_receiver',
+                        ['Map.prototype.clear', this]);
+  }
+  // Replace the internal table with a new empty table.
+  %MapInitialize(this);
+}
+
+
 function WeakMapConstructor() {
   if (%_IsConstructCall()) {
     %WeakMapInitialize(this);
@@ -237,7 +257,8 @@ function WeakMapDelete(key) {
   InstallFunctions($Set.prototype, DONT_ENUM, $Array(
     "add", SetAdd,
     "has", SetHas,
-    "delete", SetDelete
+    "delete", SetDelete,
+    "clear", SetClear
   ));

   // Set up the non-enumerable functions on the Map prototype object.
@@ -246,7 +267,8 @@ function WeakMapDelete(key) {
     "get", MapGet,
     "set", MapSet,
     "has", MapHas,
-    "delete", MapDelete
+    "delete", MapDelete,
+    "clear", MapClear
   ));

   // Set up the WeakMap constructor function.
Index: test/mjsunit/harmony/collections.js
diff --git a/test/mjsunit/harmony/collections.js b/test/mjsunit/harmony/collections.js index 6698a334882f53ae80653b6491cbd2e3f521a313..0219f39364d97a6859037a3f48de87dcefcd8ae3 100644
--- a/test/mjsunit/harmony/collections.js
+++ b/test/mjsunit/harmony/collections.js
@@ -355,3 +355,18 @@ for (var i = 9; i >= 0; i--) {
   m.delete(i);
   assertEquals(i, m.size);
 }
+
+// Test clear
+var a = new Set();
+s.add(42);
+assertTrue(s.has(42));
+s.clear();
+assertFalse(s.has(42));
+assertEquals(0, s.size);
+
+var m = new Map();
+m.set(42, true);
+assertTrue(m.has(42));
+m.clear();
+assertFalse(m.has(42));
+assertEquals(0, m.size);


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to