Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add __call metamethod support #256

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lualib/nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,15 @@ local function visitor_Call(context, node, argnodes, calleetype, calleesym, call
local attr = node.attr
if calleetype then
local sideeffect
local origintype = calleetype
if calleetype.is_record and calleetype.metafields.__call ~= nil then
calleetype = calleetype.metafields.__call.type
if not calleetype.is_procedure then
node:raisef("invalid metamethod __call in '%s'", calleetype)
end
attr.ismetacall = true
calleeobjnode = node[2]
end
if calleetype.is_procedure then -- function call
local argattrs = {}
for i=1,#argnodes do
Expand Down Expand Up @@ -1503,7 +1512,11 @@ local function visitor_Call(context, node, argnodes, calleetype, calleesym, call
end
end
end
attr.calleesym = calleesym
if attr.ismetacall then
attr.calleesym = calleetype.symbol
else
attr.calleesym = calleesym
end
if calleetype then
attr.type, attr.value = calleetype:get_return_type_and_value(1)
sideeffect = calleetype.sideeffect
Expand Down
6 changes: 5 additions & 1 deletion lualib/nelua/cgenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,11 @@ local function visitor_Call(context, node, emitter, argnodes, callee, calleeobjn
emitter:add_value(callee)
end
emitter:add_text('(')
emitter:add_converted_val(selftype, calleeobj, calleeobjtype)
if attr.ismetacall then
emitter:add_converted_val(selftype, node[2], calleeobjtype)
else
emitter:add_converted_val(selftype, calleeobj, calleeobjtype)
end
else
emitter:add_value(callee)
emitter:add_text('(')
Expand Down