Skip to content

Commit

Permalink
Allow calls directly from require statements, fixes #229
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 5, 2023
1 parent 1491231 commit 4a5e49c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
20 changes: 15 additions & 5 deletions lualib/nelua/cgenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ end
function visitors.Id(context, node, emitter, untypedinit)
local attr = node.attr
local type = attr.type
assert(not type.is_comptime)
if type.is_nilptr then
emitter:add_null()
elseif type.is_niltype then
Expand Down Expand Up @@ -689,13 +688,15 @@ function visitors.Call(context, node, emitter, untyped)
else -- usual function call
local callee = calleenode
local calleeattr = calleenode.attr
local calleesym = attr.calleesym
if calleeattr.builtin then -- is a builtin call?
local builtin = cbuiltins.calls[calleeattr.name]
callee = builtin(context, node, emitter)
elseif attr.calleesym then
if not attr.calleesym.anonfunc then -- pass node for anon functions to force its definition
callee = attr.calleesym
elseif calleesym then
if calleesym.type.is_function then -- force declaration of functions
emitter:fork():add(calleenode)
end
callee = calleesym
end
if callee then -- call not omitted?
visitor_Call(context, node, emitter, argnodes, callee)
Expand All @@ -714,7 +715,16 @@ function visitors.DotIndex(context, node, emitter, untypedinit)
local attr = node.attr
local objnode = node[2]
local objtype = objnode.attr.type
if attr.comptime then -- compile-time constant
if objnode.attr.requirename then -- require call
local rollbackpos = emitter:get_pos()
emitter:add_indent()
emitter:add(objnode)
if emitter:get_pos() == rollbackpos+1 then
emitter:rollback(rollbackpos) -- revert text added
else
emitter:add(';')
end
elseif attr.comptime then -- compile-time constant
emitter:add_literal(attr, untypedinit)
elseif objtype.is_type then -- global field
emitter:add(context:declname(attr))
Expand Down
16 changes: 16 additions & 0 deletions spec/cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3431,6 +3431,22 @@ it("require returns", function()
assert(r2.x == 2)
]])

fs.writefile('require_tmp.nelua', [[
local M = @record{}
function M.foo() return 'foo' end
return M
]])
expect.run_c([[
assert(require'require_tmp'.foo() == 'foo')
]])

fs.writefile('require_tmp.nelua', [[
return function() return 'foo' end
]])
expect.run_c([[
assert(require'require_tmp'() == 'foo')
]])

fs.deletefile('require_tmp.nelua')
end)

Expand Down
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nelua's Lua Interpreter

This is the Lua 5.4.4 interpreter used by Nelua, with the following changes:
This is the Lua 5.4.6 interpreter used by Nelua, with the following changes:

* Uses rpmalloc as the default memory allocator (usually much faster than the system's default memory allocator).
* Libraries "hasher", "lpeglabel", "sys" and "lfs" are bundled (they are required by Nelua compiler).
Expand Down

0 comments on commit 4a5e49c

Please sign in to comment.