| 1 |
// |jit-test| skip-if: !getBuildConfiguration("decorators") |
| 2 |
|
| 3 |
load(libdir + "asserts.js"); |
| 4 |
|
| 5 |
let dec1Called = false; |
| 6 |
|
| 7 |
// This explicitly tests the case where undefined is returned. |
| 8 |
function dec1(value, context) { |
| 9 |
dec1Called = true; |
| 10 |
// return undefined |
| 11 |
} |
| 12 |
|
| 13 |
function dec2(value, context) { |
| 14 |
return (x) => "replaced: " + x; |
| 15 |
} |
| 16 |
|
| 17 |
function dec3(value, context) { |
| 18 |
return (x) => "decorated: " + value.call(this, x); |
| 19 |
} |
| 20 |
|
| 21 |
let decorators = { |
| 22 |
"dec4": (value, context) => { |
| 23 |
return (x) => "decorated: " + x; |
| 24 |
}, |
| 25 |
"more": { |
| 26 |
"deeply": { |
| 27 |
"nested": { |
| 28 |
"dec5": (value, context) => { |
| 29 |
return (x) => "decorated: " + x; |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
function dec6(arg) { |
| 37 |
return (value, context) => { |
| 38 |
return (x) => arg + " decorated: " + value.call(this, x); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
function checkDecoratorContext(kind, isPrivate, isStatic, name) { |
| 43 |
return (value, context) => { |
| 44 |
assertEq(typeof value.call, "function"); |
| 45 |
assertEq(context.kind, kind); |
| 46 |
assertEq(typeof context.access, "object"); |
| 47 |
assertEq(context.private, isPrivate); |
| 48 |
assertEq(context.static, isStatic); |
| 49 |
assertEq(context.name, name); |
| 50 |
if (isStatic) { |
| 51 |
assertEq(typeof context.addInitializer, "undefined"); |
| 52 |
} else { |
| 53 |
assertEq(typeof context.addInitializer, "function"); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
class C { |
| 59 |
@dec1 f1(x) { return "called with: " + x; } |
| 60 |
@dec2 f2(x) { return "called with: " + x; } |
| 61 |
@dec1 @dec2 f3(x) { return "called with: " + x; } |
| 62 |
@dec1 @dec2 @dec3 f4(x) { return "called with: " + x; } |
| 63 |
@decorators.dec4 f5(x) { return "called with: " + x; } |
| 64 |
@decorators.more.deeply.nested.dec5 f6(x) { return "called with: " + x; } |
| 65 |
@(() => { }) f7(x) { return "called with: " + x; } |
| 66 |
@((value, context) => { return (x) => "decorated: " + x; }) f8(x) { return "called with: " + x; } |
| 67 |
@dec6("hello!") f9(x) { return "called with: " + x; } |
| 68 |
|
| 69 |
static dec7(value, context) { return function(x) { return "replaced: " + x; } } |
| 70 |
static #dec8(value, context) { return function(x) { return "replaced: " + x; } } |
| 71 |
|
| 72 |
static { |
| 73 |
this.D = class { |
| 74 |
@C.dec7 static f10(x) { return "called with: " + x; } |
| 75 |
@C.#dec8 static f11(x) { return "called with: " + x; } |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
@checkDecoratorContext("method", false, false, "f12") f12(x) { return x; } |
| 80 |
@checkDecoratorContext("method", false, true, "f13") static f13(x) { return x; } |
| 81 |
@checkDecoratorContext("method", true, false, "#f14") #f14(x) { return x; } |
| 82 |
} |
| 83 |
|
| 84 |
let c = new C(); |
| 85 |
assertEq(dec1Called, true); |
| 86 |
assertEq(c.f1("value"), "called with: value"); |
| 87 |
assertEq(c.f2("value"), "replaced: value"); |
| 88 |
assertEq(c.f3("value"), "replaced: value"); |
| 89 |
assertEq(c.f4("value"), "replaced: value"); |
| 90 |
assertEq(c.f5("value"), "decorated: value"); |
| 91 |
assertEq(c.f6("value"), "decorated: value"); |
| 92 |
assertEq(c.f7("value"), "called with: value"); |
| 93 |
assertEq(c.f8("value"), "decorated: value"); |
| 94 |
assertEq(c.f9("value"), "hello! decorated: called with: value"); |
| 95 |
assertEq(C.D.f10("value"), "replaced: value"); |
| 96 |
assertEq(C.D.f11("value"), "replaced: value"); |
| 97 |
|
| 98 |
assertThrowsInstanceOf(() => { |
| 99 |
class D { |
| 100 |
@(() => { return "hello!"; }) f(x) { return x; } |
| 101 |
} |
| 102 |
}, TypeError), "Returning a value other than undefined or a callable throws."; |
| 103 |
|
| 104 |
const decoratorOrder = []; |
| 105 |
function makeOrderedDecorator(order) { |
| 106 |
return function (value, context) { |
| 107 |
decoratorOrder.push(order); |
| 108 |
return value; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
class E { |
| 113 |
@makeOrderedDecorator(1) @makeOrderedDecorator(2) @makeOrderedDecorator(3) |
| 114 |
f(x) { return x; } |
| 115 |
} |
| 116 |
|
| 117 |
let e = new E(); |
| 118 |
assertEq(e.f("value"), "value"); |
| 119 |
assertEq(decoratorOrder.length, 3); |
| 120 |
assertEq(decoratorOrder[0], 3); |
| 121 |
assertEq(decoratorOrder[1], 2); |
| 122 |
assertEq(decoratorOrder[2], 1); |