Home c++ Lua C++ error handling

Lua C++ error handling

Author

Date

Category

How can I implement LUA error output for example: “Syntax Error in Script.lua 4 Line”. I use Luabridge and Lua5.3. Here is my class who launches Loua Script:

//. H
#Pragma OCE
#Pragma Comment (lib, "lua53.lib")
Extern "C" {
#include "Lua.h"
#Include "Lauxlib.h"
#Include "Lualib.h"
}
#Include & lt; iostream & gt;
#Include & lt; luabridge / Luabridge.h & gt;
Using Namespace STD;
Using Namespace Luabridge;
Class Lua_Sup.
{
Private:
  String M_Name;
  Lua_State * L;
  BOOL INIT_STATE = FALSE;
Public:
  Lua_SUP (String Name);
  Lua_sup () {};
  ~ Lua_sup ();
  void init ();
  void test ();
  bool get_state () {return init_state; }
};
//.CPP.
 #include "lua_sup.h"
Void PrintMessage (Const Std :: String & Amp; S) {
  COUT & LT; & LT; "Print =" & lt; & lt; S & LT; & lt; Endl;
}
String Get_String () {
  Return "Hi";
}
Lua_Sup :: Lua_Sup (String Name)
{
  M_NAME = NAME;
  L = LUAL_NEWSTATE ();
  LUAL_OPENLIBS (L);
}
Lua_sup :: ~ lua_sup ()
{
  Lua_Close (L);
}
Void Lua_Sup :: Init ()
{
  // L = LUAL_NEWSTATE ();
  GetGlobalNameSpace (L) .DDFUNCTION ("PrintMessage", PrintMessage);
  int temp = lual_loadfile (L, M_NAME.C_STR ()) || lua_pcall (L, 0, 0, 0);
  if (temp) {
    COUT & LT; & LT; "Error";
    init_state = false;
  }
  GetGlobalNameSpace (L) .Addfunction ("Get_String", get_string);
  GetGlobalNameSpace (L) .DDFUNCTION ("PrintMessage", PrintMessage);
  // int temp = lua_pcall (l, 0, 0, 0);
  init_state = true;
}
Void Lua_Sup :: Test ()
{
  Luaref Hello_World_sum = GetGlobal (L, "Hello_World_SUM");
  Luaref plot = getGlobal (L, "Plot");
  for (int i = 0; i & lt; 3; i ++)
  {
    INT RESULT = Hello_World_SUM (1, 2);
    COUT & LT; & LT; "Result =" & lt; & lt; Result & lt; & lt; Endl;
  }
  INT RESULT = Hello_World_SUM (1, 2);
  COUT & LT; & LT; "Result =" & lt; & lt; Result & lt; & lt; Endl;
  getchar ();
}
// Main.
#include "lua_sup.h"
Using Namespace Luabridge;
INT MAIN () {
  Lua_Sup Lua ("script.lua");
  lua.init ();
  lua.test ();
  lua.test ();
  Lua_SUP Lua1 ("123.lua");
  lua1.init ();
  lua1.test ();
  lua.test ();
}

Answer 1

I do not know the bridge, but in the usual Lua did so

if (lual_dofile (l, name) {MessageBox (HWND, LUA_TOSTRING (L, -1), "Error", 0); EXIT (1);}

text with an explanation to the error is placed on the stack.
Running code The function returns 0 if there are no errors in the code.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions