index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var has = require('has');
  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. },
  16. get: function (O, slot) {
  17. if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
  18. throw new $TypeError('`O` is not an object');
  19. }
  20. if (typeof slot !== 'string') {
  21. throw new $TypeError('`slot` must be a string');
  22. }
  23. var slots = channel.get(O);
  24. return slots && slots['$' + slot];
  25. },
  26. has: function (O, slot) {
  27. if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
  28. throw new $TypeError('`O` is not an object');
  29. }
  30. if (typeof slot !== 'string') {
  31. throw new $TypeError('`slot` must be a string');
  32. }
  33. var slots = channel.get(O);
  34. return !!slots && has(slots, '$' + slot);
  35. },
  36. set: function (O, slot, V) {
  37. if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
  38. throw new $TypeError('`O` is not an object');
  39. }
  40. if (typeof slot !== 'string') {
  41. throw new $TypeError('`slot` must be a string');
  42. }
  43. var slots = channel.get(O);
  44. if (!slots) {
  45. slots = {};
  46. channel.set(O, slots);
  47. }
  48. slots['$' + slot] = V;
  49. }
  50. };
  51. if (Object.freeze) {
  52. Object.freeze(SLOT);
  53. }
  54. module.exports = SLOT;