jeudi 19 février 2015

Handling Lua calls and errors in C in a bulletproof way

I work on Lua electronic device simulation plug in.

I want to make it good in design and usability, as most probably a lot of users are far from IT and debugging.

Users describe device in Lua and plug in translates it via Lua C API.

Users may misspell function names, variables types and arguments order.

Good manual helps but application shouldn't crash or provide bad described errors. I didn't find any "best practices" and want to ask experience users as I'm quite new to Lua.


Arguments' order and type


At the moment Lua functions are declared like this:



typedef struct lua_bind_func
{
int32_t ( *lua_c_api ) ( lua_State* );
const char* lua_func_name;
} lua_bind_func;


And then



{.lua_func_name="state_to_string", .lua_c_api=&lua_state_to_string},


In order to check that argument order and types are passed correct, I want to add a linked list to every function in the table. It will contain pointers to API's type checking functions in the right order:



lua_isboolean
lua_isinteger
lua_islightuserdata


In each function called from Lua I will check the stack and var types:



  1. Get the number of arguments in the stack

  2. In loop:

  3. Get the pointer from the function's arg list

  4. Call the func by the pointer to the var on stack, check result

  5. Increment index, repeat


Question: Is it OK scenario or may be there is a better one?


Misspelled function names


For example I have the following code:



set_callback(time + 100 * MSEC, PC_EVENT)
set_calback(time + 200 * MSEC, PC_EVENT)
set_callback(time + 333 * MSEC, PC_EVENT)


The second one is misspelled and after this part no code is executed.

I can't understand how to catch a call to undefined function in C in order to raise adequate error.


Other pitfalls


Maybe there are more possible pitfalls I don't know about?


Aucun commentaire:

Enregistrer un commentaire