Question

_ENV currently hates using classes. I'm attempting to create a Latin kind of Lua, and some things can't precisely be made with _ENV but it's a lot more efficient. setfenv was being uncooperative as well.
The subsequent code keeps returning the old attempt to call a nil value error. It specifies line 20, in which loadstring() is being checked, and line 23, where in the main chunk it is calling the __latin() function.

function __latin(code)
    __predecessor = [===[
    function typographia(value)
        print(value); 
    end

    chorda = {};
    chorda.__index = chorda; 

    function chorda.sub(chorda, cChorda, fChorda)
        return string.sub(chorda, cChorda, fChorda); 
    end
    function chorda:sub(chorda, cChorda, fChorda)
        return string.sub(chorda, cChorda, fChorda);
    end

    --[[ define values --]]
    _ENV = {salve="Salve, munde!",typographia=typographia,print=print,chorda=chorda,chorda.sub=chorda.sub};  ]===]; 
    __finalizer = __predecessor .. " " .. code; 
    local status, err = pcall(loadstring(__finalizer));
    print(err); 
    if (err == nil) then loadstring(__finalizer)(); end 
end
__latin('typographia(salve); chorda.sub(salve, 1, 3);'); 
Was it helpful?

Solution

You're getting "attempt to call a nil value" because loadstring returns nil (so you're calling pcall with nil). loadstring returns nil because the code you're compiling (__predecessor) contains invalid Lua:

_ENV = {
    chorda.sub = chorda.sub -- can't do this
}

FYI: The loadstring/pcall stuff is irrelevant to your problem. Had you stripped it out before posting, you would have found this error yourself.

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