proxies-with.js 7.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


// Helper.

function TestWithProxies(test, x, y, z) {
32 33
  test(function(h) { return new Proxy({}, h) }, x, y, z)
  test(function(h) {
34
      return new Proxy(function() {}, h)
35
  }, x, y, z)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
}



// Getting.

function TestWithGet(handler) {
  TestWithProxies(TestWithGet2, handler)
}

var c = "global"
var key = ""

function TestWithGet2(create, handler) {
  var b = "local"

52 53 54 55 56
  var p = create(handler);
  assertEquals("onproxy", p.a);
  assertEquals(undefined, p.b);
  assertEquals(undefined, p.c);

57
  with (p) {
58 59 60
    assertEquals("onproxy", a);
    assertEquals("local", b);
    assertEquals("global", c);
61 62 63 64 65
  }

  var o = Object.create(p, {d: {value: "own"}})
  with (o) {
    assertEquals("onproxy", a)
66
    assertEquals("local", b);
67 68 69 70 71 72
    assertEquals("global", c)
    assertEquals("own", d)
  }
}

TestWithGet({
73
  get(target, k) {
74 75 76
    key = k;
    return k === "a" ? "onproxy" : undefined
  },
77
  has(target, k) { return k === 'a' }
78 79 80 81 82
})

TestWithGet({
  get: function(r, k) { return this.get2(r, k) },
  get2: function(r, k) { key = k; return k === "a" ? "onproxy" : undefined },
83
  has(target, k) { return k === 'a' }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
})




// Invoking.

function TestWithGetCall(handler) {
  TestWithProxies(TestWithGetCall2, handler)
}

var receiver = null
var c = function() { return "global" }

function TestWithGetCall2(create, handler) {
  var b = function() { return "local" }

  var p = create(handler)
  with (p) {
    receiver = null
    assertEquals("onproxy", a())
    assertSame(p, receiver)
    assertEquals("local", b())
    assertEquals("global", c())
  }

  var o = Object.create(p, {d: {value: function() { return "own" }}})
  with (o) {
    receiver = null
    assertEquals("onproxy", a())
    assertSame(o, receiver)
    assertEquals("local", b())
    assertEquals("global", c())
    assertEquals("own", d())
  }
}

function onproxy() { receiver = this; return "onproxy" }

TestWithGetCall({
  get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
125
  has: function(t, k) {
126
    key = k;
127
    return k === "a";
128 129 130 131 132 133
  }
})

TestWithGetCall({
  get: function(r, k) { return this.get2(r, k) },
  get2: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
134
  has: function(t, k) {
135
    key = k;
136
    return k === "a";
137 138 139 140
  }
})

TestWithGetCall({
141 142 143 144 145
  get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
  has: function(t, k) {
    return this.has2(k)
  },
  has2: function(k) {
146
    key = k;
147
    return k === "a";
148 149 150 151
  }
})

TestWithGetCall({
152 153
  get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
  has: function(t, k) {
154
    key = k;
155
    return k === "a";
156 157 158 159 160 161 162 163 164 165 166 167 168
  }
})


function TestWithGetCallThrow(handler) {
  TestWithProxies(TestWithGetCallThrow2, handler)
}

function TestWithGetCallThrow2(create, handler) {
  var b = function() { return "local" }

  var p = create(handler)
  with (p) {
169
    assertThrowsEquals(function(){ a() }, "myexn")
170 171 172 173 174 175
    assertEquals("local", b())
    assertEquals("global", c())
  }

  var o = Object.create(p, {d: {value: function() { return "own" }}})
  with (o) {
176
    assertThrowsEquals(function(){ a() }, "myexn")
177 178 179 180 181 182 183 184 185
    assertEquals("local", b())
    assertEquals("global", c())
    assertEquals("own", d())
  }
}

function onproxythrow() { throw "myexn" }

TestWithGetCallThrow({
186
  has: function(r, k) { return k === "a"; },
187 188 189 190
  get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
})

TestWithGetCallThrow({
191
  has: function(r, k) { return k === "a"; },
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  get: function(r, k) { return this.get2(r, k) },
  get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
})



// Setting.

var key
var val

function TestWithSet(handler, hasSetter) {
  TestWithProxies(TestWithSet2, handler, hasSetter)
}

var c = "global"

function TestWithSet2(create, handler, hasSetter) {
  var b = "local"

  var p = create(handler)
  key = val = undefined
  with (p) {
    a = "set"
    assertEquals("a", key)
    assertEquals("set", val)
    assertEquals("local", b)
    assertEquals("global", c)
    b = "local"
    c = "global"
    assertEquals("a", key)
    assertEquals("set", val)
  }

  if (!hasSetter) return

  var o = Object.create(p, {d: {value: "own"}})
  key = val = undefined
  with (o) {
    a = "set"
    assertEquals("a", key)
    assertEquals("set", val)
    assertEquals("local", b)
    assertEquals("global", c)
    assertEquals("own", d)
    b = "local"
    c = "global"
    d = "own"
    assertEquals("a", key)
    assertEquals("set", val)
  }
}

TestWithSet({
  set: function(r, k, v) { key = k; val = v; return true },
247 248
  has: function(t, k) {
    return k === "a"
249 250 251 252 253 254
  }
})

TestWithSet({
  set: function(r, k, v) { return this.set2(r, k, v) },
  set2: function(r, k, v) { key = k; val = v; return true },
255 256
  has: function(t, k) {
    return k === "a"
257 258 259 260
  }
})

TestWithSet({
261 262
  has: function(t, k) {
    return k === "a"
263
  },
264
  defineProperty: function(t, k, desc) { key = k; val = desc.value }
265 266 267
})

TestWithSet({
268 269
  has: function(t, k) {
    return this.has2(k)
270
  },
271 272
  has2: function(k) {
    return k === "a"
273
  },
274
  defineProperty: function(t, k, desc) { this.defineProperty2(k, desc) },
275 276 277 278
  defineProperty2: function(k, desc) { key = k; val = desc.value }
})

TestWithSet({
279 280
  has: function(t, k) {
    return k === "a"
281
  },
282
  defineProperty: function(t, k, desc) { key = k; val = desc.value }
283 284 285
})

TestWithSet({
286 287 288 289
  has: function(t, k) {
    return this.has2(k) },
  has2: function(k) {
    return k === "a"
290
  },
291
  set: function(t, k, v) { key = k; val = v; return true }
292 293 294
}, true)

TestWithSet({
295 296
  has: function(t, k) {
    return k === "a"
297
  },
298
  defineProperty: function(t, k, desc) { key = k; val = desc.value }
299 300 301 302 303 304 305 306 307
})


function TestWithSetThrow(handler, hasSetter) {
  TestWithProxies(TestWithSetThrow2, handler, hasSetter)
}

function TestWithSetThrow2(create, handler, hasSetter) {
  var p = create(handler)
308
  assertThrowsEquals(function(){
309 310 311 312 313 314 315 316
    with (p) {
      a = 1
    }
  }, "myexn")

  if (!hasSetter) return

  var o = Object.create(p, {})
317
  assertThrowsEquals(function(){
318 319 320 321 322 323 324
    with (o) {
      a = 1
    }
  }, "myexn")
}

TestWithSetThrow({
325 326 327
  set: function() { throw "myexn" },
  has: function(t, k) {
    return k === "a"
328 329 330 331
  }
})

TestWithSetThrow({
332
  has: function() { throw "myexn" },
333 334 335
})

TestWithSetThrow({
336 337 338 339 340 341
  has: function() { throw "myexn" },
})

TestWithSetThrow({
  has: function(t, k) {
    return k === "a"
342
  },
343
  defineProperty: function() { throw "myexn" }
344 345 346
})

TestWithSetThrow({
347 348 349 350
  has: function(t, k) {
    return k === "a"
  },
  set: function() { throw "myexn" }
351
}, true)