summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/js/builtins/Array.prototype.join-order.html
blob: e5589803a68bd0feda0689b85be6bd72f3da1ed2 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!doctype html>
<title>Array.prototype.join</title>
<link rel=author href=mailto:Ms2ger@gmail.com title=Ms2ger>
<link rel=help href=http://es5.github.com/#x15.4.4.5>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>

<div id=log></div>
<script>
var test_error = { name: "test" };

// Step 1.
test(function() {
  assert_throws(new TypeError(), function() {
    [].join.call(null, {
      toString: function() { assert_unreached(); }
    });
  });
  assert_throws(new TypeError(), function() {
    [].join.call(undefined, {
      toString: function() { assert_unreached(); }
    });
  });
}, "Array.prototype.join must call ToObject before looking at the separator argument.")

var generateTest = function(throwing_getter, getter_name) {
  var throwing_object = {};
  var interfaces = [Boolean, Number];

  var objects = interfaces.map(function(p) { return p.prototype; });
  objects.push(throwing_object);
  objects.forEach(function(object) {
    Object.defineProperty(object, "length", {
      get: throwing_getter,
      configurable: true
    });
  });

  [throwing_object, true, false, 0, 1, Math.PI].forEach(function(that) {
    test(function() {
      assert_throws(test_error, function() {
        [].join.call(that, ",");
      });
    }, "Array.prototype.join must forward the exception from the this " +
       "object's length property with this=" + format_value(that) + " and " +
       "getter " + getter_name + ".")
    test(function() {
      assert_throws(test_error, function() {
        [].join.call(that, {
          toString: function() { assert_unreached(); }
        });
      });
    }, "Array.prototype.join must get the this object's length property " +
       "before looking at the separator argument with this=" +
       format_value(that) + " and getter " + getter_name + ".")
  });
  interfaces.forEach(function(iface) {
    delete iface.length;
  });
}

// Step 2.
test(function() {
  generateTest(function() { throw test_error; }, "function");
}, "Step 2.");

// Step 3.
test(function() {
  generateTest(function() {
    return {
      valueOf: function() { throw test_error; }
    };
  }, "valueOf");
  generateTest(function() {
    return {
      toString: function() { throw test_error; }
    };
  }, "toString");
  generateTest(function() {
    return {
      valueOf: function() { throw test_error; },
      toString: function() { assert_unreached("toString should not be invoked if valueOf exists"); }
    };
  }, "valueOf and toString");
}, "Step 3.");
</script>