11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { spawn } = require('child_process');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst replProcess = spawn(process.argv0, ['--interactive'], {
71cb0ef41Sopenharmony_ci  stdio: ['pipe', 'pipe', 'inherit'],
81cb0ef41Sopenharmony_ci  windowsHide: true,
91cb0ef41Sopenharmony_ci});
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cireplProcess.on('error', common.mustNotCall());
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst replReadyState = (async function* () {
141cb0ef41Sopenharmony_ci  let ready;
151cb0ef41Sopenharmony_ci  const SPACE = ' '.charCodeAt();
161cb0ef41Sopenharmony_ci  const BRACKET = '>'.charCodeAt();
171cb0ef41Sopenharmony_ci  const DOT = '.'.charCodeAt();
181cb0ef41Sopenharmony_ci  replProcess.stdout.on('data', (data) => {
191cb0ef41Sopenharmony_ci    ready = data[data.length - 1] === SPACE && (
201cb0ef41Sopenharmony_ci      data[data.length - 2] === BRACKET || (
211cb0ef41Sopenharmony_ci        data[data.length - 2] === DOT &&
221cb0ef41Sopenharmony_ci        data[data.length - 3] === DOT &&
231cb0ef41Sopenharmony_ci        data[data.length - 4] === DOT
241cb0ef41Sopenharmony_ci      ));
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  const processCrashed = new Promise((resolve, reject) =>
281cb0ef41Sopenharmony_ci    replProcess.on('exit', reject)
291cb0ef41Sopenharmony_ci  );
301cb0ef41Sopenharmony_ci  while (true) {
311cb0ef41Sopenharmony_ci    await Promise.race([new Promise(setImmediate), processCrashed]);
321cb0ef41Sopenharmony_ci    if (ready) {
331cb0ef41Sopenharmony_ci      ready = false;
341cb0ef41Sopenharmony_ci      yield;
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci})();
381cb0ef41Sopenharmony_ciasync function writeLn(data, expectedOutput) {
391cb0ef41Sopenharmony_ci  await replReadyState.next();
401cb0ef41Sopenharmony_ci  if (expectedOutput) {
411cb0ef41Sopenharmony_ci    replProcess.stdout.once('data', common.mustCall((data) =>
421cb0ef41Sopenharmony_ci      assert.match(data.toString('utf8'), expectedOutput)
431cb0ef41Sopenharmony_ci    ));
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci  await new Promise((resolve, reject) => replProcess.stdin.write(
461cb0ef41Sopenharmony_ci    `${data}\n`,
471cb0ef41Sopenharmony_ci    (err) => (err ? reject(err) : resolve())
481cb0ef41Sopenharmony_ci  ));
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciasync function main() {
521cb0ef41Sopenharmony_ci  await writeLn(
531cb0ef41Sopenharmony_ci    'const ArrayIteratorPrototype =' +
541cb0ef41Sopenharmony_ci      '  Object.getPrototypeOf(Array.prototype[Symbol.iterator]());'
551cb0ef41Sopenharmony_ci  );
561cb0ef41Sopenharmony_ci  await writeLn('delete Array.prototype[Symbol.iterator];');
571cb0ef41Sopenharmony_ci  await writeLn('delete ArrayIteratorPrototype.next;');
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  await writeLn(
601cb0ef41Sopenharmony_ci    'for(const x of [3, 2, 1]);',
611cb0ef41Sopenharmony_ci    /Uncaught TypeError: \[3,2,1\] is not iterable/
621cb0ef41Sopenharmony_ci  );
631cb0ef41Sopenharmony_ci  await writeLn('.exit');
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  assert(!replProcess.connected);
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cimain().then(common.mustCall());
69