index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. var hasOwn = require('hasown');
  3. var channel = require('side-channel')();
  4. var $TypeError = require('es-errors/type');
  5. var SLOT = {
  6. assert: function (O, slot) {
  7. if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
  8. throw new $TypeError('`O` is not an object');
  9. }
  10. if (typeof slot !== 'string') {
  11. throw new $TypeError('`slot` must be a string');
  12. }
  13. channel.assert(O);
  14. if (!SLOT.has(O, slot)) {
  15. throw new $TypeError('`' + slot + '` is not present on `O`');
  16. }
  17. },
  18. get: function (O, slot) {
  19. if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
  20. throw new $TypeError('`O` is not an object');
  21. }
  22. if (typeof slot !== 'string') {
  23. throw new $TypeError('`slot` must be a string');
  24. }
  25. var slots = channel.get(O);
  26. return slots && slots['$' + slot];
  27. },
  28. has: function (O, slot) {
  29. if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
  30. throw new $TypeError('`O` is not an object');
  31. }
  32. if (typeof slot !== 'string') {
  33. throw new $TypeError('`slot` must be a string');
  34. }
  35. var slots = channel.get(O);
  36. return !!slots && hasOwn(slots, '$' + slot);
  37. },
  38. set: function (O, slot, V) {
  39. if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
  40. throw new $TypeError('`O` is not an object');
  41. }
  42. if (typeof slot !== 'string') {
  43. throw new $TypeError('`slot` must be a string');
  44. }
  45. var slots = channel.get(O);
  46. if (!slots) {
  47. slots = {};
  48. channel.set(O, slots);
  49. }
  50. slots['$' + slot] = V;
  51. }
  52. };
  53. if (Object.freeze) {
  54. Object.freeze(SLOT);
  55. }
  56. module.exports = SLOT;