summaryrefslogtreecommitdiff
path: root/test/parallel/test-vm-debug-context.js
blob: 6cafc6b8bcb9cdd3fcfe8e4b7a2537aee4cebf12 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

/* eslint-disable no-debugger */
'use strict';
const common = require('../common');
const assert = require('assert');
const vm = require('vm');
const spawn = require('child_process').spawn;

assert.throws(function() {
  vm.runInDebugContext('*');
}, /SyntaxError/);

assert.throws(function() {
  vm.runInDebugContext({ toString: common.fail });
}, /AssertionError/);

assert.throws(function() {
  vm.runInDebugContext('throw URIError("BAM")');
}, /URIError/);

assert.throws(function() {
  vm.runInDebugContext('(function(f) { f(f) })(function(f) { f(f) })');
}, /RangeError/);

assert.strictEqual(typeof vm.runInDebugContext('this'), 'object');
assert.strictEqual(typeof vm.runInDebugContext('Debug'), 'object');

assert.strictEqual(vm.runInDebugContext(), undefined);
assert.strictEqual(vm.runInDebugContext(0), 0);
assert.strictEqual(vm.runInDebugContext(null), null);
assert.strictEqual(vm.runInDebugContext(undefined), undefined);

// See https://github.com/nodejs/node/issues/1190, accessing named interceptors
// and accessors inside a debug event listener should not crash.
{
  const Debug = vm.runInDebugContext('Debug');
  let breaks = 0;

  function ondebugevent(evt, exc) {
    if (evt !== Debug.DebugEvent.Break) return;
    exc.frame(0).evaluate('process.env').properties();  // Named interceptor.
    exc.frame(0).evaluate('process.title').getTruncatedValue();  // Accessor.
    breaks += 1;
  }

  function breakpoint() {
    debugger;
  }

  assert.strictEqual(breaks, 0);
  Debug.setListener(ondebugevent);
  assert.strictEqual(breaks, 0);
  breakpoint();
  assert.strictEqual(breaks, 1);
}

// Can set listeners and breakpoints on a single line file
{
  const Debug = vm.runInDebugContext('Debug');
  const fn = require(common.fixturesDir + '/exports-function-with-param');
  let called = false;

  Debug.setListener(function(event, state, data) {
    if (data.constructor.name === 'BreakEvent') {
      called = true;
    }
  });

  Debug.setBreakPoint(fn);
  fn('foo');
  assert.strictEqual(Debug.showBreakPoints(fn), '(arg) { [B0]return arg; }');
  assert.strictEqual(called, true);
}

// See https://github.com/nodejs/node/issues/1190, fatal errors should not
// crash the process.
const script = common.fixturesDir + '/vm-run-in-debug-context.js';
let proc = spawn(process.execPath, [script]);
const data = [];
proc.stdout.on('data', common.mustNotCall());
proc.stderr.on('data', data.push.bind(data));
proc.stderr.once('end', common.mustCall(function() {
  const haystack = Buffer.concat(data).toString('utf8');
  assert(/SyntaxError: Unexpected token \*/.test(haystack));
}));
proc.once('exit', common.mustCall(function(exitCode, signalCode) {
  assert.strictEqual(exitCode, 1);
  assert.strictEqual(signalCode, null);
}));

proc = spawn(process.execPath, [script, 'handle-fatal-exception']);
proc.stdout.on('data', common.mustNotCall());
proc.stderr.on('data', common.mustNotCall());
proc.once('exit', common.mustCall(function(exitCode, signalCode) {
  assert.strictEqual(exitCode, 42);
  assert.strictEqual(signalCode, null);
}));