reflect-metadata.js 2.0 KB

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