index.js 1.6 KB

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