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 <arv@chromium.org>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12909 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 64da4755
...@@ -97,6 +97,16 @@ function SetGetSize() { ...@@ -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() { function MapConstructor() {
if (%_IsConstructCall()) { if (%_IsConstructCall()) {
%MapInitialize(this); %MapInitialize(this);
...@@ -163,6 +173,16 @@ function MapGetSize() { ...@@ -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() { function WeakMapConstructor() {
if (%_IsConstructCall()) { if (%_IsConstructCall()) {
%WeakMapInitialize(this); %WeakMapInitialize(this);
...@@ -237,7 +257,8 @@ function WeakMapDelete(key) { ...@@ -237,7 +257,8 @@ function WeakMapDelete(key) {
InstallFunctions($Set.prototype, DONT_ENUM, $Array( InstallFunctions($Set.prototype, DONT_ENUM, $Array(
"add", SetAdd, "add", SetAdd,
"has", SetHas, "has", SetHas,
"delete", SetDelete "delete", SetDelete,
"clear", SetClear
)); ));
// Set up the non-enumerable functions on the Map prototype object. // Set up the non-enumerable functions on the Map prototype object.
...@@ -246,7 +267,8 @@ function WeakMapDelete(key) { ...@@ -246,7 +267,8 @@ function WeakMapDelete(key) {
"get", MapGet, "get", MapGet,
"set", MapSet, "set", MapSet,
"has", MapHas, "has", MapHas,
"delete", MapDelete "delete", MapDelete,
"clear", MapClear
)); ));
// Set up the WeakMap constructor function. // Set up the WeakMap constructor function.
......
...@@ -355,3 +355,18 @@ for (var i = 9; i >= 0; i--) { ...@@ -355,3 +355,18 @@ for (var i = 9; i >= 0; i--) {
m.delete(i); m.delete(i);
assertEquals(i, m.size); 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);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment