summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/TypedObject/set-property-with-prototype.js
blob: 96bba9e54d82bbea461463044b4c387b01589bc4 (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

if (typeof TypedObject === "undefined")
    quit();

// Test the behavior of property sets on typed objects when they are a
// prototype or their prototype has a setter.
var TO = TypedObject;

function assertThrows(fun, errorType) {
  try {
    fun();
    assertEq(true, false, "Expected error, but none was thrown");
  } catch (e) {
    assertEq(e instanceof errorType, true, "Wrong error type thrown");
  }
}

var PointType = new TO.StructType({x: TO.int32, y: TO.int32 });

function testPoint() {
    var p = new PointType();
    var sub = Object.create(p);
    var found;
    Object.defineProperty(PointType.prototype, "z", {set: function(a) { this.d = a; }});
    Object.defineProperty(PointType.prototype, "innocuous", {set: function(a) { found = a; }});

    sub.x = 5;
    assertEq(sub.x, 5);
    assertEq(p.x, 0);

    sub.z = 5;
    assertEq(sub.d, 5);
    assertEq(sub.z, undefined);

    sub[3] = 5;
    assertEq(sub[3], 5);

    p.innocuous = 10;
    assertEq(found, 10);

    assertThrows(function() {
        p.z = 10;
        assertEq(true, false);
    }, TypeError);
}
testPoint();

var IntArrayType = new TO.ArrayType(TO.int32, 3);

function testArray() {
    var arr = new IntArrayType();
    var found;
    Object.defineProperty(IntArrayType.prototype, 5, {set: function(a) { found = a; }});

    assertThrows(function() {
        arr[5] = 5;
    }, RangeError);

    assertThrows(function() {
        arr[4] = 5;
    }, RangeError);

    var p = Object.create(arr);
    p.length = 100;
    assertEq(p.length, 3);

    assertThrows(function() {
        "use strict";
        p.length = 100;
    }, TypeError);
}
testArray();