1d15660ac7de91aa24b244cc8150d572f1c0aec6
[platform/framework/web/crosswalk-tizen.git] /
1 'use strict';
2
3 var setPrototypeOf    = require('es5-ext/object/set-prototype-of')
4   , object            = require('es5-ext/object/valid-object')
5   , value             = require('es5-ext/object/valid-value')
6   , d                 = require('d')
7   , getIterator       = require('es6-iterator/get')
8   , forOf             = require('es6-iterator/for-of')
9   , toStringTagSymbol = require('es6-symbol').toStringTag
10   , isNative          = require('./is-native-implemented')
11
12   , isArray = Array.isArray, defineProperty = Object.defineProperty, random = Math.random
13   , hasOwnProperty = Object.prototype.hasOwnProperty
14   , genId, WeakMapPoly;
15
16 genId = (function () {
17         var generated = Object.create(null);
18         return function () {
19                 var id;
20                 do { id = random().toString(36).slice(2); } while (generated[id]);
21                 generated[id] = true;
22                 return id;
23         };
24 }());
25
26 module.exports = WeakMapPoly = function (/*iterable*/) {
27         var iterable = arguments[0];
28         if (!(this instanceof WeakMapPoly)) return new WeakMapPoly(iterable);
29         if (this.__weakMapData__ !== undefined) {
30                 throw new TypeError(this + " cannot be reinitialized");
31         }
32         if (iterable != null) {
33                 if (!isArray(iterable)) iterable = getIterator(iterable);
34         }
35         defineProperty(this, '__weakMapData__', d('c', '$weakMap$' + genId()));
36         if (!iterable) return;
37         forOf(iterable, function (val) {
38                 value(val);
39                 this.set(val[0], val[1]);
40         }, this);
41 };
42
43 if (isNative) {
44         if (setPrototypeOf) setPrototypeOf(WeakMapPoly, WeakMap);
45         WeakMapPoly.prototype = Object.create(WeakMap.prototype, {
46                 constructor: d(WeakMapPoly)
47         });
48 }
49
50 Object.defineProperties(WeakMapPoly.prototype, {
51         clear: d(function () {
52                 defineProperty(this, '__weakMapData__', d('c', '$weakMap$' + genId()));
53         }),
54         delete: d(function (key) {
55                 if (hasOwnProperty.call(object(key), this.__weakMapData__)) {
56                         delete key[this.__weakMapData__];
57                         return true;
58                 }
59                 return false;
60         }),
61         get: d(function (key) {
62                 if (hasOwnProperty.call(object(key), this.__weakMapData__)) {
63                         return key[this.__weakMapData__];
64                 }
65         }),
66         has: d(function (key) {
67                 return hasOwnProperty.call(object(key), this.__weakMapData__);
68         }),
69         set: d(function (key, value) {
70                 defineProperty(object(key), this.__weakMapData__, d('c', value));
71                 return this;
72         }),
73         toString: d(function () { return '[object WeakMap]'; })
74 });
75 defineProperty(WeakMapPoly.prototype, toStringTagSymbol, d('c', 'WeakMap'));