1'use strict';
2
3const common = require('../common');
4const assert = require('node:assert');
5const { spawnSync, spawn } = require('node:child_process');
6
7assert.strictEqual(RegExp.$_, '');
8assert.strictEqual(RegExp.$0, undefined);
9assert.strictEqual(RegExp.$1, '');
10assert.strictEqual(RegExp.$2, '');
11assert.strictEqual(RegExp.$3, '');
12assert.strictEqual(RegExp.$4, '');
13assert.strictEqual(RegExp.$5, '');
14assert.strictEqual(RegExp.$6, '');
15assert.strictEqual(RegExp.$7, '');
16assert.strictEqual(RegExp.$8, '');
17assert.strictEqual(RegExp.$9, '');
18assert.strictEqual(RegExp.input, '');
19assert.strictEqual(RegExp.lastMatch, '');
20assert.strictEqual(RegExp.lastParen, '');
21assert.strictEqual(RegExp.leftContext, '');
22assert.strictEqual(RegExp.rightContext, '');
23assert.strictEqual(RegExp['$&'], '');
24assert.strictEqual(RegExp['$`'], '');
25assert.strictEqual(RegExp['$+'], '');
26assert.strictEqual(RegExp["$'"], '');
27
28const allRegExpStatics =
29    'RegExp.$_ + RegExp["$&"] + RegExp["$`"] + RegExp["$+"] + RegExp["$\'"] + ' +
30    'RegExp.input + RegExp.lastMatch + RegExp.lastParen + ' +
31    'RegExp.leftContext + RegExp.rightContext + ' +
32    Array.from({ length: 10 }, (_, i) => `RegExp.$${i}`).join(' + ');
33
34{
35  const child = spawnSync(process.execPath,
36                          [ '-p', allRegExpStatics ],
37                          { stdio: ['inherit', 'pipe', 'inherit'] });
38  assert.match(child.stdout.toString(), /^undefined\r?\n$/);
39  assert.strictEqual(child.status, 0);
40  assert.strictEqual(child.signal, null);
41}
42
43{
44  const child = spawnSync(process.execPath,
45                          [ '--expose-internals', '-p', `const {
46                            SideEffectFreeRegExpPrototypeExec,
47                            SideEffectFreeRegExpPrototypeSymbolReplace,
48                            SideEffectFreeRegExpPrototypeSymbolSplit,
49                          } = require("internal/util");
50                          SideEffectFreeRegExpPrototypeExec(/foo/, "foo");
51                          SideEffectFreeRegExpPrototypeSymbolReplace(/o/, "foo", "a");
52                          SideEffectFreeRegExpPrototypeSymbolSplit(/o/, "foo");
53                          ${allRegExpStatics}` ],
54                          { stdio: ['inherit', 'pipe', 'inherit'] });
55  assert.match(child.stdout.toString(), /^undefined\r?\n$/);
56  assert.strictEqual(child.status, 0);
57  assert.strictEqual(child.signal, null);
58}
59
60{
61  const child = spawnSync(process.execPath,
62                          [ '-e', `console.log(${allRegExpStatics})`, '--input-type=module' ],
63                          { stdio: ['inherit', 'pipe', 'inherit'] });
64  assert.match(child.stdout.toString(), /^undefined\r?\n$/);
65  assert.strictEqual(child.status, 0);
66  assert.strictEqual(child.signal, null);
67}
68
69{
70  const child = spawn(process.execPath, [], { stdio: ['pipe', 'pipe', 'inherit'], encoding: 'utf8' });
71
72  let stdout = '';
73  child.stdout.on('data', (chunk) => {
74    stdout += chunk;
75  });
76
77  child.on('exit', common.mustCall((status, signal) => {
78    assert.match(stdout, /^undefined\r?\n$/);
79    assert.strictEqual(status, 0);
80    assert.strictEqual(signal, null);
81  }));
82  child.on('error', common.mustNotCall());
83
84  child.stdin.end(`console.log(${allRegExpStatics});\n`);
85}
86