suspended-generator-scopes.js 2.37 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5 6
let {session, contextGroup, Protocol} = InspectorTest.start('Tests that suspended generators produce scopes');

7 8
contextGroup.addScript(`
function *gen(a) {
9 10 11 12
  var b = 42;
  yield a;
  return b;
}
13 14

function testSuspendedGenerator() {
15 16 17
  var g = gen(420);
  g.next();
  debugger;
18
  return g;
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
InspectorTest.runAsyncTestSuite([
  async function testScopesPaused() {
    Protocol.Debugger.enable();
    Protocol.Runtime.evaluate({expression: 'testSuspendedGenerator()'});
    let {params:{callFrames:[callFrame]}} = await Protocol.Debugger.oncePaused();
    // Current local scope.
    let localScope = callFrame.scopeChain.find(scope => scope.type === 'local');
    let variables = (await Protocol.Runtime.getProperties({
      objectId: localScope.object.objectId
    })).result.result;
    let genObjectId =
      variables.find(variable => variable.name === 'g').value.objectId;
    let {result:{internalProperties}} = await Protocol.Runtime.getProperties({
      objectId: genObjectId
    });
    // Generator [[Scopes]].
    let scopes = internalProperties.find(prop => prop.name === '[[Scopes]]');
    let {result:{result}} = await Protocol.Runtime.getProperties({
      objectId: scopes.value.objectId
    });
    // Locals from generator.
    let scope = result.find(scope => scope.value.description === 'Local (gen)');
    ({result:{result}} = await Protocol.Runtime.getProperties({
      objectId: scope.value.objectId
    }));
    InspectorTest.logMessage(result);
    await Protocol.Debugger.disable();
  },
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  async function testScopesNonPaused() {
    let {result:{result:{objectId}}} = await Protocol.Runtime.evaluate({
      expression: 'gen(430)'
    });
    let {result:{internalProperties}} = await Protocol.Runtime.getProperties({
      objectId
    });
    // Generator [[Scopes]].
    let scopes = internalProperties.find(prop => prop.name === '[[Scopes]]');
    let {result:{result}} = await Protocol.Runtime.getProperties({
      objectId: scopes.value.objectId
    });
    // Locals from generator.
    let scope = result.find(scope => scope.value.description === 'Local (gen)');
    ({result:{result}} = await Protocol.Runtime.getProperties({
      objectId: scope.value.objectId
    }));
    InspectorTest.logMessage(result);
  }
]);