| 1 |
// |jit-test| skip-if: !getBuildConfiguration("decorators") |
| 2 |
|
| 3 |
load(libdir + "asserts.js"); |
| 4 |
|
| 5 |
let dec1Called = false; |
| 6 |
let dec2Called = false; |
| 7 |
|
| 8 |
// This explicitly tests the case where undefined is returned. |
| 9 |
function dec1(value, context) { |
| 10 |
dec1Called = true; |
| 11 |
// returns undefined |
| 12 |
} |
| 13 |
|
| 14 |
function dec2(value, context) { |
| 15 |
dec2Called = true; |
| 16 |
return (initialValue) => 2; |
| 17 |
} |
| 18 |
|
| 19 |
function dec3(value, context) { |
| 20 |
return (initialValue) => initialValue*2; |
| 21 |
} |
| 22 |
|
| 23 |
function checkDecoratorContext(kind, isPrivate, isStatic, name) { |
| 24 |
return (value, context) => { |
| 25 |
assertEq(value, undefined); |
| 26 |
assertEq(context.kind, kind); |
| 27 |
assertEq(typeof context.access, "object"); |
| 28 |
assertEq(context.private, isPrivate); |
| 29 |
assertEq(context.static, isStatic); |
| 30 |
assertEq(context.name, name); |
| 31 |
if (isStatic) { |
| 32 |
assertEq(typeof context.addInitializer, "undefined"); |
| 33 |
} else { |
| 34 |
assertEq(typeof context.addInitializer, "function"); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
class C { |
| 40 |
@dec1 x; |
| 41 |
@dec1 x2 = 1; |
| 42 |
@checkDecoratorContext("field", false, false, "x3") x3; |
| 43 |
@checkDecoratorContext("field", false, true, "x4") static x4; |
| 44 |
@checkDecoratorContext("field", true, false, "#x5") #x5; |
| 45 |
@dec2 x6; |
| 46 |
@dec3 x7 = 1; |
| 47 |
@dec2 @dec3 x8 = 1; |
| 48 |
@dec2 static x9; |
| 49 |
@dec2 #x10; |
| 50 |
getX10 = () => { |
| 51 |
return this.#x10; |
| 52 |
}; |
| 53 |
@dec3 #x11 = 2; |
| 54 |
getX11 = () => { |
| 55 |
return this.#x11; |
| 56 |
}; |
| 57 |
@dec1 42 = 1; |
| 58 |
@dec2 [43]; |
| 59 |
@dec2 static #x12 = 1; |
| 60 |
getX12() { |
| 61 |
return C.#x12; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
let c = new C(); |
| 67 |
assertEq(dec1Called, true); |
| 68 |
assertEq(c.x2, 1); |
| 69 |
assertEq(dec2Called, true); |
| 70 |
assertEq(c.x6, 2); |
| 71 |
assertEq(c.x7, 2); |
| 72 |
assertEq(c.x8, 2); |
| 73 |
assertEq(C.x9, 2); |
| 74 |
assertEq(c.getX10(), 2); |
| 75 |
assertEq(c.getX11(), 4); |
| 76 |
assertEq(c[42], 1); |
| 77 |
assertEq(c[43], 2); |
| 78 |
assertEq(c.getX12(), 2); |
| 79 |
|
| 80 |
assertThrowsInstanceOf(() => { |
| 81 |
class D { |
| 82 |
@(() => { return "hello!"; }) f(x) { return x; } |
| 83 |
} |
| 84 |
}, TypeError), "Returning a value other than undefined or a callable throws."; |
| 85 |
|
| 86 |
const decoratorOrder = []; |
| 87 |
function makeOrderedDecorator(order) { |
| 88 |
return function (value, context) { |
| 89 |
decoratorOrder.push(order); |
| 90 |
return value; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
class D { |
| 95 |
@makeOrderedDecorator(1) @makeOrderedDecorator(2) @makeOrderedDecorator(3) |
| 96 |
x = 1; |
| 97 |
} |
| 98 |
|
| 99 |
let d = new D(); |
| 100 |
assertEq(decoratorOrder.length, 3); |
| 101 |
assertEq(decoratorOrder[0], 3); |
| 102 |
assertEq(decoratorOrder[1], 2); |
| 103 |
assertEq(decoratorOrder[2], 1); |
| 104 |
assertEq(d.x, 1); |