Commit 29e6b443 authored by rossberg@chromium.org's avatar rossberg@chromium.org

ES6 symbols: enable symbols as weak map keys

R=mstarzinger@chromium.org
BUG=

Review URL: https://codereview.chromium.org/12456004

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13829 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 7fe9bd5a
...@@ -197,7 +197,7 @@ function WeakMapGet(key) { ...@@ -197,7 +197,7 @@ function WeakMapGet(key) {
throw MakeTypeError('incompatible_method_receiver', throw MakeTypeError('incompatible_method_receiver',
['WeakMap.prototype.get', this]); ['WeakMap.prototype.get', this]);
} }
if (!IS_SPEC_OBJECT(key)) { if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
throw %MakeTypeError('invalid_weakmap_key', [this, key]); throw %MakeTypeError('invalid_weakmap_key', [this, key]);
} }
return %WeakMapGet(this, key); return %WeakMapGet(this, key);
...@@ -209,7 +209,7 @@ function WeakMapSet(key, value) { ...@@ -209,7 +209,7 @@ function WeakMapSet(key, value) {
throw MakeTypeError('incompatible_method_receiver', throw MakeTypeError('incompatible_method_receiver',
['WeakMap.prototype.set', this]); ['WeakMap.prototype.set', this]);
} }
if (!IS_SPEC_OBJECT(key)) { if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
throw %MakeTypeError('invalid_weakmap_key', [this, key]); throw %MakeTypeError('invalid_weakmap_key', [this, key]);
} }
return %WeakMapSet(this, key, value); return %WeakMapSet(this, key, value);
...@@ -221,7 +221,7 @@ function WeakMapHas(key) { ...@@ -221,7 +221,7 @@ function WeakMapHas(key) {
throw MakeTypeError('incompatible_method_receiver', throw MakeTypeError('incompatible_method_receiver',
['WeakMap.prototype.has', this]); ['WeakMap.prototype.has', this]);
} }
if (!IS_SPEC_OBJECT(key)) { if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
throw %MakeTypeError('invalid_weakmap_key', [this, key]); throw %MakeTypeError('invalid_weakmap_key', [this, key]);
} }
return %WeakMapHas(this, key); return %WeakMapHas(this, key);
...@@ -233,7 +233,7 @@ function WeakMapDelete(key) { ...@@ -233,7 +233,7 @@ function WeakMapDelete(key) {
throw MakeTypeError('incompatible_method_receiver', throw MakeTypeError('incompatible_method_receiver',
['WeakMap.prototype.delete', this]); ['WeakMap.prototype.delete', this]);
} }
if (!IS_SPEC_OBJECT(key)) { if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
throw %MakeTypeError('invalid_weakmap_key', [this, key]); throw %MakeTypeError('invalid_weakmap_key', [this, key]);
} }
return %WeakMapDelete(this, key); return %WeakMapDelete(this, key);
......
...@@ -899,7 +899,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapGet) { ...@@ -899,7 +899,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapGet) {
HandleScope scope(isolate); HandleScope scope(isolate);
ASSERT(args.length() == 2); ASSERT(args.length() == 2);
CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0);
CONVERT_ARG_HANDLE_CHECKED(JSReceiver, key, 1); CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table())); Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table()));
Handle<Object> lookup(table->Lookup(*key), isolate); Handle<Object> lookup(table->Lookup(*key), isolate);
return lookup->IsTheHole() ? isolate->heap()->undefined_value() : *lookup; return lookup->IsTheHole() ? isolate->heap()->undefined_value() : *lookup;
...@@ -910,7 +910,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapHas) { ...@@ -910,7 +910,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapHas) {
HandleScope scope(isolate); HandleScope scope(isolate);
ASSERT(args.length() == 2); ASSERT(args.length() == 2);
CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0);
CONVERT_ARG_HANDLE_CHECKED(JSReceiver, key, 1); CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table())); Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table()));
Handle<Object> lookup(table->Lookup(*key), isolate); Handle<Object> lookup(table->Lookup(*key), isolate);
return isolate->heap()->ToBoolean(!lookup->IsTheHole()); return isolate->heap()->ToBoolean(!lookup->IsTheHole());
...@@ -921,7 +921,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapDelete) { ...@@ -921,7 +921,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapDelete) {
HandleScope scope(isolate); HandleScope scope(isolate);
ASSERT(args.length() == 2); ASSERT(args.length() == 2);
CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0);
CONVERT_ARG_HANDLE_CHECKED(JSReceiver, key, 1); CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table())); Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table()));
Handle<Object> lookup(table->Lookup(*key), isolate); Handle<Object> lookup(table->Lookup(*key), isolate);
Handle<ObjectHashTable> new_table = Handle<ObjectHashTable> new_table =
...@@ -935,7 +935,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapSet) { ...@@ -935,7 +935,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapSet) {
HandleScope scope(isolate); HandleScope scope(isolate);
ASSERT(args.length() == 3); ASSERT(args.length() == 3);
CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0);
CONVERT_ARG_HANDLE_CHECKED(JSReceiver, key, 1); CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
Handle<Object> value(args[2], isolate); Handle<Object> value(args[2], isolate);
Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table())); Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table()));
Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value); Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value);
......
...@@ -114,17 +114,33 @@ function TestSet() { ...@@ -114,17 +114,33 @@ function TestSet() {
TestSet() TestSet()
function TestMap() { function TestCollections() {
var set = new Set
var map = new Map var map = new Map
var weakmap = new WeakMap
for (var i in symbols) { for (var i in symbols) {
set.add(symbols[i])
map.set(symbols[i], i) map.set(symbols[i], i)
weakmap.set(symbols[i], i)
} }
assertEquals(symbols.length, set.size)
assertEquals(symbols.length, map.size)
for (var i in symbols) { for (var i in symbols) {
assertTrue(set.has(symbols[i]))
assertTrue(map.has(symbols[i])) assertTrue(map.has(symbols[i]))
assertTrue(weakmap.has(symbols[i]))
assertEquals(i, map.get(symbols[i])) assertEquals(i, map.get(symbols[i]))
assertEquals(i, weakmap.get(symbols[i]))
} }
for (var i in symbols) {
assertTrue(set.delete(symbols[i]))
assertTrue(map.delete(symbols[i]))
assertTrue(weakmap.delete(symbols[i]))
}
assertEquals(0, set.size)
assertEquals(0, map.size)
} }
TestMap() TestCollections()
......
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