Lua C Function

This is because in Lua functions are regular values (like numbers, strings, tables, etc.), and you can do anything with them that you can do with any other value. This is very different from many other languages that you might know (like C), where functions have permanent names fixed at compile-time, and can't be manipulated like values. Calling C functions from Lua. It gets very interesting when Lua programs call back to your C or C functions. We'll create a function called howdy that prints its input arguments and returns the integer 123. To be on the safe side, we'll declare C linkage for the function in the C version of the program. You can get anything that’s a callable in Lua, including C functions you bind using setfunction or similar. Sol::protectedfunction behaves similarly to sol::function, but has a errorhandler variable you can set to a Lua function. This catches all errors and runs them through the error-handling function.

Api

This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.

Programming in Lua
Part IV. The C APIChapter 25. Extending your Application

25.2 – Calling Lua Functions

A great strength of Lua is that a configuration file can definefunctions to be called by the application.For instance, you can write an application to plot the graphof a functionand use Lua to define the functions to be plotted.

The API protocol to call a function is simple:First, you push the function to be called;second, you push the arguments to the call;then you use lua_pcall to do the actual call;finally, you pop the results from the stack.

As an example, let us assume that our configuration filehas a function likeand you want to evaluate, in C,z = f(x, y) for given x and y.Assuming that you have already opened the Lua library and runthe configuration file,you can encapsulate this call in the following C function:

Lua C Function

You call lua_pcall with the number of arguments you are passingand the number of results you want.The fourth argument indicates an error-handling function;we will discuss it in a moment.As in a Lua assignment,lua_pcall adjusts the actual number of results to whatyou have asked for, pushing nils or discarding extra values as needed.Before pushing the results, lua_pcall removes from the stackthe function and its arguments.If a function returns multiple results,the first result is pushed first;so, if there are n results,the first one will be at index -nand the last at index -1.

Lua C Function Static

If there is any error while lua_pcall is running,lua_pcall returns a value different from zero;moreover, it pushes the error message on the stack(but still pops the function and its arguments).Before pushing the message, however,lua_pcall calls the error handler function,if there is one.To specify an error handler function,we use the last argument of lua_pcall.A zero means no error handler function;that is, the final error message is the original message.Otherwise, that argument should be the index in the stack wherethe error handler function is located.Notice that, in such cases,the handler must be pushed in the stackbefore the function to be called and its arguments.

Lua And C

For normal errors, lua_pcall returns theerror code LUA_ERRRUN.Two special kinds of errors deserve different codes,because they never run the error handler.The first kind is a memory allocation error.For such errors, lua_pcall always returns LUA_ERRMEM.The second kind is an error while Lua is running the error handler itself.In that case it is of little use to call the error handler again,so lua_pcall returns immediately with a code LUA_ERRERR.

Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.