Call C Function In Lua

This article will present an introductory example of calling lua functions from C++.

26 – Calling C from Lua One of the basic means for extending Lua is for the application to register new C functions into Lua. When we say that Lua can call C functions, this does not mean that Lua can call any C function. (There are packages that allow Lua to call any C function, but they are neither portable nor robust.). Alternatively, the Lua code may be a callback in a GUI framework, which needs to call C functions to change the state of the GUI. Vegas pro 11 serial number. Any C function called from within Lua must have a particular signature, and must assume a particular calling convention. The arguments are pushed onto the Lua stack, then the function is called.

If you want to extend and customize the capabilities of a C++ applicationwithout requiring a full recompilation, then using a embedded scripting languageis the way to go. Lua is one such embeddable scripting language, and is very popularamong game developers. The main advantage of Lua, in my opinion, is that the core APIis very minimal, has very small memory footprint. The availability of LuaJIT makesit a very performant alternative as well.

Lua Call Dll

This article is a continuation of the earlier introductory article Minimal C++ Example < filename minimal-lua.rst>.In this article we will discuss how to call lua functions from C++.

Code Explained

The initial part of the code initialises the lua_State loads and executes the script with lua_pcall(L,0,0,0).Once the script is loaded, the functions are available in the global namespace. Here the script basically is a functionthat takes two numbers and returns the sum of the two. Using the lua_getglobal(L, 'sum') call,we load the function into the stack. We can check if the function was loaded correctly using the lua_isfunction(L, -1).Then we pass the two arguments of the function by pushing them into the stack. Then the lua_pcall method executes thefunction and loads the result onto the stack. The successful execution of the function can be checked by checkingthat the stack is not nil using !lua_isnil(L,-1). The returned value can then be accessed by casting the resultin the top of the stack using lua_tonumber(L,-1). We use lua_pop to clear the result from the stack.

Running this example should print:


on the screen.


This article gave a very minimal example explaining how to call a lua function from C++.

Lua Register C Function

Related Post

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 26. Calling C from Lua

One of the basic means for extending Luais for the application to registernew C functions into Lua.

When we say that Lua can call C functions,this does not mean that Lua can call any C function.(There are packages that allow Lua to call any C function,but they are neither portable nor robust.)As we saw previously,when C calls a Lua function,it must follow a simple protocol to pass the arguments andto get the results.Similarly,for a C function to be called from Lua,it must follow a protocolto get its arguments and to return its results.Moreover, for a C function to be called from Lua,we must register it, that is,we must give its address to Lua in an appropriate way.

When Lua calls a C function,it uses the same kind of stack that C uses to call Lua.The C function gets its arguments from the stackand pushes the results on the stack.To distinguish the results from other values on the stack,the function returns (in C) the number of results itis leaving on the stack.An important concept here is that the stack is not a global structure;each function has its own private local stack.When Lua calls a C function,the first argument will always be at index 1 of this local stack.Even when a C function calls Lua code that calls thesame (or another) C function again,each of these invocations sees only its own private stack,with its first argument at index 1.

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