get-properties-on-proxy.js 4.57 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("Check that while Runtime.getProperties call on proxy object no user defined trap will be executed.");
6

7
contextGroup.addScript(`
8
var self = this;
9
function testFunction(revocable)
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
  self.counter = 0;
  var handler = {
    get: function(target, name){
      self.counter++;
      return Reflect.get.apply(this, arguments);
    },
    set: function(target, name){
      self.counter++;
      return Reflect.set.apply(this, arguments);
    },
    getPrototypeOf: function(target) {
      self.counter++;
      return Reflect.getPrototypeOf.apply(this, arguments);
    },
    setPrototypeOf: function(target) {
      self.counter++;
      return Reflect.setPrototypeOf.apply(this, arguments);
    },
    isExtensible: function(target) {
      self.counter++;
      return Reflect.isExtensible.apply(this, arguments);
    },
    isExtensible: function(target) {
      self.counter++;
      return Reflect.isExtensible.apply(this, arguments);
    },
    isExtensible: function(target) {
      self.counter++;
      return Reflect.isExtensible.apply(this, arguments);
    },
    preventExtensions: function() {
      self.counter++;
      return Reflect.preventExtensions.apply(this, arguments);
    },
    getOwnPropertyDescriptor: function() {
      self.counter++;
      return Reflect.getOwnPropertyDescriptor.apply(this, arguments);
    },
    defineProperty: function() {
      self.counter++;
      return Reflect.defineProperty.apply(this, arguments);
    },
    has: function() {
      self.counter++;
      return Reflect.has.apply(this, arguments);
    },
    get: function() {
      self.counter++;
      return Reflect.get.apply(this, arguments);
    },
    set: function() {
      self.counter++;
      return Reflect.set.apply(this, arguments);
    },
    deleteProperty: function() {
      self.counter++;
      return Reflect.deleteProperty.apply(this, arguments);
    },
    ownKeys: function() {
      self.counter++;
      return Reflect.ownKeys.apply(this, arguments);
    },
    apply: function() {
      self.counter++;
      return Reflect.apply.apply(this, arguments);
    },
    construct: function() {
      self.counter++;
      return Reflect.construct.apply(this, arguments);
    }
  };
  var obj = { a : 1 };
  if (revocable) {
    var revocableProxy = Proxy.revocable(obj, handler);
    return [revocableProxy.proxy, revocableProxy.revoke]
  } else {
    return new Proxy(obj, handler);
  }
89 90
}`);

91 92 93 94 95 96
function getArrayElement(arrayObjectId, idx) {
  return Protocol.Runtime.callFunctionOn({
    functionDeclaration: `function() { return this[${idx}]; }`,
    objectId: arrayObjectId
  });
}
97

98 99 100 101 102 103 104
async function testRegular() {
  InspectorTest.logMessage("Testing regular Proxy");

  var result = await Protocol.Runtime.evaluate({ expression: "testFunction(false)", generatePreview: true });
  InspectorTest.logMessage(result);
  var proxyId = result.result.result.objectId;
  InspectorTest.logMessage(await Protocol.Runtime.getProperties({ objectId: proxyId, generatePreview: true }));
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
async function testRevocable() {
  InspectorTest.logMessage("Testing revocable Proxy");

  var result = await Protocol.Runtime.evaluate({ expression: "testFunction(true)" });
  var proxyInfo = await getArrayElement(result.result.result.objectId, 0);
  var revokeInfo = await getArrayElement(result.result.result.objectId, 1);
  var proxyId = proxyInfo.result.result.objectId;
  InspectorTest.logMessage(await Protocol.Runtime.callFunctionOn({
    functionDeclaration: `function() { return this; }`,
    objectId: proxyId,
    generatePreview: true
  }))
  InspectorTest.logMessage(await Protocol.Runtime.getProperties({ objectId: proxyId, generatePreview: true }));
  await Protocol.Runtime.callFunctionOn({
    functionDeclaration: `function() { this(); }`,
    objectId: revokeInfo.result.result.objectId
  });
  InspectorTest.logMessage(await Protocol.Runtime.callFunctionOn({
    functionDeclaration: `function() { return this; }`,
    objectId: proxyId,
    generatePreview: true
  }))
  InspectorTest.logMessage(await Protocol.Runtime.getProperties({ objectId: proxyId, generatePreview: true }));
130 131
}

132 133 134 135
async function checkCounter() {
  InspectorTest.logMessage("Checking counter");

  var result = await Protocol.Runtime.evaluate({ expression: "self.counter" });
136
  InspectorTest.logMessage(result);
137
}
138 139 140 141 142 143 144

(async function test() {
  await testRegular();
  await testRevocable();
  await checkCounter();
  InspectorTest.completeTest();
})();