1 |
|
2 | var Map = require('../modules/es.map');
|
3 | var WeakMap = require('../modules/es.weak-map');
|
4 | var shared = require('../internals/shared');
|
5 |
|
6 | var metadata = shared('metadata');
|
7 | var store = metadata.store || (metadata.store = new WeakMap());
|
8 |
|
9 | var getOrCreateMetadataMap = function (target, targetKey, create) {
|
10 | var targetMetadata = store.get(target);
|
11 | if (!targetMetadata) {
|
12 | if (!create) return;
|
13 | store.set(target, targetMetadata = new Map());
|
14 | }
|
15 | var keyMetadata = targetMetadata.get(targetKey);
|
16 | if (!keyMetadata) {
|
17 | if (!create) return;
|
18 | targetMetadata.set(targetKey, keyMetadata = new Map());
|
19 | } return keyMetadata;
|
20 | };
|
21 |
|
22 | var ordinaryHasOwnMetadata = function (MetadataKey, O, P) {
|
23 | var metadataMap = getOrCreateMetadataMap(O, P, false);
|
24 | return metadataMap === undefined ? false : metadataMap.has(MetadataKey);
|
25 | };
|
26 |
|
27 | var ordinaryGetOwnMetadata = function (MetadataKey, O, P) {
|
28 | var metadataMap = getOrCreateMetadataMap(O, P, false);
|
29 | return metadataMap === undefined ? undefined : metadataMap.get(MetadataKey);
|
30 | };
|
31 |
|
32 | var ordinaryDefineOwnMetadata = function (MetadataKey, MetadataValue, O, P) {
|
33 | getOrCreateMetadataMap(O, P, true).set(MetadataKey, MetadataValue);
|
34 | };
|
35 |
|
36 | var ordinaryOwnMetadataKeys = function (target, targetKey) {
|
37 | var metadataMap = getOrCreateMetadataMap(target, targetKey, false);
|
38 | var keys = [];
|
39 | if (metadataMap) metadataMap.forEach(function (_, key) { keys.push(key); });
|
40 | return keys;
|
41 | };
|
42 |
|
43 | var toMetadataKey = function (it) {
|
44 | return it === undefined || typeof it == 'symbol' ? it : String(it);
|
45 | };
|
46 |
|
47 | module.exports = {
|
48 | store: store,
|
49 | getMap: getOrCreateMetadataMap,
|
50 | has: ordinaryHasOwnMetadata,
|
51 | get: ordinaryGetOwnMetadata,
|
52 | set: ordinaryDefineOwnMetadata,
|
53 | keys: ordinaryOwnMetadataKeys,
|
54 | toKey: toMetadataKey
|
55 | };
|