11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
41cb0ef41Sopenharmony_ciconst { NodeInstance } = require('../common/inspector-helper.js');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst script = `
81cb0ef41Sopenharmony_ci  'use strict';
91cb0ef41Sopenharmony_ci  const assert = require('assert');
101cb0ef41Sopenharmony_ci  const vm = require('vm');
111cb0ef41Sopenharmony_ci  global.outer = true;
121cb0ef41Sopenharmony_ci  global.inner = false;
131cb0ef41Sopenharmony_ci  const context = vm.createContext({
141cb0ef41Sopenharmony_ci    outer: false,
151cb0ef41Sopenharmony_ci    inner: true
161cb0ef41Sopenharmony_ci  });
171cb0ef41Sopenharmony_ci  const script = new vm.Script("outer");
181cb0ef41Sopenharmony_ci  debugger;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  assert.strictEqual(script.runInThisContext(), true);
211cb0ef41Sopenharmony_ci  assert.strictEqual(script.runInContext(context), false);
221cb0ef41Sopenharmony_ci  debugger;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  vm.runInContext('inner', context);
251cb0ef41Sopenharmony_ci  debugger;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  vm.runInNewContext('Array', {});
281cb0ef41Sopenharmony_ci  debugger;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  vm.runInNewContext('debugger', {});
311cb0ef41Sopenharmony_ci`;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciasync function getContext(session) {
341cb0ef41Sopenharmony_ci  const created =
351cb0ef41Sopenharmony_ci      await session.waitForNotification('Runtime.executionContextCreated');
361cb0ef41Sopenharmony_ci  return created.params.context;
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciasync function checkScriptContext(session, context) {
401cb0ef41Sopenharmony_ci  const scriptParsed =
411cb0ef41Sopenharmony_ci      await session.waitForNotification('Debugger.scriptParsed');
421cb0ef41Sopenharmony_ci  assert.strictEqual(scriptParsed.params.executionContextId, context.id);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciasync function runTests() {
461cb0ef41Sopenharmony_ci  const instance = new NodeInstance(['--inspect-brk=0', '--expose-internals'],
471cb0ef41Sopenharmony_ci                                    script);
481cb0ef41Sopenharmony_ci  const session = await instance.connectInspectorSession();
491cb0ef41Sopenharmony_ci  await session.send([
501cb0ef41Sopenharmony_ci    { 'method': 'Debugger.enable' },
511cb0ef41Sopenharmony_ci    { 'method': 'Runtime.runIfWaitingForDebugger' },
521cb0ef41Sopenharmony_ci  ]);
531cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(2, '[eval]');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  await session.send({ 'method': 'Runtime.enable' });
561cb0ef41Sopenharmony_ci  await getContext(session);
571cb0ef41Sopenharmony_ci  await session.send({ 'method': 'Debugger.resume' });
581cb0ef41Sopenharmony_ci  const childContext = await getContext(session);
591cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(11, '[eval]');
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  console.error('[test]', 'Script is unbound');
621cb0ef41Sopenharmony_ci  await session.send({ 'method': 'Debugger.resume' });
631cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(15, '[eval]');
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  console.error('[test]', 'vm.runInContext associates script with context');
661cb0ef41Sopenharmony_ci  await session.send({ 'method': 'Debugger.resume' });
671cb0ef41Sopenharmony_ci  await checkScriptContext(session, childContext);
681cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(18, '[eval]');
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  console.error('[test]', 'vm.runInNewContext associates script with context');
711cb0ef41Sopenharmony_ci  await session.send({ 'method': 'Debugger.resume' });
721cb0ef41Sopenharmony_ci  const thirdContext = await getContext(session);
731cb0ef41Sopenharmony_ci  await checkScriptContext(session, thirdContext);
741cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(21, '[eval]');
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  console.error('[test]', 'vm.runInNewContext can contain debugger statements');
771cb0ef41Sopenharmony_ci  await session.send({ 'method': 'Debugger.resume' });
781cb0ef41Sopenharmony_ci  const fourthContext = await getContext(session);
791cb0ef41Sopenharmony_ci  await checkScriptContext(session, fourthContext);
801cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(0, 'evalmachine.<anonymous>');
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  await session.runToCompletion();
831cb0ef41Sopenharmony_ci  assert.strictEqual((await instance.expectShutdown()).exitCode, 0);
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_cirunTests().then(common.mustCall());
87