Fix Harmony sets and maps to allow undefined as keys.

This uses a global sentinel as a replacement for undefined keys, which
are not supported internally but required for Harmony sets and maps.

R=rossberg@chromium.org
BUG=v8:1622
TEST=mjsunit/harmony/collections

Review URL: http://codereview.chromium.org/8439069

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9873 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
mstarzinger@chromium.org 2011-11-03 14:33:58 +00:00
parent ea18539244
commit 0bbfb46aa6
2 changed files with 34 additions and 3 deletions

View File

@ -32,6 +32,11 @@ const $WeakMap = global.WeakMap;
//-------------------------------------------------------------------
// Global sentinel to be used instead of undefined keys, which are not
// supported internally but required for Harmony sets and maps.
var undefined_sentinel = {};
function SetConstructor() {
if (%_IsConstructCall()) {
%SetInitialize(this);
@ -42,16 +47,25 @@ function SetConstructor() {
function SetAdd(key) {
if (IS_UNDEFINED(key)) {
key = undefined_sentinel;
}
return %SetAdd(this, key);
}
function SetHas(key) {
if (IS_UNDEFINED(key)) {
key = undefined_sentinel;
}
return %SetHas(this, key);
}
function SetDelete(key) {
if (IS_UNDEFINED(key)) {
key = undefined_sentinel;
}
return %SetDelete(this, key);
}
@ -66,21 +80,33 @@ function MapConstructor() {
function MapGet(key) {
if (IS_UNDEFINED(key)) {
key = undefined_sentinel;
}
return %MapGet(this, key);
}
function MapSet(key, value) {
if (IS_UNDEFINED(key)) {
key = undefined_sentinel;
}
return %MapSet(this, key, value);
}
function MapHas(key) {
if (IS_UNDEFINED(key)) {
key = undefined_sentinel;
}
return !IS_UNDEFINED(%MapGet(this, key));
}
function MapDelete(key) {
if (IS_UNDEFINED(key)) {
key = undefined_sentinel;
}
if (!IS_UNDEFINED(%MapGet(this, key))) {
%MapSet(this, key, void 0);
return true;

View File

@ -73,10 +73,15 @@ function TestSet(set, key) {
function TestSetBehavior(set) {
for (var i = 0; i < 20; i++) {
TestSet(set, new Object);
TestSet(set, i);
TestSet(set, i / 100);
TestSet(set, 'key-' + i);
}
var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ];
for (var i = 0; i < keys.length; i++) {
TestSet(set, keys[i]);
}
}
TestSet(new Set, 23);
TestSet(new Set, 'foo');
TestSetBehavior(new Set);
@ -101,7 +106,7 @@ function TestMapBehavior2(m) {
TestMapping(m, i / 10, new Object);
TestMapping(m, 'key-' + i, new Object);
}
var keys = [ +0, -0, +Infinity, -Infinity, true, false, null ];
var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ];
for (var i = 0; i < keys.length; i++) {
TestMapping(m, keys[i], new Object);
}