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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// |jit-test| skip-if: !getBuildConfiguration("decorators")
load(libdir + "asserts.js");
let dec1Called = false;
// This explicitly tests the case where undefined is returned.
function dec(value, context) {
dec1Called = true;
// return undefined
}
function decorate_getter(value, context) {
return {
get: function() {
return 2 * value.get.call(this);
}
};
}
function decorate_setter(value, context) {
return {
set: function(x) {
return value.set.call(this, 2*x);
}
};
}
function decorate_initializer(value, context) {
return {
init: function(initialValue) {
return 2 * initialValue;
}
};
}
function checkDecoratorContext(kind, isPrivate, isStatic, name) {
return (value, context) => {
assertEq(typeof value, "object");
assertEq(typeof value.get, "function");
assertEq(typeof value.set, "function");
assertEq(context.kind, kind);
assertEq(typeof context.access, "object");
assertEq(context.private, isPrivate);
assertEq(context.static, isStatic);
assertEq(context.name, name);
if (isStatic) {
assertEq(typeof context.addInitializer, "undefined");
} else {
assertEq(typeof context.addInitializer, "function");
}
// return undefined
}
}
class C {
@dec accessor x = 1;
@decorate_getter accessor x2 = 1;
@decorate_setter @decorate_getter accessor x3 = 1;
@decorate_initializer accessor x4 = 1;
@decorate_initializer @decorate_initializer accessor x5 = 1;
@decorate_setter @decorate_getter @decorate_initializer accessor x6 = 1;
@checkDecoratorContext("accessor", true, false, "x8 accessor storage") accessor x8 = 1;
@checkDecoratorContext("accessor", true, true, "x9 accessor storage") static accessor x9 = 1;
@checkDecoratorContext("accessor", true, false, "#x10 accessor storage") accessor #x10 = 1;
}
let c = new C();
assertEq(dec1Called, true);
assertEq(c.x, 1);
c.x = 2;
assertEq(c.x, 2);
assertEq(c.x2, 2);
assertEq(c.x3, 2);
c.x3 = 4;
assertEq(c.x3, 16);
assertEq(c.x4, 2);
assertEq(c.x5, 4);
assertEq(c.x6, 4);
c.x6 = 4;
assertEq(c.x6, 16);
class D {
@decorate_initializer accessor #x = 1;
@decorate_getter accessor #x2 = 1;
@decorate_setter @decorate_getter accessor #x3 = 1;
getX() {
return this.#x;
}
setX(v) {
this.#x = v;
}
getX2() {
return this.#x2;
}
setX2(v) {
this.#x2 = v;
}
getX3() {
return this.#x3;
}
setX3(v) {
this.#x3 = v;
}
}
let d = new D();
assertEq(d.getX(), 2);
d.setX(4);
assertEq(d.getX(), 4);
assertEq(d.getX2(), 2);
d.setX2(4);
assertEq(d.getX2(), 8);
assertEq(d.getX3(), 2);
d.setX3(4);
assertEq(d.getX3(), 16);
class E {
@decorate_getter static accessor x = 1;
}
assertEq(E.x, 2);
E.x = 2;
assertEq(E.x, 4);
class F {
@decorate_getter static accessor #x = 1;
getX() {
return F.#x;
}
setX(v) {
F.#x = v;
}
}
let f = new F();
assertEq(f.getX(), 2);
f.setX(4);
assertEq(f.getX(), 8);
assertThrowsInstanceOf(() => {
class G {
@(() => { return "hello!"; }) accessor x;
}
}, TypeError), "Returning a value other than undefined or a callable throws.";
assertThrowsInstanceOf(() => {
class G {
@(() => { return {get: "hello!"}; }) accessor x;
}
}, TypeError), "Returning a value other than undefined or a callable throws.";
assertThrowsInstanceOf(() => {
class G {
@(() => { return {set: "hello!"}; }) accessor x;
}
}, TypeError), "Returning a value other than undefined or a callable throws.";
assertThrowsInstanceOf(() => {
class G {
@(() => { return {init: "hello!"}; }) accessor x;
}
}, TypeError), "Returning a value other than undefined or a callable throws.";
const decoratorOrder = [];
function makeOrderedDecorator(order) {
return function (value, context) {
decoratorOrder.push(order);
return value;
}
}
class H {
@makeOrderedDecorator(1) @makeOrderedDecorator(2) @makeOrderedDecorator(3)
accessor x = 1;
}
let h = new H();
assertEq(decoratorOrder.length, 3);
assertEq(decoratorOrder[0], 3);
assertEq(decoratorOrder[1], 2);
assertEq(decoratorOrder[2], 1);
|