WeakRefDeref.js 625 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var $deref = callBound('WeakRef.prototype.deref', true);
  6. var isWeakRef = require('is-weakref');
  7. var AddToKeptObjects = require('./AddToKeptObjects');
  8. // https://ecma-international.org/ecma-262/12.0/#sec-weakrefderef
  9. module.exports = function WeakRefDeref(weakRef) {
  10. if (!isWeakRef(weakRef)) {
  11. throw new $TypeError('Assertion failed: `weakRef` must be a WeakRef');
  12. }
  13. var target = $deref(weakRef);
  14. if (target) {
  15. AddToKeptObjects(target);
  16. }
  17. return target;
  18. };