11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst ArrayStream = require('../common/arraystream');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst repl = require('repl');
61cb0ef41Sopenharmony_ciconst vm = require('vm');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Create a dummy stream that does nothing.
91cb0ef41Sopenharmony_ciconst stream = new ArrayStream();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// Test context when useGlobal is false.
121cb0ef41Sopenharmony_ci{
131cb0ef41Sopenharmony_ci  const r = repl.start({
141cb0ef41Sopenharmony_ci    input: stream,
151cb0ef41Sopenharmony_ci    output: stream,
161cb0ef41Sopenharmony_ci    useGlobal: false
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  let output = '';
201cb0ef41Sopenharmony_ci  stream.write = function(d) {
211cb0ef41Sopenharmony_ci    output += d;
221cb0ef41Sopenharmony_ci  };
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  // Ensure that the repl context gets its own "console" instance.
251cb0ef41Sopenharmony_ci  assert(r.context.console);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  // Ensure that the repl console instance is not the global one.
281cb0ef41Sopenharmony_ci  assert.notStrictEqual(r.context.console, console);
291cb0ef41Sopenharmony_ci  assert.notStrictEqual(r.context.Object, Object);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  stream.run(['({} instanceof Object)']);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  assert.strictEqual(output, 'true\n> ');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  const context = r.createContext();
361cb0ef41Sopenharmony_ci  // Ensure that the repl context gets its own "console" instance.
371cb0ef41Sopenharmony_ci  assert(context.console instanceof require('console').Console);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // Ensure that the repl's global property is the context.
401cb0ef41Sopenharmony_ci  assert.strictEqual(context.global, context);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  // Ensure that the repl console instance is writable.
431cb0ef41Sopenharmony_ci  context.console = 'foo';
441cb0ef41Sopenharmony_ci  r.close();
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci// Test for context side effects.
481cb0ef41Sopenharmony_ci{
491cb0ef41Sopenharmony_ci  const server = repl.start({ input: stream, output: stream });
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  assert.ok(!server.underscoreAssigned);
521cb0ef41Sopenharmony_ci  assert.strictEqual(server.lines.length, 0);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  // An assignment to '_' in the repl server
551cb0ef41Sopenharmony_ci  server.write('_ = 500;\n');
561cb0ef41Sopenharmony_ci  assert.ok(server.underscoreAssigned);
571cb0ef41Sopenharmony_ci  assert.strictEqual(server.lines.length, 1);
581cb0ef41Sopenharmony_ci  assert.strictEqual(server.lines[0], '_ = 500;');
591cb0ef41Sopenharmony_ci  assert.strictEqual(server.last, 500);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  // Use the server to create a new context
621cb0ef41Sopenharmony_ci  const context = server.createContext();
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  // Ensure that creating a new context does not
651cb0ef41Sopenharmony_ci  // have side effects on the server
661cb0ef41Sopenharmony_ci  assert.ok(server.underscoreAssigned);
671cb0ef41Sopenharmony_ci  assert.strictEqual(server.lines.length, 1);
681cb0ef41Sopenharmony_ci  assert.strictEqual(server.lines[0], '_ = 500;');
691cb0ef41Sopenharmony_ci  assert.strictEqual(server.last, 500);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  // Reset the server context
721cb0ef41Sopenharmony_ci  server.resetContext();
731cb0ef41Sopenharmony_ci  assert.ok(!server.underscoreAssigned);
741cb0ef41Sopenharmony_ci  assert.strictEqual(server.lines.length, 0);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  // Ensure that assigning to '_' in the new context
771cb0ef41Sopenharmony_ci  // does not change the value in our server.
781cb0ef41Sopenharmony_ci  assert.ok(!server.underscoreAssigned);
791cb0ef41Sopenharmony_ci  vm.runInContext('_ = 1000;\n', context);
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  assert.ok(!server.underscoreAssigned);
821cb0ef41Sopenharmony_ci  assert.strictEqual(server.lines.length, 0);
831cb0ef41Sopenharmony_ci  server.close();
841cb0ef41Sopenharmony_ci}
85