v8/test/inspector/runtime/call-function-on-side-effect-free.js
Benedikt Meurer 32328edd54 [inspector] Add throwOnSideEffect to Runtime.callFunctionOn.
In order to implement eager (side effect free) evaluation of arbitrary
accessor properties correctly, we need the ability to call getters while
guaranteeing that we don't trigger side effects. This is accomplished by
adding a `throwOnSideEffect` flag to the `Runtime.callFunctionOn` API,
similar to what's already available with the `Runtime.evaluate` and the
`Debugger.evaluateOnCallFrame` APIs.

Bug: chromium:1076820, chromium:1119900, chromium:1222114
Change-Id: If2d6c51376669cbc71a9dd3c79403d24d62aee43
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3001360
Auto-Submit: Benedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Yang Guo <yangguo@chromium.org>
Reviewed-by: Yang Guo <yangguo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#75556}
2021-07-05 12:53:07 +00:00

32 lines
1.4 KiB
JavaScript

// Copyright 2021 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.
let {session, contextGroup, Protocol} =
InspectorTest.start('Tests side-effect-free Runtime.callFunctionOn()');
async function check(callable, object, ...args) {
const functionDeclaration = callable.toString();
const {result:{exceptionDetails}} = await Protocol.Runtime.callFunctionOn({
objectId: object.objectId,
functionDeclaration,
arguments: args.map(arg => (arg && arg.objectId) ? {objectId: arg.objectId} : {value: arg}),
throwOnSideEffect: true,
});
InspectorTest.log(`${functionDeclaration}: ${exceptionDetails ? 'throws' : 'ok'}`);
};
InspectorTest.runAsyncTestSuite([
async function testCallFunctionOnSideEffectFree() {
await Protocol.Runtime.enable();
const {result: {result: object}} = await Protocol.Runtime.evaluate({ expression: '({a: 1, b: ""})'});
await check(function getA() { return this.a; }, object);
await check(function setA(a) { this.a = a; }, object, 1);
await check(function getB() { return this.b; }, object);
await check(function setB(b) { this.b = b; }, object, "lala");
await check(function setSomeGlobal() { globalThis.someGlobal = this; }, object);
await check(function localSideEffect() { const date = new Date(); date.setDate(this.a); return date; }, object);
await Protocol.Runtime.disable();
}
]);