_metadata.js 1.8 KB

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