summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/language/statements/class/definition/this-access-restriction-2.js
blob: e1f855aa1c5a0503fa3384a9bafce7805193216c (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
87
88
89
90
// Copyright (C) 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 14.5
description: >
    class this access restriction 2
---*/
class Base {
  constructor(a, b) {
    var o = new Object();
    o.prp = a + b;
    return o;
  }
}

class Subclass extends Base {
  constructor(a, b) {
    var exn;
    try {
      this.prp1 = 3;
    } catch (e) {
      exn = e;
    }
    assert.sameValue(
      exn instanceof ReferenceError,
      true,
      "The result of `exn instanceof ReferenceError` is `true`"
    );
    super(a, b);
    assert.sameValue(this.prp, a + b, "The value of `this.prp` is `a + b`");
    assert.sameValue(this.prp1, undefined, "The value of `this.prp1` is `undefined`");
    assert.sameValue(
      this.hasOwnProperty("prp1"),
      false,
      "`this.hasOwnProperty(\"prp1\")` returns `false`"
    );
    return this;
  }
}

var b = new Base(1, 2);
assert.sameValue(b.prp, 3, "The value of `b.prp` is `3`");


var s = new Subclass(2, -1);
assert.sameValue(s.prp, 1, "The value of `s.prp` is `1`");
assert.sameValue(s.prp1, undefined, "The value of `s.prp1` is `undefined`");
assert.sameValue(
  s.hasOwnProperty("prp1"),
  false,
  "`s.hasOwnProperty(\"prp1\")` returns `false`"
);

class Subclass2 extends Base {
  constructor(x) {
    super(1,2);

    if (x < 0) return;

    var called = false;
    function tmp() { called = true; return 3; }
    var exn = null;
    try {
      super(tmp(),4);
    } catch (e) { exn = e; }
    assert.sameValue(
      exn instanceof ReferenceError,
      true,
      "The result of `exn instanceof ReferenceError` is `true`"
    );
    assert.sameValue(called, true, "The value of `called` is `true`");
  }
}

var s2 = new Subclass2(1);
assert.sameValue(s2.prp, 3, "The value of `s2.prp` is `3`");

var s3 = new Subclass2(-1);
assert.sameValue(s3.prp, 3, "The value of `s3.prp` is `3`");

assert.throws(TypeError, function() { Subclass.call(new Object(), 1, 2); });
assert.throws(TypeError, function() { Base.call(new Object(), 1, 2); });

class BadSubclass extends Base {
  constructor() {}
}

assert.throws(ReferenceError, function() { new BadSubclass(); });

reportCompare(0, 0);