Skip to content

Commit ff7a42c

Browse files
committed
Add tests for __call metamethod
1 parent 10194b4 commit ff7a42c

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

docs/pages/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,7 @@ Complete list of metamethods that can be defined for records:
13281328
| `__index` | `a[b]`{:.language-nelua} | indexing | array index |
13291329
| `__atindex` | `a[b]`{:.language-nelua} | indexing | array index via reference |
13301330
| `__tostring` | tostring(a) | cast | explicit/implicit cast to string |
1331+
| `__call` | `a(...)`{:.language-nelua} | call | call a record |
13311332
| `__convert` | | cast | implicit cast from anything |
13321333
| `__gc` | | gc | called when collected by the GC |
13331334
| `__close` | | close | called when `<close>` variables goes out of scope |

lualib/nelua/analyzer.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,11 +1318,10 @@ local function visitor_Call(context, node, argnodes, calleetype, calleesym, call
13181318
local attr = node.attr
13191319
if calleetype then
13201320
local sideeffect
1321-
local origintype = calleetype
1322-
if calleetype.is_record and calleetype.metafields.__call ~= nil then
1321+
if calleetype.is_record and calleetype.metafields.__call then
13231322
calleetype = calleetype.metafields.__call.type
13241323
if not calleetype.is_procedure then
1325-
node:raisef("invalid metamethod __call in '%s'", calleetype)
1324+
node:raisef("in record call: expected meta field '__call' to be a procedure but is of type '%s'", calleetype)
13261325
end
13271326
attr.ismetacall = true
13281327
calleeobjnode = node[2]

spec/analyzer_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,12 @@ it("records metamethods", function()
13061306
r[0] = x
13071307
local len = #r
13081308
]])
1309+
expect.analyze_error([[
1310+
local R = @record{}
1311+
global R.__call: integer = 1
1312+
local r: R
1313+
r()
1314+
]], "expected meta field '__call' to be a procedure")
13091315
expect.analyze_error([[
13101316
local R = @record{}
13111317
function R:__atindex(x: integer): integer return 0 end

spec/cgenerator_spec.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,13 @@ it("record metametods", function()
25572557
assert(3 == a)
25582558
assert(0 ~= a)
25592559
]])
2560+
2561+
expect.run_c([[
2562+
local Foo = @record{value: number}
2563+
function Foo:__call(a: number, b: number): number return a + b + self.value end
2564+
local foo: Foo = {1}
2565+
assert(foo(2, 3) == 6)
2566+
]])
25602567
end)
25612568

25622569
it("record operator overloading", function()

0 commit comments

Comments
 (0)