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// Our own join method if the internal join is not implemented.
16function join(sep)
17{
18  sep = sep ? sep : ",";
19  var result = "";
20
21  for (var i = 0; i < this.length; ++i) {
22    result += this[i];
23    if (i + 1 < this.length) {
24      result += sep;
25    }
26  }
27
28  return result;
29}
30
31// Force fallback to object.prototype.toString()
32Array.prototype.join = 1;
33
34assert ([1].toString() === "[object Array]");
35
36Array.prototype.join = join;
37
38assert ([1, 2].toString() === "1,2");
39
40var test = [1,2,3];
41test.join = function() { throw ReferenceError ("foo"); };
42
43try {
44  test.toString();
45
46  assert (false);
47} catch (e) {
48  assert (e.message === "foo");
49  assert (e instanceof ReferenceError);
50}
51
52
53// Test if the join returns a ReferenceError
54var arr = [1,2]
55Object.defineProperty(arr, 'join', { 'get' : function () {throw new ReferenceError ("foo"); } });
56try {
57  arr.toString();
58
59  assert (false);
60} catch (e) {
61  assert (e.message === "foo");
62  assert (e instanceof ReferenceError);
63}
64