Reviewers: rossberg,

Description:
Implement WeakMap.prototype.clear function.

[email protected]
BUG=v8:2753
TEST=mjsunit/harmony/collections

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

SVN Base: https://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 950c7e73742f8ace234ed4cdb86a81647d6a8632..c5604ab30f359b1287a664ed22d3656d8843b075 100644
--- a/src/collection.js
+++ b/src/collection.js
@@ -295,6 +295,16 @@ function WeakMapDelete(key) {
 }


+function WeakMapClear() {
+  if (!IS_WEAKMAP(this)) {
+    throw MakeTypeError('incompatible_method_receiver',
+                        ['WeakMap.prototype.clear', this]);
+  }
+  // Replace the internal table with a new empty table.
+  %WeakMapInitialize(this);
+}
+
+
 // -------------------------------------------------------------------

 function SetUpWeakMap() {
@@ -309,7 +319,8 @@ function SetUpWeakMap() {
     "get", WeakMapGet,
     "set", WeakMapSet,
     "has", WeakMapHas,
-    "delete", WeakMapDelete
+    "delete", WeakMapDelete,
+    "clear", WeakMapClear
   ));
 }

Index: test/mjsunit/harmony/collections.js
diff --git a/test/mjsunit/harmony/collections.js b/test/mjsunit/harmony/collections.js index d60c59c90712edc3a84b077280d11a8964d12f76..cf18745ae85a30d5f2748838777d156de2799143 100644
--- a/test/mjsunit/harmony/collections.js
+++ b/test/mjsunit/harmony/collections.js
@@ -377,17 +377,39 @@ for (var i = 9; i >= 0; 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);
+// Test Set clear
+(function() {
+  var s = new Set();
+  s.add(42);
+  assertTrue(s.has(42));
+  assertEquals(1, s.size);
+  s.clear();
+  assertFalse(s.has(42));
+  assertEquals(0, s.size);
+})();
+
+
+// Test Map clear
+(function() {
+  var m = new Map();
+  m.set(42, true);
+  assertTrue(m.has(42));
+  assertEquals(1, m.size);
+  m.clear();
+  assertFalse(m.has(42));
+  assertEquals(0, m.size);
+})();
+
+
+// Test WeakMap clear
+(function() {
+  var k = new Object();
+  var w = new WeakMap();
+  w.set(k, 23);
+  assertTrue(w.has(k));
+  assertEquals(23, w.get(k));
+  w.clear();
+  assertFalse(w.has(k));
+  assertEquals(undefined, w.get(k));
+})();


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to