ma3oun
ma3oun

Reputation: 3790

Lua: Unable to load a module that loads another module

I am not able to load a module that requires another module in Lua. I have tried reading the official documentation and it is still unclear to me. Loading a single module works fine by using a combination of package.path and require. But nested calls to require fail and yield an error: too many C levels (limit is 200) in function at line

I have a project structured as follows:

./exeDir: contains tBig.lua

./utils: contains pkgBig.lua and pkgSmall.lua

pkgSmall.lua

-- this module is loaded later in pkgBig.lua
local function toto(s)
    print('Toto says: ' .. s)
end

local function dummy()
    print('Dummy')
end

pkgSmall = 
{
    toto = toto,
    dummy = dummy,  
}

return pkgSmall

pkgBig.lua

local myPkg = require 'pkgSmall'

local function titi(s)
    print('Titi says (followed by dummy): ' .. s)
    myPkg.dummy()
end

local function fifi()
    print('Calling toto from fifi...')
    myPkg.toto('FiFi called me')
end

pkgBig = 
{
    titi = titi,
    fifi = fifi,
}

return pkgBig

The main script:

tBig.lua

package.path = package.path .. ';' .. 'pathToUtils/pkgBig.lua'
local big = require 'pkgBig'

big.titi(' called from main')
big.fifi(' pkgSmall test')

Calling this script yields the "too many C levels..." error.

Upvotes: 1

Views: 585

Answers (1)

lhf
lhf

Reputation: 72312

I cannot reproduce your error.

But note that pathToUtils needs to contain patterns for module names.

This works fine for me:

pathToUtils = 'utils/?.lua'

Upvotes: 1

Related Questions