call-function-on-async.js 6.25 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 Runtime.callFunctionOn works with awaitPromise flag.');
let callFunctionOn = Protocol.Runtime.callFunctionOn.bind(Protocol.Runtime);
7

8 9
let remoteObject1;
let remoteObject2;
10
let executionContextId;
11

12 13 14 15 16 17 18
Protocol.Runtime.enable();
Protocol.Runtime.onExecutionContextCreated(messageObject => {
  executionContextId = messageObject.params.context.id;
  InspectorTest.runAsyncTestSuite(testSuite);
});

let testSuite = [
19 20 21 22 23
  async function prepareTestSuite() {
    let result = await Protocol.Runtime.evaluate({ expression: '({a : 1})' });
    remoteObject1 = result.result.result;
    result = await Protocol.Runtime.evaluate({ expression: '({a : 2})' });
    remoteObject2 = result.result.result;
24 25

    await Protocol.Runtime.evaluate({ expression: 'globalObjectProperty = 42;' });
26 27
  },

28 29 30 31 32 33 34 35 36
  async function testArguments() {
    InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: 'function(arg1, arg2, arg3, arg4) { return \'\' + arg1 + \'|\' + arg2 + \'|\' + arg3 + \'|\' + arg4; }',
      arguments: prepareArguments([undefined, NaN, remoteObject2, remoteObject1]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: false
    }));
37 38
  },

39 40 41 42 43 44 45 46 47 48 49
  async function testUnserializableArguments() {
    InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: 'function(arg1, arg2, arg3, arg4, arg5) { return \'\' + Object.is(arg1, -0) + \'|\' + Object.is(arg2, NaN) + \'|\' + Object.is(arg3, Infinity) + \'|\' + Object.is(arg4, -Infinity) + \'|\' + (typeof arg5); }',
      arguments: prepareArguments([-0, NaN, Infinity, -Infinity, 2n]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: false
    }));
  },

50 51 52 53 54 55 56 57 58 59 60
  async function testComplexArguments() {
    InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: 'function(arg) { return arg.foo; }',
      arguments: prepareArguments([{foo: 'bar'}]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: false
    }));
  },

61 62 63 64 65 66 67 68 69
  async function testSyntaxErrorInFunction() {
    InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: '\n  }',
      arguments: prepareArguments([]),
      returnByValue: false,
      generatePreview: false,
      awaitPromise: true
    }));
70 71
  },

72 73 74 75 76 77 78 79 80
  async function testExceptionInFunctionExpression() {
    InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: '(function() { throw new Error() })()',
      arguments: prepareArguments([]),
      returnByValue: false,
      generatePreview: false,
      awaitPromise: true
    }));
81 82
  },

83 84 85 86 87 88 89 90 91
  async function testFunctionReturnNotPromise() {
   InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: '(function() { return 239; })',
      arguments: prepareArguments([]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: true
    }));
92 93
  },

94 95 96 97 98 99 100 101 102
  async function testFunctionReturnResolvedPromiseReturnByValue() {
    InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: '(function(arg) { return Promise.resolve({a : this.a + arg.a}); })',
      arguments: prepareArguments([ remoteObject2 ]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: true
    }));
103 104
  },

105 106 107 108 109 110 111 112 113 114
  async function testFunctionReturnResolvedPromiseWithPreview() {
    InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: '(function(arg) { return Promise.resolve({a : this.a + arg.a}); })',
      arguments: prepareArguments([ remoteObject2 ]),
      returnByValue: false,
      generatePreview: true,
      awaitPromise: true
    }));
  },
115

116 117 118 119 120 121 122 123 124
  async function testFunctionReturnRejectedPromise() {
    InspectorTest.logMessage(await callFunctionOn({
      objectId: remoteObject1.objectId,
      functionDeclaration: '(function(arg) { return Promise.reject({a : this.a + arg.a}); })',
      arguments: prepareArguments([ remoteObject2 ]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: true
    }));
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  },

  async function testEvaluateOnExecutionContext() {
    InspectorTest.logMessage(await callFunctionOn({
      executionContextId,
      functionDeclaration: '(function(arg) { return this.globalObjectProperty + arg; })',
      arguments: prepareArguments([ 28 ]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: false
    }));
  },

  async function testPassingBothObjectIdAndExecutionContextId() {
    InspectorTest.logMessage(await callFunctionOn({
      executionContextId,
      objectId: remoteObject1.objectId,
      functionDeclaration: '(function() { return 42; })',
      arguments: prepareArguments([]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: false
    }));
  },
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  async function testThrowNumber() {
    InspectorTest.logMessage(await callFunctionOn({
      executionContextId,
      functionDeclaration: '(() => { throw 100500; } )',
      arguments: prepareArguments([]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: true
    }));
  },

  async function testAsyncFunctionWithUnknownReferenceReturnByValue() {
    InspectorTest.logMessage(await callFunctionOn({
      executionContextId,
      functionDeclaration: '(async () => does_not_exist.click())',
      arguments: prepareArguments([]),
      returnByValue: true,
      generatePreview: false,
      awaitPromise: true
    }));
  },
171
];
172

173 174 175 176 177 178
function prepareArguments(args) {
  return args.map(arg => {
    if (Object.is(arg, -0))
      return {unserializableValue: '-0'};
    if (Object.is(arg, NaN) || Object.is(arg, Infinity) || Object.is(arg, -Infinity))
      return {unserializableValue: arg + ''};
179 180
    if (typeof arg === 'bigint')
      return {unserializableValue: arg + 'n'};
181 182 183 184
    if (arg && arg.objectId)
      return {objectId: arg.objectId};
    return {value: arg};
  });
185
}