The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / mjsunit / apply.js @ 40c0f755

History | View | Annotate | Download (6.08 KB)

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

    
28
function f0() {
29
  return this;
30
}
31

    
32
function f1(a) {
33
  return a;
34
}
35

    
36
assertTrue(this === f0.apply(), "1-0");
37

    
38
assertTrue(this === f0.apply(this), "2a");
39
assertTrue(this === f0.apply(this, new Array(1)), "2b");
40
assertTrue(this === f0.apply(this, new Array(2)), "2c");
41
assertTrue(this === f0.apply(this, new Array(4242)), "2c");
42

    
43
assertTrue(this === f0.apply(null), "3a");
44
assertTrue(this === f0.apply(null, new Array(1)), "3b");
45
assertTrue(this === f0.apply(null, new Array(2)), "3c");
46
assertTrue(this === f0.apply(this, new Array(4242)), "2c");
47

    
48
assertTrue(this === f0.apply(void 0), "4a");
49
assertTrue(this === f0.apply(void 0, new Array(1)), "4b");
50
assertTrue(this === f0.apply(void 0, new Array(2)), "4c");
51

    
52
assertTrue(void 0 === f1.apply(), "1-1");
53

    
54
assertTrue(void 0 === f1.apply(this), "2a");
55
assertTrue(void 0 === f1.apply(this, new Array(1)), "2b");
56
assertTrue(void 0 === f1.apply(this, new Array(2)), "2c");
57
assertTrue(void 0 === f1.apply(this, new Array(4242)), "2c");
58
assertTrue(42 === f1.apply(this, new Array(42, 43)), "2c");
59
assertEquals("foo", f1.apply(this, new Array("foo", "bar", "baz", "boo")), "2c");
60

    
61
assertTrue(void 0 === f1.apply(null), "3a");
62
assertTrue(void 0 === f1.apply(null, new Array(1)), "3b");
63
assertTrue(void 0 === f1.apply(null, new Array(2)), "3c");
64
assertTrue(void 0 === f1.apply(null, new Array(4242)), "2c");
65
assertTrue(42 === f1.apply(null, new Array(42, 43)), "2c");
66
assertEquals("foo", f1.apply(null, new Array("foo", "bar", "baz", "boo")), "2c");
67

    
68
assertTrue(void 0 === f1.apply(void 0), "4a");
69
assertTrue(void 0 === f1.apply(void 0, new Array(1)), "4b");
70
assertTrue(void 0 === f1.apply(void 0, new Array(2)), "4c");
71
assertTrue(void 0 === f1.apply(void 0, new Array(4242)), "4c");
72
assertTrue(42 === f1.apply(void 0, new Array(42, 43)), "2c");
73
assertEquals("foo", f1.apply(void 0, new Array("foo", "bar", "baz", "boo")), "2c");
74

    
75
var arr = new Array(42, "foo", "fish", "horse");
76
function j(a, b, c, d, e, f, g, h, i, j, k, l) {
77
  return "" + a + b + c + d + e + f + g + h + i + j + k + l;
78
}
79

    
80

    
81
var expect = "42foofishhorse";
82
for (var i = 0; i < 8; i++)
83
  expect += "undefined";
84
assertEquals(expect, j.apply(undefined, arr));
85

    
86
assertThrows("f0.apply(this, 1);");
87
assertThrows("f0.apply(this, 1, 2);");
88
assertThrows("f0.apply(this, 1, new Array(2));");
89

    
90
function f() {
91
  var doo = "";
92
  for (var i = 0; i < arguments.length; i++) {
93
    doo += arguments[i];
94
  }
95
  return doo;
96
}
97
  
98
assertEquals("42foofishhorse", f.apply(this, arr));
99

    
100
function s() {
101
  var doo = this;
102
  for (var i = 0; i < arguments.length; i++) {
103
    doo += arguments[i];
104
  }
105
  return doo;
106
}
107

    
108
assertEquals("bar42foofishhorse", s.apply("bar", arr));
109

    
110
function al() {
111
  assertEquals(345, this);
112
  return arguments.length + arguments[arguments.length - 1];
113
}
114

    
115
for (var j = 1; j < 0x40000000; j <<= 1) {
116
  try {
117
    var a = new Array(j);
118
    a[j - 1] = 42;
119
    assertEquals(42 + j, al.apply(345, a));
120
  } catch (e) {
121
    assertTrue(e.toString().indexOf("Function.prototype.apply") != -1);
122
    for (; j < 0x40000000; j <<= 1) {
123
      var caught = false;
124
      try {
125
        a = new Array(j);
126
        a[j - 1] = 42;
127
        al.apply(345, a);
128
        assertEquals("Shouldn't get", "here");
129
      } catch (e) {
130
        assertTrue(e.toString().indexOf("Function.prototype.apply") != -1);
131
        caught = true;
132
      }
133
      assertTrue(caught);
134
    }
135
    break;
136
  }
137
}
138

    
139
var primes = new Array(0);
140

    
141
function isPrime(possible_prime) {
142
  for (var d = 0; d < primes.length; d++) {
143
    var p = primes[d];
144
    if (possible_prime % p == 0)
145
      return false;
146
    if (p * p > possible_prime)
147
      return true;
148
  }
149
  return true;
150
}
151

    
152
for (var i = 2; i < 10000; i++) {
153
  if (isPrime(i)) {
154
    primes.push(i);
155
  }
156
}
157

    
158
assertEquals(1229, primes.length);
159

    
160
var same_primes = Array.prototype.constructor.apply(Array, primes);
161

    
162
for (var i = 0; i < primes.length; i++)
163
  assertEquals(primes[i], same_primes[i]);
164
assertEquals(primes.length, same_primes.length);
165

    
166

    
167
Array.prototype["1"] = "sep";
168

    
169
var holey = new Array(3);
170
holey[0] = "mor";
171
holey[2] = "er";
172

    
173
assertEquals("morseper", String.prototype.concat.apply("", holey));
174
assertEquals("morseper", String.prototype.concat.apply("", holey, 1));
175
assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2));
176
assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2, 3));
177
assertEquals("morseper", String.prototype.concat.apply("", holey, 1, 2, 3, 4));
178

    
179
primes[0] = "";
180
primes[1] = holey;
181
assertThrows("String.prototype.concat.apply.apply('foo', primes)");
182
assertEquals("morseper", String.prototype.concat.apply.apply(String.prototype.concat, primes));
183

    
184
delete(Array.prototype["1"]);