From 493d1f1c218cbf85d4dba855da0e36dd46e56c07 Mon Sep 17 00:00:00 2001 From: "mstarzinger@chromium.org" Date: Mon, 1 Jul 2013 13:56:48 +0000 Subject: [PATCH] Implement WeakMap.prototype.clear function. R=rossberg@chromium.org BUG=v8:2753 TEST=mjsunit/harmony/collections Review URL: https://codereview.chromium.org/18352002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@15421 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/collection.js | 13 +++++++- test/mjsunit/harmony/collections.js | 48 +++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/collection.js b/src/collection.js index 950c7e7374..c5604ab30f 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 )); } diff --git a/test/mjsunit/harmony/collections.js b/test/mjsunit/harmony/collections.js index d60c59c907..cf18745ae8 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)); +})();