Tao.Lua SDK Documentation

Lua.lua_pcall Method 

Calls a function in protected mode.

[Visual Basic]
Public Shared Function lua_pcall( _
   ByVal L As IntPtr, _
   ByVal nargs As Integer, _
   ByVal nresults As Integer, _
   ByVal errfunc As Integer _
) As Integer
[C#]
public static int lua_pcall(
   IntPtr L,
   int nargs,
   int nresults,
   int errfunc
);

Parameters

L
nargs
Both nargs and nresults have the same meaning as in lua_call. If there are no errors during the call, lua_pcall behaves exactly like lua_call. However, if there is any error, lua_pcall catches it, pushes a single value on the stack (the error message), and returns an error code. Like lua_call, lua_pcall always removes the function and its arguments from the stack.
nresults
Both nargs and nresults have the same meaning as in lua_call. If there are no errors during the call, lua_pcall behaves exactly like lua_call. However, if there is any error, lua_pcall catches it, pushes a single value on the stack (the error message), and returns an error code. Like lua_call, lua_pcall always removes the function and its arguments from the stack.
errfunc
If errfunc is 0, then the error message returned on the stack is exactly the original error message. Otherwise, errfunc is the stack index of an error handler function. (In the current implementation, this index cannot be a pseudo-index.) In case of runtime errors, this function will be called with the error message and its return value will be the message returned on the stack by lua_pcall.

Return Value

The lua_pcall function returns 0 in case of success or one of the following error codes (defined in lua.h): LUA_ERRRUN: a runtime error. LUA_ERRMEM: memory allocation error. For such errors, Lua does not call the error handler function. LUA_ERRERR: error while running the error handler function.

Remarks

Typically, the error handler function is used to add more debug information to the error message, such as a stack traceback. Such information cannot be gathered after the return of lua_pcall, since by then the stack has unwound.

See Also

Lua Class | Tao.Lua Namespace