RegExpExec.js 887 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var regexExec = require('call-bind/callBound')('RegExp.prototype.exec');
  5. var Call = require('./Call');
  6. var Get = require('./Get');
  7. var IsCallable = require('./IsCallable');
  8. var Type = require('./Type');
  9. // https://ecma-international.org/ecma-262/6.0/#sec-regexpexec
  10. module.exports = function RegExpExec(R, S) {
  11. if (Type(R) !== 'Object') {
  12. throw new $TypeError('Assertion failed: `R` must be an Object');
  13. }
  14. if (Type(S) !== 'String') {
  15. throw new $TypeError('Assertion failed: `S` must be a String');
  16. }
  17. var exec = Get(R, 'exec');
  18. if (IsCallable(exec)) {
  19. var result = Call(exec, R, [S]);
  20. if (result === null || Type(result) === 'Object') {
  21. return result;
  22. }
  23. throw new $TypeError('"exec" method must return `null` or an Object');
  24. }
  25. return regexExec(R, S);
  26. };