Need help printing file and line numbers

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help printing file and line numbers

Eric Wing
Hi, I'm trying out LuaBind for the first time. I saw the section for
"file and line numbers" in the Error Handling section. Being able to
print this information is really important to me so I would like to
get a working example. But I am having a very bad time getting this to
work.

I have created a trivial C++ class and wrote a simple Lua script that
passes the wrong argument type that trips an error:
no overload of  'MyClass:Add' matched the arguments (MyClass, number, string)
candidates are:
MyClass:Add(custom [d], custom [d])

But I have been unable to figure out how to get the additional
information printed. Below is my test code. So far, the closest I've
been able to get is for pcall to call my designated error function
(copied from the documentation), but it was crashing for me. I
discovered that the call to lua_getstack was not returning as the
manual suggests it should.

Can somebody tell me how to do this correctly?

Thanks,
Eric



//#include <luabind/lua_include.h>
extern "C" {
        #include "lua.h"
        #include "lualib.h"
        #include "lauxlib.h"
}

#include <luabind/luabind.hpp>
#include <string>
#include <stdio.h>
#include <iostream>
#include <sstream>



int add_file_and_line(lua_State* L)
{
   int error;
   lua_Debug d;
   error = lua_getstack(L, 1, &d);
   if(1 != error)
   {
      fprintf(stderr, "Error occurred from lua_getstack\n");
      return 0;
   }

   error = lua_getinfo(L, "Sln", &d);
   if(0 == error)
   {
      fprintf(stderr, "Error occurred from lua_getinfo\n");
      return 0;
   }
       
   std::string err = lua_tostring(L, -1);
   lua_pop(L, 1);
   std::stringstream msg;
   msg << d.short_src << ":" << d.currentline;

   if (d.name != 0)
   {
      msg << "(" << d.namewhat << " " << d.name << ")";
   }
   msg << " " << err;
   lua_pushstring(L, msg.str().c_str());

   return 1;
}


class MyClass
{
public:
        MyClass() {}
        double Add(double a, double b) { return a + b; }
};

int main()
{
        lua_State* L = lua_open();
        lua_baselibopen(L);

        luabind::open(L);

        using namespace luabind;
        module(L)
        [
                class_<MyClass>("MyClass")
                        .def(constructor<>())
                        .def("Add", (double(MyClass::*)(double,double))&MyClass::Add)
        ];

        set_pcall_callback(add_file_and_line2);

        try
        {
                int error;

/*
                lua_dofile(L, "MyExample.lua");

*/
                luaL_loadfile(L, "MyExample.lua");
                lua_pushcfunction(L, add_file_and_line);
                int errfunc = lua_gettop(L);

                error = lua_pcall(L, 0, 0, errfunc);
                if(error)
                {
                        fprintf(stderr, "Error from pcall: \n");
                }
        }
        catch(const luabind::error& the_error)
        {
                luabind::object error_msg(luabind::from_stack(the_error.state(), -1));
                std::cout << error_msg << std::endl;
        }
       
        lua_close(L);
        return 0;
}

-- Lua script
foo = MyClass()
sum = foo:Add(4,"6") -- should complain about wrong type because 6 is
a string, not a number


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
<a href="http://sel.as-us.falkag.net/sel?cmd=lnk&kid3432&bid#0486&dat1642">http://sel.as-us.falkag.net/sel?cmd=lnk&kid3432&bid#0486&dat1642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Need help printing file and line numbers

Arvid Norberg

On Jan 31, 2006, at 03:40, E. Wing wrote:

> Hi, I'm trying out LuaBind for the first time. I saw the section for
> "file and line numbers" in the Error Handling section. Being able to
> print this information is really important to me so I would like to
> get a working example. But I am having a very bad time getting this to
> work.
>
> I have created a trivial C++ class and wrote a simple Lua script that
> passes the wrong argument type that trips an error:
> no overload of  'MyClass:Add' matched the arguments (MyClass,  
> number, string)
> candidates are:
> MyClass:Add(custom [d], custom [d])
>
> But I have been unable to figure out how to get the additional
> information printed. Below is my test code. So far, the closest I've
> been able to get is for pcall to call my designated error function
> (copied from the documentation), but it was crashing for me. I
> discovered that the call to lua_getstack was not returning as the
> manual suggests it should.
>
> Can somebody tell me how to do this correctly?
>
> Thanks,
> Eric
>
>
>
> //#include <luabind/lua_include.h>
> extern "C" {
> #include "lua.h"
> #include "lualib.h"
> #include "lauxlib.h"
> }
>
> #include <luabind/luabind.hpp>
> #include <string>
> #include <stdio.h>
> #include <iostream>
> #include <sstream>
>
>
>
> int add_file_and_line(lua_State* L)
> {
>    int error;
>    lua_Debug d;
>    error = lua_getstack(L, 1, &d);
>    if(1 != error)
>    {
>       fprintf(stderr, "Error occurred from lua_getstack\n");
>       return 0;
>    }
>
>    error = lua_getinfo(L, "Sln", &d);
>    if(0 == error)
>    {
>       fprintf(stderr, "Error occurred from lua_getinfo\n");
>       return 0;
>    }
>
>    std::string err = lua_tostring(L, -1);
>    lua_pop(L, 1);
>    std::stringstream msg;
>    msg << d.short_src << ":" << d.currentline;
>
>    if (d.name != 0)
>    {
>       msg << "(" << d.namewhat << " " << d.name << ")";
>    }
>    msg << " " << err;
>    lua_pushstring(L, msg.str().c_str());
>
>    return 1;
> }
>
>
> class MyClass
> {
> public:
> MyClass() {}
> double Add(double a, double b) { return a + b; }
> };
>
> int main()
> {
> lua_State* L = lua_open();
> lua_baselibopen(L);
>
> luabind::open(L);
>
> using namespace luabind;
> module(L)
> [
> class_<MyClass>("MyClass")
> .def(constructor<>())
> .def("Add", (double(MyClass::*)(double,double))&MyClass::Add)
> ];
>
> set_pcall_callback(add_file_and_line2);
>
> try
> {
> int error;
>
> /*
> lua_dofile(L, "MyExample.lua");
>
> */
   since lua_pcall() calls the function on the top of the stack,  
lua_loadfile() should be the last thing to call before lua_pcall. So,  
try these changes:

                lua_pushcfunction(L, add_file_and_line);
                luaL_loadfile(L, "MyExample.lua"); error = lua_pcall(L, 0, 0, -2);

> if(error)
> {
> fprintf(stderr, "Error from pcall: \n");
> }
> }
> catch(const luabind::error& the_error)
> {
> luabind::object error_msg(luabind::from_stack(the_error.state(),  
> -1));
> std::cout << error_msg << std::endl;
> }
>
> lua_close(L);
> return 0;
> }
>
> -- Lua script
> foo = MyClass()
> sum = foo:Add(4,"6") -- should complain about wrong type because 6 is
> a string, not a number


--
Arvid Norberg




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user