_metadata.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. };