proxies-function.js 24.9 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
// 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.

28
// Flags: --harmony-proxies --allow-natives-syntax
29 30 31 32 33 34 35 36 37 38 39 40


// Helper.

function CreateFrozen(handler, callTrap, constructTrap) {
  if (handler.fix === undefined) handler.fix = function() { return {} }
  var f = Proxy.createFunction(handler, callTrap, constructTrap)
  Object.freeze(f)
  return f
}


41 42 43 44 45 46 47
// Ensures that checking the "length" property of a function proxy doesn't
// crash due to lack of a [[Get]] method.
var handler = {
  get : function(r, n) { return n == "length" ? 2 : undefined }
}


48 49 50 51 52 53 54 55
// Calling (call, Function.prototype.call, Function.prototype.apply,
//          Function.prototype.bind).

var global_object = this
var receiver

function TestCall(isStrict, callTrap) {
  assertEquals(42, callTrap(5, 37))
56
  assertSame(isStrict ? undefined : global_object, receiver)
57 58 59 60 61 62 63

  var handler = {
    get: function(r, k) {
      return k == "length" ? 2 : Function.prototype[k]
    }
  }
  var f = Proxy.createFunction(handler, callTrap)
64 65
  var o = {f: f}
  global_object.f = f
66 67 68

  receiver = 333
  assertEquals(42, f(11, 31))
69
  assertSame(isStrict ? undefined : global_object, receiver)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  receiver = 333
  assertEquals(42, o.f(10, 32))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, o["f"](9, 33))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, (1, o).f(8, 34))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, (1, o)["f"](7, 35))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, f.call(o, 32, 10))
  assertSame(o, receiver)
  receiver = 333
86 87 88
  assertEquals(42, f.call(undefined, 33, 9))
  assertSame(isStrict ? undefined : global_object, receiver)
  receiver = 333
89 90 91 92 93 94
  assertEquals(42, f.call(null, 33, 9))
  assertSame(isStrict ? null : global_object, receiver)
  receiver = 333
  assertEquals(44, f.call(2, 21, 23))
  assertSame(2, receiver.valueOf())
  receiver = 333
95
  assertEquals(42, Function.prototype.call.call(f, o, 20, 22))
96 97
  assertSame(o, receiver)
  receiver = 333
98
  assertEquals(43, Function.prototype.call.call(f, null, 20, 23))
99
  assertSame(isStrict ? null : global_object, receiver)
100 101 102
  assertEquals(44, Function.prototype.call.call(f, 2, 21, 23))
  assertEquals(2, receiver.valueOf())
  receiver = 333
103 104 105
  assertEquals(32, f.apply(o, [16, 16]))
  assertSame(o, receiver)
  receiver = 333
106
  assertEquals(32, Function.prototype.apply.call(f, o, [17, 15]))
107
  assertSame(o, receiver)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  receiver = 333
  assertEquals(42, %Call(o, 11, 31, f))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %Call(null, 11, 31, f))
  assertSame(isStrict ? null : global_object, receiver)
  receiver = 333
  assertEquals(42, %Apply(f, o, [11, 31], 0, 2))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %Apply(f, null, [11, 31], 0, 2))
  assertSame(isStrict ? null : global_object, receiver)
  receiver = 333
  assertEquals(42, %_CallFunction(o, 11, 31, f))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %_CallFunction(null, 11, 31, f))
  assertSame(isStrict ? null : global_object, receiver)
126

127
  var ff = Function.prototype.bind.call(f, o, 12)
128
  assertTrue(ff.length <= 1)  // TODO(rossberg): Not spec'ed yet, be lax.
129 130
  receiver = 333
  assertEquals(42, ff(30))
131
  assertSame(o, receiver)
132
  receiver = 333
133 134 135
  assertEquals(33, Function.prototype.call.call(ff, {}, 21))
  assertSame(o, receiver)
  receiver = 333
136
  assertEquals(32, Function.prototype.apply.call(ff, {}, [20]))
137
  assertSame(o, receiver)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  receiver = 333
  assertEquals(23, %Call({}, 11, ff))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(23, %Call({}, 11, 3, ff))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(24, %Apply(ff, {}, [12, 13], 0, 1))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(24, %Apply(ff, {}, [12, 13], 0, 2))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(34, %_CallFunction({}, 22, ff))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(34, %_CallFunction({}, 22, 3, ff))
  assertSame(o, receiver)
156 157 158 159 160 161 162 163 164

  var fff = Function.prototype.bind.call(ff, o, 30)
  assertEquals(0, fff.length)
  receiver = 333
  assertEquals(42, fff())
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, Function.prototype.call.call(fff, {}))
  assertSame(o, receiver)
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  receiver = 333
  assertEquals(42, Function.prototype.apply.call(fff, {}))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %Call({}, fff))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %Call({}, 11, 3, fff))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %Apply(fff, {}, [], 0, 0))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %Apply(fff, {}, [12, 13], 0, 0))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %Apply(fff, {}, [12, 13], 0, 2))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %_CallFunction({}, fff))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %_CallFunction({}, 3, 4, 5, fff))
  assertSame(o, receiver)
189 190 191 192

  var f = CreateFrozen({}, callTrap)
  receiver = 333
  assertEquals(42, f(11, 31))
193 194 195 196 197 198 199 200 201 202 203 204 205 206
  assertSame(isStrict ? undefined : global_object, receiver)
  var o = {f: f}
  receiver = 333
  assertEquals(42, o.f(10, 32))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, o["f"](9, 33))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, (1, o).f(8, 34))
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, (1, o)["f"](7, 35))
  assertSame(o, receiver)
207 208
  receiver = 333
  assertEquals(42, Function.prototype.call.call(f, o, 20, 22))
209
  assertSame(o, receiver)
210 211
  receiver = 333
  assertEquals(32, Function.prototype.apply.call(f, o, [17, 15]))
212
  assertSame(o, receiver)
213
  receiver = 333
214
  assertEquals(23, %Call(o, 11, 12, f))
215
  assertSame(o, receiver)
216
  receiver = 333
217
  assertEquals(27, %Apply(f, o, [12, 13, 14], 1, 2))
218 219 220
  assertSame(o, receiver)
  receiver = 333
  assertEquals(42, %_CallFunction(o, 18, 24, f))
221
  assertSame(o, receiver)
222 223 224
}

TestCall(false, function(x, y) {
225 226
  receiver = this
  return x + y
227 228 229
})

TestCall(true, function(x, y) {
230 231 232
  "use strict"
  receiver = this
  return x + y
233 234
})

235
TestCall(false, function() {
236 237
  receiver = this
  return arguments[0] + arguments[1]
238 239
})

240 241 242
TestCall(false, Proxy.createFunction(handler, function(x, y) {
  receiver = this
  return x + y
243 244
}))

245 246 247 248
TestCall(true, Proxy.createFunction(handler, function(x, y) {
  "use strict"
  receiver = this
  return x + y
249 250
}))

251 252 253
TestCall(false, CreateFrozen(handler, function(x, y) {
  receiver = this
  return x + y
254 255 256
}))


257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

// Using intrinsics as call traps.

function TestCallIntrinsic(type, callTrap) {
  var f = Proxy.createFunction({}, callTrap)
  var x = f()
  assertTrue(typeof x == type)
}

TestCallIntrinsic("boolean", Boolean)
TestCallIntrinsic("number", Number)
TestCallIntrinsic("string", String)
TestCallIntrinsic("object", Object)
TestCallIntrinsic("function", Function)



// Throwing from call trap.

276 277 278
function TestCallThrow(callTrap) {
  var f = Proxy.createFunction({}, callTrap)
  assertThrows(function(){ f(11) }, "myexn")
279 280
  assertThrows(function(){ ({x: f}).x(11) }, "myexn")
  assertThrows(function(){ ({x: f})["x"](11) }, "myexn")
281 282
  assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn")
  assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn")
283 284 285 286 287 288
  assertThrows(function(){ %Call({}, f) }, "myexn")
  assertThrows(function(){ %Call({}, 1, 2, f) }, "myexn")
  assertThrows(function(){ %Apply({}, f, [], 3, 0) }, "myexn")
  assertThrows(function(){ %Apply({}, f, [3, 4], 0, 1) }, "myexn")
  assertThrows(function(){ %_CallFunction({}, f) }, "myexn")
  assertThrows(function(){ %_CallFunction({}, 1, 2, f) }, "myexn")
289 290 291

  var f = CreateFrozen({}, callTrap)
  assertThrows(function(){ f(11) }, "myexn")
292 293
  assertThrows(function(){ ({x: f}).x(11) }, "myexn")
  assertThrows(function(){ ({x: f})["x"](11) }, "myexn")
294 295
  assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn")
  assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn")
296 297 298 299 300 301
  assertThrows(function(){ %Call({}, f) }, "myexn")
  assertThrows(function(){ %Call({}, 1, 2, f) }, "myexn")
  assertThrows(function(){ %Apply({}, f, [], 3, 0) }, "myexn")
  assertThrows(function(){ %Apply({}, f, [3, 4], 0, 1) }, "myexn")
  assertThrows(function(){ %_CallFunction({}, f) }, "myexn")
  assertThrows(function(){ %_CallFunction({}, 1, 2, f) }, "myexn")
302 303 304 305 306 307 308 309 310 311
}

TestCallThrow(function() { throw "myexn" })
TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" }))
TestCallThrow(CreateFrozen({}, function() { throw "myexn" }))



// Construction (new).

312
var prototype = {myprop: 0}
313 314 315
var receiver

var handlerWithPrototype = {
316 317 318 319 320 321
  fix: function() { return { prototype: { value: prototype } }; },
  get: function(r, n) {
    if (n == "length") return 2;
    assertEquals("prototype", n);
    return prototype;
  }
322 323 324
}

var handlerSansPrototype = {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  fix: function() { return { length: { value: 2 } } },
  get: function(r, n) {
    if (n == "length") return 2;
    assertEquals("prototype", n);
    return undefined;
  }
}

function ReturnUndef(x, y) {
  "use strict";
  receiver = this;
  this.sum = x + y;
}

function ReturnThis(x, y) {
  "use strict";
  receiver = this;
  this.sum = x + y;
  return this;
}

function ReturnNew(x, y) {
  "use strict";
  receiver = this;
  return {sum: x + y};
350 351 352 353 354
}

function ReturnNewWithProto(x, y) {
  "use strict";
  receiver = this;
355 356 357
  var result = Object.create(prototype);
  result.sum = x + y;
  return result;
358 359 360 361 362 363 364 365 366 367
}

function TestConstruct(proto, constructTrap) {
  TestConstruct2(proto, constructTrap, handlerWithPrototype)
  TestConstruct2(proto, constructTrap, handlerSansPrototype)
}

function TestConstruct2(proto, constructTrap, handler) {
  var f = Proxy.createFunction(handler, function() {}, constructTrap)
  var o = new f(11, 31)
368
  assertEquals(undefined, receiver)
369 370 371 372 373
  assertEquals(42, o.sum)
  assertSame(proto, Object.getPrototypeOf(o))

  var f = CreateFrozen(handler, function() {}, constructTrap)
  var o = new f(11, 32)
374
  assertEquals(undefined, receiver)
375 376 377 378 379 380 381
  assertEquals(43, o.sum)
  assertSame(proto, Object.getPrototypeOf(o))
}

TestConstruct(Object.prototype, ReturnNew)
TestConstruct(prototype, ReturnNewWithProto)

382 383
TestConstruct(Object.prototype, Proxy.createFunction(handler, ReturnNew))
TestConstruct(prototype, Proxy.createFunction(handler, ReturnNewWithProto))
384

385 386
TestConstruct(Object.prototype, CreateFrozen(handler, ReturnNew))
TestConstruct(prototype, CreateFrozen(handler, ReturnNewWithProto))
387 388


389 390 391

// Construction with derived construct trap.

392
function TestConstructFromCall(proto, returnsThis, callTrap) {
393
  TestConstructFromCall2(prototype, returnsThis, callTrap, handlerWithPrototype)
394 395 396 397
  TestConstructFromCall2(proto, returnsThis, callTrap, handlerSansPrototype)
}

function TestConstructFromCall2(proto, returnsThis, callTrap, handler) {
398 399
  // TODO(rossberg): handling of prototype for derived construct trap will be
  // fixed in a separate change. Commenting out checks below for now.
400 401 402 403
  var f = Proxy.createFunction(handler, callTrap)
  var o = new f(11, 31)
  if (returnsThis) assertEquals(o, receiver)
  assertEquals(42, o.sum)
404
  // assertSame(proto, Object.getPrototypeOf(o))
405

406 407 408
  var g = CreateFrozen(handler, callTrap)
  // assertSame(f.prototype, g.prototype)
  var o = new g(11, 32)
409 410
  if (returnsThis) assertEquals(o, receiver)
  assertEquals(43, o.sum)
411
  // assertSame(proto, Object.getPrototypeOf(o))
412 413 414 415 416 417 418
}

TestConstructFromCall(Object.prototype, true, ReturnUndef)
TestConstructFromCall(Object.prototype, true, ReturnThis)
TestConstructFromCall(Object.prototype, false, ReturnNew)
TestConstructFromCall(prototype, false, ReturnNewWithProto)

419 420 421 422 423 424 425 426
TestConstructFromCall(Object.prototype, true,
                      Proxy.createFunction(handler, ReturnUndef))
TestConstructFromCall(Object.prototype, true,
                      Proxy.createFunction(handler, ReturnThis))
TestConstructFromCall(Object.prototype, false,
                      Proxy.createFunction(handler, ReturnNew))
TestConstructFromCall(prototype, false,
                      Proxy.createFunction(handler, ReturnNewWithProto))
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnUndef))
TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnThis))
TestConstructFromCall(Object.prototype, false, CreateFrozen({}, ReturnNew))
TestConstructFromCall(prototype, false, CreateFrozen({}, ReturnNewWithProto))

ReturnUndef.prototype = prototype
ReturnThis.prototype = prototype
ReturnNew.prototype = prototype
ReturnNewWithProto.prototype = prototype

TestConstructFromCall(prototype, true, ReturnUndef)
TestConstructFromCall(prototype, true, ReturnThis)
TestConstructFromCall(Object.prototype, false, ReturnNew)
TestConstructFromCall(prototype, false, ReturnNewWithProto)

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
TestConstructFromCall(Object.prototype, true,
                      Proxy.createFunction(handler, ReturnUndef))
TestConstructFromCall(Object.prototype, true,
                      Proxy.createFunction(handler, ReturnThis))
TestConstructFromCall(Object.prototype, false,
                      Proxy.createFunction(handler, ReturnNew))
TestConstructFromCall(prototype, false,
                      Proxy.createFunction(handler, ReturnNewWithProto))

TestConstructFromCall(prototype, true,
                      Proxy.createFunction(handlerWithPrototype, ReturnUndef))
TestConstructFromCall(prototype, true,
                      Proxy.createFunction(handlerWithPrototype, ReturnThis))
TestConstructFromCall(Object.prototype, false,
                      Proxy.createFunction(handlerWithPrototype, ReturnNew))
TestConstructFromCall(prototype, false,
                      Proxy.createFunction(handlerWithPrototype,
                                           ReturnNewWithProto))

TestConstructFromCall(prototype, true,
                      CreateFrozen(handlerWithPrototype, ReturnUndef))
TestConstructFromCall(prototype, true,
                      CreateFrozen(handlerWithPrototype, ReturnThis))
TestConstructFromCall(Object.prototype, false,
                      CreateFrozen(handlerWithPrototype, ReturnNew))
TestConstructFromCall(prototype, false,
                      CreateFrozen(handlerWithPrototype, ReturnNewWithProto))
470 471


472 473 474

// Throwing from the construct trap.

475
function TestConstructThrow(trap) {
476 477 478 479 480
  TestConstructThrow2(Proxy.createFunction({ fix: function() {return {};} },
                                           trap))
  TestConstructThrow2(Proxy.createFunction({ fix: function() {return {};} },
                                           function() {},
                                           trap))
481 482 483 484 485 486 487 488 489 490 491 492 493 494
}

function TestConstructThrow2(f) {
  assertThrows(function(){ new f(11) }, "myexn")
  Object.freeze(f)
  assertThrows(function(){ new f(11) }, "myexn")
}

TestConstructThrow(function() { throw "myexn" })
TestConstructThrow(Proxy.createFunction({}, function() { throw "myexn" }))
TestConstructThrow(CreateFrozen({}, function() { throw "myexn" }))



495
// Using function proxies as getters and setters.
496 497 498 499 500

var value
var receiver

function TestAccessorCall(getterCallTrap, setterCallTrap) {
501
  var handler = { fix: function() { return {} } }
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
  var pgetter = Proxy.createFunction(handler, getterCallTrap)
  var psetter = Proxy.createFunction(handler, setterCallTrap)

  var o = {}
  var oo = Object.create(o)
  Object.defineProperty(o, "a", {get: pgetter, set: psetter})
  Object.defineProperty(o, "b", {get: pgetter})
  Object.defineProperty(o, "c", {set: psetter})
  Object.defineProperty(o, "3", {get: pgetter, set: psetter})
  Object.defineProperty(oo, "a", {value: 43})

  receiver = ""
  assertEquals(42, o.a)
  assertSame(o, receiver)
  receiver = ""
  assertEquals(42, o.b)
  assertSame(o, receiver)
  receiver = ""
  assertEquals(undefined, o.c)
  assertEquals("", receiver)
  receiver = ""
  assertEquals(42, o["a"])
  assertSame(o, receiver)
  receiver = ""
  assertEquals(42, o[3])
  assertSame(o, receiver)

  receiver = ""
  assertEquals(43, oo.a)
  assertEquals("", receiver)
  receiver = ""
  assertEquals(42, oo.b)
534
  assertSame(oo, receiver)
535 536 537 538 539 540 541 542
  receiver = ""
  assertEquals(undefined, oo.c)
  assertEquals("", receiver)
  receiver = ""
  assertEquals(43, oo["a"])
  assertEquals("", receiver)
  receiver = ""
  assertEquals(42, oo[3])
543
  assertSame(oo, receiver)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

  receiver = ""
  assertEquals(50, o.a = 50)
  assertSame(o, receiver)
  assertEquals(50, value)
  receiver = ""
  assertEquals(51, o.b = 51)
  assertEquals("", receiver)
  assertEquals(50, value)  // no setter
  assertThrows(function() { "use strict"; o.b = 51 }, TypeError)
  receiver = ""
  assertEquals(52, o.c = 52)
  assertSame(o, receiver)
  assertEquals(52, value)
  receiver = ""
  assertEquals(53, o["a"] = 53)
  assertSame(o, receiver)
  assertEquals(53, value)
  receiver = ""
  assertEquals(54, o[3] = 54)
  assertSame(o, receiver)
  assertEquals(54, value)

  value = 0
  receiver = ""
  assertEquals(60, oo.a = 60)
  assertEquals("", receiver)
  assertEquals(0, value)  // oo has own 'a'
  assertEquals(61, oo.b = 61)
  assertSame("", receiver)
  assertEquals(0, value)  // no setter
  assertThrows(function() { "use strict"; oo.b = 61 }, TypeError)
  receiver = ""
  assertEquals(62, oo.c = 62)
  assertSame(oo, receiver)
  assertEquals(62, value)
  receiver = ""
  assertEquals(63, oo["c"] = 63)
  assertSame(oo, receiver)
  assertEquals(63, value)
  receiver = ""
  assertEquals(64, oo[3] = 64)
  assertSame(oo, receiver)
  assertEquals(64, value)
}

TestAccessorCall(
  function() { receiver = this; return 42 },
  function(x) { receiver = this; value = x }
)

TestAccessorCall(
  function() { "use strict"; receiver = this; return 42 },
  function(x) { "use strict"; receiver = this; value = x }
)

TestAccessorCall(
  Proxy.createFunction({}, function() { receiver = this; return 42 }),
  Proxy.createFunction({}, function(x) { receiver = this; value = x })
)

TestAccessorCall(
  CreateFrozen({}, function() { receiver = this; return 42 }),
  CreateFrozen({}, function(x) { receiver = this; value = x })
)
609 610 611



612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
// Passing a proxy function to higher-order library functions.

function TestHigherOrder(f) {
  assertEquals(6, [6, 2].map(f)[0])
  assertEquals(4, [5, 2].reduce(f, 4))
  assertTrue([1, 2].some(f))
  assertEquals("a.b.c", "a.b.c".replace(".", f))
}

TestHigherOrder(function(x) { return x })
TestHigherOrder(function(x) { "use strict"; return x })
TestHigherOrder(Proxy.createFunction({}, function(x) { return x }))
TestHigherOrder(CreateFrozen({}, function(x) { return x }))



628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
// TODO(rossberg): Ultimately, I want to have the following test function
// run through, but it currently fails on so many cases (some not even
// involving proxies), that I leave that for later...
/*
function TestCalls() {
  var handler = {
    get: function(r, k) {
      return k == "length" ? 2 : Function.prototype[k]
    }
  }
  var bind = Function.prototype.bind
  var o = {}

  var traps = [
    function(x, y) {
      return {receiver: this, result: x + y, strict: false}
    },
    function(x, y) { "use strict";
      return {receiver: this, result: x + y, strict: true}
    },
    function() {
      var x = arguments[0], y = arguments[1]
      return {receiver: this, result: x + y, strict: false}
    },
    Proxy.createFunction(handler, function(x, y) {
      return {receiver: this, result: x + y, strict: false}
    }),
    Proxy.createFunction(handler, function() {
      var x = arguments[0], y = arguments[1]
      return {receiver: this, result: x + y, strict: false}
    }),
    Proxy.createFunction(handler, function(x, y) { "use strict"
      return {receiver: this, result: x + y, strict: true}
    }),
    CreateFrozen(handler, function(x, y) {
      return {receiver: this, result: x + y, strict: false}
    }),
    CreateFrozen(handler, function(x, y) { "use strict"
      return {receiver: this, result: x + y, strict: true}
    }),
  ]
  var creates = [
    function(trap) { return trap },
    function(trap) { return CreateFrozen({}, callTrap) },
    function(trap) { return Proxy.createFunction(handler, callTrap) },
    function(trap) {
      return Proxy.createFunction(handler, CreateFrozen({}, callTrap))
    },
    function(trap) {
      return Proxy.createFunction(handler, Proxy.createFunction(handler, callTrap))
    },
  ]
  var binds = [
    function(f, o, x, y) { return f },
    function(f, o, x, y) { return bind.call(f, o) },
    function(f, o, x, y) { return bind.call(f, o, x) },
    function(f, o, x, y) { return bind.call(f, o, x, y) },
    function(f, o, x, y) { return bind.call(f, o, x, y, 5) },
    function(f, o, x, y) { return bind.call(bind.call(f, o), {}, x, y) },
    function(f, o, x, y) { return bind.call(bind.call(f, o, x), {}, y) },
    function(f, o, x, y) { return bind.call(bind.call(f, o, x, y), {}, 5) },
  ]
  var calls = [
    function(f, x, y) { return f(x, y) },
    function(f, x, y) { var g = f; return g(x, y) },
    function(f, x, y) { with ({}) return f(x, y) },
    function(f, x, y) { var g = f; with ({}) return g(x, y) },
    function(f, x, y, o) { with (o) return f(x, y) },
    function(f, x, y, o) { return f.call(o, x, y) },
    function(f, x, y, o) { return f.apply(o, [x, y]) },
    function(f, x, y, o) { return Function.prototype.call.call(f, o, x, y) },
    function(f, x, y, o) { return Function.prototype.apply.call(f, o, [x, y]) },
    function(f, x, y, o) { return %_CallFunction(o, x, y, f) },
    function(f, x, y, o) { return %Call(o, x, y, f) },
    function(f, x, y, o) { return %Apply(f, o, [null, x, y, null], 1, 2) },
    function(f, x, y, o) { return %Apply(f, o, arguments, 2, 2) },
    function(f, x, y, o) { if (typeof o == "object") return o.f(x, y) },
    function(f, x, y, o) { if (typeof o == "object") return o["f"](x, y) },
    function(f, x, y, o) { if (typeof o == "object") return (1, o).f(x, y) },
    function(f, x, y, o) { if (typeof o == "object") return (1, o)["f"](x, y) },
  ]
  var receivers = [o, global_object, undefined, null, 2, "bla", true]
710
  var expectedSloppies = [o, global_object, global_object, global_object]
711 712 713 714 715 716 717 718 719 720 721

  for (var t = 0; t < traps.length; ++t) {
    for (var i = 0; i < creates.length; ++i) {
      for (var j = 0; j < binds.length; ++j) {
        for (var k = 0; k < calls.length; ++k) {
          for (var m = 0; m < receivers.length; ++m) {
            for (var n = 0; n < receivers.length; ++n) {
              var bound = receivers[m]
              var receiver = receivers[n]
              var func = binds[j](creates[i](traps[t]), bound, 31, 11)
              var expected = j > 0 ? bound : receiver
722
              var expectedSloppy = expectedSloppies[j > 0 ? m : n]
723 724 725 726 727 728 729 730 731
              o.f = func
              global_object.f = func
              var x = calls[k](func, 11, 31, receiver)
              if (x !== undefined) {
                assertEquals(42, x.result)
                if (calls[k].length < 4)
                  assertSame(x.strict ? undefined : global_object, x.receiver)
                else if (x.strict)
                  assertSame(expected, x.receiver)
732
                else if (expectedSloppy === undefined)
733 734
                  assertSame(expected, x.receiver.valueOf())
                else
735
                  assertSame(expectedSloppy, x.receiver)
736 737 738 739 740 741 742 743 744 745 746
              }
            }
          }
        }
      }
    }
  }
}

TestCalls()
*/
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765

var realms = [Realm.create(), Realm.create()];
Realm.shared = {};

Realm.eval(realms[0], "function f() { return this; };");
Realm.eval(realms[0], "Realm.shared.f = f;");
Realm.eval(realms[0], "Realm.shared.fg = this;");
Realm.eval(realms[1], "function g() { return this; };");
Realm.eval(realms[1], "Realm.shared.g = g;");
Realm.eval(realms[1], "Realm.shared.gg = this;");

var fp = Proxy.createFunction({}, Realm.shared.f);
var gp = Proxy.createFunction({}, Realm.shared.g);

for (var i = 0; i < 10; i++) {
  assertEquals(Realm.shared.fg, fp());
  assertEquals(Realm.shared.gg, gp());

  with (this) {
766 767
    assertEquals(this, fp());
    assertEquals(this, gp());
768 769 770 771 772 773 774
  }

  with ({}) {
    assertEquals(Realm.shared.fg, fp());
    assertEquals(Realm.shared.gg, gp());
  }
}