Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

lua - Why the interpreter complains that library named "math" does not exist?

Why the interpreter complains that library named "math" does not exist?

As far as I know, this library is loaded when invoking luaL_newstate on Lua-5.3.5.

#include "lua.hpp"
#include <iostream>
#include <assert.h>
#include <fstream>

int main()
{
    struct lua_State *L = luaL_newstate();

    int ret;

    std::string fileName("co.lua");
    if(fileName.empty())
    {
        std::cout << "the filename is empty" << std::endl;
        return -1;
    }

    std::ifstream fileScript(fileName, fileScript.in|std::ios::ate);

    if(!fileScript.is_open())
    {
        std::cout << "open file failed" << std::endl;
        return -2;
    }

    size_t size = fileScript.tellg();

    if(size <= 0)
    {
        std::cout << "file has no valid content" << std::endl;
        return -3;
    }

    std::string textCont(size, '');

    fileScript.seekg(0);
    fileScript.read(&textCont[0], size);

    if((ret=luaL_loadbuffer(L, textCont.data(), textCont.length(), "co.lua")) == LUA_OK)
    {
        if((ret=lua_pcall(L, 0, LUA_MULTRET, 0)) != LUA_OK)   
        {
            std::cout << "error in invoking lua_pcall():" << ret << std::endl;
            if(lua_isstring(L, -1))
            {
                const char *errMsg = lua_tostring(L, -1);
                lua_pop(L, 1);
                std::cout << "script run encounter err:" << errMsg << std::endl;
            }
        }
    }
}

Here is the code snippet(it's very simple) for the file named "co.lua":

  a = 1;
  b=2;

  a=a+1;
  math.sin(a)

Here is the error message in the console:

error in invoking lua_pcall():2
script run encounter err:[string "co.lua"]:29: attempt to index a nil value (global 'math')
question from:https://stackoverflow.com/questions/65626447/why-the-interpreter-complains-that-library-named-math-does-not-exist

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The documentation states that you need to call luaL_openlibs or luaL_requiref which does not seem to be the case with your posted program.

To have access to these libraries, the C host program should call the luaL_openlibs function, which opens all standard libraries.

Alternatively (emphasis mine):

Alternatively, the host program can open them individually by using luaL_requiref to call:

  • luaopen_base (for the basic library)
  • luaopen_package (for the package library)
  • luaopen_coroutine (for the coroutine library)
  • luaopen_string (for the string library)
  • luaopen_utf8 (for the UTF8 library)
  • luaopen_table (for the table library)
  • luaopen_math (for the mathematical library)
  • luaopen_io (for the I/O library)
  • luaopen_os (for the operating system library)
  • luaopen_debug (for the debug library). These functions are declared in lualib.h.

So change your program's first few lines to something like below.

You also need to compare the return value from luaL_newstate with NULL and handle that error condition.

int main()
{
    struct lua_State *L = luaL_newstate();
    if( L == NULL ) {
        puts( "Lua failed to initialize." );
        exit(1);
    }

    luaL_openlibs( L );

    // etc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...