Question

TLDR; My functions are not callable from the _G scope, and I want them to be so I can call functions by parsing a list of strings. This could be exacerbated by a package.seeall required by the framework.

I am working in the luci framework, which is an UCI front end for OpenWRT routers. In an attempt to create customized functions that are called when specific UCI values are parsed I have created functions that share the name of the return value which are called through _G.

The parsed data looks something like this:

data = {Name="value"} 

I code looks like this:

function main()
    local pageValues={modules={}}
    for i,x in pairs(data) do
        --here is the part that throws the error
        pageValues.modules[x]=_G[x]()
        --also throws error when "newValue=_G[x]()
    end
end

function value()
--also does not work with "local function value()"
    return 'YEA'
end

The error I am getting returned to me is attempt to call field '?' (a nil value) When I directly pass it the string "value" with pageValues.modules['value']=_G['value']()I receive attempt to call field 'value' (a nil value) When is create a dummy variable as such

dummy = 'value'
pageValues.modules[dummy]=_G[dummy]()

I receive attempt to call field '?' (a nil value). This is the same if I use local dummy = 'value'

When I parse _G to see if any of my functions are in it I find that they are not. NOTE Luci automatically calls package.seeall, and I don't know if that is what is messing with my global's. But, it may be important in answering this question.

My most urgent need is to get this function running correctly. My secondary interest is in how I can determine the scope in Lua to be able to troubleshot sand-boxing issues in LuCI in the future.

Was it helpful?

Solution

Its a little hard to understand what is going on in your code, since its not a contained executable example, but one thing that could be happening is that, if the module you defined the globals has called the module function then whenever you try to set a global it will instead set a key in the module's table. Additionally, if you run module with the package.seeall parameter then global assignments are still redirected - the only difference is that the module can read other predefined global variables.

If this is the case, you would be able to solve your problem by either not trying to read the values from the global table (reading from the appropriate module instead) or by setting _G directly when creating those values in the first place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top