diff --git a/src/weakmap.js b/src/weakmap.js index b209776829..15056c7f82 100644 --- a/src/weakmap.js +++ b/src/weakmap.js @@ -58,13 +58,36 @@ function WeakMapSet(key, value) { return %WeakMapSet(this, key, value); } + +function WeakMapHas(key) { + if (!IS_SPEC_OBJECT(key)) { + throw %MakeTypeError('invalid_weakmap_key', [this, key]); + } + return !IS_UNDEFINED(%WeakMapGet(this, key)); +} + + +function WeakMapDelete(key) { + if (!IS_SPEC_OBJECT(key)) { + throw %MakeTypeError('invalid_weakmap_key', [this, key]); + } + if (!IS_UNDEFINED(%WeakMapGet(this, key))) { + %WeakMapSet(this, key, void 0); + return true; + } else { + return false; + } +} + // ------------------------------------------------------------------- function SetupWeakMap() { // Setup the non-enumerable functions on the WeakMap prototype object. InstallFunctionsOnHiddenPrototype($WeakMap.prototype, DONT_ENUM, $Array( "get", WeakMapGet, - "set", WeakMapSet + "set", WeakMapSet, + "has", WeakMapHas, + "delete", WeakMapDelete )); } diff --git a/test/mjsunit/harmony/weakmaps.js b/test/mjsunit/harmony/weakmaps.js index 52b022bbd8..d98db10df3 100644 --- a/test/mjsunit/harmony/weakmaps.js +++ b/test/mjsunit/harmony/weakmaps.js @@ -32,6 +32,8 @@ var m = new WeakMap; assertDoesNotThrow(function () { m.get(new Object) }); assertDoesNotThrow(function () { m.set(new Object) }); +assertDoesNotThrow(function () { m.has(new Object) }); +assertDoesNotThrow(function () { m.delete(new Object) }); // Test invalid getter and setter calls @@ -55,6 +57,27 @@ TestMapping(m, new Object, 'the-value'); TestMapping(m, new Object, new Object); +// Test expected querying behavior +var m = new WeakMap; +var key = new Object; +TestMapping(m, key, 'to-be-present'); +assertTrue(m.has(key)); +assertFalse(m.has(new Object)); +TestMapping(m, key, undefined); +assertFalse(m.has(key)); +assertFalse(m.has(new Object)); + + +// Test expected deletion behavior +var m = new WeakMap; +var key = new Object; +TestMapping(m, key, 'to-be-deleted'); +assertTrue(m.delete(key)); +assertFalse(m.delete(key)); +assertFalse(m.delete(new Object)); +assertSame(m.get(key), undefined); + + // Test GC of map with entry var m = new WeakMap; var key = new Object; @@ -112,6 +135,8 @@ var m = new WeakMap; assertTrue(m instanceof WeakMap); assertTrue(WeakMap.prototype.set instanceof Function) assertTrue(WeakMap.prototype.get instanceof Function) +assertTrue(WeakMap.prototype.has instanceof Function) +assertTrue(WeakMap.prototype.delete instanceof Function) // Stress Test