How to print errors in lua script without running script?

Viewed 256

I'm new using Lua in C++. I have some problem with printing error of script. My purpose is :

  1. Find errors of syntax, function name using luaL_dofile or luaL_dostring without running code like
    build codes in Visual studio. (When using luaL_loadfile or luaL_loadstring, it just find syntax error like 'a == 1', but can't find function name error like 'prit(a)')
  2. Want to print all errors.(In lua, if there was 2 or more errors, it only prints error that was occurred first)

For example I made lua script like this :

  • test.lua
function sayHello(name)
  print("Hello, " .. name .. "!")
end

for a=0, 10

  print(a)
end

sayHello()

and in c++ code :

  • test.cpp
...
int ret;

ret = luaL_dofile(LuaState, filename);
if(ret != 0) {
  cout << "error : " << lua_tostring(LuaState, -1) << endl;
  lua_pop(LuaState, 1);
}

I want to print errors like :

lua: test.lua:2: attempt to concatenate local `name' (a nil value)
lua: test.lua:7: `do' expected near `print'

Could anybody teach me?

0 Answers
Related