evaluate-on-call-frame.js 2.97 KB
Newer Older
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
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const {session, contextGroup, Protocol} =
    InspectorTest.start(`Test for Debugger.evaluateOnCallFrame`);

Protocol.Debugger.enable();
InspectorTest.runAsyncTestSuite([
  async function testFoo() {
    contextGroup.addInlineScript(`
      function foo(x) {
        var a;
        y = 0;
        a = x;
        y = 0;
      }
    `, 'foo.js');

    InspectorTest.log('Set breakpoint before a = x.');
    let {result:{breakpointId}} = await Protocol.Debugger.setBreakpointByUrl({
      lineNumber: 14,
      url: 'foo.js'
    });
    await evaluateOnDump('foo()', ['x', 'a']);
    await evaluateOnDump('foo("Hello, world!")', ['x', 'a']);

    await Protocol.Debugger.removeBreakpoint({
      breakpointId
    });

    InspectorTest.log('Set breakpoint after a = x.');
    await Protocol.Debugger.setBreakpointByUrl({
      lineNumber: 16,
      url: 'foo.js'
    });
    await evaluateOnDump('foo("Hello, world!")', ['x', 'a']);
  },

  async function testZoo() {
    contextGroup.addInlineScript(`
      x = undefined;
      function zoo(t) {
        var a = x;
        Object.prototype.x = 42;
        x = t;
        y = 0;
        delete Object.prototype.x;
        x = a;
      }
    `, 'zoo.js');

    InspectorTest.log('Set breakpoint before y = 0.');
    await Protocol.Debugger.setBreakpointByUrl({
      lineNumber: 47,
      url: 'zoo.js'
    });
    await evaluateOnDump('zoo("Hello, world!")', ['x', 'a']);
  },

  async function testBar() {
    contextGroup.addInlineScript(`
      y = 0;
      x = 'Goodbye, world!';
      function bar(x, b) {
        var a;
        function barbar() {
          y = 0;
          x = b;
          a = x;
        }
        barbar();
        y = 0;
      }
    `, 'bar.js');

    InspectorTest.log('Set breakpoint before a = x.');
    let {result:{breakpointId}} = await Protocol.Debugger.setBreakpointByUrl({
      lineNumber: 68,
      url: 'bar.js'
    });
    await evaluateOnDump('bar(undefined, "Hello, world!")', ['x', 'a']);

    await Protocol.Debugger.removeBreakpoint({
      breakpointId
    });
    InspectorTest.log('Set breakpoint after a = x.');
    await Protocol.Debugger.setBreakpointByUrl({
      lineNumber: 73,
      url: 'bar.js'
    });
    await evaluateOnDump('bar(undefined, "Hello, world!")', ['x', 'a']);
  }
]);

async function evaluateOnDump(expression, variables) {
  InspectorTest.log(expression);
  Protocol.Runtime.evaluate({
    expression: `${expression}//# sourceURL=expr.js`
  });
  const {params:{callFrames:[{callFrameId}]}} =
      await Protocol.Debugger.oncePaused();
  for (const variable of variables) {
    InspectorTest.log(`${variable} = `);
    const {result:{result}} = await Protocol.Debugger.evaluateOnCallFrame({
      callFrameId,
      expression: variable
    });
    InspectorTest.logMessage(result);
  }
  await Protocol.Debugger.resume();
  InspectorTest.log('');
}