Skip to content

Instantly share code, notes, and snippets.

@Raynos
Forked from Gozala/weak-map.js
Last active September 18, 2019 07:49
Show Gist options
  • Select an option

  • Save Raynos/1638059 to your computer and use it in GitHub Desktop.

Select an option

Save Raynos/1638059 to your computer and use it in GitHub Desktop.

Revisions

  1. Raynos revised this gist Mar 24, 2014. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions weak-map.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,8 @@ window.WeakMap = window.WeakMap || (function () {
    return {
    get: function (key, fallback) {
    var store = privates(key)
    return store.hasOwnProperty("value") ? store.value : fallback
    return store.hasOwnProperty("value") ?
    store.value : fallback
    },
    set: function (key, value) {
    privates(key).value = value
    @@ -19,12 +20,13 @@ window.WeakMap = window.WeakMap || (function () {
    }

    function namespace(obj, key) {
    var store = {},
    var store = { identity: key },
    valueOf = obj.valueOf

    Object.defineProperty(obj, "valueOf", {
    value: function (value) {
    return value !== key ? valueOf.apply(this, arguments) : store
    return value !== key ?
    valueOf.apply(this, arguments) : store
    },
    writable: true
    })
    @@ -36,7 +38,8 @@ window.WeakMap = window.WeakMap || (function () {
    var key = {}
    return function (obj) {
    var store = obj.valueOf(key)
    return store !== obj ? store : namespace(obj, key)
    return store && store.identity === key ?
    store : namespace(obj, key)
    }
    }
    }())
  2. Raynos revised this gist Mar 24, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions weak-map.js
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,8 @@ window.WeakMap = window.WeakMap || (function () {

    return {
    get: function (key, fallback) {
    var content = privates(key).value
    return content === undefined ? fallback : content
    var store = privates(key)
    return store.hasOwnProperty("value") ? store.value : fallback
    },
    set: function (key, value) {
    privates(key).value = value
  3. Raynos revised this gist Mar 24, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion weak-map.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@ window.WeakMap = window.WeakMap || (function () {

    return {
    get: function (key, fallback) {
    return privates(key).value || fallback
    var content = privates(key).value
    return content === undefined ? fallback : content
    },
    set: function (key, value) {
    privates(key).value = value
  4. Raynos revised this gist Apr 14, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion weak-map.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // Original - @Gozola. This is a reimplemented version (with a few bug fixes).
    window.WeakMap = WeakMap || (function () {
    window.WeakMap = window.WeakMap || (function () {
    var privates = Name()

    return {
  5. Raynos revised this gist Apr 14, 2012. No changes.
  6. Raynos revised this gist Apr 14, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions weak-map.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ window.WeakMap = WeakMap || (function () {
    valueOf = obj.valueOf

    Object.defineProperty(obj, "valueOf", {
    value: function _valueOf(value) {
    value: function (value) {
    return value !== key ? valueOf.apply(this, arguments) : store
    },
    writable: true
    @@ -33,7 +33,7 @@ window.WeakMap = WeakMap || (function () {

    function Name() {
    var key = {}
    return function name(obj) {
    return function (obj) {
    var store = obj.valueOf(key)
    return store !== obj ? store : namespace(obj, key)
    }
  7. Raynos revised this gist Apr 14, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions weak-map.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ window.WeakMap = WeakMap || (function () {
    "delete": function (key) {
    return delete privates(key).value
    }
    };
    }

    function namespace(obj, key) {
    var store = {},
    @@ -38,4 +38,4 @@ window.WeakMap = WeakMap || (function () {
    return store !== obj ? store : namespace(obj, key)
    }
    }
    }());
    }())
  8. Raynos revised this gist Apr 14, 2012. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions weak-map.js
    Original file line number Diff line number Diff line change
    @@ -1,40 +1,40 @@
    // Original - @Gozola. This is a reimplemented version (with a few bug fixes).
    window.WeakMap = WeakMap || (function () {
    var namespace = Name();
    var privates = Name()

    return {
    get: function get(key, fallback) {
    return namespace(key).value || fallback;
    get: function (key, fallback) {
    return privates(key).value || fallback
    },
    set: function set(key, value) {
    namespace(key).value = value;
    set: function (key, value) {
    privates(key).value = value
    },
    has: function has(key) {
    return "value" in namespace(key);
    has: function(key) {
    return "value" in privates(key)
    },
    delete: function delete(key) {
    return delete namespace(key).value;
    "delete": function (key) {
    return delete privates(key).value
    }
    };

    function namespace(obj, key) {
    var store = {},
    valueOf = obj.valueOf;
    valueOf = obj.valueOf

    Object.defineProperty(obj, "valueOf", {
    value: function _valueOf(value) {
    return value !== key ? valueOf.apply(this, arguments) : store;
    return value !== key ? valueOf.apply(this, arguments) : store
    },
    writable: true
    });
    })

    return store;
    return store
    }

    function Name() {
    var key = {};
    var key = {}
    return function name(obj) {
    var store = obj.valueOf(key);
    var store = obj.valueOf(key)
    return store !== obj ? store : namespace(obj, key)
    }
    }
  9. Raynos revised this gist Jan 19, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions weak-map.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Original - @Gozola. This is a reimplemented version (with a few bug fixes).
    window.WeakMap = WeakMap || (function () {
    var namespace = Name();

  10. Raynos revised this gist Jan 19, 2012. 1 changed file with 36 additions and 96 deletions.
    132 changes: 36 additions & 96 deletions weak-map.js
    Original file line number Diff line number Diff line change
    @@ -1,100 +1,40 @@
    /* vim:set ts=2 sw=2 sts=2 expandtab */
    /*jshint asi: true undef: true es5: true node: true devel: true
    forin: false latedef: false */
    /*global define: true */


    if (typeof(WeakMap) === 'undefined') WeakMap = (function(global) {
    "use strict";

    function defineNamespace(object, namespace) {
    /**
    Utility function takes `object` and `namespace` and overrides `valueOf`
    method of `object`, so that when called with a `namespace` argument,
    `private` object associated with this `namespace` is returned. If argument
    is different, `valueOf` falls back to original `valueOf` property.
    **/

    // Private inherits from `object`, so that `this.foo` will refer to the
    // `object.foo`. Also, original `valueOf` is saved in order to be able to
    // delegate to it when necessary.
    var privates = Object.create(object), base = object.valueOf
    Object.defineProperty(object, 'valueOf', { value: function valueOf(value) {
    // If `this` or `namespace` is not associated with a `privates` being
    // stored we fallback to original `valueOf`, otherwise we return privates.
    return value != namespace || this != object ? base.apply(this, arguments)
    : privates
    }})
    return privates
    }

    function Name() {
    /**
    Desugared implementation of private names proposal. API is different as
    it's not possible to implement API proposed for harmony with in ES5. In
    terms of provided functionality it supposed to be same.
    http://wiki.ecmascript.org/doku.php?id=strawman:private_names
    **/

    var namespace = {}
    return function name(object) {
    var privates = object.valueOf(namespace)
    return privates !== object ? privates : defineNamespace(object, namespace)
    }
    }

    function guard(key) {
    /**
    Utility function to guard WeakMap methods from keys that are not
    a non-null objects.
    **/

    if (key !== Object(key)) throw TypeError("value is not a non-null object")
    return key
    }

    function WeakMap() {
    /**
    Implementation of harmony `WeakMaps`, in ES5. This implementation will
    work only with keys that have configurable `valueOf` property (which is
    a default for all non-frozen objects).
    http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps
    **/

    var privates = Name()

    return Object.freeze(Object.create(WeakMap.prototype, {
    has: {
    value: function has(object) {
    return 'value' in privates(object)
    window.WeakMap = WeakMap || (function () {
    var namespace = Name();

    return {
    get: function get(key, fallback) {
    return namespace(key).value || fallback;
    },
    configurable: true,
    enumerable: false,
    writable: true
    },
    get: {
    value: function get(key, fallback) {
    return privates(guard(key)).value || fallback
    set: function set(key, value) {
    namespace(key).value = value;
    },
    configurable: true,
    enumerable: false,
    writable: true
    },
    set: {
    value: function set(key, value) {
    privates(guard(key)).value = value
    has: function has(key) {
    return "value" in namespace(key);
    },
    configurable: true,
    enumerable: false,
    writable: true
    },
    'delete': {
    value: function set(key) {
    return delete privates(guard(key)).value
    delete: function delete(key) {
    return delete namespace(key).value;
    }
    };

    function namespace(obj, key) {
    var store = {},
    valueOf = obj.valueOf;

    Object.defineProperty(obj, "valueOf", {
    value: function _valueOf(value) {
    return value !== key ? valueOf.apply(this, arguments) : store;
    },
    writable: true
    });

    return store;
    }

    function Name() {
    var key = {};
    return function name(obj) {
    var store = obj.valueOf(key);
    return store !== obj ? store : namespace(obj, key)
    }
    }
    }))
    }

    return global.WeakMap = WeakMap
    })(this)
    }
    }());
  11. @Gozala Gozala created this gist Oct 7, 2011.
    100 changes: 100 additions & 0 deletions weak-map.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    /* vim:set ts=2 sw=2 sts=2 expandtab */
    /*jshint asi: true undef: true es5: true node: true devel: true
    forin: false latedef: false */
    /*global define: true */


    if (typeof(WeakMap) === 'undefined') WeakMap = (function(global) {
    "use strict";

    function defineNamespace(object, namespace) {
    /**
    Utility function takes `object` and `namespace` and overrides `valueOf`
    method of `object`, so that when called with a `namespace` argument,
    `private` object associated with this `namespace` is returned. If argument
    is different, `valueOf` falls back to original `valueOf` property.
    **/

    // Private inherits from `object`, so that `this.foo` will refer to the
    // `object.foo`. Also, original `valueOf` is saved in order to be able to
    // delegate to it when necessary.
    var privates = Object.create(object), base = object.valueOf
    Object.defineProperty(object, 'valueOf', { value: function valueOf(value) {
    // If `this` or `namespace` is not associated with a `privates` being
    // stored we fallback to original `valueOf`, otherwise we return privates.
    return value != namespace || this != object ? base.apply(this, arguments)
    : privates
    }})
    return privates
    }

    function Name() {
    /**
    Desugared implementation of private names proposal. API is different as
    it's not possible to implement API proposed for harmony with in ES5. In
    terms of provided functionality it supposed to be same.
    http://wiki.ecmascript.org/doku.php?id=strawman:private_names
    **/

    var namespace = {}
    return function name(object) {
    var privates = object.valueOf(namespace)
    return privates !== object ? privates : defineNamespace(object, namespace)
    }
    }

    function guard(key) {
    /**
    Utility function to guard WeakMap methods from keys that are not
    a non-null objects.
    **/

    if (key !== Object(key)) throw TypeError("value is not a non-null object")
    return key
    }

    function WeakMap() {
    /**
    Implementation of harmony `WeakMaps`, in ES5. This implementation will
    work only with keys that have configurable `valueOf` property (which is
    a default for all non-frozen objects).
    http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps
    **/

    var privates = Name()

    return Object.freeze(Object.create(WeakMap.prototype, {
    has: {
    value: function has(object) {
    return 'value' in privates(object)
    },
    configurable: true,
    enumerable: false,
    writable: true
    },
    get: {
    value: function get(key, fallback) {
    return privates(guard(key)).value || fallback
    },
    configurable: true,
    enumerable: false,
    writable: true
    },
    set: {
    value: function set(key, value) {
    privates(guard(key)).value = value
    },
    configurable: true,
    enumerable: false,
    writable: true
    },
    'delete': {
    value: function set(key) {
    return delete privates(guard(key)).value
    }
    }
    }))
    }

    return global.WeakMap = WeakMap
    })(this)