Revision: 12909
Author:   [email protected]
Date:     Fri Nov  9 01:01:29 2012
Log:      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=v8:2400

Review URL: https://codereview.chromium.org/11409002
Patch from Erik Arvidsson <[email protected]>.
http://code.google.com/p/v8/source/detail?r=12909

Modified:
 /branches/bleeding_edge/src/collection.js
 /branches/bleeding_edge/test/mjsunit/harmony/collections.js

=======================================
--- /branches/bleeding_edge/src/collection.js   Tue Nov  6 10:14:45 2012
+++ /branches/bleeding_edge/src/collection.js   Fri Nov  9 01:01:29 2012
@@ -95,6 +95,16 @@
   }
   return %SetGetSize(this);
 }
+
+
+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() {
@@ -161,6 +171,16 @@
   }
   return %MapGetSize(this);
 }
+
+
+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() {
@@ -237,7 +257,8 @@
   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 @@
     "get", MapGet,
     "set", MapSet,
     "has", MapHas,
-    "delete", MapDelete
+    "delete", MapDelete,
+    "clear", MapClear
   ));

   // Set up the WeakMap constructor function.
=======================================
--- /branches/bleeding_edge/test/mjsunit/harmony/collections.js Tue Nov 6 10:14:45 2012 +++ /branches/bleeding_edge/test/mjsunit/harmony/collections.js Fri Nov 9 01:01:29 2012
@@ -355,3 +355,18 @@
   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