LuaInterface question

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

LuaInterface question

Geoff Smith
Hi

I am using LuaInterface, I am stuck on how to solve this annoying issue.

In C# code I  am using the RegisterFunction method to bind my lua functions to my C# functions
For example  in C# something like

lua.RegisterFunction("sleep", this, GetType().GetMethod("sleepLua"));

public void sleepLua(int periodInMs)
{ ... }

This all works fine until I write a lua script and make a mistake and pass in the wrong number of arguments to the  function.

For example in Lua if I have by mistake
sleep()   -- forgot to include the sleep period

I get the following error text displayed
"invalid arguments to method call"

This inadequate error message is a real pain for a big source file, as it gives me no clue where the issue is.

Is there a way I can find out the script line number where it hit the error, or maybe the lua file line number where it hit the error ?

Any help would be most appreciated.

Regards




Reply | Threaded
Open this post in threaded view
|

Re: LuaInterface question

Sven Olsen
Is there a way I can find out the script line number where it hit the error, or maybe the lua file line number where it hit the error ?

As per mentioned in this stackoverflow thread, there are at least two different ways of making this happen.  Either you can mod your copy of the lua source code to implicitly append a stack trace to any error message, or, you can define an error handler callback and use it with xpcall/lua_pcall, and potentially get a stack-trace extended message that way. I don't know anything about LuaInterface though -- so I'm not sure whether either of these would be applicable in your situation.

I've found patching the lua source to append stack traces to all error messages to be easy enough -- you basically just need to define an append_stack_trace(L) function, and then call it from ldebug.c:luaG_runerror() (and a few other places, like luaL_error, if you want the stack traces implicitly appended to errors thrown from your own code).  But, if you're not fluent in C, or don't have access to the sources for your lua interpretor, this approach may be non-trivial...  

If you don't want to start hacking your lua sources, just making a custom error handler function that does the stack trace append and then making sure you wrap whatever you're doing in either an xpcall/lua_pcall invocation that references said error handler is probably the faster/easier solution.

-Sven

Reply | Threaded
Open this post in threaded view
|

Re: LuaInterface question

Russell Haley


On Tue, Sep 3, 2019 at 2:27 PM Sven Olsen <[hidden email]> wrote:
Is there a way I can find out the script line number where it hit the error, or maybe the lua file line number where it hit the error ?

As per mentioned in this stackoverflow thread, there are at least two different ways of making this happen.  Either you can mod your copy of the lua source code to implicitly append a stack trace to any error message, or, you can define an error handler callback and use it with xpcall/lua_pcall, and potentially get a stack-trace extended message that way. I don't know anything about LuaInterface though -- so I'm not sure whether either of these would be applicable in your situation.

I've found patching the lua source to append stack traces to all error messages to be easy enough -- you basically just need to define an append_stack_trace(L) function, and then call it from ldebug.c:luaG_runerror() (and a few other places, like luaL_error, if you want the stack traces implicitly appended to errors thrown from your own code).  But, if you're not fluent in C, or don't have access to the sources for your lua interpretor, this approach may be non-trivial...  

If you don't want to start hacking your lua sources, just making a custom error handler function that does the stack trace append and then making sure you wrap whatever you're doing in either an xpcall/lua_pcall invocation that references said error handler is probably the faster/easier solution.

-Sven

NLua is the "spiritual successor" to LuaInterface, though I'm not sure if there is better debugging capabilities. I've also used NeoLua which is written on top of the .Net dynamic runtime. I know that NeoLua has debugging features that you need to explicitly turn on in order to use (which I have not). 

Either way, both NLua and NeoLua are currently supported so you might have better luck getting support?

For what it's worth,
Russ