PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

LUA scripting and typos

I've got a project that I'm using LUA scripting with ( http://www.lua.org/ ) and I know there are several people here familiar with it...

My problem is that the people using it are going to mess up and type something like

x = sin(t)
instead of
x = math.sin(t)

When the above error is entered, it gives no error when executing "lua_dostring (L, luacode)" - but crashes my whole program when the LUA code is run.

Is there anything I'm missing with respect to running a check on the code? Or catching the error when it executes?

Thanks.
How is your C code calling into lua? Are you using lua_pcall?

Dan East
Oops, didn't read your post carefully enough. Instead of lua_dostring try luaL_loadbuffer to parse / compile then lua_pcall to execute.

Dan East
Ah... thanks for pointing me in the right direction Dan.

The compilation (lua_dostring vs. luaL_loadbuffer) step didn't matter, but I was using lua_call() instead of lua_pcall() - which does return an error code.

edit:
A nice descriptive error message is also pushed onto the stack by calling lua_tostring( L, lua_gettop( L ) ) ...
Code:
function calc(a,b,c,d,e,f) ..."]:2: attempt to call global `sin' (a nil value)