value-wrapper.js 6 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
// Copyright 2008 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.

// When calling user-defined functions on strings, booleans or
// numbers, we should create a wrapper object.

31 32 33
// When running the tests use loops to ensure that the call site moves through
// the different IC states and that both the runtime system and the generated
// IC code is tested.
verwaest@chromium.org's avatar
verwaest@chromium.org committed
34

35 36 37 38 39 40 41 42
function RunTests() {
  for (var i = 0; i < 10; i++) {
    assertEquals('object', 'xxx'.TypeOfThis());
    assertEquals('object', true.TypeOfThis(2,3));
    assertEquals('object', false.TypeOfThis());
    assertEquals('object', (42).TypeOfThis());
    assertEquals('object', (3.14).TypeOfThis());
  }
43

44 45 46 47 48 49 50
  for (var i = 0; i < 10; i++) {
    assertEquals('object', 'xxx'['TypeOfThis']());
    assertEquals('object', true['TypeOfThis']());
    assertEquals('object', false['TypeOfThis']());
    assertEquals('object', (42)['TypeOfThis']());
    assertEquals('object', (3.14)['TypeOfThis']());
  }
51

52 53 54
  function CallTypeOfThis(obj) {
    assertEquals('object', obj.TypeOfThis());
  }
55

56 57 58 59 60 61 62
  for (var i = 0; i < 10; i++) {
    CallTypeOfThis('xxx');
    CallTypeOfThis(true);
    CallTypeOfThis(false);
    CallTypeOfThis(42);
    CallTypeOfThis(3.14);
  }
63

64 65 66 67 68 69 70
  function TestWithWith(obj) {
    with (obj) {
      for (var i = 0; i < 10; i++) {
        assertEquals('object', TypeOfThis());
      }
    }
  }
71

72 73 74 75 76
  TestWithWith('xxx');
  TestWithWith(true);
  TestWithWith(false);
  TestWithWith(42);
  TestWithWith(3.14);
77

78 79 80 81 82 83
  for (var i = 0; i < 10; i++) {
    assertEquals('object', true[7]());
    assertEquals('object', false[7]());
    assertEquals('object', (42)[7]());
    assertEquals('object', (3.14)[7]());
  }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

  for (var i = 0; i < 10; i++) {
    assertEquals('object', typeof 'xxx'.ObjectValueOf());
    assertEquals('object', typeof true.ObjectValueOf());
    assertEquals('object', typeof false.ObjectValueOf());
    assertEquals('object', typeof (42).ObjectValueOf());
    assertEquals('object', typeof (3.14).ObjectValueOf());
  }

  for (var i = 0; i < 10; i++) {
    assertEquals('[object String]', 'xxx'.ObjectToString());
    assertEquals('[object Boolean]', true.ObjectToString());
    assertEquals('[object Boolean]', false.ObjectToString());
    assertEquals('[object Number]', (42).ObjectToString());
    assertEquals('[object Number]', (3.14).ObjectToString());
  }
100 101 102 103
}

function TypeOfThis() { return typeof this; }

104
// Test with normal setup of prototype.
105 106 107 108 109
String.prototype.TypeOfThis = TypeOfThis;
Boolean.prototype.TypeOfThis = TypeOfThis;
Number.prototype.TypeOfThis = TypeOfThis;
Boolean.prototype[7] = TypeOfThis;
Number.prototype[7] = TypeOfThis;
110 111 112 113 114 115 116 117

String.prototype.ObjectValueOf = Object.prototype.valueOf;
Boolean.prototype.ObjectValueOf = Object.prototype.valueOf;
Number.prototype.ObjectValueOf = Object.prototype.valueOf;

String.prototype.ObjectToString = Object.prototype.toString;
Boolean.prototype.ObjectToString = Object.prototype.toString;
Number.prototype.ObjectToString = Object.prototype.toString;
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

RunTests();

// Run test after properties have been set to a different value.
String.prototype.TypeOfThis = 'x';
Boolean.prototype.TypeOfThis = 'x';
Number.prototype.TypeOfThis = 'x';
Boolean.prototype[7] = 'x';
Number.prototype[7] = 'x';

String.prototype.TypeOfThis = TypeOfThis;
Boolean.prototype.TypeOfThis = TypeOfThis;
Number.prototype.TypeOfThis = TypeOfThis;
Boolean.prototype[7] = TypeOfThis;
Number.prototype[7] = TypeOfThis;

RunTests();

// Force the prototype into slow case and run the test again.
delete String.prototype.TypeOfThis;
delete Boolean.prototype.TypeOfThis;
delete Number.prototype.TypeOfThis;
Boolean.prototype[7];
Number.prototype[7];

String.prototype.TypeOfThis = TypeOfThis;
Boolean.prototype.TypeOfThis = TypeOfThis;
Number.prototype.TypeOfThis = TypeOfThis;
Boolean.prototype[7] = TypeOfThis;
Number.prototype[7] = TypeOfThis;

RunTests();

// According to ES3 15.3.4.3 the this value passed to Function.prototyle.apply
// should wrapped. According to ES5 it should not.
assertEquals('object', TypeOfThis.apply('xxx', []));
assertEquals('object', TypeOfThis.apply(true, []));
assertEquals('object', TypeOfThis.apply(false, []));
assertEquals('object', TypeOfThis.apply(42, []));
assertEquals('object', TypeOfThis.apply(3.14, []));

// According to ES3 15.3.4.3 the this value passed to Function.prototyle.call
// should wrapped. According to ES5 it should not.
assertEquals('object', TypeOfThis.call('xxx'));
assertEquals('object', TypeOfThis.call(true));
assertEquals('object', TypeOfThis.call(false));
assertEquals('object', TypeOfThis.call(42));
assertEquals('object', TypeOfThis.call(3.14));