1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// See a general usage: number addition.
16function addNum ()
17{
18  var sum = 0;
19  for(var i = 0; i < arguments.length; i++)
20  {
21    sum += arguments[i];
22  }
23  return sum;
24}
25
26var array = [6720, 4, 42];
27var obj;
28
29obj = addNum.apply(obj, array);
30assert (obj === 6766);
31
32// If the arguments are missing.
33obj = addNum.apply();
34assert (obj === 0);
35
36obj = addNum.apply(obj);
37assert (obj === 0);
38
39// If the function is a builtin.
40assert (Math.min.apply(null, array) === 4);
41assert (Math.max.apply(null, array) === 6720);
42
43// If the function can't be used as caller.
44try {
45  obj = new Function.prototype.apply();
46  assert (false);
47} catch (e) {
48  assert (e instanceof TypeError);
49}
50
51// If the called function throws an error.
52function throwError ()
53{
54  throw new ReferenceError ("foo");
55}
56
57try {
58  obj = throwError.apply(obj, array);
59  assert (false);
60} catch (e) {
61  assert (e.message === "foo");
62  assert (e instanceof ReferenceError);
63}
64
65// If the array access throws an error.
66Object.defineProperty(array, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
67
68try {
69  obj = addNum.apply(obj, array);
70  assert (false);
71} catch (e) {
72  assert (e.message === "foo");
73  assert (e instanceof ReferenceError);
74}
75