1 The `__call__` metamethod makes a value invocable. It is invoked as a method 2 on the instance, receiving the call arguments. 3 4 -- Testcase -- 5 {% 6 let c = proto({ base: 10 }, { 7 __call__(a, b) { 8 return this.base + a + b; 9 } 10 }); 11 12 print(c(1, 2), " ", length(c), "\n"); 13 %} 14 -- End -- 15 16 -- Expect stdout -- 17 13 1 18 -- End -- 19 20 A method call on a non-callable property value dispatches `__call__` of that 21 value, with the value itself as `this`. 22 23 -- Testcase -- 24 {% 25 let inner = proto({ tag: "inner" }, { 26 __call__(n) { 27 return this.tag + ":" + n; 28 } 29 }); 30 31 let outer = { m: inner }; 32 33 print(outer.m(7), "\n"); 34 print(inner("x"), "\n"); 35 %} 36 -- End -- 37 38 -- Expect stdout -- 39 inner:7 40 inner:x 41 -- End -- 42 43 A `__call__` which is not a function is not honored, and a value referring 44 to itself as `__call__` does not make it callable. 45 46 -- Testcase -- 47 {% 48 let o = proto({}, { __call__: "not callable" }); 49 50 try { 51 o(); 52 } 53 catch (e) { 54 print(e, "\n"); 55 } 56 57 let c = {}; 58 c.__call__ = c; 59 60 try { 61 c(); 62 } 63 catch (e) { 64 print(e, "\n"); 65 } 66 %} 67 -- End -- 68 69 -- Expect stdout -- 70 `o` is not a function, got { } instead 71 `c` is not a function, got { "__call__": null } instead 72 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt