Source: polyfill/WeakMap.js

// Licensed Materials - Property of IBM
//
// IBM Watson Analytics
//
// (C) Copyright IBM Corp. 2015
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

module.exports = ( function( ObjectPolyfill, Symbol )
{
"use strict";

// The property set by the first keymap that takes an object as key.
// All WeakMaps look at the same property
var KEY_PROP = Symbol( "WeakMap" );
var hOP = Object.prototype.hasOwnProperty;
var nextId = 0;

/**
 * Mimics the implementation of a native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap WeakMap}.
 * The only way to achieve this is by putting a property on the key object.
 * @class module:barejs/polyfill.WeakMap
 * @param {any} [_iterable=null] Optional: an iterable whose values will be added to the WeakSet.
 */
function WeakMap( _iterable )
{
    // Ensure map has no prototype chain (so we can safely use the in operator instead of hasOwnProperty)
    ObjectPolyfill.defineProperty( this, "_map", { value: Object.create( null ) } );

    if ( _iterable )
    {
        for ( var i = 0; i < _iterable.length; ++i )
            this.set( _iterable[i][0], _iterable[i][1] );
    }
}

return ( ObjectPolyfill.defineProperties( WeakMap.prototype,
/** @lends module:barejs/polyfill.WeakMap# */
{
    _map: { value: null },

    /**
     * Adds a new element with a specified key and value to a WeakMap object.
     * @function
     * @param {object} _key The object to use as key.
     * @param _value The value to add.
     * @returns {module:barejs/polyfill.WeakMap} The WeakMap (for chaining)
     */
    "set": { value: function set( _key, _value )
    {
        if ( !ObjectPolyfill.isObject( _key ) )
            throw new TypeError( "Invalid value used as weak map key" );

        if ( !hOP.call( _key, KEY_PROP ) )
            ObjectPolyfill.defineProperty( _key, KEY_PROP, { value: ++nextId } );

        this._map[ _key[KEY_PROP] ] = _value;
        return this;
    } },

    /**
     * Get the value for the specified key
     * @function
     * @param {object} _key The object to use as key.
     * @returns The value, or undefined if the key is not known.
     */
    "get": { value: function get( _key )
    {
        return this.has( _key ) ? this._map[ _key[KEY_PROP] ] : undefined;
    } },

    /**
     * Check if the WeakMap has an entry for the specified key
     * @function
     * @param {object} _key The object to use as key.
     * @returns {boolean} True if there is an entry for the key, false otherwise
     */
    has: { value: function has( _key )
    {
        // If there is no WeakMap key property on the object, it has never been used as key for a WeakMap
        return ObjectPolyfill.isObject( _key ) && hOP.call( _key, KEY_PROP ) && ( _key[KEY_PROP] in this._map );
    } },

    /**
     * Remove the value for the specified key
     * @function
     * @param {object} _key The object to use as key.
     * @returns {boolean} True if the entry was deleted, false otherwise
     */
    "delete": { value: function _delete( _key )
    {
        // Remove the entry from our map (will be a NOOP if there is no property for the key value)
        // Note: delete is not called if this.has( _key ) returns false. If the delete statement
        // (incorrectly) returns false, we force it to return true using "|| true".
        return this.has( _key ) && ( ( delete this._map[_key[KEY_PROP]] ) || true );
    } }
} ), WeakMap );

}( require( "./Object" ), require( "../Symbol" ) ) );