InstanceofOperator.js 918 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var $hasInstance = GetIntrinsic('Symbol.hasInstance', true);
  5. var Call = require('./Call');
  6. var GetMethod = require('./GetMethod');
  7. var IsCallable = require('./IsCallable');
  8. var OrdinaryHasInstance = require('./OrdinaryHasInstance');
  9. var ToBoolean = require('./ToBoolean');
  10. var Type = require('./Type');
  11. // https://ecma-international.org/ecma-262/6.0/#sec-instanceofoperator
  12. module.exports = function InstanceofOperator(O, C) {
  13. if (Type(O) !== 'Object') {
  14. throw new $TypeError('Assertion failed: Type(O) is not Object');
  15. }
  16. var instOfHandler = $hasInstance ? GetMethod(C, $hasInstance) : void 0;
  17. if (typeof instOfHandler !== 'undefined') {
  18. return ToBoolean(Call(instOfHandler, C, [O]));
  19. }
  20. if (!IsCallable(C)) {
  21. throw new $TypeError('`C` is not Callable');
  22. }
  23. return OrdinaryHasInstance(C, O);
  24. };