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