clear-of-command-line-api.js 3.9 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
let {session, contextGroup, Protocol} = InspectorTest.start("Tests that CommandLineAPI is presented only while evaluation.");
6

7
contextGroup.addScript(
8 9 10 11 12 13 14 15
`
var methods = ["dir","dirxml","profile","profileEnd","clear","table","keys","values","debug","undebug","monitor","unmonitor","inspect","copy"];
var window = this;
function presentedAPIMethods()
{
    var methodCount = 0;
    for (var method of methods) {
        try {
16
            if (eval("window." + method + "&&" + method + ".toString ? " + method + ".toString().indexOf(\\"[native code]\\") !== -1 : false"))
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
                ++methodCount;
        } catch (e) {
        }
    }
    methodCount += eval("\\"$_\\" in window ? $_ === 239 : false") ? 1 : 0;
    return methodCount;
}

function setPropertyForMethod()
{
    window.dir = 42;
}

function defineValuePropertyForMethod()
{
    Object.defineProperty(window, "dir", { value: 42 });
}

function defineAccessorPropertyForMethod()
{
    Object.defineProperty(window, "dir", { set: function() {}, get: function(){ return 42 } });
}

function definePropertiesForMethod()
{
    Object.defineProperties(window, { "dir": { set: function() {}, get: function(){ return 42 } }});
}

var builtinGetOwnPropertyDescriptorOnObject;
var builtinGetOwnPropertyDescriptorOnObjectPrototype;
var builtinGetOwnPropertyDescriptorOnWindow;

function redefineGetOwnPropertyDescriptors()
{
    builtinGetOwnPropertyDescriptorOnObject = Object.getOwnPropertyDescriptor;
    Object.getOwnPropertyDescriptor = function() {}
    builtinGetOwnPropertyDescriptorOnObjectPrototype = Object.prototype.getOwnPropertyDescriptor;
    Object.prototype.getOwnPropertyDescriptor = function() {}
    builtinGetOwnPropertyDescriptorOnWindow = window.getOwnPropertyDescriptor;
    window.getOwnPropertyDescriptor = function() {}
}

function restoreGetOwnPropertyDescriptors()
{
    Object.getOwnPropertyDescriptor = builtinGetOwnPropertyDescriptorOnObject;
    Object.prototype.getOwnPropertyDescriptor = builtinGetOwnPropertyDescriptorOnObjectPrototype;
    window.getOwnPropertyDescriptor = builtinGetOwnPropertyDescriptorOnWindow;
}`);

runExpressionAndDumpPresentedMethods("")
  .then(dumpLeftMethods)
  .then(() => runExpressionAndDumpPresentedMethods("setPropertyForMethod()"))
  .then(dumpLeftMethods)
  .then(dumpDir)
  .then(() => runExpressionAndDumpPresentedMethods("defineValuePropertyForMethod()"))
  .then(dumpLeftMethods)
  .then(dumpDir)
  .then(() => runExpressionAndDumpPresentedMethods("definePropertiesForMethod()"))
  .then(dumpLeftMethods)
  .then(dumpDir)
  .then(() => runExpressionAndDumpPresentedMethods("defineAccessorPropertyForMethod()"))
  .then(dumpLeftMethods)
  .then(dumpDir)
  .then(() => runExpressionAndDumpPresentedMethods("redefineGetOwnPropertyDescriptors()"))
  .then(dumpLeftMethods)
  .then(dumpDir)
  .then(() => evaluate("restoreGetOwnPropertyDescriptors()", false))
  .then(InspectorTest.completeTest);

function evaluate(expression, includeCommandLineAPI)
{
88
  return Protocol.Runtime.evaluate({ expression: expression, objectGroup: "console", includeCommandLineAPI: includeCommandLineAPI });
89 90 91 92 93 94 95 96 97 98 99 100
}

function setLastEvaluationResultTo239()
{
  return evaluate("239", false);
}

function runExpressionAndDumpPresentedMethods(expression)
{
  InspectorTest.log(expression);
  return setLastEvaluationResultTo239()
    .then(() => evaluate(expression + "; var a = presentedAPIMethods(); a", true))
101
    .then((result) => InspectorTest.logMessage(result));
102 103 104 105 106 107 108
}

function dumpLeftMethods()
{
  // Should always be zero.
  return setLastEvaluationResultTo239()
    .then(() => evaluate("presentedAPIMethods()", false))
109
    .then((result) => InspectorTest.logMessage(result));
110 111 112 113 114 115
}

function dumpDir()
{
  // Should always be presented.
  return evaluate("dir", false)
116
    .then((result) => InspectorTest.logMessage(result));
117
}