summaryrefslogtreecommitdiff
path: root/test/parallel/test-repl-unsafe-array-iteration.js
blob: 3fc65f54cf1f3714ff15fd500d9f2530abf02e64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
const common = require('../common');
const assert = require('assert');
const { spawn } = require('child_process');

const replProcess = spawn(process.argv0, ['--interactive'], {
  stdio: ['pipe', 'pipe', 'inherit'],
  windowsHide: true,
});

replProcess.on('error', common.mustNotCall());

const replReadyState = (async function* () {
  let ready;
  const SPACE = ' '.charCodeAt();
  const BRACKET = '>'.charCodeAt();
  const DOT = '.'.charCodeAt();
  replProcess.stdout.on('data', (data) => {
    ready = data[data.length - 1] === SPACE && (
      data[data.length - 2] === BRACKET || (
        data[data.length - 2] === DOT &&
        data[data.length - 3] === DOT &&
        data[data.length - 4] === DOT
      ));
  });

  const processCrashed = new Promise((resolve, reject) =>
    replProcess.on('exit', reject)
  );
  while (true) {
    await Promise.race([new Promise(setImmediate), processCrashed]);
    if (ready) {
      ready = false;
      yield;
    }
  }
})();
async function writeLn(data, expectedOutput) {
  await replReadyState.next();
  if (expectedOutput) {
    replProcess.stdout.once('data', common.mustCall((data) =>
      assert.match(data.toString('utf8'), expectedOutput)
    ));
  }
  await new Promise((resolve, reject) => replProcess.stdin.write(
    `${data}\n`,
    (err) => (err ? reject(err) : resolve())
  ));
}

async function main() {
  await writeLn(
    'const ArrayIteratorPrototype =' +
      '  Object.getPrototypeOf(Array.prototype[Symbol.iterator]());'
  );
  await writeLn('delete Array.prototype[Symbol.iterator];');
  await writeLn('delete ArrayIteratorPrototype.next;');

  await writeLn(
    'for(const x of [3, 2, 1]);',
    /Uncaught TypeError: \[3,2,1\] is not iterable/
  );
  await writeLn('.exit');

  assert(!replProcess.connected);
}

main().then(common.mustCall());